123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- /**
- * Copyright (c) Tiny Technologies, Inc. All rights reserved.
- * Licensed under the LGPL or a commercial license.
- * For LGPL see License.txt in the project root for license information.
- * For commercial licenses see https://www.tiny.cloud/
- *
- * Version: 5.10.2 (2021-11-17)
- */
- (function () {
- 'use strict';
- var Cell = function (initial) {
- var value = initial;
- var get = function () {
- return value;
- };
- var set = function (v) {
- value = v;
- };
- return {
- get: get,
- set: set
- };
- };
- var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
- var get$2 = function (toggleState) {
- var isEnabled = function () {
- return toggleState.get();
- };
- return { isEnabled: isEnabled };
- };
- var fireVisualChars = function (editor, state) {
- return editor.fire('VisualChars', { state: state });
- };
- var typeOf = function (x) {
- var t = typeof x;
- if (x === null) {
- return 'null';
- } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
- return 'array';
- } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
- return 'string';
- } else {
- return t;
- }
- };
- var isType$1 = function (type) {
- return function (value) {
- return typeOf(value) === type;
- };
- };
- var isSimpleType = function (type) {
- return function (value) {
- return typeof value === type;
- };
- };
- var isString = isType$1('string');
- var isBoolean = isSimpleType('boolean');
- var isNumber = isSimpleType('number');
- var noop = function () {
- };
- var constant = function (value) {
- return function () {
- return value;
- };
- };
- var identity = function (x) {
- return x;
- };
- var never = constant(false);
- var always = constant(true);
- var none = function () {
- return NONE;
- };
- var NONE = function () {
- var call = function (thunk) {
- return thunk();
- };
- var id = identity;
- var me = {
- fold: function (n, _s) {
- return n();
- },
- isSome: never,
- isNone: always,
- getOr: id,
- getOrThunk: call,
- getOrDie: function (msg) {
- throw new Error(msg || 'error: getOrDie called on none.');
- },
- getOrNull: constant(null),
- getOrUndefined: constant(undefined),
- or: id,
- orThunk: call,
- map: none,
- each: noop,
- bind: none,
- exists: never,
- forall: always,
- filter: function () {
- return none();
- },
- toArray: function () {
- return [];
- },
- toString: constant('none()')
- };
- return me;
- }();
- var some = function (a) {
- var constant_a = constant(a);
- var self = function () {
- return me;
- };
- var bind = function (f) {
- return f(a);
- };
- var me = {
- fold: function (n, s) {
- return s(a);
- },
- isSome: always,
- isNone: never,
- getOr: constant_a,
- getOrThunk: constant_a,
- getOrDie: constant_a,
- getOrNull: constant_a,
- getOrUndefined: constant_a,
- or: self,
- orThunk: self,
- map: function (f) {
- return some(f(a));
- },
- each: function (f) {
- f(a);
- },
- bind: bind,
- exists: bind,
- forall: bind,
- filter: function (f) {
- return f(a) ? me : NONE;
- },
- toArray: function () {
- return [a];
- },
- toString: function () {
- return 'some(' + a + ')';
- }
- };
- return me;
- };
- var from = function (value) {
- return value === null || value === undefined ? NONE : some(value);
- };
- var Optional = {
- some: some,
- none: none,
- from: from
- };
- var map = function (xs, f) {
- var len = xs.length;
- var r = new Array(len);
- for (var i = 0; i < len; i++) {
- var x = xs[i];
- r[i] = f(x, i);
- }
- return r;
- };
- var each$1 = function (xs, f) {
- for (var i = 0, len = xs.length; i < len; i++) {
- var x = xs[i];
- f(x, i);
- }
- };
- var filter = function (xs, pred) {
- var r = [];
- for (var i = 0, len = xs.length; i < len; i++) {
- var x = xs[i];
- if (pred(x, i)) {
- r.push(x);
- }
- }
- return r;
- };
- var keys = Object.keys;
- var each = function (obj, f) {
- var props = keys(obj);
- for (var k = 0, len = props.length; k < len; k++) {
- var i = props[k];
- var x = obj[i];
- f(x, i);
- }
- };
- typeof window !== 'undefined' ? window : Function('return this;')();
- var TEXT = 3;
- var type = function (element) {
- return element.dom.nodeType;
- };
- var value = function (element) {
- return element.dom.nodeValue;
- };
- var isType = function (t) {
- return function (element) {
- return type(element) === t;
- };
- };
- var isText = isType(TEXT);
- var rawSet = function (dom, key, value) {
- if (isString(value) || isBoolean(value) || isNumber(value)) {
- dom.setAttribute(key, value + '');
- } else {
- console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
- throw new Error('Attribute value was not simple');
- }
- };
- var set = function (element, key, value) {
- rawSet(element.dom, key, value);
- };
- var get$1 = function (element, key) {
- var v = element.dom.getAttribute(key);
- return v === null ? undefined : v;
- };
- var remove$3 = function (element, key) {
- element.dom.removeAttribute(key);
- };
- var read = function (element, attr) {
- var value = get$1(element, attr);
- return value === undefined || value === '' ? [] : value.split(' ');
- };
- var add$2 = function (element, attr, id) {
- var old = read(element, attr);
- var nu = old.concat([id]);
- set(element, attr, nu.join(' '));
- return true;
- };
- var remove$2 = function (element, attr, id) {
- var nu = filter(read(element, attr), function (v) {
- return v !== id;
- });
- if (nu.length > 0) {
- set(element, attr, nu.join(' '));
- } else {
- remove$3(element, attr);
- }
- return false;
- };
- var supports = function (element) {
- return element.dom.classList !== undefined;
- };
- var get = function (element) {
- return read(element, 'class');
- };
- var add$1 = function (element, clazz) {
- return add$2(element, 'class', clazz);
- };
- var remove$1 = function (element, clazz) {
- return remove$2(element, 'class', clazz);
- };
- var add = function (element, clazz) {
- if (supports(element)) {
- element.dom.classList.add(clazz);
- } else {
- add$1(element, clazz);
- }
- };
- var cleanClass = function (element) {
- var classList = supports(element) ? element.dom.classList : get(element);
- if (classList.length === 0) {
- remove$3(element, 'class');
- }
- };
- var remove = function (element, clazz) {
- if (supports(element)) {
- var classList = element.dom.classList;
- classList.remove(clazz);
- } else {
- remove$1(element, clazz);
- }
- cleanClass(element);
- };
- var fromHtml = function (html, scope) {
- var doc = scope || document;
- var div = doc.createElement('div');
- div.innerHTML = html;
- if (!div.hasChildNodes() || div.childNodes.length > 1) {
- console.error('HTML does not have a single root node', html);
- throw new Error('HTML must have a single root node');
- }
- return fromDom(div.childNodes[0]);
- };
- var fromTag = function (tag, scope) {
- var doc = scope || document;
- var node = doc.createElement(tag);
- return fromDom(node);
- };
- var fromText = function (text, scope) {
- var doc = scope || document;
- var node = doc.createTextNode(text);
- return fromDom(node);
- };
- var fromDom = function (node) {
- if (node === null || node === undefined) {
- throw new Error('Node cannot be null or undefined');
- }
- return { dom: node };
- };
- var fromPoint = function (docElm, x, y) {
- return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
- };
- var SugarElement = {
- fromHtml: fromHtml,
- fromTag: fromTag,
- fromText: fromText,
- fromDom: fromDom,
- fromPoint: fromPoint
- };
- var charMap = {
- '\xA0': 'nbsp',
- '\xAD': 'shy'
- };
- var charMapToRegExp = function (charMap, global) {
- var regExp = '';
- each(charMap, function (_value, key) {
- regExp += key;
- });
- return new RegExp('[' + regExp + ']', global ? 'g' : '');
- };
- var charMapToSelector = function (charMap) {
- var selector = '';
- each(charMap, function (value) {
- if (selector) {
- selector += ',';
- }
- selector += 'span.mce-' + value;
- });
- return selector;
- };
- var regExp = charMapToRegExp(charMap);
- var regExpGlobal = charMapToRegExp(charMap, true);
- var selector = charMapToSelector(charMap);
- var nbspClass = 'mce-nbsp';
- var wrapCharWithSpan = function (value) {
- return '<span data-mce-bogus="1" class="mce-' + charMap[value] + '">' + value + '</span>';
- };
- var isMatch = function (n) {
- var value$1 = value(n);
- return isText(n) && value$1 !== undefined && regExp.test(value$1);
- };
- var filterDescendants = function (scope, predicate) {
- var result = [];
- var dom = scope.dom;
- var children = map(dom.childNodes, SugarElement.fromDom);
- each$1(children, function (x) {
- if (predicate(x)) {
- result = result.concat([x]);
- }
- result = result.concat(filterDescendants(x, predicate));
- });
- return result;
- };
- var findParentElm = function (elm, rootElm) {
- while (elm.parentNode) {
- if (elm.parentNode === rootElm) {
- return elm;
- }
- elm = elm.parentNode;
- }
- };
- var replaceWithSpans = function (text) {
- return text.replace(regExpGlobal, wrapCharWithSpan);
- };
- var isWrappedNbsp = function (node) {
- return node.nodeName.toLowerCase() === 'span' && node.classList.contains('mce-nbsp-wrap');
- };
- var show = function (editor, rootElm) {
- var nodeList = filterDescendants(SugarElement.fromDom(rootElm), isMatch);
- each$1(nodeList, function (n) {
- var parent = n.dom.parentNode;
- if (isWrappedNbsp(parent)) {
- add(SugarElement.fromDom(parent), nbspClass);
- } else {
- var withSpans = replaceWithSpans(editor.dom.encode(value(n)));
- var div = editor.dom.create('div', null, withSpans);
- var node = void 0;
- while (node = div.lastChild) {
- editor.dom.insertAfter(node, n.dom);
- }
- editor.dom.remove(n.dom);
- }
- });
- };
- var hide = function (editor, rootElm) {
- var nodeList = editor.dom.select(selector, rootElm);
- each$1(nodeList, function (node) {
- if (isWrappedNbsp(node)) {
- remove(SugarElement.fromDom(node), nbspClass);
- } else {
- editor.dom.remove(node, true);
- }
- });
- };
- var toggle = function (editor) {
- var body = editor.getBody();
- var bookmark = editor.selection.getBookmark();
- var parentNode = findParentElm(editor.selection.getNode(), body);
- parentNode = parentNode !== undefined ? parentNode : body;
- hide(editor, parentNode);
- show(editor, parentNode);
- editor.selection.moveToBookmark(bookmark);
- };
- var applyVisualChars = function (editor, toggleState) {
- fireVisualChars(editor, toggleState.get());
- var body = editor.getBody();
- if (toggleState.get() === true) {
- show(editor, body);
- } else {
- hide(editor, body);
- }
- };
- var toggleVisualChars = function (editor, toggleState) {
- toggleState.set(!toggleState.get());
- var bookmark = editor.selection.getBookmark();
- applyVisualChars(editor, toggleState);
- editor.selection.moveToBookmark(bookmark);
- };
- var register$1 = function (editor, toggleState) {
- editor.addCommand('mceVisualChars', function () {
- toggleVisualChars(editor, toggleState);
- });
- };
- var isEnabledByDefault = function (editor) {
- return editor.getParam('visualchars_default_state', false);
- };
- var hasForcedRootBlock = function (editor) {
- return editor.getParam('forced_root_block') !== false;
- };
- var setup$1 = function (editor, toggleState) {
- editor.on('init', function () {
- applyVisualChars(editor, toggleState);
- });
- };
- var global = tinymce.util.Tools.resolve('tinymce.util.Delay');
- var setup = function (editor, toggleState) {
- var debouncedToggle = global.debounce(function () {
- toggle(editor);
- }, 300);
- if (hasForcedRootBlock(editor)) {
- editor.on('keydown', function (e) {
- if (toggleState.get() === true) {
- e.keyCode === 13 ? toggle(editor) : debouncedToggle();
- }
- });
- }
- editor.on('remove', debouncedToggle.stop);
- };
- var toggleActiveState = function (editor, enabledStated) {
- return function (api) {
- api.setActive(enabledStated.get());
- var editorEventCallback = function (e) {
- return api.setActive(e.state);
- };
- editor.on('VisualChars', editorEventCallback);
- return function () {
- return editor.off('VisualChars', editorEventCallback);
- };
- };
- };
- var register = function (editor, toggleState) {
- var onAction = function () {
- return editor.execCommand('mceVisualChars');
- };
- editor.ui.registry.addToggleButton('visualchars', {
- tooltip: 'Show invisible characters',
- icon: 'visualchars',
- onAction: onAction,
- onSetup: toggleActiveState(editor, toggleState)
- });
- editor.ui.registry.addToggleMenuItem('visualchars', {
- text: 'Show invisible characters',
- icon: 'visualchars',
- onAction: onAction,
- onSetup: toggleActiveState(editor, toggleState)
- });
- };
- function Plugin () {
- global$1.add('visualchars', function (editor) {
- var toggleState = Cell(isEnabledByDefault(editor));
- register$1(editor, toggleState);
- register(editor, toggleState);
- setup(editor, toggleState);
- setup$1(editor, toggleState);
- return get$2(toggleState);
- });
- }
- Plugin();
- }());
|