plugin.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.0.1 (2019-02-21)
  8. */
  9. (function () {
  10. var autoresize = (function () {
  11. 'use strict';
  12. var Cell = function (initial) {
  13. var value = initial;
  14. var get = function () {
  15. return value;
  16. };
  17. var set = function (v) {
  18. value = v;
  19. };
  20. var clone = function () {
  21. return Cell(get());
  22. };
  23. return {
  24. get: get,
  25. set: set,
  26. clone: clone
  27. };
  28. };
  29. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  30. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  31. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  32. var getAutoResizeMinHeight = function (editor) {
  33. return editor.getParam('min_height', editor.getElement().offsetHeight, 'number');
  34. };
  35. var getAutoResizeMaxHeight = function (editor) {
  36. return editor.getParam('max_height', 0, 'number');
  37. };
  38. var getAutoResizeOverflowPadding = function (editor) {
  39. return editor.getParam('autoresize_overflow_padding', 1, 'number');
  40. };
  41. var getAutoResizeBottomMargin = function (editor) {
  42. return editor.getParam('autoresize_bottom_margin', 50, 'number');
  43. };
  44. var shouldAutoResizeOnInit = function (editor) {
  45. return editor.getParam('autoresize_on_init', true, 'boolean');
  46. };
  47. var Settings = {
  48. getAutoResizeMinHeight: getAutoResizeMinHeight,
  49. getAutoResizeMaxHeight: getAutoResizeMaxHeight,
  50. getAutoResizeOverflowPadding: getAutoResizeOverflowPadding,
  51. getAutoResizeBottomMargin: getAutoResizeBottomMargin,
  52. shouldAutoResizeOnInit: shouldAutoResizeOnInit
  53. };
  54. var isFullscreen = function (editor) {
  55. return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
  56. };
  57. var wait = function (editor, oldSize, times, interval, callback) {
  58. global$2.setEditorTimeout(editor, function () {
  59. resize(editor, oldSize);
  60. if (times--) {
  61. wait(editor, oldSize, times, interval, callback);
  62. } else if (callback) {
  63. callback();
  64. }
  65. }, interval);
  66. };
  67. var toggleScrolling = function (editor, state) {
  68. var body = editor.getBody();
  69. if (body) {
  70. body.style.overflowY = state ? '' : 'hidden';
  71. if (!state) {
  72. body.scrollTop = 0;
  73. }
  74. }
  75. };
  76. var parseCssValueToInt = function (dom, elm, name, computed) {
  77. var value = parseInt(dom.getStyle(elm, name, computed), 10);
  78. return isNaN(value) ? 0 : value;
  79. };
  80. var resize = function (editor, oldSize) {
  81. var deltaSize, resizeHeight, contentHeight;
  82. var dom = editor.dom;
  83. var doc = editor.getDoc();
  84. if (!doc) {
  85. return;
  86. }
  87. if (isFullscreen(editor)) {
  88. toggleScrolling(editor, true);
  89. return;
  90. }
  91. var body = doc.body;
  92. resizeHeight = Settings.getAutoResizeMinHeight(editor);
  93. var marginTop = parseCssValueToInt(dom, body, 'margin-top', true);
  94. var marginBottom = parseCssValueToInt(dom, body, 'margin-bottom', true);
  95. contentHeight = body.offsetHeight + marginTop + marginBottom;
  96. if (contentHeight < 0) {
  97. contentHeight = 0;
  98. }
  99. var containerHeight = editor.getContainer().offsetHeight;
  100. var contentAreaHeight = editor.getContentAreaContainer().offsetHeight;
  101. var chromeHeight = containerHeight - contentAreaHeight;
  102. if (contentHeight + chromeHeight > Settings.getAutoResizeMinHeight(editor)) {
  103. resizeHeight = contentHeight + chromeHeight;
  104. }
  105. var maxHeight = Settings.getAutoResizeMaxHeight(editor);
  106. if (maxHeight && resizeHeight > maxHeight) {
  107. resizeHeight = maxHeight;
  108. toggleScrolling(editor, true);
  109. } else {
  110. toggleScrolling(editor, false);
  111. }
  112. if (resizeHeight !== oldSize.get()) {
  113. deltaSize = resizeHeight - oldSize.get();
  114. dom.setStyle(editor.getContainer(), 'height', resizeHeight + 'px');
  115. oldSize.set(resizeHeight);
  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 = Settings.getAutoResizeOverflowPadding(editor);
  124. var bottomMargin = Settings.getAutoResizeBottomMargin(editor);
  125. var dom = editor.dom;
  126. dom.setStyles(editor.getBody(), {
  127. 'paddingLeft': overflowPadding,
  128. 'paddingRight': overflowPadding,
  129. 'paddingBottom': bottomMargin,
  130. 'min-height': 0
  131. });
  132. });
  133. editor.on('nodechange setcontent keyup FullscreenStateChanged', function (e) {
  134. resize(editor, oldSize);
  135. });
  136. if (Settings.shouldAutoResizeOnInit(editor)) {
  137. editor.on('init', function () {
  138. wait(editor, oldSize, 20, 100, function () {
  139. wait(editor, oldSize, 5, 1000);
  140. });
  141. });
  142. }
  143. };
  144. var Resize = {
  145. setup: setup,
  146. resize: resize
  147. };
  148. var register = function (editor, oldSize) {
  149. editor.addCommand('mceAutoResize', function () {
  150. Resize.resize(editor, oldSize);
  151. });
  152. };
  153. var Commands = { register: register };
  154. global.add('autoresize', function (editor) {
  155. if (!editor.settings.hasOwnProperty('resize')) {
  156. editor.settings.resize = false;
  157. }
  158. if (!editor.inline) {
  159. var oldSize = Cell(0);
  160. Commands.register(editor, oldSize);
  161. Resize.setup(editor, oldSize);
  162. }
  163. });
  164. function Plugin () {
  165. }
  166. return Plugin;
  167. }());
  168. })();