X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/784f914b1e4b1c62d6657e86397c2e83bcee4295..HEAD:/wp-admin/js/editor-expand.js diff --git a/wp-admin/js/editor-expand.js b/wp-admin/js/editor-expand.js index ba5b0952..cc3aac4f 100644 --- a/wp-admin/js/editor-expand.js +++ b/wp-admin/js/editor-expand.js @@ -15,7 +15,8 @@ $visualEditor = $(), $textTop = $( '#ed_toolbar' ), $textEditor = $( '#content' ), - $textEditorClone = $( '
' ), + textEditor = $textEditor[0], + oldTextLength = 0, $bottom = $( '#post-status-info' ), $menuBar = $(), $statusBar = $(), @@ -52,15 +53,46 @@ sideSortablesHeight: 0 }; - $textEditorClone.insertAfter( $textEditor ); + var shrinkTextarea = window._.throttle( function() { + var x = window.scrollX || document.documentElement.scrollLeft; + var y = window.scrollY || document.documentElement.scrollTop; + var height = parseInt( textEditor.style.height, 10 ); - $textEditorClone.css( { - 'font-family': $textEditor.css( 'font-family' ), - 'font-size': $textEditor.css( 'font-size' ), - 'line-height': $textEditor.css( 'line-height' ), - 'white-space': 'pre-wrap', - 'word-wrap': 'break-word' - } ); + textEditor.style.height = autoresizeMinHeight + 'px'; + + if ( textEditor.scrollHeight > autoresizeMinHeight ) { + textEditor.style.height = textEditor.scrollHeight + 'px'; + } + + if ( typeof x !== 'undefined' ) { + window.scrollTo( x, y ); + } + + if ( textEditor.scrollHeight < height ) { + adjust(); + } + }, 300 ); + + function textEditorResize() { + var length = textEditor.value.length; + + if ( mceEditor && ! mceEditor.isHidden() ) { + return; + } + + if ( ! mceEditor && initialMode === 'tinymce' ) { + return; + } + + if ( length < oldTextLength ) { + shrinkTextarea(); + } else if ( parseInt( textEditor.style.height, 10 ) < textEditor.scrollHeight ) { + textEditor.style.height = Math.ceil( textEditor.scrollHeight ) + 'px'; + adjust(); + } + + oldTextLength = length; + } function getHeights() { var windowWidth = $window.width(); @@ -84,70 +116,6 @@ } } - function textEditorKeyup( event ) { - var VK = jQuery.ui.keyCode, - key = event.keyCode, - range = document.createRange(), - selStart = $textEditor[0].selectionStart, - selEnd = $textEditor[0].selectionEnd, - textNode = $textEditorClone[0].firstChild, - buffer = 10, - offset, cursorTop, cursorBottom, editorTop, editorBottom; - - if ( selStart && selEnd && selStart !== selEnd ) { - return; - } - - // These are not TinyMCE ranges. - try { - range.setStart( textNode, selStart ); - range.setEnd( textNode, selEnd + 1 ); - } catch ( ex ) {} - - offset = range.getBoundingClientRect(); - - if ( ! offset.height ) { - return; - } - - cursorTop = offset.top - buffer; - cursorBottom = cursorTop + offset.height + buffer; - editorTop = heights.adminBarHeight + heights.toolsHeight + heights.textTopHeight; - editorBottom = heights.windowHeight - heights.bottomHeight; - - if ( cursorTop < editorTop && ( key === VK.UP || key === VK.LEFT || key === VK.BACKSPACE ) ) { - window.scrollTo( window.pageXOffset, cursorTop + window.pageYOffset - editorTop ); - } else if ( cursorBottom > editorBottom ) { - window.scrollTo( window.pageXOffset, cursorBottom + window.pageYOffset - editorBottom ); - } - } - - function textEditorResize() { - if ( ( mceEditor && ! mceEditor.isHidden() ) || ( ! mceEditor && initialMode === 'tinymce' ) ) { - return; - } - - var textEditorHeight = $textEditor.height(), - hiddenHeight; - - $textEditorClone.width( $textEditor.width() - 22 ); - $textEditorClone.text( $textEditor.val() + ' ' ); - - hiddenHeight = $textEditorClone.height(); - - if ( hiddenHeight < autoresizeMinHeight ) { - hiddenHeight = autoresizeMinHeight; - } - - if ( hiddenHeight === textEditorHeight ) { - return; - } - - $textEditor.height( hiddenHeight ); - - adjust(); - } - // We need to wait for TinyMCE to initialize. $document.on( 'tinymce-editor-init.editor-expand', function( event, editor ) { var VK = window.tinymce.util.VK, @@ -350,7 +318,7 @@ topHeight = heights.textTopHeight; } - // TinyMCE still intializing. + // TinyMCE still initializing. if ( ! visual && ! $top.length ) { return; } @@ -474,7 +442,7 @@ if ( event && event.deltaHeight > 0 && event.deltaHeight < 100 ) { window.scrollBy( 0, event.deltaHeight ); - } else if ( advanced ) { + } else if ( visual && advanced ) { fixedBottom = true; $statusBar.css( { @@ -603,8 +571,6 @@ $textEditor.css( { marginTop: heights.textTopHeight } ); - - $textEditorClone.width( contentWrapWidth - 20 - ( borderWidth * 2 ) ); } } } @@ -642,7 +608,7 @@ // Adjust when collapsing the menu, changing the columns, changing the body class. $document.on( 'wp-collapse-menu.editor-expand postboxes-columnchange.editor-expand editor-classchange.editor-expand', adjust ) - .on( 'postbox-toggled.editor-expand', function() { + .on( 'postbox-toggled.editor-expand postbox-moved.editor-expand', function() { if ( ! fixedSideTop && ! fixedSideBottom && window.pageYOffset > pinnedToolsTop ) { fixedSideBottom = true; window.scrollBy( 0, -1 ); @@ -660,7 +626,6 @@ }); $textEditor.on( 'focus.editor-expand input.editor-expand propertychange.editor-expand', textEditorResize ); - $textEditor.on( 'keyup.editor-expand', textEditorKeyup ); mceBind(); // Adjust when entering/exiting fullscreen mode.