diff --git a/admin/js/typecho.js b/admin/js/typecho.js index e4ef5e13..86796265 100644 --- a/admin/js/typecho.js +++ b/admin/js/typecho.js @@ -1,6 +1,14 @@ (function (w) { w.Typecho = { - insertFileToEditor : function (file, url, isImage) {} + insertFileToEditor : function (file, url, isImage) {}, + editorResize : function (id, url) { + $('#' + id).resizeable({ + minHeight : 100, + afterResize : function (h) { + $.post(url, {size : h}); + } + }) + } }; })(window); @@ -23,6 +31,69 @@ }); }; + $.fn.resizeable = function (options) { + var s = $.extend({ + minHeight : 100, + afterResize : null + }, options); + + return this.each(function () { + var r = $('<span class="resize"><i></i></span>').insertAfter(this), + staticOffset, iLastMousePos = 0, iMin = s.minHeight, t = this; + + function startDrag(e) { + textarea = $(e.data.el); + textarea.blur(); + iLastMousePos = mousePosition(e).y; + + staticOffset = textarea.height() - iLastMousePos; + textarea.css('opacity', 0.25); + + $(document).mousemove(performDrag).mouseup(endDrag); + return false; + } + + function performDrag(e) { + var iThisMousePos = mousePosition(e).y, + iMousePos = staticOffset + iThisMousePos; + if (iLastMousePos >= (iThisMousePos)) { + iMousePos -= 5; + } + + iLastMousePos = iThisMousePos; + iMousePos = Math.max(iMin, iMousePos); + textarea.height(iMousePos + 'px'); + + if (iMousePos < iMin) { + endDrag(e); + } + return false; + } + + function endDrag(e) { + var h = textarea.outerHeight(); + $(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag); + + textarea.css('opacity', 1); + textarea.focus(); + textarea = null; + + staticOffset = null; + iLastMousePos = 0; + + if (s.afterResize) { + s.afterResize.call(t, h); + } + } + + function mousePosition(e) { + return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop }; + } + + r.bind('mousedown', {el : this}, startDrag); + }); + }; + // 表格选择插件 $.fn.tableSelectable = function (options) { var table = this, s = $.extend({ diff --git a/admin/write-js.php b/admin/write-js.php index a96c69d8..89c1a6f8 100644 --- a/admin/write-js.php +++ b/admin/write-js.php @@ -34,6 +34,9 @@ $(document).ready(function() { minute : (new Date()).getMinutes() }); + // text 自动拉伸 + Typecho.editorResize('text', '<?php $options->index('/action/ajax?do=editorResize'); ?>'); + // tag autocomplete 提示 var tags = $('#tags'), tagsPre = []; diff --git a/admin/write-page.php b/admin/write-page.php index cd65c9d1..90dfd96b 100644 --- a/admin/write-page.php +++ b/admin/write-page.php @@ -29,7 +29,6 @@ Typecho_Widget::widget('Widget_Contents_Page_Edit')->to($page); <p class="mono url-slug"><?php echo preg_replace("/\{slug\}/i", $input, $permalink); ?></p> <p> <textarea style="height: <?php $options->editorSize(); ?>px" autocomplete="off" id="text" name="text" class="w-100 mono"><?php echo htmlspecialchars($page->text); ?></textarea> - <span class="resize"><i></i></span> </p> <?php include 'file-upload.php'; ?> diff --git a/admin/write-post.php b/admin/write-post.php index c1aad8e7..f2bbdcb6 100644 --- a/admin/write-post.php +++ b/admin/write-post.php @@ -34,7 +34,6 @@ Typecho_Widget::widget('Widget_Contents_Post_Edit')->to($post); <p> <textarea style="height: <?php $options->editorSize(); ?>px" autocomplete="off" id="text" name="text" class="w-100 mono"><?php echo htmlspecialchars($post->text); ?></textarea> - <span class="resize"><i></i></span> </p> <?php include 'file-upload.php'; ?>