4
* Copyright, Moxiecode Systems AB
5
* Released under LGPL License.
7
* License: http://www.tinymce.com/license
8
* Contributing: http://www.tinymce.com/contributing
11
// Forked for WordPress so it can be turned on/off after loading.
13
/*global tinymce:true */
14
/*eslint no-nested-ternary:0 */
19
* This plugin automatically resizes the content area to fit its content height.
20
* It will retain a minimum height, which is the height of the content area when
23
tinymce.PluginManager.add( 'wpautoresize', function( editor ) {
24
var settings = editor.settings,
28
function isFullscreen() {
29
return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
32
if ( editor.settings.inline ) {
36
function getInt( n ) {
37
return parseInt( n, 10 ) || 0;
41
* This method gets executed each time the editor needs to resize.
43
function resize( e ) {
44
var deltaSize, doc, body, docElm, DOM = tinymce.DOM, resizeHeight, myHeight,
45
marginTop, marginBottom, paddingTop, paddingBottom, borderTop, borderBottom;
51
doc = editor.getDoc();
58
docElm = doc.documentElement;
59
resizeHeight = settings.autoresize_min_height;
61
if ( ! body || ( e && e.type === 'setcontent' && e.initial ) || isFullscreen() ) {
62
if ( body && docElm ) {
63
body.style.overflowY = 'auto';
64
docElm.style.overflowY = 'auto'; // Old IE
70
// Calculate outer height of the body element using CSS styles
71
marginTop = editor.dom.getStyle( body, 'margin-top', true );
72
marginBottom = editor.dom.getStyle( body, 'margin-bottom', true );
73
paddingTop = editor.dom.getStyle( body, 'padding-top', true );
74
paddingBottom = editor.dom.getStyle( body, 'padding-bottom', true );
75
borderTop = editor.dom.getStyle( body, 'border-top-width', true );
76
borderBottom = editor.dom.getStyle( body, 'border-bottom-width', true );
77
myHeight = body.offsetHeight + getInt( marginTop ) + getInt( marginBottom ) +
78
getInt( paddingTop ) + getInt( paddingBottom ) +
79
getInt( borderTop ) + getInt( borderBottom );
82
if ( myHeight && myHeight < docElm.offsetHeight ) {
83
myHeight = docElm.offsetHeight;
86
// Make sure we have a valid height
87
if ( isNaN( myHeight ) || myHeight <= 0 ) {
88
// Get height differently depending on the browser used
89
myHeight = tinymce.Env.ie ? body.scrollHeight : ( tinymce.Env.webkit && body.clientHeight === 0 ? 0 : body.offsetHeight );
92
// Don't make it smaller than the minimum height
93
if ( myHeight > settings.autoresize_min_height ) {
94
resizeHeight = myHeight;
97
// If a maximum height has been defined don't exceed this height
98
if ( settings.autoresize_max_height && myHeight > settings.autoresize_max_height ) {
99
resizeHeight = settings.autoresize_max_height;
100
body.style.overflowY = 'auto';
101
docElm.style.overflowY = 'auto'; // Old IE
103
body.style.overflowY = 'hidden';
104
docElm.style.overflowY = 'hidden'; // Old IE
108
// Resize content element
109
if (resizeHeight !== oldSize) {
110
deltaSize = resizeHeight - oldSize;
111
DOM.setStyle( editor.iframeElement, 'height', resizeHeight + 'px' );
112
oldSize = resizeHeight;
114
// WebKit doesn't decrease the size of the body element until the iframe gets resized
115
// So we need to continue to resize the iframe down until the size gets fixed
116
if ( tinymce.isWebKit && deltaSize < 0 ) {
120
editor.fire( 'wp-autoresize', { height: resizeHeight } );
125
* Calls the resize x times in 100ms intervals. We can't wait for load events since
126
* the CSS files might load async.
128
function wait( times, interval, callback ) {
129
setTimeout( function() {
133
wait( times, interval, callback );
134
} else if ( callback ) {
140
// Define minimum height
141
settings.autoresize_min_height = parseInt(editor.getParam( 'autoresize_min_height', editor.getElement().offsetHeight), 10 );
143
// Define maximum height
144
settings.autoresize_max_height = parseInt(editor.getParam( 'autoresize_max_height', 0), 10 );
147
if ( ! editor.dom.hasClass( editor.getBody(), 'wp-autoresize' ) ) {
149
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
150
// Add appropriate listeners for resizing the content area
151
editor.on( 'nodechange setcontent keyup FullscreenStateChanged', resize );
159
// Don't turn off if the setting is 'on'
160
if ( ! settings.wp_autoresize_on ) {
162
doc = editor.getDoc();
163
editor.dom.removeClass( editor.getBody(), 'wp-autoresize' );
164
editor.off( 'nodechange setcontent keyup FullscreenStateChanged', resize );
165
doc.body.style.overflowY = 'auto';
166
doc.documentElement.style.overflowY = 'auto'; // Old IE
171
if ( settings.wp_autoresize_on ) {
172
// Turn resizing on when the editor loads
175
editor.on( 'init', function() {
176
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
179
editor.on( 'nodechange keyup FullscreenStateChanged', resize );
181
editor.on( 'setcontent', function() {
185
if ( editor.getParam( 'autoresize_on_init', true ) ) {
186
editor.on( 'init', function() {
187
// Hit it 10 times in 200 ms intervals
188
wait( 10, 200, function() {
189
// Hit it 5 times in 1 sec intervals
196
// Reset the stored size
197
editor.on( 'show', function() {
201
// Register the command
202
editor.addCommand( 'wpAutoResize', resize );
205
editor.addCommand( 'wpAutoResizeOn', on );
206
editor.addCommand( 'wpAutoResizeOff', off );