plugin.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.0 (2021-10-11)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var applyListFormat = function (editor, listName, styleValue) {
  13. var cmd = listName === 'UL' ? 'InsertUnorderedList' : 'InsertOrderedList';
  14. editor.execCommand(cmd, false, styleValue === false ? null : { 'list-style-type': styleValue });
  15. };
  16. var register$1 = function (editor) {
  17. editor.addCommand('ApplyUnorderedListStyle', function (ui, value) {
  18. applyListFormat(editor, 'UL', value['list-style-type']);
  19. });
  20. editor.addCommand('ApplyOrderedListStyle', function (ui, value) {
  21. applyListFormat(editor, 'OL', value['list-style-type']);
  22. });
  23. };
  24. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  25. var getNumberStyles = function (editor) {
  26. var styles = editor.getParam('advlist_number_styles', 'default,lower-alpha,lower-greek,lower-roman,upper-alpha,upper-roman');
  27. return styles ? styles.split(/[ ,]/) : [];
  28. };
  29. var getBulletStyles = function (editor) {
  30. var styles = editor.getParam('advlist_bullet_styles', 'default,circle,square');
  31. return styles ? styles.split(/[ ,]/) : [];
  32. };
  33. var noop = function () {
  34. };
  35. var constant = function (value) {
  36. return function () {
  37. return value;
  38. };
  39. };
  40. var identity = function (x) {
  41. return x;
  42. };
  43. var never = constant(false);
  44. var always = constant(true);
  45. var none = function () {
  46. return NONE;
  47. };
  48. var NONE = function () {
  49. var call = function (thunk) {
  50. return thunk();
  51. };
  52. var id = identity;
  53. var me = {
  54. fold: function (n, _s) {
  55. return n();
  56. },
  57. isSome: never,
  58. isNone: always,
  59. getOr: id,
  60. getOrThunk: call,
  61. getOrDie: function (msg) {
  62. throw new Error(msg || 'error: getOrDie called on none.');
  63. },
  64. getOrNull: constant(null),
  65. getOrUndefined: constant(undefined),
  66. or: id,
  67. orThunk: call,
  68. map: none,
  69. each: noop,
  70. bind: none,
  71. exists: never,
  72. forall: always,
  73. filter: function () {
  74. return none();
  75. },
  76. toArray: function () {
  77. return [];
  78. },
  79. toString: constant('none()')
  80. };
  81. return me;
  82. }();
  83. var some = function (a) {
  84. var constant_a = constant(a);
  85. var self = function () {
  86. return me;
  87. };
  88. var bind = function (f) {
  89. return f(a);
  90. };
  91. var me = {
  92. fold: function (n, s) {
  93. return s(a);
  94. },
  95. isSome: always,
  96. isNone: never,
  97. getOr: constant_a,
  98. getOrThunk: constant_a,
  99. getOrDie: constant_a,
  100. getOrNull: constant_a,
  101. getOrUndefined: constant_a,
  102. or: self,
  103. orThunk: self,
  104. map: function (f) {
  105. return some(f(a));
  106. },
  107. each: function (f) {
  108. f(a);
  109. },
  110. bind: bind,
  111. exists: bind,
  112. forall: bind,
  113. filter: function (f) {
  114. return f(a) ? me : NONE;
  115. },
  116. toArray: function () {
  117. return [a];
  118. },
  119. toString: function () {
  120. return 'some(' + a + ')';
  121. }
  122. };
  123. return me;
  124. };
  125. var from = function (value) {
  126. return value === null || value === undefined ? NONE : some(value);
  127. };
  128. var Optional = {
  129. some: some,
  130. none: none,
  131. from: from
  132. };
  133. var isChildOfBody = function (editor, elm) {
  134. return editor.$.contains(editor.getBody(), elm);
  135. };
  136. var isTableCellNode = function (node) {
  137. return node && /^(TH|TD)$/.test(node.nodeName);
  138. };
  139. var isListNode = function (editor) {
  140. return function (node) {
  141. return node && /^(OL|UL|DL)$/.test(node.nodeName) && isChildOfBody(editor, node);
  142. };
  143. };
  144. var getSelectedStyleType = function (editor) {
  145. var listElm = editor.dom.getParent(editor.selection.getNode(), 'ol,ul');
  146. var style = editor.dom.getStyle(listElm, 'listStyleType');
  147. return Optional.from(style);
  148. };
  149. var findIndex = function (list, predicate) {
  150. for (var index = 0; index < list.length; index++) {
  151. var element = list[index];
  152. if (predicate(element)) {
  153. return index;
  154. }
  155. }
  156. return -1;
  157. };
  158. var styleValueToText = function (styleValue) {
  159. return styleValue.replace(/\-/g, ' ').replace(/\b\w/g, function (chr) {
  160. return chr.toUpperCase();
  161. });
  162. };
  163. var isWithinList = function (editor, e, nodeName) {
  164. var tableCellIndex = findIndex(e.parents, isTableCellNode);
  165. var parents = tableCellIndex !== -1 ? e.parents.slice(0, tableCellIndex) : e.parents;
  166. var lists = global.grep(parents, isListNode(editor));
  167. return lists.length > 0 && lists[0].nodeName === nodeName;
  168. };
  169. var makeSetupHandler = function (editor, nodeName) {
  170. return function (api) {
  171. var nodeChangeHandler = function (e) {
  172. api.setActive(isWithinList(editor, e, nodeName));
  173. };
  174. editor.on('NodeChange', nodeChangeHandler);
  175. return function () {
  176. return editor.off('NodeChange', nodeChangeHandler);
  177. };
  178. };
  179. };
  180. var addSplitButton = function (editor, id, tooltip, cmd, nodeName, styles) {
  181. editor.ui.registry.addSplitButton(id, {
  182. tooltip: tooltip,
  183. icon: nodeName === 'OL' ? 'ordered-list' : 'unordered-list',
  184. presets: 'listpreview',
  185. columns: 3,
  186. fetch: function (callback) {
  187. var items = global.map(styles, function (styleValue) {
  188. var iconStyle = nodeName === 'OL' ? 'num' : 'bull';
  189. var iconName = styleValue === 'disc' || styleValue === 'decimal' ? 'default' : styleValue;
  190. var itemValue = styleValue === 'default' ? '' : styleValue;
  191. var displayText = styleValueToText(styleValue);
  192. return {
  193. type: 'choiceitem',
  194. value: itemValue,
  195. icon: 'list-' + iconStyle + '-' + iconName,
  196. text: displayText
  197. };
  198. });
  199. callback(items);
  200. },
  201. onAction: function () {
  202. return editor.execCommand(cmd);
  203. },
  204. onItemAction: function (_splitButtonApi, value) {
  205. applyListFormat(editor, nodeName, value);
  206. },
  207. select: function (value) {
  208. var listStyleType = getSelectedStyleType(editor);
  209. return listStyleType.map(function (listStyle) {
  210. return value === listStyle;
  211. }).getOr(false);
  212. },
  213. onSetup: makeSetupHandler(editor, nodeName)
  214. });
  215. };
  216. var addButton = function (editor, id, tooltip, cmd, nodeName, _styles) {
  217. editor.ui.registry.addToggleButton(id, {
  218. active: false,
  219. tooltip: tooltip,
  220. icon: nodeName === 'OL' ? 'ordered-list' : 'unordered-list',
  221. onSetup: makeSetupHandler(editor, nodeName),
  222. onAction: function () {
  223. return editor.execCommand(cmd);
  224. }
  225. });
  226. };
  227. var addControl = function (editor, id, tooltip, cmd, nodeName, styles) {
  228. if (styles.length > 1) {
  229. addSplitButton(editor, id, tooltip, cmd, nodeName, styles);
  230. } else {
  231. addButton(editor, id, tooltip, cmd, nodeName);
  232. }
  233. };
  234. var register = function (editor) {
  235. addControl(editor, 'numlist', 'Numbered list', 'InsertOrderedList', 'OL', getNumberStyles(editor));
  236. addControl(editor, 'bullist', 'Bullet list', 'InsertUnorderedList', 'UL', getBulletStyles(editor));
  237. };
  238. function Plugin () {
  239. global$1.add('advlist', function (editor) {
  240. if (editor.hasPlugin('lists')) {
  241. register(editor);
  242. register$1(editor);
  243. } else {
  244. console.error('Please use the Lists plugin together with the Advanced List plugin.');
  245. }
  246. });
  247. }
  248. Plugin();
  249. }());