plugin.js 9.2 KB

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