plugin.js 5.6 KB

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