plugin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.10.2 (2021-11-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var typeOf = function (x) {
  13. var t = typeof x;
  14. if (x === null) {
  15. return 'null';
  16. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  17. return 'array';
  18. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  19. return 'string';
  20. } else {
  21. return t;
  22. }
  23. };
  24. var isType$1 = function (type) {
  25. return function (value) {
  26. return typeOf(value) === type;
  27. };
  28. };
  29. var isSimpleType = function (type) {
  30. return function (value) {
  31. return typeof value === type;
  32. };
  33. };
  34. var isString = isType$1('string');
  35. var isBoolean = isSimpleType('boolean');
  36. var isNullable = function (a) {
  37. return a === null || a === undefined;
  38. };
  39. var isNonNullable = function (a) {
  40. return !isNullable(a);
  41. };
  42. var isFunction = isSimpleType('function');
  43. var isNumber = isSimpleType('number');
  44. var noop = function () {
  45. };
  46. var compose1 = function (fbc, fab) {
  47. return function (a) {
  48. return fbc(fab(a));
  49. };
  50. };
  51. var constant = function (value) {
  52. return function () {
  53. return value;
  54. };
  55. };
  56. var identity = function (x) {
  57. return x;
  58. };
  59. var never = constant(false);
  60. var always = constant(true);
  61. var none = function () {
  62. return NONE;
  63. };
  64. var NONE = function () {
  65. var call = function (thunk) {
  66. return thunk();
  67. };
  68. var id = identity;
  69. var me = {
  70. fold: function (n, _s) {
  71. return n();
  72. },
  73. isSome: never,
  74. isNone: always,
  75. getOr: id,
  76. getOrThunk: call,
  77. getOrDie: function (msg) {
  78. throw new Error(msg || 'error: getOrDie called on none.');
  79. },
  80. getOrNull: constant(null),
  81. getOrUndefined: constant(undefined),
  82. or: id,
  83. orThunk: call,
  84. map: none,
  85. each: noop,
  86. bind: none,
  87. exists: never,
  88. forall: always,
  89. filter: function () {
  90. return none();
  91. },
  92. toArray: function () {
  93. return [];
  94. },
  95. toString: constant('none()')
  96. };
  97. return me;
  98. }();
  99. var some = function (a) {
  100. var constant_a = constant(a);
  101. var self = function () {
  102. return me;
  103. };
  104. var bind = function (f) {
  105. return f(a);
  106. };
  107. var me = {
  108. fold: function (n, s) {
  109. return s(a);
  110. },
  111. isSome: always,
  112. isNone: never,
  113. getOr: constant_a,
  114. getOrThunk: constant_a,
  115. getOrDie: constant_a,
  116. getOrNull: constant_a,
  117. getOrUndefined: constant_a,
  118. or: self,
  119. orThunk: self,
  120. map: function (f) {
  121. return some(f(a));
  122. },
  123. each: function (f) {
  124. f(a);
  125. },
  126. bind: bind,
  127. exists: bind,
  128. forall: bind,
  129. filter: function (f) {
  130. return f(a) ? me : NONE;
  131. },
  132. toArray: function () {
  133. return [a];
  134. },
  135. toString: function () {
  136. return 'some(' + a + ')';
  137. }
  138. };
  139. return me;
  140. };
  141. var from = function (value) {
  142. return value === null || value === undefined ? NONE : some(value);
  143. };
  144. var Optional = {
  145. some: some,
  146. none: none,
  147. from: from
  148. };
  149. var map = function (xs, f) {
  150. var len = xs.length;
  151. var r = new Array(len);
  152. for (var i = 0; i < len; i++) {
  153. var x = xs[i];
  154. r[i] = f(x, i);
  155. }
  156. return r;
  157. };
  158. var each = function (xs, f) {
  159. for (var i = 0, len = xs.length; i < len; i++) {
  160. var x = xs[i];
  161. f(x, i);
  162. }
  163. };
  164. var filter = function (xs, pred) {
  165. var r = [];
  166. for (var i = 0, len = xs.length; i < len; i++) {
  167. var x = xs[i];
  168. if (pred(x, i)) {
  169. r.push(x);
  170. }
  171. }
  172. return r;
  173. };
  174. var DOCUMENT = 9;
  175. var DOCUMENT_FRAGMENT = 11;
  176. var ELEMENT = 1;
  177. var TEXT = 3;
  178. var fromHtml = function (html, scope) {
  179. var doc = scope || document;
  180. var div = doc.createElement('div');
  181. div.innerHTML = html;
  182. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  183. console.error('HTML does not have a single root node', html);
  184. throw new Error('HTML must have a single root node');
  185. }
  186. return fromDom(div.childNodes[0]);
  187. };
  188. var fromTag = function (tag, scope) {
  189. var doc = scope || document;
  190. var node = doc.createElement(tag);
  191. return fromDom(node);
  192. };
  193. var fromText = function (text, scope) {
  194. var doc = scope || document;
  195. var node = doc.createTextNode(text);
  196. return fromDom(node);
  197. };
  198. var fromDom = function (node) {
  199. if (node === null || node === undefined) {
  200. throw new Error('Node cannot be null or undefined');
  201. }
  202. return { dom: node };
  203. };
  204. var fromPoint = function (docElm, x, y) {
  205. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  206. };
  207. var SugarElement = {
  208. fromHtml: fromHtml,
  209. fromTag: fromTag,
  210. fromText: fromText,
  211. fromDom: fromDom,
  212. fromPoint: fromPoint
  213. };
  214. var is = function (element, selector) {
  215. var dom = element.dom;
  216. if (dom.nodeType !== ELEMENT) {
  217. return false;
  218. } else {
  219. var elem = dom;
  220. if (elem.matches !== undefined) {
  221. return elem.matches(selector);
  222. } else if (elem.msMatchesSelector !== undefined) {
  223. return elem.msMatchesSelector(selector);
  224. } else if (elem.webkitMatchesSelector !== undefined) {
  225. return elem.webkitMatchesSelector(selector);
  226. } else if (elem.mozMatchesSelector !== undefined) {
  227. return elem.mozMatchesSelector(selector);
  228. } else {
  229. throw new Error('Browser lacks native selectors');
  230. }
  231. }
  232. };
  233. typeof window !== 'undefined' ? window : Function('return this;')();
  234. var name = function (element) {
  235. var r = element.dom.nodeName;
  236. return r.toLowerCase();
  237. };
  238. var type = function (element) {
  239. return element.dom.nodeType;
  240. };
  241. var isType = function (t) {
  242. return function (element) {
  243. return type(element) === t;
  244. };
  245. };
  246. var isElement = isType(ELEMENT);
  247. var isText = isType(TEXT);
  248. var isDocument = isType(DOCUMENT);
  249. var isDocumentFragment = isType(DOCUMENT_FRAGMENT);
  250. var isTag = function (tag) {
  251. return function (e) {
  252. return isElement(e) && name(e) === tag;
  253. };
  254. };
  255. var owner = function (element) {
  256. return SugarElement.fromDom(element.dom.ownerDocument);
  257. };
  258. var documentOrOwner = function (dos) {
  259. return isDocument(dos) ? dos : owner(dos);
  260. };
  261. var parent = function (element) {
  262. return Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  263. };
  264. var children$2 = function (element) {
  265. return map(element.dom.childNodes, SugarElement.fromDom);
  266. };
  267. var rawSet = function (dom, key, value) {
  268. if (isString(value) || isBoolean(value) || isNumber(value)) {
  269. dom.setAttribute(key, value + '');
  270. } else {
  271. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  272. throw new Error('Attribute value was not simple');
  273. }
  274. };
  275. var set = function (element, key, value) {
  276. rawSet(element.dom, key, value);
  277. };
  278. var remove = function (element, key) {
  279. element.dom.removeAttribute(key);
  280. };
  281. var isShadowRoot = function (dos) {
  282. return isDocumentFragment(dos) && isNonNullable(dos.dom.host);
  283. };
  284. var supported = isFunction(Element.prototype.attachShadow) && isFunction(Node.prototype.getRootNode);
  285. var getRootNode = supported ? function (e) {
  286. return SugarElement.fromDom(e.dom.getRootNode());
  287. } : documentOrOwner;
  288. var getShadowRoot = function (e) {
  289. var r = getRootNode(e);
  290. return isShadowRoot(r) ? Optional.some(r) : Optional.none();
  291. };
  292. var getShadowHost = function (e) {
  293. return SugarElement.fromDom(e.dom.host);
  294. };
  295. var inBody = function (element) {
  296. var dom = isText(element) ? element.dom.parentNode : element.dom;
  297. if (dom === undefined || dom === null || dom.ownerDocument === null) {
  298. return false;
  299. }
  300. var doc = dom.ownerDocument;
  301. return getShadowRoot(SugarElement.fromDom(dom)).fold(function () {
  302. return doc.body.contains(dom);
  303. }, compose1(inBody, getShadowHost));
  304. };
  305. var ancestor$1 = function (scope, predicate, isRoot) {
  306. var element = scope.dom;
  307. var stop = isFunction(isRoot) ? isRoot : never;
  308. while (element.parentNode) {
  309. element = element.parentNode;
  310. var el = SugarElement.fromDom(element);
  311. if (predicate(el)) {
  312. return Optional.some(el);
  313. } else if (stop(el)) {
  314. break;
  315. }
  316. }
  317. return Optional.none();
  318. };
  319. var ancestor = function (scope, selector, isRoot) {
  320. return ancestor$1(scope, function (e) {
  321. return is(e, selector);
  322. }, isRoot);
  323. };
  324. var isSupported = function (dom) {
  325. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  326. };
  327. var get = function (element, property) {
  328. var dom = element.dom;
  329. var styles = window.getComputedStyle(dom);
  330. var r = styles.getPropertyValue(property);
  331. return r === '' && !inBody(element) ? getUnsafeProperty(dom, property) : r;
  332. };
  333. var getUnsafeProperty = function (dom, property) {
  334. return isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  335. };
  336. var getDirection = function (element) {
  337. return get(element, 'direction') === 'rtl' ? 'rtl' : 'ltr';
  338. };
  339. var children$1 = function (scope, predicate) {
  340. return filter(children$2(scope), predicate);
  341. };
  342. var children = function (scope, selector) {
  343. return children$1(scope, function (e) {
  344. return is(e, selector);
  345. });
  346. };
  347. var getParentElement = function (element) {
  348. return parent(element).filter(isElement);
  349. };
  350. var getNormalizedBlock = function (element, isListItem) {
  351. var normalizedElement = isListItem ? ancestor(element, 'ol,ul') : Optional.some(element);
  352. return normalizedElement.getOr(element);
  353. };
  354. var isListItem = isTag('li');
  355. var setDir = function (editor, dir) {
  356. var selectedBlocks = editor.selection.getSelectedBlocks();
  357. if (selectedBlocks.length > 0) {
  358. each(selectedBlocks, function (block) {
  359. var blockElement = SugarElement.fromDom(block);
  360. var isBlockElementListItem = isListItem(blockElement);
  361. var normalizedBlock = getNormalizedBlock(blockElement, isBlockElementListItem);
  362. var normalizedBlockParent = getParentElement(normalizedBlock);
  363. normalizedBlockParent.each(function (parent) {
  364. var parentDirection = getDirection(parent);
  365. if (parentDirection !== dir) {
  366. set(normalizedBlock, 'dir', dir);
  367. } else if (getDirection(normalizedBlock) !== dir) {
  368. remove(normalizedBlock, 'dir');
  369. }
  370. if (isBlockElementListItem) {
  371. var listItems = children(normalizedBlock, 'li[dir]');
  372. each(listItems, function (listItem) {
  373. return remove(listItem, 'dir');
  374. });
  375. }
  376. });
  377. });
  378. editor.nodeChanged();
  379. }
  380. };
  381. var register$1 = function (editor) {
  382. editor.addCommand('mceDirectionLTR', function () {
  383. setDir(editor, 'ltr');
  384. });
  385. editor.addCommand('mceDirectionRTL', function () {
  386. setDir(editor, 'rtl');
  387. });
  388. };
  389. var getNodeChangeHandler = function (editor, dir) {
  390. return function (api) {
  391. var nodeChangeHandler = function (e) {
  392. var element = SugarElement.fromDom(e.element);
  393. api.setActive(getDirection(element) === dir);
  394. };
  395. editor.on('NodeChange', nodeChangeHandler);
  396. return function () {
  397. return editor.off('NodeChange', nodeChangeHandler);
  398. };
  399. };
  400. };
  401. var register = function (editor) {
  402. editor.ui.registry.addToggleButton('ltr', {
  403. tooltip: 'Left to right',
  404. icon: 'ltr',
  405. onAction: function () {
  406. return editor.execCommand('mceDirectionLTR');
  407. },
  408. onSetup: getNodeChangeHandler(editor, 'ltr')
  409. });
  410. editor.ui.registry.addToggleButton('rtl', {
  411. tooltip: 'Right to left',
  412. icon: 'rtl',
  413. onAction: function () {
  414. return editor.execCommand('mceDirectionRTL');
  415. },
  416. onSetup: getNodeChangeHandler(editor, 'rtl')
  417. });
  418. };
  419. function Plugin () {
  420. global.add('directionality', function (editor) {
  421. register$1(editor);
  422. register(editor);
  423. });
  424. }
  425. Plugin();
  426. }());