plugin.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.10.2 (2021-11-17)
  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. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var hasOwnProperty = Object.hasOwnProperty;
  25. var has = function (obj, key) {
  26. return hasOwnProperty.call(obj, key);
  27. };
  28. var global$2 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  29. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  30. var global = tinymce.util.Tools.resolve('tinymce.util.Delay');
  31. var fireResizeEditor = function (editor) {
  32. return editor.fire('ResizeEditor');
  33. };
  34. var getAutoResizeMinHeight = function (editor) {
  35. return editor.getParam('min_height', editor.getElement().offsetHeight, 'number');
  36. };
  37. var getAutoResizeMaxHeight = function (editor) {
  38. return editor.getParam('max_height', 0, 'number');
  39. };
  40. var getAutoResizeOverflowPadding = function (editor) {
  41. return editor.getParam('autoresize_overflow_padding', 1, 'number');
  42. };
  43. var getAutoResizeBottomMargin = function (editor) {
  44. return editor.getParam('autoresize_bottom_margin', 50, 'number');
  45. };
  46. var shouldAutoResizeOnInit = function (editor) {
  47. return editor.getParam('autoresize_on_init', true, 'boolean');
  48. };
  49. var isFullscreen = function (editor) {
  50. return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
  51. };
  52. var wait = function (editor, oldSize, times, interval, callback) {
  53. global.setEditorTimeout(editor, function () {
  54. resize(editor, oldSize);
  55. if (times--) {
  56. wait(editor, oldSize, times, interval, callback);
  57. } else if (callback) {
  58. callback();
  59. }
  60. }, interval);
  61. };
  62. var toggleScrolling = function (editor, state) {
  63. var body = editor.getBody();
  64. if (body) {
  65. body.style.overflowY = state ? '' : 'hidden';
  66. if (!state) {
  67. body.scrollTop = 0;
  68. }
  69. }
  70. };
  71. var parseCssValueToInt = function (dom, elm, name, computed) {
  72. var value = parseInt(dom.getStyle(elm, name, computed), 10);
  73. return isNaN(value) ? 0 : value;
  74. };
  75. var shouldScrollIntoView = function (trigger) {
  76. if ((trigger === null || trigger === void 0 ? void 0 : trigger.type.toLowerCase()) === 'setcontent') {
  77. var setContentEvent = trigger;
  78. return setContentEvent.selection === true || setContentEvent.paste === true;
  79. } else {
  80. return false;
  81. }
  82. };
  83. var resize = function (editor, oldSize, trigger) {
  84. var dom = editor.dom;
  85. var doc = editor.getDoc();
  86. if (!doc) {
  87. return;
  88. }
  89. if (isFullscreen(editor)) {
  90. toggleScrolling(editor, true);
  91. return;
  92. }
  93. var docEle = doc.documentElement;
  94. var resizeBottomMargin = getAutoResizeBottomMargin(editor);
  95. var resizeHeight = getAutoResizeMinHeight(editor);
  96. var marginTop = parseCssValueToInt(dom, docEle, 'margin-top', true);
  97. var marginBottom = parseCssValueToInt(dom, docEle, 'margin-bottom', true);
  98. var contentHeight = docEle.offsetHeight + marginTop + marginBottom + resizeBottomMargin;
  99. if (contentHeight < 0) {
  100. contentHeight = 0;
  101. }
  102. var containerHeight = editor.getContainer().offsetHeight;
  103. var contentAreaHeight = editor.getContentAreaContainer().offsetHeight;
  104. var chromeHeight = containerHeight - contentAreaHeight;
  105. if (contentHeight + chromeHeight > getAutoResizeMinHeight(editor)) {
  106. resizeHeight = contentHeight + chromeHeight;
  107. }
  108. var maxHeight = getAutoResizeMaxHeight(editor);
  109. if (maxHeight && resizeHeight > maxHeight) {
  110. resizeHeight = maxHeight;
  111. toggleScrolling(editor, true);
  112. } else {
  113. toggleScrolling(editor, false);
  114. }
  115. if (resizeHeight !== oldSize.get()) {
  116. var deltaSize = resizeHeight - oldSize.get();
  117. dom.setStyle(editor.getContainer(), 'height', resizeHeight + 'px');
  118. oldSize.set(resizeHeight);
  119. fireResizeEditor(editor);
  120. if (global$1.browser.isSafari() && global$1.mac) {
  121. var win = editor.getWin();
  122. win.scrollTo(win.pageXOffset, win.pageYOffset);
  123. }
  124. if (editor.hasFocus() && shouldScrollIntoView(trigger)) {
  125. editor.selection.scrollIntoView();
  126. }
  127. if (global$1.webkit && deltaSize < 0) {
  128. resize(editor, oldSize, trigger);
  129. }
  130. }
  131. };
  132. var setup = function (editor, oldSize) {
  133. editor.on('init', function () {
  134. var overflowPadding = getAutoResizeOverflowPadding(editor);
  135. var dom = editor.dom;
  136. dom.setStyles(editor.getDoc().documentElement, { height: 'auto' });
  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 (e) {
  144. resize(editor, oldSize, e);
  145. });
  146. if (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 register = function (editor, oldSize) {
  155. editor.addCommand('mceAutoResize', function () {
  156. resize(editor, oldSize);
  157. });
  158. };
  159. function Plugin () {
  160. global$2.add('autoresize', function (editor) {
  161. if (!has(editor.settings, 'resize')) {
  162. editor.settings.resize = false;
  163. }
  164. if (!editor.inline) {
  165. var oldSize = Cell(0);
  166. register(editor, oldSize);
  167. setup(editor, oldSize);
  168. }
  169. });
  170. }
  171. Plugin();
  172. }());