plugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.2.0 (2020-02-13)
  8. */
  9. (function () {
  10. 'use strict';
  11. var Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. var clone = function () {
  20. return Cell(get());
  21. };
  22. return {
  23. get: get,
  24. set: set,
  25. clone: clone
  26. };
  27. };
  28. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  29. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  30. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  31. var fireResizeEditor = function (editor) {
  32. return editor.fire('ResizeEditor');
  33. };
  34. var Events = { fireResizeEditor: fireResizeEditor };
  35. var getAutoResizeMinHeight = function (editor) {
  36. return editor.getParam('min_height', editor.getElement().offsetHeight, 'number');
  37. };
  38. var getAutoResizeMaxHeight = function (editor) {
  39. return editor.getParam('max_height', 0, 'number');
  40. };
  41. var getAutoResizeOverflowPadding = function (editor) {
  42. return editor.getParam('autoresize_overflow_padding', 1, 'number');
  43. };
  44. var getAutoResizeBottomMargin = function (editor) {
  45. return editor.getParam('autoresize_bottom_margin', 50, 'number');
  46. };
  47. var shouldAutoResizeOnInit = function (editor) {
  48. return editor.getParam('autoresize_on_init', true, 'boolean');
  49. };
  50. var Settings = {
  51. getAutoResizeMinHeight: getAutoResizeMinHeight,
  52. getAutoResizeMaxHeight: getAutoResizeMaxHeight,
  53. getAutoResizeOverflowPadding: getAutoResizeOverflowPadding,
  54. getAutoResizeBottomMargin: getAutoResizeBottomMargin,
  55. shouldAutoResizeOnInit: shouldAutoResizeOnInit
  56. };
  57. var isFullscreen = function (editor) {
  58. return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
  59. };
  60. var wait = function (editor, oldSize, times, interval, callback) {
  61. global$2.setEditorTimeout(editor, function () {
  62. resize(editor, oldSize);
  63. if (times--) {
  64. wait(editor, oldSize, times, interval, callback);
  65. } else if (callback) {
  66. callback();
  67. }
  68. }, interval);
  69. };
  70. var toggleScrolling = function (editor, state) {
  71. var body = editor.getBody();
  72. if (body) {
  73. body.style.overflowY = state ? '' : 'hidden';
  74. if (!state) {
  75. body.scrollTop = 0;
  76. }
  77. }
  78. };
  79. var parseCssValueToInt = function (dom, elm, name, computed) {
  80. var value = parseInt(dom.getStyle(elm, name, computed), 10);
  81. return isNaN(value) ? 0 : value;
  82. };
  83. var resize = function (editor, oldSize) {
  84. var deltaSize, resizeHeight, contentHeight;
  85. var dom = editor.dom;
  86. var doc = editor.getDoc();
  87. if (!doc) {
  88. return;
  89. }
  90. if (isFullscreen(editor)) {
  91. toggleScrolling(editor, true);
  92. return;
  93. }
  94. var docEle = doc.documentElement;
  95. var resizeBottomMargin = Settings.getAutoResizeBottomMargin(editor);
  96. resizeHeight = Settings.getAutoResizeMinHeight(editor);
  97. var marginTop = parseCssValueToInt(dom, docEle, 'margin-top', true);
  98. var marginBottom = parseCssValueToInt(dom, docEle, 'margin-bottom', true);
  99. contentHeight = docEle.offsetHeight + marginTop + marginBottom + resizeBottomMargin;
  100. if (contentHeight < 0) {
  101. contentHeight = 0;
  102. }
  103. var containerHeight = editor.getContainer().offsetHeight;
  104. var contentAreaHeight = editor.getContentAreaContainer().offsetHeight;
  105. var chromeHeight = containerHeight - contentAreaHeight;
  106. if (contentHeight + chromeHeight > Settings.getAutoResizeMinHeight(editor)) {
  107. resizeHeight = contentHeight + chromeHeight;
  108. }
  109. var maxHeight = Settings.getAutoResizeMaxHeight(editor);
  110. if (maxHeight && resizeHeight > maxHeight) {
  111. resizeHeight = maxHeight;
  112. toggleScrolling(editor, true);
  113. } else {
  114. toggleScrolling(editor, false);
  115. }
  116. if (resizeHeight !== oldSize.get()) {
  117. deltaSize = resizeHeight - oldSize.get();
  118. dom.setStyle(editor.getContainer(), 'height', resizeHeight + 'px');
  119. oldSize.set(resizeHeight);
  120. Events.fireResizeEditor(editor);
  121. if (global$1.browser.isSafari() && global$1.mac) {
  122. var win = editor.getWin();
  123. win.scrollTo(win.pageXOffset, win.pageYOffset);
  124. }
  125. if (editor.hasFocus()) {
  126. editor.selection.scrollIntoView(editor.selection.getNode());
  127. }
  128. if (global$1.webkit && deltaSize < 0) {
  129. resize(editor, oldSize);
  130. }
  131. }
  132. };
  133. var setup = function (editor, oldSize) {
  134. editor.on('init', function () {
  135. var overflowPadding = Settings.getAutoResizeOverflowPadding(editor);
  136. var dom = editor.dom;
  137. dom.setStyles(editor.getBody(), {
  138. 'paddingLeft': overflowPadding,
  139. 'paddingRight': overflowPadding,
  140. 'min-height': 0
  141. });
  142. });
  143. editor.on('NodeChange SetContent keyup FullscreenStateChanged ResizeContent', function () {
  144. resize(editor, oldSize);
  145. });
  146. if (Settings.shouldAutoResizeOnInit(editor)) {
  147. editor.on('init', function () {
  148. wait(editor, oldSize, 20, 100, function () {
  149. wait(editor, oldSize, 5, 1000);
  150. });
  151. });
  152. }
  153. };
  154. var Resize = {
  155. setup: setup,
  156. resize: resize
  157. };
  158. var register = function (editor, oldSize) {
  159. editor.addCommand('mceAutoResize', function () {
  160. Resize.resize(editor, oldSize);
  161. });
  162. };
  163. var Commands = { register: register };
  164. function Plugin () {
  165. global.add('autoresize', function (editor) {
  166. if (!editor.settings.hasOwnProperty('resize')) {
  167. editor.settings.resize = false;
  168. }
  169. if (!editor.inline) {
  170. var oldSize = Cell(0);
  171. Commands.register(editor, oldSize);
  172. Resize.setup(editor, oldSize);
  173. }
  174. });
  175. }
  176. Plugin();
  177. }());