From 22ff75ac5e063b9c4cf1ed1615126fd87a93c923 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sun, 28 Aug 2016 18:40:00 +0000 Subject: [PATCH] Editor: fix jumpiness on pressing backspace and delete in the Text editor. Fixes #37690 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@38426 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/editor-expand.js | 78 ++++++++++++++++--------------- src/wp-includes/script-loader.php | 2 +- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/wp-admin/js/editor-expand.js b/src/wp-admin/js/editor-expand.js index bf67c64e04..aaf34b0bb1 100644 --- a/src/wp-admin/js/editor-expand.js +++ b/src/wp-admin/js/editor-expand.js @@ -53,6 +53,47 @@ sideSortablesHeight: 0 }; + 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 ); + + 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(); @@ -75,43 +116,6 @@ } } - function textEditorResize() { - if ( mceEditor && ! mceEditor.isHidden() ) { - return; - } - - if ( ! mceEditor && initialMode === 'tinymce' ) { - return; - } - - var length = textEditor.value.length; - var height = parseInt( textEditor.style.height, 10 ); - var top = window.scrollTop; - - if ( length < oldTextLength ) { - // textEditor.scrollHeight is not adjusted until the next line. - textEditor.style.height = 'auto'; - - if ( textEditor.scrollHeight > autoresizeMinHeight ) { - textEditor.style.height = textEditor.scrollHeight + 'px'; - } else { - textEditor.style.height = autoresizeMinHeight + 'px'; - } - - // Prevent scroll-jumping in Firefox and IE. - window.scrollTop = top; - - if ( textEditor.scrollHeight < height ) { - adjust(); - } - } else if ( height < textEditor.scrollHeight ) { - textEditor.style.height = textEditor.scrollHeight + 'px'; - adjust(); - } - - oldTextLength = length; - } - // We need to wait for TinyMCE to initialize. $document.on( 'tinymce-editor-init.editor-expand', function( event, editor ) { var VK = window.tinymce.util.VK, diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 15464a17cb..b04bb9e6e4 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -554,7 +554,7 @@ function wp_default_scripts( &$scripts ) { 'suggestedImgAlt' => __( 'Suggested image #%d' ), ) ); - $scripts->add( 'editor-expand', "/wp-admin/js/editor-expand$suffix.js", array( 'jquery' ), false, 1 ); + $scripts->add( 'editor-expand', "/wp-admin/js/editor-expand$suffix.js", array( 'jquery', 'underscore' ), false, 1 ); $scripts->add( 'link', "/wp-admin/js/link$suffix.js", array( 'wp-lists', 'postbox' ), false, 1 );