plugin.js
5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* plugin.js
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
// Forked for WordPress so it can be turned on/off after loading.
/*global tinymce:true */
/*eslint no-nested-ternary:0 */
/**
* Auto Resize
*
* This plugin automatically resizes the content area to fit its content height.
* It will retain a minimum height, which is the height of the content area when
* it's initialized.
*/
tinymce.PluginManager.add( 'wpautoresize', function( editor ) {
var settings = editor.settings,
oldSize = 300,
isActive = false;
if ( editor.settings.inline || tinymce.Env.iOS ) {
return;
}
function isFullscreen() {
return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
}
function getInt( n ) {
return parseInt( n, 10 ) || 0;
}
/**
* This method gets executed each time the editor needs to resize.
*/
function resize( e ) {
var deltaSize, doc, body, docElm, DOM = tinymce.DOM, resizeHeight, myHeight,
marginTop, marginBottom, paddingTop, paddingBottom, borderTop, borderBottom;
if ( ! isActive ) {
return;
}
doc = editor.getDoc();
if ( ! doc ) {
return;
}
e = e || {};
body = doc.body;
docElm = doc.documentElement;
resizeHeight = settings.autoresize_min_height;
if ( ! body || ( e && e.type === 'setcontent' && e.initial ) || isFullscreen() ) {
if ( body && docElm ) {
body.style.overflowY = 'auto';
docElm.style.overflowY = 'auto'; // Old IE.
}
return;
}
// Calculate outer height of the body element using CSS styles.
marginTop = editor.dom.getStyle( body, 'margin-top', true );
marginBottom = editor.dom.getStyle( body, 'margin-bottom', true );
paddingTop = editor.dom.getStyle( body, 'padding-top', true );
paddingBottom = editor.dom.getStyle( body, 'padding-bottom', true );
borderTop = editor.dom.getStyle( body, 'border-top-width', true );
borderBottom = editor.dom.getStyle( body, 'border-bottom-width', true );
myHeight = body.offsetHeight + getInt( marginTop ) + getInt( marginBottom ) +
getInt( paddingTop ) + getInt( paddingBottom ) +
getInt( borderTop ) + getInt( borderBottom );
// IE < 11, other?
if ( myHeight && myHeight < docElm.offsetHeight ) {
myHeight = docElm.offsetHeight;
}
// Make sure we have a valid height.
if ( isNaN( myHeight ) || myHeight <= 0 ) {
// Get height differently depending on the browser used.
myHeight = tinymce.Env.ie ? body.scrollHeight : ( tinymce.Env.webkit && body.clientHeight === 0 ? 0 : body.offsetHeight );
}
// Don't make it smaller than the minimum height.
if ( myHeight > settings.autoresize_min_height ) {
resizeHeight = myHeight;
}
// If a maximum height has been defined don't exceed this height.
if ( settings.autoresize_max_height && myHeight > settings.autoresize_max_height ) {
resizeHeight = settings.autoresize_max_height;
body.style.overflowY = 'auto';
docElm.style.overflowY = 'auto'; // Old IE.
} else {
body.style.overflowY = 'hidden';
docElm.style.overflowY = 'hidden'; // Old IE.
body.scrollTop = 0;
}
// Resize content element.
if (resizeHeight !== oldSize) {
deltaSize = resizeHeight - oldSize;
DOM.setStyle( editor.iframeElement, 'height', resizeHeight + 'px' );
oldSize = resizeHeight;
// WebKit doesn't decrease the size of the body element until the iframe gets resized.
// So we need to continue to resize the iframe down until the size gets fixed.
if ( tinymce.isWebKit && deltaSize < 0 ) {
resize( e );
}
editor.fire( 'wp-autoresize', { height: resizeHeight, deltaHeight: e.type === 'nodechange' ? deltaSize : null } );
}
}
/**
* Calls the resize x times in 100ms intervals. We can't wait for load events since
* the CSS files might load async.
*/
function wait( times, interval, callback ) {
setTimeout( function() {
resize();
if ( times-- ) {
wait( times, interval, callback );
} else if ( callback ) {
callback();
}
}, interval );
}
// Define minimum height.
settings.autoresize_min_height = parseInt(editor.getParam( 'autoresize_min_height', editor.getElement().offsetHeight), 10 );
// Define maximum height.
settings.autoresize_max_height = parseInt(editor.getParam( 'autoresize_max_height', 0), 10 );
function on() {
if ( ! editor.dom.hasClass( editor.getBody(), 'wp-autoresize' ) ) {
isActive = true;
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
// Add appropriate listeners for resizing the content area.
editor.on( 'nodechange setcontent keyup FullscreenStateChanged', resize );
resize();
}
}
function off() {
var doc;
// Don't turn off if the setting is 'on'.
if ( ! settings.wp_autoresize_on ) {
isActive = false;
doc = editor.getDoc();
editor.dom.removeClass( editor.getBody(), 'wp-autoresize' );
editor.off( 'nodechange setcontent keyup FullscreenStateChanged', resize );
doc.body.style.overflowY = 'auto';
doc.documentElement.style.overflowY = 'auto'; // Old IE.
oldSize = 0;
}
}
if ( settings.wp_autoresize_on ) {
// Turn resizing on when the editor loads.
isActive = true;
editor.on( 'init', function() {
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
});
editor.on( 'nodechange keyup FullscreenStateChanged', resize );
editor.on( 'setcontent', function() {
wait( 3, 100 );
});
if ( editor.getParam( 'autoresize_on_init', true ) ) {
editor.on( 'init', function() {
// Hit it 10 times in 200 ms intervals.
wait( 10, 200, function() {
// Hit it 5 times in 1 sec intervals.
wait( 5, 1000 );
});
});
}
}
// Reset the stored size.
editor.on( 'show', function() {
oldSize = 0;
});
// Register the command.
editor.addCommand( 'wpAutoResize', resize );
// On/off.
editor.addCommand( 'wpAutoResizeOn', on );
editor.addCommand( 'wpAutoResizeOff', off );
});