plugin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. * TinyMCE version 8.0.2 (2025-08-14)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. const applyListFormat = (editor, listName, styleValue) => {
  8. const cmd = listName === 'UL' ? 'InsertUnorderedList' : 'InsertOrderedList';
  9. editor.execCommand(cmd, false, styleValue === false ? null : { 'list-style-type': styleValue });
  10. };
  11. const register$2 = (editor) => {
  12. editor.addCommand('ApplyUnorderedListStyle', (ui, value) => {
  13. applyListFormat(editor, 'UL', value['list-style-type']);
  14. });
  15. editor.addCommand('ApplyOrderedListStyle', (ui, value) => {
  16. applyListFormat(editor, 'OL', value['list-style-type']);
  17. });
  18. };
  19. const option = (name) => (editor) => editor.options.get(name);
  20. const register$1 = (editor) => {
  21. const registerOption = editor.options.register;
  22. registerOption('advlist_number_styles', {
  23. processor: 'string[]',
  24. default: 'default,lower-alpha,lower-greek,lower-roman,upper-alpha,upper-roman'.split(',')
  25. });
  26. registerOption('advlist_bullet_styles', {
  27. processor: 'string[]',
  28. default: 'default,disc,circle,square'.split(',')
  29. });
  30. };
  31. const getNumberStyles = option('advlist_number_styles');
  32. const getBulletStyles = option('advlist_bullet_styles');
  33. /* eslint-disable @typescript-eslint/no-wrapper-object-types */
  34. const isNullable = (a) => a === null || a === undefined;
  35. const isNonNullable = (a) => !isNullable(a);
  36. /**
  37. * The `Optional` type represents a value (of any type) that potentially does
  38. * not exist. Any `Optional<T>` can either be a `Some<T>` (in which case the
  39. * value does exist) or a `None` (in which case the value does not exist). This
  40. * module defines a whole lot of FP-inspired utility functions for dealing with
  41. * `Optional` objects.
  42. *
  43. * Comparison with null or undefined:
  44. * - We don't get fancy null coalescing operators with `Optional`
  45. * - We do get fancy helper functions with `Optional`
  46. * - `Optional` support nesting, and allow for the type to still be nullable (or
  47. * another `Optional`)
  48. * - There is no option to turn off strict-optional-checks like there is for
  49. * strict-null-checks
  50. */
  51. class Optional {
  52. // The internal representation has a `tag` and a `value`, but both are
  53. // private: able to be console.logged, but not able to be accessed by code
  54. constructor(tag, value) {
  55. this.tag = tag;
  56. this.value = value;
  57. }
  58. // --- Identities ---
  59. /**
  60. * Creates a new `Optional<T>` that **does** contain a value.
  61. */
  62. static some(value) {
  63. return new Optional(true, value);
  64. }
  65. /**
  66. * Create a new `Optional<T>` that **does not** contain a value. `T` can be
  67. * any type because we don't actually have a `T`.
  68. */
  69. static none() {
  70. return Optional.singletonNone;
  71. }
  72. /**
  73. * Perform a transform on an `Optional` type. Regardless of whether this
  74. * `Optional` contains a value or not, `fold` will return a value of type `U`.
  75. * If this `Optional` does not contain a value, the `U` will be created by
  76. * calling `onNone`. If this `Optional` does contain a value, the `U` will be
  77. * created by calling `onSome`.
  78. *
  79. * For the FP enthusiasts in the room, this function:
  80. * 1. Could be used to implement all of the functions below
  81. * 2. Forms a catamorphism
  82. */
  83. fold(onNone, onSome) {
  84. if (this.tag) {
  85. return onSome(this.value);
  86. }
  87. else {
  88. return onNone();
  89. }
  90. }
  91. /**
  92. * Determine if this `Optional` object contains a value.
  93. */
  94. isSome() {
  95. return this.tag;
  96. }
  97. /**
  98. * Determine if this `Optional` object **does not** contain a value.
  99. */
  100. isNone() {
  101. return !this.tag;
  102. }
  103. // --- Functor (name stolen from Haskell / maths) ---
  104. /**
  105. * Perform a transform on an `Optional` object, **if** there is a value. If
  106. * you provide a function to turn a T into a U, this is the function you use
  107. * to turn an `Optional<T>` into an `Optional<U>`. If this **does** contain
  108. * a value then the output will also contain a value (that value being the
  109. * output of `mapper(this.value)`), and if this **does not** contain a value
  110. * then neither will the output.
  111. */
  112. map(mapper) {
  113. if (this.tag) {
  114. return Optional.some(mapper(this.value));
  115. }
  116. else {
  117. return Optional.none();
  118. }
  119. }
  120. // --- Monad (name stolen from Haskell / maths) ---
  121. /**
  122. * Perform a transform on an `Optional` object, **if** there is a value.
  123. * Unlike `map`, here the transform itself also returns an `Optional`.
  124. */
  125. bind(binder) {
  126. if (this.tag) {
  127. return binder(this.value);
  128. }
  129. else {
  130. return Optional.none();
  131. }
  132. }
  133. // --- Traversable (name stolen from Haskell / maths) ---
  134. /**
  135. * For a given predicate, this function finds out if there **exists** a value
  136. * inside this `Optional` object that meets the predicate. In practice, this
  137. * means that for `Optional`s that do not contain a value it returns false (as
  138. * no predicate-meeting value exists).
  139. */
  140. exists(predicate) {
  141. return this.tag && predicate(this.value);
  142. }
  143. /**
  144. * For a given predicate, this function finds out if **all** the values inside
  145. * this `Optional` object meet the predicate. In practice, this means that
  146. * for `Optional`s that do not contain a value it returns true (as all 0
  147. * objects do meet the predicate).
  148. */
  149. forall(predicate) {
  150. return !this.tag || predicate(this.value);
  151. }
  152. filter(predicate) {
  153. if (!this.tag || predicate(this.value)) {
  154. return this;
  155. }
  156. else {
  157. return Optional.none();
  158. }
  159. }
  160. // --- Getters ---
  161. /**
  162. * Get the value out of the inside of the `Optional` object, using a default
  163. * `replacement` value if the provided `Optional` object does not contain a
  164. * value.
  165. */
  166. getOr(replacement) {
  167. return this.tag ? this.value : replacement;
  168. }
  169. /**
  170. * Get the value out of the inside of the `Optional` object, using a default
  171. * `replacement` value if the provided `Optional` object does not contain a
  172. * value. Unlike `getOr`, in this method the `replacement` object is also
  173. * `Optional` - meaning that this method will always return an `Optional`.
  174. */
  175. or(replacement) {
  176. return this.tag ? this : replacement;
  177. }
  178. /**
  179. * Get the value out of the inside of the `Optional` object, using a default
  180. * `replacement` value if the provided `Optional` object does not contain a
  181. * value. Unlike `getOr`, in this method the `replacement` value is
  182. * "thunked" - that is to say that you don't pass a value to `getOrThunk`, you
  183. * pass a function which (if called) will **return** the `value` you want to
  184. * use.
  185. */
  186. getOrThunk(thunk) {
  187. return this.tag ? this.value : thunk();
  188. }
  189. /**
  190. * Get the value out of the inside of the `Optional` object, using a default
  191. * `replacement` value if the provided Optional object does not contain a
  192. * value.
  193. *
  194. * Unlike `or`, in this method the `replacement` value is "thunked" - that is
  195. * to say that you don't pass a value to `orThunk`, you pass a function which
  196. * (if called) will **return** the `value` you want to use.
  197. *
  198. * Unlike `getOrThunk`, in this method the `replacement` value is also
  199. * `Optional`, meaning that this method will always return an `Optional`.
  200. */
  201. orThunk(thunk) {
  202. return this.tag ? this : thunk();
  203. }
  204. /**
  205. * Get the value out of the inside of the `Optional` object, throwing an
  206. * exception if the provided `Optional` object does not contain a value.
  207. *
  208. * WARNING:
  209. * You should only be using this function if you know that the `Optional`
  210. * object **is not** empty (otherwise you're throwing exceptions in production
  211. * code, which is bad).
  212. *
  213. * In tests this is more acceptable.
  214. *
  215. * Prefer other methods to this, such as `.each`.
  216. */
  217. getOrDie(message) {
  218. if (!this.tag) {
  219. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  220. }
  221. else {
  222. return this.value;
  223. }
  224. }
  225. // --- Interop with null and undefined ---
  226. /**
  227. * Creates an `Optional` value from a nullable (or undefined-able) input.
  228. * Null, or undefined, is converted to `None`, and anything else is converted
  229. * to `Some`.
  230. */
  231. static from(value) {
  232. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  233. }
  234. /**
  235. * Converts an `Optional` to a nullable type, by getting the value if it
  236. * exists, or returning `null` if it does not.
  237. */
  238. getOrNull() {
  239. return this.tag ? this.value : null;
  240. }
  241. /**
  242. * Converts an `Optional` to an undefined-able type, by getting the value if
  243. * it exists, or returning `undefined` if it does not.
  244. */
  245. getOrUndefined() {
  246. return this.value;
  247. }
  248. // --- Utilities ---
  249. /**
  250. * If the `Optional` contains a value, perform an action on that value.
  251. * Unlike the rest of the methods on this type, `.each` has side-effects. If
  252. * you want to transform an `Optional<T>` **into** something, then this is not
  253. * the method for you. If you want to use an `Optional<T>` to **do**
  254. * something, then this is the method for you - provided you're okay with not
  255. * doing anything in the case where the `Optional` doesn't have a value inside
  256. * it. If you're not sure whether your use-case fits into transforming
  257. * **into** something or **doing** something, check whether it has a return
  258. * value. If it does, you should be performing a transform.
  259. */
  260. each(worker) {
  261. if (this.tag) {
  262. worker(this.value);
  263. }
  264. }
  265. /**
  266. * Turn the `Optional` object into an array that contains all of the values
  267. * stored inside the `Optional`. In practice, this means the output will have
  268. * either 0 or 1 elements.
  269. */
  270. toArray() {
  271. return this.tag ? [this.value] : [];
  272. }
  273. /**
  274. * Turn the `Optional` object into a string for debugging or printing. Not
  275. * recommended for production code, but good for debugging. Also note that
  276. * these days an `Optional` object can be logged to the console directly, and
  277. * its inner value (if it exists) will be visible.
  278. */
  279. toString() {
  280. return this.tag ? `some(${this.value})` : 'none()';
  281. }
  282. }
  283. // Sneaky optimisation: every instance of Optional.none is identical, so just
  284. // reuse the same object
  285. Optional.singletonNone = new Optional(false);
  286. const nativeIndexOf = Array.prototype.indexOf;
  287. const rawIndexOf = (ts, t) => nativeIndexOf.call(ts, t);
  288. const contains = (xs, x) => rawIndexOf(xs, x) > -1;
  289. const findUntil = (xs, pred, until) => {
  290. for (let i = 0, len = xs.length; i < len; i++) {
  291. const x = xs[i];
  292. if (pred(x, i)) {
  293. return Optional.some(x);
  294. }
  295. else if (until(x, i)) {
  296. break;
  297. }
  298. }
  299. return Optional.none();
  300. };
  301. // There are many variations of Object iteration that are faster than the 'for-in' style:
  302. // http://jsperf.com/object-keys-iteration/107
  303. //
  304. // Use the native keys if it is available (IE9+), otherwise fall back to manually filtering
  305. const keys = Object.keys;
  306. const each = (obj, f) => {
  307. const props = keys(obj);
  308. for (let k = 0, len = props.length; k < len; k++) {
  309. const i = props[k];
  310. const x = obj[i];
  311. f(x, i);
  312. }
  313. };
  314. const map = (obj, f) => {
  315. return tupleMap(obj, (x, i) => ({
  316. k: i,
  317. v: f(x, i)
  318. }));
  319. };
  320. const tupleMap = (obj, f) => {
  321. const r = {};
  322. each(obj, (x, i) => {
  323. const tuple = f(x, i);
  324. r[tuple.k] = tuple.v;
  325. });
  326. return r;
  327. };
  328. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  329. const isCustomList = (list) => /\btox\-/.test(list.className);
  330. const isChildOfBody = (editor, elm) => {
  331. return editor.dom.isChildOf(elm, editor.getBody());
  332. };
  333. const matchNodeNames = (regex) => (node) => isNonNullable(node) && regex.test(node.nodeName);
  334. const isListNode = matchNodeNames(/^(OL|UL|DL)$/);
  335. const isTableCellNode = matchNodeNames(/^(TH|TD)$/);
  336. const inList = (editor, parents, nodeName) => findUntil(parents, (parent) => isListNode(parent) && !isCustomList(parent), isTableCellNode)
  337. .exists((list) => list.nodeName === nodeName && isChildOfBody(editor, list));
  338. const getSelectedStyleType = (editor) => {
  339. const listElm = editor.dom.getParent(editor.selection.getNode(), 'ol,ul');
  340. const style = editor.dom.getStyle(listElm, 'listStyleType');
  341. return Optional.from(style);
  342. };
  343. // Lists/core/Util.ts - Duplicated in Lists plugin
  344. const isWithinNonEditable = (editor, element) => element !== null && !editor.dom.isEditable(element);
  345. const isWithinNonEditableList = (editor, element) => {
  346. const parentList = editor.dom.getParent(element, 'ol,ul,dl');
  347. return isWithinNonEditable(editor, parentList) || !editor.selection.isEditable();
  348. };
  349. const setNodeChangeHandler = (editor, nodeChangeHandler) => {
  350. const initialNode = editor.selection.getNode();
  351. // Set the initial state
  352. nodeChangeHandler({
  353. parents: editor.dom.getParents(initialNode),
  354. element: initialNode
  355. });
  356. editor.on('NodeChange', nodeChangeHandler);
  357. return () => editor.off('NodeChange', nodeChangeHandler);
  358. };
  359. // <ListStyles>
  360. const styleValueToText = (styleValue) => {
  361. return styleValue.replace(/\-/g, ' ').replace(/\b\w/g, (chr) => {
  362. return chr.toUpperCase();
  363. });
  364. };
  365. const normalizeStyleValue = (styleValue) => isNullable(styleValue) || styleValue === 'default' ? '' : styleValue;
  366. const makeSetupHandler = (editor, nodeName) => (api) => {
  367. const updateButtonState = (editor, parents) => {
  368. const element = editor.selection.getStart(true);
  369. api.setActive(inList(editor, parents, nodeName));
  370. api.setEnabled(!isWithinNonEditableList(editor, element));
  371. };
  372. const nodeChangeHandler = (e) => updateButtonState(editor, e.parents);
  373. return setNodeChangeHandler(editor, nodeChangeHandler);
  374. };
  375. const addSplitButton = (editor, id, tooltip, cmd, nodeName, styles) => {
  376. const listStyleTypeAliases = {
  377. 'lower-latin': 'lower-alpha',
  378. 'upper-latin': 'upper-alpha',
  379. 'lower-alpha': 'lower-latin',
  380. 'upper-alpha': 'upper-latin'
  381. };
  382. const stylesContainsAliasMap = map(listStyleTypeAliases, (alias) => contains(styles, alias));
  383. editor.ui.registry.addSplitButton(id, {
  384. tooltip,
  385. chevronTooltip: `${tooltip} menu`,
  386. icon: nodeName === "OL" /* ListType.OrderedList */ ? 'ordered-list' : 'unordered-list',
  387. presets: 'listpreview',
  388. columns: nodeName === "OL" /* ListType.OrderedList */ ? 3 : 4,
  389. fetch: (callback) => {
  390. const items = global.map(styles, (styleValue) => {
  391. const iconStyle = nodeName === "OL" /* ListType.OrderedList */ ? 'num' : 'bull';
  392. const iconName = styleValue === 'decimal' ? 'default' : styleValue;
  393. const itemValue = normalizeStyleValue(styleValue);
  394. const displayText = styleValueToText(styleValue);
  395. return {
  396. type: 'choiceitem',
  397. value: itemValue,
  398. icon: 'list-' + iconStyle + '-' + iconName,
  399. text: displayText
  400. };
  401. });
  402. callback(items);
  403. },
  404. onAction: () => editor.execCommand(cmd),
  405. onItemAction: (_splitButtonApi, value) => {
  406. applyListFormat(editor, nodeName, value);
  407. },
  408. select: (value) => {
  409. const listStyleType = getSelectedStyleType(editor);
  410. return listStyleType.exists((listStyle) => value === listStyle || (listStyleTypeAliases[listStyle] === value && !stylesContainsAliasMap[value]));
  411. },
  412. onSetup: makeSetupHandler(editor, nodeName)
  413. });
  414. };
  415. const addButton = (editor, id, tooltip, cmd, nodeName, styleValue) => {
  416. editor.ui.registry.addToggleButton(id, {
  417. active: false,
  418. tooltip,
  419. icon: nodeName === "OL" /* ListType.OrderedList */ ? 'ordered-list' : 'unordered-list',
  420. onSetup: makeSetupHandler(editor, nodeName),
  421. // Need to make sure the button removes rather than applies if a list of the same type is selected
  422. onAction: () => editor.queryCommandState(cmd) || styleValue === '' ? editor.execCommand(cmd) : applyListFormat(editor, nodeName, styleValue)
  423. });
  424. };
  425. const addControl = (editor, id, tooltip, cmd, nodeName, styles) => {
  426. if (styles.length > 1) {
  427. addSplitButton(editor, id, tooltip, cmd, nodeName, styles);
  428. }
  429. else {
  430. addButton(editor, id, tooltip, cmd, nodeName, normalizeStyleValue(styles[0]));
  431. }
  432. };
  433. const register = (editor) => {
  434. addControl(editor, 'numlist', 'Numbered list', 'InsertOrderedList', "OL" /* ListType.OrderedList */, getNumberStyles(editor));
  435. addControl(editor, 'bullist', 'Bullet list', 'InsertUnorderedList', "UL" /* ListType.UnorderedList */, getBulletStyles(editor));
  436. };
  437. var Plugin = () => {
  438. global$1.add('advlist', (editor) => {
  439. if (editor.hasPlugin('lists')) {
  440. register$1(editor);
  441. register(editor);
  442. register$2(editor);
  443. }
  444. else {
  445. // eslint-disable-next-line no-console
  446. console.error('Please use the Lists plugin together with the List Styles plugin.');
  447. }
  448. });
  449. };
  450. Plugin();
  451. /** *****
  452. * DO NOT EXPORT ANYTHING
  453. *
  454. * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
  455. *******/
  456. })();