You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

208 lines
5.8 KiB

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