plugin.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  13. var global$2 = tinymce.util.Tools.resolve('tinymce.util.I18n');
  14. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  15. var getTocClass = function (editor) {
  16. return editor.getParam('toc_class', 'mce-toc');
  17. };
  18. var getTocHeader = function (editor) {
  19. var tagName = editor.getParam('toc_header', 'h2');
  20. return /^h[1-6]$/.test(tagName) ? tagName : 'h2';
  21. };
  22. var getTocDepth = function (editor) {
  23. var depth = parseInt(editor.getParam('toc_depth', '3'), 10);
  24. return depth >= 1 && depth <= 9 ? depth : 3;
  25. };
  26. var Settings = {
  27. getTocClass: getTocClass,
  28. getTocHeader: getTocHeader,
  29. getTocDepth: getTocDepth
  30. };
  31. var create = function (prefix) {
  32. var counter = 0;
  33. return function () {
  34. var guid = new Date().getTime().toString(32);
  35. return prefix + guid + (counter++).toString(32);
  36. };
  37. };
  38. var Guid = { create: create };
  39. var tocId = Guid.create('mcetoc_');
  40. var generateSelector = function generateSelector(depth) {
  41. var i;
  42. var selector = [];
  43. for (i = 1; i <= depth; i++) {
  44. selector.push('h' + i);
  45. }
  46. return selector.join(',');
  47. };
  48. var hasHeaders = function (editor) {
  49. return readHeaders(editor).length > 0;
  50. };
  51. var readHeaders = function (editor) {
  52. var tocClass = Settings.getTocClass(editor);
  53. var headerTag = Settings.getTocHeader(editor);
  54. var selector = generateSelector(Settings.getTocDepth(editor));
  55. var headers = editor.$(selector);
  56. if (headers.length && /^h[1-9]$/i.test(headerTag)) {
  57. headers = headers.filter(function (i, el) {
  58. return !editor.dom.hasClass(el.parentNode, tocClass);
  59. });
  60. }
  61. return global$3.map(headers, function (h) {
  62. return {
  63. id: h.id ? h.id : tocId(),
  64. level: parseInt(h.nodeName.replace(/^H/i, ''), 10),
  65. title: editor.$.text(h),
  66. element: h
  67. };
  68. });
  69. };
  70. var getMinLevel = function (headers) {
  71. var i, minLevel = 9;
  72. for (i = 0; i < headers.length; i++) {
  73. if (headers[i].level < minLevel) {
  74. minLevel = headers[i].level;
  75. }
  76. if (minLevel === 1) {
  77. return minLevel;
  78. }
  79. }
  80. return minLevel;
  81. };
  82. var generateTitle = function (tag, title) {
  83. var openTag = '<' + tag + ' contenteditable="true">';
  84. var closeTag = '</' + tag + '>';
  85. return openTag + global$1.DOM.encode(title) + closeTag;
  86. };
  87. var generateTocHtml = function (editor) {
  88. var html = generateTocContentHtml(editor);
  89. return '<div class="' + editor.dom.encode(Settings.getTocClass(editor)) + '" contenteditable="false">' + html + '</div>';
  90. };
  91. var generateTocContentHtml = function (editor) {
  92. var html = '';
  93. var headers = readHeaders(editor);
  94. var prevLevel = getMinLevel(headers) - 1;
  95. var i, ii, h, nextLevel;
  96. if (!headers.length) {
  97. return '';
  98. }
  99. html += generateTitle(Settings.getTocHeader(editor), global$2.translate('Table of Contents'));
  100. for (i = 0; i < headers.length; i++) {
  101. h = headers[i];
  102. h.element.id = h.id;
  103. nextLevel = headers[i + 1] && headers[i + 1].level;
  104. if (prevLevel === h.level) {
  105. html += '<li>';
  106. } else {
  107. for (ii = prevLevel; ii < h.level; ii++) {
  108. html += '<ul><li>';
  109. }
  110. }
  111. html += '<a href="#' + h.id + '">' + h.title + '</a>';
  112. if (nextLevel === h.level || !nextLevel) {
  113. html += '</li>';
  114. if (!nextLevel) {
  115. html += '</ul>';
  116. }
  117. } else {
  118. for (ii = h.level; ii > nextLevel; ii--) {
  119. html += '</li></ul><li>';
  120. }
  121. }
  122. prevLevel = h.level;
  123. }
  124. return html;
  125. };
  126. var isEmptyOrOffscren = function (editor, nodes) {
  127. return !nodes.length || editor.dom.getParents(nodes[0], '.mce-offscreen-selection').length > 0;
  128. };
  129. var insertToc = function (editor) {
  130. var tocClass = Settings.getTocClass(editor);
  131. var $tocElm = editor.$('.' + tocClass);
  132. if (isEmptyOrOffscren(editor, $tocElm)) {
  133. editor.insertContent(generateTocHtml(editor));
  134. } else {
  135. updateToc(editor);
  136. }
  137. };
  138. var updateToc = function (editor) {
  139. var tocClass = Settings.getTocClass(editor);
  140. var $tocElm = editor.$('.' + tocClass);
  141. if ($tocElm.length) {
  142. editor.undoManager.transact(function () {
  143. $tocElm.html(generateTocContentHtml(editor));
  144. });
  145. }
  146. };
  147. var Toc = {
  148. hasHeaders: hasHeaders,
  149. insertToc: insertToc,
  150. updateToc: updateToc
  151. };
  152. var register = function (editor) {
  153. editor.addCommand('mceInsertToc', function () {
  154. Toc.insertToc(editor);
  155. });
  156. editor.addCommand('mceUpdateToc', function () {
  157. Toc.updateToc(editor);
  158. });
  159. };
  160. var Commands = { register: register };
  161. var setup = function (editor) {
  162. var $ = editor.$, tocClass = Settings.getTocClass(editor);
  163. editor.on('PreProcess', function (e) {
  164. var $tocElm = $('.' + tocClass, e.node);
  165. if ($tocElm.length) {
  166. $tocElm.removeAttr('contentEditable');
  167. $tocElm.find('[contenteditable]').removeAttr('contentEditable');
  168. }
  169. });
  170. editor.on('SetContent', function () {
  171. var $tocElm = $('.' + tocClass);
  172. if ($tocElm.length) {
  173. $tocElm.attr('contentEditable', false);
  174. $tocElm.children(':first-child').attr('contentEditable', true);
  175. }
  176. });
  177. };
  178. var FilterContent = { setup: setup };
  179. var toggleState = function (editor) {
  180. return function (api) {
  181. var toggleDisabledState = function () {
  182. return api.setDisabled(editor.readonly || !Toc.hasHeaders(editor));
  183. };
  184. toggleDisabledState();
  185. editor.on('LoadContent SetContent change', toggleDisabledState);
  186. return function () {
  187. return editor.on('LoadContent SetContent change', toggleDisabledState);
  188. };
  189. };
  190. };
  191. var isToc = function (editor) {
  192. return function (elm) {
  193. return elm && editor.dom.is(elm, '.' + Settings.getTocClass(editor)) && editor.getBody().contains(elm);
  194. };
  195. };
  196. var register$1 = function (editor) {
  197. editor.ui.registry.addButton('toc', {
  198. icon: 'toc',
  199. tooltip: 'Table of contents',
  200. onAction: function () {
  201. return editor.execCommand('mceInsertToc');
  202. },
  203. onSetup: toggleState(editor)
  204. });
  205. editor.ui.registry.addButton('tocupdate', {
  206. icon: 'reload',
  207. tooltip: 'Update',
  208. onAction: function () {
  209. return editor.execCommand('mceUpdateToc');
  210. }
  211. });
  212. editor.ui.registry.addMenuItem('toc', {
  213. icon: 'toc',
  214. text: 'Table of contents',
  215. onAction: function () {
  216. return editor.execCommand('mceInsertToc');
  217. },
  218. onSetup: toggleState(editor)
  219. });
  220. editor.ui.registry.addContextToolbar('toc', {
  221. items: 'tocupdate',
  222. predicate: isToc(editor),
  223. scope: 'node',
  224. position: 'node'
  225. });
  226. };
  227. var Buttons = { register: register$1 };
  228. function Plugin () {
  229. global.add('toc', function (editor) {
  230. Commands.register(editor);
  231. Buttons.register(editor);
  232. FilterContent.setup(editor);
  233. });
  234. }
  235. Plugin();
  236. }());