plugin.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.9.1 (2021-08-27)
  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 resize = function (editor, oldSize) {
  76. var dom = editor.dom;
  77. var doc = editor.getDoc();
  78. if (!doc) {
  79. return;
  80. }
  81. if (isFullscreen(editor)) {
  82. toggleScrolling(editor, true);
  83. return;
  84. }
  85. var docEle = doc.documentElement;
  86. var resizeBottomMargin = getAutoResizeBottomMargin(editor);
  87. var resizeHeight = getAutoResizeMinHeight(editor);
  88. var marginTop = parseCssValueToInt(dom, docEle, 'margin-top', true);
  89. var marginBottom = parseCssValueToInt(dom, docEle, 'margin-bottom', true);
  90. var contentHeight = docEle.offsetHeight + marginTop + marginBottom + resizeBottomMargin;
  91. if (contentHeight < 0) {
  92. contentHeight = 0;
  93. }
  94. var containerHeight = editor.getContainer().offsetHeight;
  95. var contentAreaHeight = editor.getContentAreaContainer().offsetHeight;
  96. var chromeHeight = containerHeight - contentAreaHeight;
  97. if (contentHeight + chromeHeight > getAutoResizeMinHeight(editor)) {
  98. resizeHeight = contentHeight + chromeHeight;
  99. }
  100. var maxHeight = getAutoResizeMaxHeight(editor);
  101. if (maxHeight && resizeHeight > maxHeight) {
  102. resizeHeight = maxHeight;
  103. toggleScrolling(editor, true);
  104. } else {
  105. toggleScrolling(editor, false);
  106. }
  107. if (resizeHeight !== oldSize.get()) {
  108. var deltaSize = resizeHeight - oldSize.get();
  109. dom.setStyle(editor.getContainer(), 'height', resizeHeight + 'px');
  110. oldSize.set(resizeHeight);
  111. fireResizeEditor(editor);
  112. if (global$1.browser.isSafari() && global$1.mac) {
  113. var win = editor.getWin();
  114. win.scrollTo(win.pageXOffset, win.pageYOffset);
  115. }
  116. if (editor.hasFocus()) {
  117. editor.selection.scrollIntoView(editor.selection.getNode());
  118. }
  119. if (global$1.webkit && deltaSize < 0) {
  120. resize(editor, oldSize);
  121. }
  122. }
  123. };
  124. var setup = function (editor, oldSize) {
  125. editor.on('init', function () {
  126. var overflowPadding = getAutoResizeOverflowPadding(editor);
  127. var dom = editor.dom;
  128. dom.setStyles(editor.getDoc().documentElement, { height: 'auto' });
  129. dom.setStyles(editor.getBody(), {
  130. 'paddingLeft': overflowPadding,
  131. 'paddingRight': overflowPadding,
  132. 'min-height': 0
  133. });
  134. });
  135. editor.on('NodeChange SetContent keyup FullscreenStateChanged ResizeContent', function () {
  136. resize(editor, oldSize);
  137. });
  138. if (shouldAutoResizeOnInit(editor)) {
  139. editor.on('init', function () {
  140. wait(editor, oldSize, 20, 100, function () {
  141. wait(editor, oldSize, 5, 1000);
  142. });
  143. });
  144. }
  145. };
  146. var register = function (editor, oldSize) {
  147. editor.addCommand('mceAutoResize', function () {
  148. resize(editor, oldSize);
  149. });
  150. };
  151. function Plugin () {
  152. global$2.add('autoresize', function (editor) {
  153. if (!has(editor.settings, 'resize')) {
  154. editor.settings.resize = false;
  155. }
  156. if (!editor.inline) {
  157. var oldSize = Cell(0);
  158. register(editor, oldSize);
  159. setup(editor, oldSize);
  160. }
  161. });
  162. }
  163. Plugin();
  164. }());