plugin.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /**
  2. * TinyMCE version 6.4.2 (2023-04-26)
  3. */
  4. (function () {
  5. 'use strict';
  6. const Cell = initial => {
  7. let value = initial;
  8. const get = () => {
  9. return value;
  10. };
  11. const set = v => {
  12. value = v;
  13. };
  14. return {
  15. get,
  16. set
  17. };
  18. };
  19. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  20. const get$2 = toggleState => {
  21. const isEnabled = () => {
  22. return toggleState.get();
  23. };
  24. return { isEnabled };
  25. };
  26. const fireVisualChars = (editor, state) => {
  27. return editor.dispatch('VisualChars', { state });
  28. };
  29. const hasProto = (v, constructor, predicate) => {
  30. var _a;
  31. if (predicate(v, constructor.prototype)) {
  32. return true;
  33. } else {
  34. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  35. }
  36. };
  37. const typeOf = x => {
  38. const t = typeof x;
  39. if (x === null) {
  40. return 'null';
  41. } else if (t === 'object' && Array.isArray(x)) {
  42. return 'array';
  43. } else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  44. return 'string';
  45. } else {
  46. return t;
  47. }
  48. };
  49. const isType$1 = type => value => typeOf(value) === type;
  50. const isSimpleType = type => value => typeof value === type;
  51. const eq = t => a => t === a;
  52. const isString = isType$1('string');
  53. const isObject = isType$1('object');
  54. const isNull = eq(null);
  55. const isBoolean = isSimpleType('boolean');
  56. const isNullable = a => a === null || a === undefined;
  57. const isNonNullable = a => !isNullable(a);
  58. const isNumber = isSimpleType('number');
  59. class Optional {
  60. constructor(tag, value) {
  61. this.tag = tag;
  62. this.value = value;
  63. }
  64. static some(value) {
  65. return new Optional(true, value);
  66. }
  67. static none() {
  68. return Optional.singletonNone;
  69. }
  70. fold(onNone, onSome) {
  71. if (this.tag) {
  72. return onSome(this.value);
  73. } else {
  74. return onNone();
  75. }
  76. }
  77. isSome() {
  78. return this.tag;
  79. }
  80. isNone() {
  81. return !this.tag;
  82. }
  83. map(mapper) {
  84. if (this.tag) {
  85. return Optional.some(mapper(this.value));
  86. } else {
  87. return Optional.none();
  88. }
  89. }
  90. bind(binder) {
  91. if (this.tag) {
  92. return binder(this.value);
  93. } else {
  94. return Optional.none();
  95. }
  96. }
  97. exists(predicate) {
  98. return this.tag && predicate(this.value);
  99. }
  100. forall(predicate) {
  101. return !this.tag || predicate(this.value);
  102. }
  103. filter(predicate) {
  104. if (!this.tag || predicate(this.value)) {
  105. return this;
  106. } else {
  107. return Optional.none();
  108. }
  109. }
  110. getOr(replacement) {
  111. return this.tag ? this.value : replacement;
  112. }
  113. or(replacement) {
  114. return this.tag ? this : replacement;
  115. }
  116. getOrThunk(thunk) {
  117. return this.tag ? this.value : thunk();
  118. }
  119. orThunk(thunk) {
  120. return this.tag ? this : thunk();
  121. }
  122. getOrDie(message) {
  123. if (!this.tag) {
  124. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  125. } else {
  126. return this.value;
  127. }
  128. }
  129. static from(value) {
  130. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  131. }
  132. getOrNull() {
  133. return this.tag ? this.value : null;
  134. }
  135. getOrUndefined() {
  136. return this.value;
  137. }
  138. each(worker) {
  139. if (this.tag) {
  140. worker(this.value);
  141. }
  142. }
  143. toArray() {
  144. return this.tag ? [this.value] : [];
  145. }
  146. toString() {
  147. return this.tag ? `some(${ this.value })` : 'none()';
  148. }
  149. }
  150. Optional.singletonNone = new Optional(false);
  151. const map = (xs, f) => {
  152. const len = xs.length;
  153. const r = new Array(len);
  154. for (let i = 0; i < len; i++) {
  155. const x = xs[i];
  156. r[i] = f(x, i);
  157. }
  158. return r;
  159. };
  160. const each$1 = (xs, f) => {
  161. for (let i = 0, len = xs.length; i < len; i++) {
  162. const x = xs[i];
  163. f(x, i);
  164. }
  165. };
  166. const filter = (xs, pred) => {
  167. const r = [];
  168. for (let i = 0, len = xs.length; i < len; i++) {
  169. const x = xs[i];
  170. if (pred(x, i)) {
  171. r.push(x);
  172. }
  173. }
  174. return r;
  175. };
  176. const keys = Object.keys;
  177. const each = (obj, f) => {
  178. const props = keys(obj);
  179. for (let k = 0, len = props.length; k < len; k++) {
  180. const i = props[k];
  181. const x = obj[i];
  182. f(x, i);
  183. }
  184. };
  185. const Global = typeof window !== 'undefined' ? window : Function('return this;')();
  186. const path = (parts, scope) => {
  187. let o = scope !== undefined && scope !== null ? scope : Global;
  188. for (let i = 0; i < parts.length && o !== undefined && o !== null; ++i) {
  189. o = o[parts[i]];
  190. }
  191. return o;
  192. };
  193. const resolve = (p, scope) => {
  194. const parts = p.split('.');
  195. return path(parts, scope);
  196. };
  197. const unsafe = (name, scope) => {
  198. return resolve(name, scope);
  199. };
  200. const getOrDie = (name, scope) => {
  201. const actual = unsafe(name, scope);
  202. if (actual === undefined || actual === null) {
  203. throw new Error(name + ' not available on this browser');
  204. }
  205. return actual;
  206. };
  207. const getPrototypeOf = Object.getPrototypeOf;
  208. const sandHTMLElement = scope => {
  209. return getOrDie('HTMLElement', scope);
  210. };
  211. const isPrototypeOf = x => {
  212. const scope = resolve('ownerDocument.defaultView', x);
  213. return isObject(x) && (sandHTMLElement(scope).prototype.isPrototypeOf(x) || /^HTML\w*Element$/.test(getPrototypeOf(x).constructor.name));
  214. };
  215. const ELEMENT = 1;
  216. const TEXT = 3;
  217. const type = element => element.dom.nodeType;
  218. const value = element => element.dom.nodeValue;
  219. const isType = t => element => type(element) === t;
  220. const isHTMLElement = element => isElement(element) && isPrototypeOf(element.dom);
  221. const isElement = isType(ELEMENT);
  222. const isText = isType(TEXT);
  223. const rawSet = (dom, key, value) => {
  224. if (isString(value) || isBoolean(value) || isNumber(value)) {
  225. dom.setAttribute(key, value + '');
  226. } else {
  227. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  228. throw new Error('Attribute value was not simple');
  229. }
  230. };
  231. const set = (element, key, value) => {
  232. rawSet(element.dom, key, value);
  233. };
  234. const get$1 = (element, key) => {
  235. const v = element.dom.getAttribute(key);
  236. return v === null ? undefined : v;
  237. };
  238. const remove$3 = (element, key) => {
  239. element.dom.removeAttribute(key);
  240. };
  241. const read = (element, attr) => {
  242. const value = get$1(element, attr);
  243. return value === undefined || value === '' ? [] : value.split(' ');
  244. };
  245. const add$2 = (element, attr, id) => {
  246. const old = read(element, attr);
  247. const nu = old.concat([id]);
  248. set(element, attr, nu.join(' '));
  249. return true;
  250. };
  251. const remove$2 = (element, attr, id) => {
  252. const nu = filter(read(element, attr), v => v !== id);
  253. if (nu.length > 0) {
  254. set(element, attr, nu.join(' '));
  255. } else {
  256. remove$3(element, attr);
  257. }
  258. return false;
  259. };
  260. const supports = element => element.dom.classList !== undefined;
  261. const get = element => read(element, 'class');
  262. const add$1 = (element, clazz) => add$2(element, 'class', clazz);
  263. const remove$1 = (element, clazz) => remove$2(element, 'class', clazz);
  264. const add = (element, clazz) => {
  265. if (supports(element)) {
  266. element.dom.classList.add(clazz);
  267. } else {
  268. add$1(element, clazz);
  269. }
  270. };
  271. const cleanClass = element => {
  272. const classList = supports(element) ? element.dom.classList : get(element);
  273. if (classList.length === 0) {
  274. remove$3(element, 'class');
  275. }
  276. };
  277. const remove = (element, clazz) => {
  278. if (supports(element)) {
  279. const classList = element.dom.classList;
  280. classList.remove(clazz);
  281. } else {
  282. remove$1(element, clazz);
  283. }
  284. cleanClass(element);
  285. };
  286. const fromHtml = (html, scope) => {
  287. const doc = scope || document;
  288. const div = doc.createElement('div');
  289. div.innerHTML = html;
  290. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  291. const message = 'HTML does not have a single root node';
  292. console.error(message, html);
  293. throw new Error(message);
  294. }
  295. return fromDom(div.childNodes[0]);
  296. };
  297. const fromTag = (tag, scope) => {
  298. const doc = scope || document;
  299. const node = doc.createElement(tag);
  300. return fromDom(node);
  301. };
  302. const fromText = (text, scope) => {
  303. const doc = scope || document;
  304. const node = doc.createTextNode(text);
  305. return fromDom(node);
  306. };
  307. const fromDom = node => {
  308. if (node === null || node === undefined) {
  309. throw new Error('Node cannot be null or undefined');
  310. }
  311. return { dom: node };
  312. };
  313. const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  314. const SugarElement = {
  315. fromHtml,
  316. fromTag,
  317. fromText,
  318. fromDom,
  319. fromPoint
  320. };
  321. const charMap = {
  322. '\xA0': 'nbsp',
  323. '\xAD': 'shy'
  324. };
  325. const charMapToRegExp = (charMap, global) => {
  326. let regExp = '';
  327. each(charMap, (_value, key) => {
  328. regExp += key;
  329. });
  330. return new RegExp('[' + regExp + ']', global ? 'g' : '');
  331. };
  332. const charMapToSelector = charMap => {
  333. let selector = '';
  334. each(charMap, value => {
  335. if (selector) {
  336. selector += ',';
  337. }
  338. selector += 'span.mce-' + value;
  339. });
  340. return selector;
  341. };
  342. const regExp = charMapToRegExp(charMap);
  343. const regExpGlobal = charMapToRegExp(charMap, true);
  344. const selector = charMapToSelector(charMap);
  345. const nbspClass = 'mce-nbsp';
  346. const getRaw = element => element.dom.contentEditable;
  347. const wrapCharWithSpan = value => '<span data-mce-bogus="1" class="mce-' + charMap[value] + '">' + value + '</span>';
  348. const isWrappedNbsp = node => node.nodeName.toLowerCase() === 'span' && node.classList.contains('mce-nbsp-wrap');
  349. const isMatch = n => {
  350. const value$1 = value(n);
  351. return isText(n) && isString(value$1) && regExp.test(value$1);
  352. };
  353. const isContentEditableFalse = node => isHTMLElement(node) && getRaw(node) === 'false';
  354. const isChildEditable = (node, currentState) => {
  355. if (isHTMLElement(node) && !isWrappedNbsp(node.dom)) {
  356. const value = getRaw(node);
  357. if (value === 'true') {
  358. return true;
  359. } else if (value === 'false') {
  360. return false;
  361. }
  362. }
  363. return currentState;
  364. };
  365. const filterEditableDescendants = (scope, predicate, editable) => {
  366. let result = [];
  367. const dom = scope.dom;
  368. const children = map(dom.childNodes, SugarElement.fromDom);
  369. const isEditable = node => isWrappedNbsp(node.dom) || !isContentEditableFalse(node);
  370. each$1(children, x => {
  371. if (editable && isEditable(x) && predicate(x)) {
  372. result = result.concat([x]);
  373. }
  374. result = result.concat(filterEditableDescendants(x, predicate, isChildEditable(x, editable)));
  375. });
  376. return result;
  377. };
  378. const findParentElm = (elm, rootElm) => {
  379. while (elm.parentNode) {
  380. if (elm.parentNode === rootElm) {
  381. return rootElm;
  382. }
  383. elm = elm.parentNode;
  384. }
  385. return undefined;
  386. };
  387. const replaceWithSpans = text => text.replace(regExpGlobal, wrapCharWithSpan);
  388. const show = (editor, rootElm) => {
  389. const dom = editor.dom;
  390. const nodeList = filterEditableDescendants(SugarElement.fromDom(rootElm), isMatch, editor.dom.isEditable(rootElm));
  391. each$1(nodeList, n => {
  392. var _a;
  393. const parent = n.dom.parentNode;
  394. if (isWrappedNbsp(parent)) {
  395. add(SugarElement.fromDom(parent), nbspClass);
  396. } else {
  397. const withSpans = replaceWithSpans(dom.encode((_a = value(n)) !== null && _a !== void 0 ? _a : ''));
  398. const div = dom.create('div', {}, withSpans);
  399. let node;
  400. while (node = div.lastChild) {
  401. dom.insertAfter(node, n.dom);
  402. }
  403. editor.dom.remove(n.dom);
  404. }
  405. });
  406. };
  407. const hide = (editor, rootElm) => {
  408. const nodeList = editor.dom.select(selector, rootElm);
  409. each$1(nodeList, node => {
  410. if (isWrappedNbsp(node)) {
  411. remove(SugarElement.fromDom(node), nbspClass);
  412. } else {
  413. editor.dom.remove(node, true);
  414. }
  415. });
  416. };
  417. const toggle = editor => {
  418. const body = editor.getBody();
  419. const bookmark = editor.selection.getBookmark();
  420. let parentNode = findParentElm(editor.selection.getNode(), body);
  421. parentNode = parentNode !== undefined ? parentNode : body;
  422. hide(editor, parentNode);
  423. show(editor, parentNode);
  424. editor.selection.moveToBookmark(bookmark);
  425. };
  426. const applyVisualChars = (editor, toggleState) => {
  427. fireVisualChars(editor, toggleState.get());
  428. const body = editor.getBody();
  429. if (toggleState.get() === true) {
  430. show(editor, body);
  431. } else {
  432. hide(editor, body);
  433. }
  434. };
  435. const toggleVisualChars = (editor, toggleState) => {
  436. toggleState.set(!toggleState.get());
  437. const bookmark = editor.selection.getBookmark();
  438. applyVisualChars(editor, toggleState);
  439. editor.selection.moveToBookmark(bookmark);
  440. };
  441. const register$2 = (editor, toggleState) => {
  442. editor.addCommand('mceVisualChars', () => {
  443. toggleVisualChars(editor, toggleState);
  444. });
  445. };
  446. const option = name => editor => editor.options.get(name);
  447. const register$1 = editor => {
  448. const registerOption = editor.options.register;
  449. registerOption('visualchars_default_state', {
  450. processor: 'boolean',
  451. default: false
  452. });
  453. };
  454. const isEnabledByDefault = option('visualchars_default_state');
  455. const setup$1 = (editor, toggleState) => {
  456. editor.on('init', () => {
  457. applyVisualChars(editor, toggleState);
  458. });
  459. };
  460. const first = (fn, rate) => {
  461. let timer = null;
  462. const cancel = () => {
  463. if (!isNull(timer)) {
  464. clearTimeout(timer);
  465. timer = null;
  466. }
  467. };
  468. const throttle = (...args) => {
  469. if (isNull(timer)) {
  470. timer = setTimeout(() => {
  471. timer = null;
  472. fn.apply(null, args);
  473. }, rate);
  474. }
  475. };
  476. return {
  477. cancel,
  478. throttle
  479. };
  480. };
  481. const setup = (editor, toggleState) => {
  482. const debouncedToggle = first(() => {
  483. toggle(editor);
  484. }, 300);
  485. editor.on('keydown', e => {
  486. if (toggleState.get() === true) {
  487. e.keyCode === 13 ? toggle(editor) : debouncedToggle.throttle();
  488. }
  489. });
  490. editor.on('remove', debouncedToggle.cancel);
  491. };
  492. const toggleActiveState = (editor, enabledStated) => api => {
  493. api.setActive(enabledStated.get());
  494. const editorEventCallback = e => api.setActive(e.state);
  495. editor.on('VisualChars', editorEventCallback);
  496. return () => editor.off('VisualChars', editorEventCallback);
  497. };
  498. const register = (editor, toggleState) => {
  499. const onAction = () => editor.execCommand('mceVisualChars');
  500. editor.ui.registry.addToggleButton('visualchars', {
  501. tooltip: 'Show invisible characters',
  502. icon: 'visualchars',
  503. onAction,
  504. onSetup: toggleActiveState(editor, toggleState)
  505. });
  506. editor.ui.registry.addToggleMenuItem('visualchars', {
  507. text: 'Show invisible characters',
  508. icon: 'visualchars',
  509. onAction,
  510. onSetup: toggleActiveState(editor, toggleState)
  511. });
  512. };
  513. var Plugin = () => {
  514. global.add('visualchars', editor => {
  515. register$1(editor);
  516. const toggleState = Cell(isEnabledByDefault(editor));
  517. register$2(editor, toggleState);
  518. register(editor, toggleState);
  519. setup(editor, toggleState);
  520. setup$1(editor, toggleState);
  521. return get$2(toggleState);
  522. });
  523. };
  524. Plugin();
  525. })();