plugin.js 8.7 KB

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