Skip to content

Commit 8cc11af

Browse files
author
iseulde
committed
Editor: better resizing for text editor
* Use `scrollHeight` to resize the text editor. * Don't scroll the cursor into view when under the text editor's toolbar, it involves cloning the content which is quite slow. Props iseulde, azaozz. See #36482. Built from https://develop.svn.wordpress.org/trunk@37596
1 parent a21ac1e commit 8cc11af

File tree

7 files changed

+20
-92
lines changed

7 files changed

+20
-92
lines changed

wp-admin/css/edit-rtl.css

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,6 @@ form#tags-filter {
377377
position: relative;
378378
}
379379

380-
#content-textarea-clone {
381-
z-index: -1;
382-
position: absolute;
383-
top: 0;
384-
visibility: hidden;
385-
overflow: hidden;
386-
max-width: 100%;
387-
border: 1px solid transparent;
388-
}
389-
390380
.wp-editor-expand #wp-content-editor-tools {
391381
z-index: 1000;
392382
border-bottom: 1px solid #e5e5e5;

wp-admin/css/edit-rtl.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wp-admin/css/edit.css

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,6 @@ form#tags-filter {
377377
position: relative;
378378
}
379379

380-
#content-textarea-clone {
381-
z-index: -1;
382-
position: absolute;
383-
top: 0;
384-
visibility: hidden;
385-
overflow: hidden;
386-
max-width: 100%;
387-
border: 1px solid transparent;
388-
}
389-
390380
.wp-editor-expand #wp-content-editor-tools {
391381
z-index: 1000;
392382
border-bottom: 1px solid #e5e5e5;

wp-admin/css/edit.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wp-admin/js/editor-expand.js

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
$visualEditor = $(),
1616
$textTop = $( '#ed_toolbar' ),
1717
$textEditor = $( '#content' ),
18-
$textEditorClone = $( '<div id="content-textarea-clone" class="wp-exclude-emoji"></div>' ),
18+
textEditor = $textEditor[0],
19+
textEditorLength = 0,
1920
$bottom = $( '#post-status-info' ),
2021
$menuBar = $(),
2122
$statusBar = $(),
@@ -52,16 +53,6 @@
5253
sideSortablesHeight: 0
5354
};
5455

55-
$textEditorClone.insertAfter( $textEditor );
56-
57-
$textEditorClone.css( {
58-
'font-family': $textEditor.css( 'font-family' ),
59-
'font-size': $textEditor.css( 'font-size' ),
60-
'line-height': $textEditor.css( 'line-height' ),
61-
'white-space': 'pre-wrap',
62-
'word-wrap': 'break-word'
63-
} );
64-
6556
function getHeights() {
6657
var windowWidth = $window.width();
6758

@@ -84,68 +75,28 @@
8475
}
8576
}
8677

87-
function textEditorKeyup( event ) {
88-
var VK = jQuery.ui.keyCode,
89-
key = event.keyCode,
90-
range = document.createRange(),
91-
selStart = $textEditor[0].selectionStart,
92-
selEnd = $textEditor[0].selectionEnd,
93-
textNode = $textEditorClone[0].firstChild,
94-
buffer = 10,
95-
offset, cursorTop, cursorBottom, editorTop, editorBottom;
96-
97-
if ( selStart && selEnd && selStart !== selEnd ) {
98-
return;
99-
}
100-
101-
// These are not TinyMCE ranges.
102-
try {
103-
range.setStart( textNode, selStart );
104-
range.setEnd( textNode, selEnd + 1 );
105-
} catch ( ex ) {}
106-
107-
offset = range.getBoundingClientRect();
78+
function textEditorResize() {
79+
var reduce, scrollHeight;
10880

109-
if ( ! offset.height ) {
81+
if ( mceEditor && ! mceEditor.isHidden() ) {
11082
return;
11183
}
11284

113-
cursorTop = offset.top - buffer;
114-
cursorBottom = cursorTop + offset.height + buffer;
115-
editorTop = heights.adminBarHeight + heights.toolsHeight + heights.textTopHeight;
116-
editorBottom = heights.windowHeight - heights.bottomHeight;
117-
118-
if ( cursorTop < editorTop && ( key === VK.UP || key === VK.LEFT || key === VK.BACKSPACE ) ) {
119-
window.scrollTo( window.pageXOffset, cursorTop + window.pageYOffset - editorTop );
120-
} else if ( cursorBottom > editorBottom ) {
121-
window.scrollTo( window.pageXOffset, cursorBottom + window.pageYOffset - editorBottom );
122-
}
123-
}
124-
125-
function textEditorResize() {
126-
if ( ( mceEditor && ! mceEditor.isHidden() ) || ( ! mceEditor && initialMode === 'tinymce' ) ) {
85+
if ( ! mceEditor && initialMode === 'tinymce' ) {
12786
return;
12887
}
12988

130-
var textEditorHeight = $textEditor.height(),
131-
hiddenHeight;
132-
133-
$textEditorClone.width( $textEditor.width() - 22 );
134-
$textEditorClone.text( $textEditor.val() + '&nbsp;' );
135-
136-
hiddenHeight = $textEditorClone.height();
89+
reduce = textEditorLength > ( textEditorLength = textEditor.value.length );
90+
scrollHeight = textEditor.scrollHeight;
13791

138-
if ( hiddenHeight < autoresizeMinHeight ) {
139-
hiddenHeight = autoresizeMinHeight;
140-
}
141-
142-
if ( hiddenHeight === textEditorHeight ) {
143-
return;
92+
if ( reduce ) {
93+
textEditor.style.height = 'auto';
94+
textEditor.style.height = scrollHeight + 'px';
95+
adjust();
96+
} else if ( parseInt( textEditor.style.height, 10 ) < scrollHeight ) {
97+
textEditor.style.height = scrollHeight + 'px';
98+
adjust();
14499
}
145-
146-
$textEditor.height( hiddenHeight );
147-
148-
adjust();
149100
}
150101

151102
// We need to wait for TinyMCE to initialize.
@@ -474,7 +425,7 @@
474425

475426
if ( event && event.deltaHeight > 0 && event.deltaHeight < 100 ) {
476427
window.scrollBy( 0, event.deltaHeight );
477-
} else if ( advanced ) {
428+
} else if ( visual && advanced ) {
478429
fixedBottom = true;
479430

480431
$statusBar.css( {
@@ -603,8 +554,6 @@
603554
$textEditor.css( {
604555
marginTop: heights.textTopHeight
605556
} );
606-
607-
$textEditorClone.width( contentWrapWidth - 20 - ( borderWidth * 2 ) );
608557
}
609558
}
610559
}
@@ -660,7 +609,6 @@
660609
});
661610

662611
$textEditor.on( 'focus.editor-expand input.editor-expand propertychange.editor-expand', textEditorResize );
663-
$textEditor.on( 'keyup.editor-expand', textEditorKeyup );
664612
mceBind();
665613

666614
// Adjust when entering/exiting fullscreen mode.

wp-admin/js/editor-expand.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* @global string $wp_version
66
*/
7-
$wp_version = '4.6-alpha-37595';
7+
$wp_version = '4.6-alpha-37596';
88

99
/**
1010
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)