plugin.js 14 KB

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