plugin.js 9.7 KB

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