plugin.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /**
  2. * TinyMCE version 8.0.2 (2025-08-14)
  3. */
  4. (function () {
  5. 'use strict';
  6. /* eslint-disable @typescript-eslint/no-wrapper-object-types */
  7. const hasProto = (v, constructor, predicate) => {
  8. var _a;
  9. if (predicate(v, constructor.prototype)) {
  10. return true;
  11. }
  12. else {
  13. // String-based fallback time
  14. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  15. }
  16. };
  17. const typeOf = (x) => {
  18. const t = typeof x;
  19. if (x === null) {
  20. return 'null';
  21. }
  22. else if (t === 'object' && Array.isArray(x)) {
  23. return 'array';
  24. }
  25. else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  26. return 'string';
  27. }
  28. else {
  29. return t;
  30. }
  31. };
  32. const isType$1 = (type) => (value) => typeOf(value) === type;
  33. const isSimpleType = (type) => (value) => typeof value === type;
  34. const eq = (t) => (a) => t === a;
  35. const isString = isType$1('string');
  36. const isObject = isType$1('object');
  37. const isNull = eq(null);
  38. const isBoolean = isSimpleType('boolean');
  39. const isNullable = (a) => a === null || a === undefined;
  40. const isNonNullable = (a) => !isNullable(a);
  41. const isFunction = isSimpleType('function');
  42. const isNumber = isSimpleType('number');
  43. /**
  44. * The `Optional` type represents a value (of any type) that potentially does
  45. * not exist. Any `Optional<T>` can either be a `Some<T>` (in which case the
  46. * value does exist) or a `None` (in which case the value does not exist). This
  47. * module defines a whole lot of FP-inspired utility functions for dealing with
  48. * `Optional` objects.
  49. *
  50. * Comparison with null or undefined:
  51. * - We don't get fancy null coalescing operators with `Optional`
  52. * - We do get fancy helper functions with `Optional`
  53. * - `Optional` support nesting, and allow for the type to still be nullable (or
  54. * another `Optional`)
  55. * - There is no option to turn off strict-optional-checks like there is for
  56. * strict-null-checks
  57. */
  58. class Optional {
  59. // The internal representation has a `tag` and a `value`, but both are
  60. // private: able to be console.logged, but not able to be accessed by code
  61. constructor(tag, value) {
  62. this.tag = tag;
  63. this.value = value;
  64. }
  65. // --- Identities ---
  66. /**
  67. * Creates a new `Optional<T>` that **does** contain a value.
  68. */
  69. static some(value) {
  70. return new Optional(true, value);
  71. }
  72. /**
  73. * Create a new `Optional<T>` that **does not** contain a value. `T` can be
  74. * any type because we don't actually have a `T`.
  75. */
  76. static none() {
  77. return Optional.singletonNone;
  78. }
  79. /**
  80. * Perform a transform on an `Optional` type. Regardless of whether this
  81. * `Optional` contains a value or not, `fold` will return a value of type `U`.
  82. * If this `Optional` does not contain a value, the `U` will be created by
  83. * calling `onNone`. If this `Optional` does contain a value, the `U` will be
  84. * created by calling `onSome`.
  85. *
  86. * For the FP enthusiasts in the room, this function:
  87. * 1. Could be used to implement all of the functions below
  88. * 2. Forms a catamorphism
  89. */
  90. fold(onNone, onSome) {
  91. if (this.tag) {
  92. return onSome(this.value);
  93. }
  94. else {
  95. return onNone();
  96. }
  97. }
  98. /**
  99. * Determine if this `Optional` object contains a value.
  100. */
  101. isSome() {
  102. return this.tag;
  103. }
  104. /**
  105. * Determine if this `Optional` object **does not** contain a value.
  106. */
  107. isNone() {
  108. return !this.tag;
  109. }
  110. // --- Functor (name stolen from Haskell / maths) ---
  111. /**
  112. * Perform a transform on an `Optional` object, **if** there is a value. If
  113. * you provide a function to turn a T into a U, this is the function you use
  114. * to turn an `Optional<T>` into an `Optional<U>`. If this **does** contain
  115. * a value then the output will also contain a value (that value being the
  116. * output of `mapper(this.value)`), and if this **does not** contain a value
  117. * then neither will the output.
  118. */
  119. map(mapper) {
  120. if (this.tag) {
  121. return Optional.some(mapper(this.value));
  122. }
  123. else {
  124. return Optional.none();
  125. }
  126. }
  127. // --- Monad (name stolen from Haskell / maths) ---
  128. /**
  129. * Perform a transform on an `Optional` object, **if** there is a value.
  130. * Unlike `map`, here the transform itself also returns an `Optional`.
  131. */
  132. bind(binder) {
  133. if (this.tag) {
  134. return binder(this.value);
  135. }
  136. else {
  137. return Optional.none();
  138. }
  139. }
  140. // --- Traversable (name stolen from Haskell / maths) ---
  141. /**
  142. * For a given predicate, this function finds out if there **exists** a value
  143. * inside this `Optional` object that meets the predicate. In practice, this
  144. * means that for `Optional`s that do not contain a value it returns false (as
  145. * no predicate-meeting value exists).
  146. */
  147. exists(predicate) {
  148. return this.tag && predicate(this.value);
  149. }
  150. /**
  151. * For a given predicate, this function finds out if **all** the values inside
  152. * this `Optional` object meet the predicate. In practice, this means that
  153. * for `Optional`s that do not contain a value it returns true (as all 0
  154. * objects do meet the predicate).
  155. */
  156. forall(predicate) {
  157. return !this.tag || predicate(this.value);
  158. }
  159. filter(predicate) {
  160. if (!this.tag || predicate(this.value)) {
  161. return this;
  162. }
  163. else {
  164. return Optional.none();
  165. }
  166. }
  167. // --- Getters ---
  168. /**
  169. * Get the value out of the inside of the `Optional` object, using a default
  170. * `replacement` value if the provided `Optional` object does not contain a
  171. * value.
  172. */
  173. getOr(replacement) {
  174. return this.tag ? this.value : replacement;
  175. }
  176. /**
  177. * Get the value out of the inside of the `Optional` object, using a default
  178. * `replacement` value if the provided `Optional` object does not contain a
  179. * value. Unlike `getOr`, in this method the `replacement` object is also
  180. * `Optional` - meaning that this method will always return an `Optional`.
  181. */
  182. or(replacement) {
  183. return this.tag ? this : replacement;
  184. }
  185. /**
  186. * Get the value out of the inside of the `Optional` object, using a default
  187. * `replacement` value if the provided `Optional` object does not contain a
  188. * value. Unlike `getOr`, in this method the `replacement` value is
  189. * "thunked" - that is to say that you don't pass a value to `getOrThunk`, you
  190. * pass a function which (if called) will **return** the `value` you want to
  191. * use.
  192. */
  193. getOrThunk(thunk) {
  194. return this.tag ? this.value : thunk();
  195. }
  196. /**
  197. * Get the value out of the inside of the `Optional` object, using a default
  198. * `replacement` value if the provided Optional object does not contain a
  199. * value.
  200. *
  201. * Unlike `or`, in this method the `replacement` value is "thunked" - that is
  202. * to say that you don't pass a value to `orThunk`, you pass a function which
  203. * (if called) will **return** the `value` you want to use.
  204. *
  205. * Unlike `getOrThunk`, in this method the `replacement` value is also
  206. * `Optional`, meaning that this method will always return an `Optional`.
  207. */
  208. orThunk(thunk) {
  209. return this.tag ? this : thunk();
  210. }
  211. /**
  212. * Get the value out of the inside of the `Optional` object, throwing an
  213. * exception if the provided `Optional` object does not contain a value.
  214. *
  215. * WARNING:
  216. * You should only be using this function if you know that the `Optional`
  217. * object **is not** empty (otherwise you're throwing exceptions in production
  218. * code, which is bad).
  219. *
  220. * In tests this is more acceptable.
  221. *
  222. * Prefer other methods to this, such as `.each`.
  223. */
  224. getOrDie(message) {
  225. if (!this.tag) {
  226. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  227. }
  228. else {
  229. return this.value;
  230. }
  231. }
  232. // --- Interop with null and undefined ---
  233. /**
  234. * Creates an `Optional` value from a nullable (or undefined-able) input.
  235. * Null, or undefined, is converted to `None`, and anything else is converted
  236. * to `Some`.
  237. */
  238. static from(value) {
  239. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  240. }
  241. /**
  242. * Converts an `Optional` to a nullable type, by getting the value if it
  243. * exists, or returning `null` if it does not.
  244. */
  245. getOrNull() {
  246. return this.tag ? this.value : null;
  247. }
  248. /**
  249. * Converts an `Optional` to an undefined-able type, by getting the value if
  250. * it exists, or returning `undefined` if it does not.
  251. */
  252. getOrUndefined() {
  253. return this.value;
  254. }
  255. // --- Utilities ---
  256. /**
  257. * If the `Optional` contains a value, perform an action on that value.
  258. * Unlike the rest of the methods on this type, `.each` has side-effects. If
  259. * you want to transform an `Optional<T>` **into** something, then this is not
  260. * the method for you. If you want to use an `Optional<T>` to **do**
  261. * something, then this is the method for you - provided you're okay with not
  262. * doing anything in the case where the `Optional` doesn't have a value inside
  263. * it. If you're not sure whether your use-case fits into transforming
  264. * **into** something or **doing** something, check whether it has a return
  265. * value. If it does, you should be performing a transform.
  266. */
  267. each(worker) {
  268. if (this.tag) {
  269. worker(this.value);
  270. }
  271. }
  272. /**
  273. * Turn the `Optional` object into an array that contains all of the values
  274. * stored inside the `Optional`. In practice, this means the output will have
  275. * either 0 or 1 elements.
  276. */
  277. toArray() {
  278. return this.tag ? [this.value] : [];
  279. }
  280. /**
  281. * Turn the `Optional` object into a string for debugging or printing. Not
  282. * recommended for production code, but good for debugging. Also note that
  283. * these days an `Optional` object can be logged to the console directly, and
  284. * its inner value (if it exists) will be visible.
  285. */
  286. toString() {
  287. return this.tag ? `some(${this.value})` : 'none()';
  288. }
  289. }
  290. // Sneaky optimisation: every instance of Optional.none is identical, so just
  291. // reuse the same object
  292. Optional.singletonNone = new Optional(false);
  293. const nativeSlice = Array.prototype.slice;
  294. const map = (xs, f) => {
  295. // pre-allocating array size when it's guaranteed to be known
  296. // http://jsperf.com/push-allocated-vs-dynamic/22
  297. const len = xs.length;
  298. const r = new Array(len);
  299. for (let i = 0; i < len; i++) {
  300. const x = xs[i];
  301. r[i] = f(x, i);
  302. }
  303. return r;
  304. };
  305. // Unwound implementing other functions in terms of each.
  306. // The code size is roughly the same, and it should allow for better optimisation.
  307. // const each = function<T, U>(xs: T[], f: (x: T, i?: number, xs?: T[]) => void): void {
  308. const each$1 = (xs, f) => {
  309. for (let i = 0, len = xs.length; i < len; i++) {
  310. const x = xs[i];
  311. f(x, i);
  312. }
  313. };
  314. const filter = (xs, pred) => {
  315. const r = [];
  316. for (let i = 0, len = xs.length; i < len; i++) {
  317. const x = xs[i];
  318. if (pred(x, i)) {
  319. r.push(x);
  320. }
  321. }
  322. return r;
  323. };
  324. isFunction(Array.from) ? Array.from : (x) => nativeSlice.call(x);
  325. // There are many variations of Object iteration that are faster than the 'for-in' style:
  326. // http://jsperf.com/object-keys-iteration/107
  327. //
  328. // Use the native keys if it is available (IE9+), otherwise fall back to manually filtering
  329. const keys = Object.keys;
  330. const each = (obj, f) => {
  331. const props = keys(obj);
  332. for (let k = 0, len = props.length; k < len; k++) {
  333. const i = props[k];
  334. const x = obj[i];
  335. f(x, i);
  336. }
  337. };
  338. const Cell = (initial) => {
  339. let value = initial;
  340. const get = () => {
  341. return value;
  342. };
  343. const set = (v) => {
  344. value = v;
  345. };
  346. return {
  347. get,
  348. set
  349. };
  350. };
  351. // Use window object as the global if it's available since CSP will block script evals
  352. // eslint-disable-next-line @typescript-eslint/no-implied-eval
  353. const Global = typeof window !== 'undefined' ? window : Function('return this;')();
  354. /** path :: ([String], JsObj?) -> JsObj */
  355. const path = (parts, scope) => {
  356. let o = scope !== undefined && scope !== null ? scope : Global;
  357. for (let i = 0; i < parts.length && o !== undefined && o !== null; ++i) {
  358. o = o[parts[i]];
  359. }
  360. return o;
  361. };
  362. /** resolve :: (String, JsObj?) -> JsObj */
  363. const resolve = (p, scope) => {
  364. const parts = p.split('.');
  365. return path(parts, scope);
  366. };
  367. // Run a function fn after rate ms. If another invocation occurs
  368. // during the time it is waiting, ignore it completely.
  369. const first = (fn, rate) => {
  370. let timer = null;
  371. const cancel = () => {
  372. if (!isNull(timer)) {
  373. clearTimeout(timer);
  374. timer = null;
  375. }
  376. };
  377. const throttle = (...args) => {
  378. if (isNull(timer)) {
  379. timer = setTimeout(() => {
  380. timer = null;
  381. fn.apply(null, args);
  382. }, rate);
  383. }
  384. };
  385. return {
  386. cancel,
  387. throttle
  388. };
  389. };
  390. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  391. const get$2 = (toggleState) => {
  392. const isEnabled = () => {
  393. return toggleState.get();
  394. };
  395. return {
  396. isEnabled
  397. };
  398. };
  399. const fireVisualChars = (editor, state) => {
  400. return editor.dispatch('VisualChars', { state });
  401. };
  402. const fromHtml = (html, scope) => {
  403. const doc = scope || document;
  404. const div = doc.createElement('div');
  405. div.innerHTML = html;
  406. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  407. const message = 'HTML does not have a single root node';
  408. // eslint-disable-next-line no-console
  409. console.error(message, html);
  410. throw new Error(message);
  411. }
  412. return fromDom(div.childNodes[0]);
  413. };
  414. const fromTag = (tag, scope) => {
  415. const doc = scope || document;
  416. const node = doc.createElement(tag);
  417. return fromDom(node);
  418. };
  419. const fromText = (text, scope) => {
  420. const doc = scope || document;
  421. const node = doc.createTextNode(text);
  422. return fromDom(node);
  423. };
  424. const fromDom = (node) => {
  425. // TODO: Consider removing this check, but left atm for safety
  426. if (node === null || node === undefined) {
  427. throw new Error('Node cannot be null or undefined');
  428. }
  429. return {
  430. dom: node
  431. };
  432. };
  433. const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  434. // tslint:disable-next-line:variable-name
  435. const SugarElement = {
  436. fromHtml,
  437. fromTag,
  438. fromText,
  439. fromDom,
  440. fromPoint
  441. };
  442. const ELEMENT = 1;
  443. const TEXT = 3;
  444. const unsafe = (name, scope) => {
  445. return resolve(name, scope);
  446. };
  447. const getOrDie = (name, scope) => {
  448. const actual = unsafe(name, scope);
  449. if (actual === undefined || actual === null) {
  450. throw new Error(name + ' not available on this browser');
  451. }
  452. return actual;
  453. };
  454. const getPrototypeOf = Object.getPrototypeOf;
  455. /*
  456. * IE9 and above
  457. *
  458. * MDN no use on this one, but here's the link anyway:
  459. * https://developer.mozilla.org/en/docs/Web/API/HTMLElement
  460. */
  461. const sandHTMLElement = (scope) => {
  462. return getOrDie('HTMLElement', scope);
  463. };
  464. const isPrototypeOf = (x) => {
  465. // use Resolve to get the window object for x and just return undefined if it can't find it.
  466. // undefined scope later triggers using the global window.
  467. const scope = resolve('ownerDocument.defaultView', x);
  468. // TINY-7374: We can't rely on looking at the owner window HTMLElement as the element may have
  469. // been constructed in a different window and then appended to the current window document.
  470. return isObject(x) && (sandHTMLElement(scope).prototype.isPrototypeOf(x) || /^HTML\w*Element$/.test(getPrototypeOf(x).constructor.name));
  471. };
  472. const type = (element) => element.dom.nodeType;
  473. const value = (element) => element.dom.nodeValue;
  474. const isType = (t) => (element) => type(element) === t;
  475. const isHTMLElement = (element) => isElement(element) && isPrototypeOf(element.dom);
  476. const isElement = isType(ELEMENT);
  477. const isText = isType(TEXT);
  478. const rawSet = (dom, key, value) => {
  479. /*
  480. * JQuery coerced everything to a string, and silently did nothing on text node/null/undefined.
  481. *
  482. * We fail on those invalid cases, only allowing numbers and booleans.
  483. */
  484. if (isString(value) || isBoolean(value) || isNumber(value)) {
  485. dom.setAttribute(key, value + '');
  486. }
  487. else {
  488. // eslint-disable-next-line no-console
  489. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  490. throw new Error('Attribute value was not simple');
  491. }
  492. };
  493. const set = (element, key, value) => {
  494. rawSet(element.dom, key, value);
  495. };
  496. const get$1 = (element, key) => {
  497. const v = element.dom.getAttribute(key);
  498. // undefined is the more appropriate value for JS, and this matches JQuery
  499. return v === null ? undefined : v;
  500. };
  501. const remove$3 = (element, key) => {
  502. element.dom.removeAttribute(key);
  503. };
  504. // Methods for handling attributes that contain a list of values <div foo="alpha beta theta">
  505. const read = (element, attr) => {
  506. const value = get$1(element, attr);
  507. return value === undefined || value === '' ? [] : value.split(' ');
  508. };
  509. const add$2 = (element, attr, id) => {
  510. const old = read(element, attr);
  511. const nu = old.concat([id]);
  512. set(element, attr, nu.join(' '));
  513. return true;
  514. };
  515. const remove$2 = (element, attr, id) => {
  516. const nu = filter(read(element, attr), (v) => v !== id);
  517. if (nu.length > 0) {
  518. set(element, attr, nu.join(' '));
  519. }
  520. else {
  521. remove$3(element, attr);
  522. }
  523. return false;
  524. };
  525. // IE11 Can return undefined for a classList on elements such as math, so we make sure it's not undefined before attempting to use it.
  526. const supports = (element) => element.dom.classList !== undefined;
  527. const get = (element) => read(element, 'class');
  528. const add$1 = (element, clazz) => add$2(element, 'class', clazz);
  529. const remove$1 = (element, clazz) => remove$2(element, 'class', clazz);
  530. /*
  531. * ClassList is IE10 minimum:
  532. * https://developer.mozilla.org/en-US/docs/Web/API/Element.classList
  533. *
  534. * Note that IE doesn't support the second argument to toggle (at all).
  535. * If it did, the toggler could be better.
  536. */
  537. const add = (element, clazz) => {
  538. if (supports(element)) {
  539. element.dom.classList.add(clazz);
  540. }
  541. else {
  542. add$1(element, clazz);
  543. }
  544. };
  545. const cleanClass = (element) => {
  546. const classList = supports(element) ? element.dom.classList : get(element);
  547. // classList is a "live list", so this is up to date already
  548. if (classList.length === 0) {
  549. // No more classes left, remove the class attribute as well
  550. remove$3(element, 'class');
  551. }
  552. };
  553. const remove = (element, clazz) => {
  554. if (supports(element)) {
  555. const classList = element.dom.classList;
  556. classList.remove(clazz);
  557. }
  558. else {
  559. remove$1(element, clazz);
  560. }
  561. cleanClass(element);
  562. };
  563. const getRaw = (element) => element.dom.contentEditable;
  564. const charMap = {
  565. '\u00a0': 'nbsp',
  566. '\u00ad': 'shy'
  567. };
  568. const charMapToRegExp = (charMap, global) => {
  569. let regExp = '';
  570. each(charMap, (_value, key) => {
  571. regExp += key;
  572. });
  573. return new RegExp('[' + regExp + ']', global ? 'g' : '');
  574. };
  575. const charMapToSelector = (charMap) => {
  576. let selector = '';
  577. each(charMap, (value) => {
  578. if (selector) {
  579. selector += ',';
  580. }
  581. selector += 'span.mce-' + value;
  582. });
  583. return selector;
  584. };
  585. const regExp = charMapToRegExp(charMap);
  586. const regExpGlobal = charMapToRegExp(charMap, true);
  587. const selector = charMapToSelector(charMap);
  588. const nbspClass = 'mce-nbsp';
  589. const wrapCharWithSpan = (value) => '<span data-mce-bogus="1" class="mce-' + charMap[value] + '">' + value + '</span>';
  590. const isWrappedNbsp = (node) => node.nodeName.toLowerCase() === 'span' && node.classList.contains('mce-nbsp-wrap');
  591. const isMatch = (n) => {
  592. const value$1 = value(n);
  593. return isText(n) &&
  594. isString(value$1) &&
  595. regExp.test(value$1);
  596. };
  597. const isContentEditableFalse = (node) => isHTMLElement(node) && getRaw(node) === 'false';
  598. const isChildEditable = (node, currentState) => {
  599. if (isHTMLElement(node) && !isWrappedNbsp(node.dom)) {
  600. const value = getRaw(node);
  601. if (value === 'true') {
  602. return true;
  603. }
  604. else if (value === 'false') {
  605. return false;
  606. }
  607. }
  608. return currentState;
  609. };
  610. // inlined sugars PredicateFilter.descendants for file size but also make it only act on editable nodes it changes the current editable state when it traveses down
  611. const filterEditableDescendants = (scope, predicate, editable) => {
  612. let result = [];
  613. const dom = scope.dom;
  614. const children = map(dom.childNodes, SugarElement.fromDom);
  615. const isEditable = (node) => isWrappedNbsp(node.dom) || !isContentEditableFalse(node);
  616. each$1(children, (x) => {
  617. if (editable && isEditable(x) && predicate(x)) {
  618. result = result.concat([x]);
  619. }
  620. result = result.concat(filterEditableDescendants(x, predicate, isChildEditable(x, editable)));
  621. });
  622. return result;
  623. };
  624. const findParentElm = (elm, rootElm) => {
  625. while (elm.parentNode) {
  626. if (elm.parentNode === rootElm) {
  627. return rootElm;
  628. }
  629. elm = elm.parentNode;
  630. }
  631. return undefined;
  632. };
  633. const replaceWithSpans = (text) => text.replace(regExpGlobal, wrapCharWithSpan);
  634. const show = (editor, rootElm) => {
  635. const dom = editor.dom;
  636. const nodeList = filterEditableDescendants(SugarElement.fromDom(rootElm), isMatch, editor.dom.isEditable(rootElm));
  637. each$1(nodeList, (n) => {
  638. var _a;
  639. const parent = n.dom.parentNode;
  640. if (isWrappedNbsp(parent)) {
  641. add(SugarElement.fromDom(parent), nbspClass);
  642. }
  643. else {
  644. const withSpans = replaceWithSpans(dom.encode((_a = value(n)) !== null && _a !== void 0 ? _a : ''));
  645. const div = dom.create('div', {}, withSpans);
  646. let node;
  647. while ((node = div.lastChild)) {
  648. dom.insertAfter(node, n.dom);
  649. }
  650. editor.dom.remove(n.dom);
  651. }
  652. });
  653. };
  654. const hide = (editor, rootElm) => {
  655. const nodeList = editor.dom.select(selector, rootElm);
  656. each$1(nodeList, (node) => {
  657. if (isWrappedNbsp(node)) {
  658. remove(SugarElement.fromDom(node), nbspClass);
  659. }
  660. else {
  661. editor.dom.remove(node, true);
  662. }
  663. });
  664. };
  665. const toggle = (editor) => {
  666. const body = editor.getBody();
  667. const bookmark = editor.selection.getBookmark();
  668. let parentNode = findParentElm(editor.selection.getNode(), body);
  669. // if user does select all the parentNode will be undefined
  670. parentNode = parentNode !== undefined ? parentNode : body;
  671. hide(editor, parentNode);
  672. show(editor, parentNode);
  673. editor.selection.moveToBookmark(bookmark);
  674. };
  675. const applyVisualChars = (editor, toggleState) => {
  676. fireVisualChars(editor, toggleState.get());
  677. const body = editor.getBody();
  678. if (toggleState.get() === true) {
  679. show(editor, body);
  680. }
  681. else {
  682. hide(editor, body);
  683. }
  684. };
  685. // Toggle state and save selection bookmark before applying visualChars
  686. const toggleVisualChars = (editor, toggleState) => {
  687. toggleState.set(!toggleState.get());
  688. const bookmark = editor.selection.getBookmark();
  689. applyVisualChars(editor, toggleState);
  690. editor.selection.moveToBookmark(bookmark);
  691. };
  692. const register$2 = (editor, toggleState) => {
  693. editor.addCommand('mceVisualChars', () => {
  694. toggleVisualChars(editor, toggleState);
  695. });
  696. };
  697. const option = (name) => (editor) => editor.options.get(name);
  698. const register$1 = (editor) => {
  699. const registerOption = editor.options.register;
  700. registerOption('visualchars_default_state', {
  701. processor: 'boolean',
  702. default: false
  703. });
  704. };
  705. const isEnabledByDefault = option('visualchars_default_state');
  706. const setup$1 = (editor, toggleState) => {
  707. /*
  708. Note: applyVisualChars does not place a bookmark before modifying the DOM on init.
  709. This will cause a loss of selection if the following conditions are met:
  710. - Autofocus enabled, or editor is manually focused on init
  711. - The first piece of text in the editor must be a nbsp
  712. - Integrator has manually set the selection before init
  713. Another improvement would be to ensure DOM elements aren't destroyed/recreated,
  714. but rather wrapped/unwrapped when applying styling for visualchars so that selection
  715. is not lost.
  716. */
  717. editor.on('init', () => {
  718. applyVisualChars(editor, toggleState);
  719. });
  720. };
  721. const setup = (editor, toggleState) => {
  722. const debouncedToggle = first(() => {
  723. toggle(editor);
  724. }, 300);
  725. editor.on('keydown', (e) => {
  726. if (toggleState.get() === true) {
  727. e.keyCode === 13 ? toggle(editor) : debouncedToggle.throttle();
  728. }
  729. });
  730. editor.on('remove', debouncedToggle.cancel);
  731. };
  732. const toggleActiveState = (editor, enabledStated) => (api) => {
  733. api.setActive(enabledStated.get());
  734. const editorEventCallback = (e) => api.setActive(e.state);
  735. editor.on('VisualChars', editorEventCallback);
  736. return () => editor.off('VisualChars', editorEventCallback);
  737. };
  738. const register = (editor, toggleState) => {
  739. const onAction = () => editor.execCommand('mceVisualChars');
  740. editor.ui.registry.addToggleButton('visualchars', {
  741. tooltip: 'Show invisible characters',
  742. icon: 'visualchars',
  743. onAction,
  744. onSetup: toggleActiveState(editor, toggleState),
  745. context: 'any'
  746. });
  747. editor.ui.registry.addToggleMenuItem('visualchars', {
  748. text: 'Show invisible characters',
  749. icon: 'visualchars',
  750. onAction,
  751. onSetup: toggleActiveState(editor, toggleState),
  752. context: 'any'
  753. });
  754. };
  755. var Plugin = () => {
  756. global.add('visualchars', (editor) => {
  757. register$1(editor);
  758. const toggleState = Cell(isEnabledByDefault(editor));
  759. register$2(editor, toggleState);
  760. register(editor, toggleState);
  761. setup(editor, toggleState);
  762. setup$1(editor, toggleState);
  763. return get$2(toggleState);
  764. });
  765. };
  766. Plugin();
  767. /** *****
  768. * DO NOT EXPORT ANYTHING
  769. *
  770. * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
  771. *******/
  772. })();