plugin.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.3.0 (2020-05-21)
  8. */
  9. (function (domGlobals) {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  13. var setDir = function (editor, dir) {
  14. var dom = editor.dom;
  15. var curDir;
  16. var blocks = editor.selection.getSelectedBlocks();
  17. if (blocks.length) {
  18. curDir = dom.getAttrib(blocks[0], 'dir');
  19. global$1.each(blocks, function (block) {
  20. if (!dom.getParent(block.parentNode, '*[dir="' + dir + '"]', dom.getRoot())) {
  21. dom.setAttrib(block, 'dir', curDir !== dir ? dir : null);
  22. }
  23. });
  24. editor.nodeChanged();
  25. }
  26. };
  27. var register = function (editor) {
  28. editor.addCommand('mceDirectionLTR', function () {
  29. setDir(editor, 'ltr');
  30. });
  31. editor.addCommand('mceDirectionRTL', function () {
  32. setDir(editor, 'rtl');
  33. });
  34. };
  35. var noop = function () {
  36. };
  37. var constant = function (value) {
  38. return function () {
  39. return value;
  40. };
  41. };
  42. var never = constant(false);
  43. var always = constant(true);
  44. var none = function () {
  45. return NONE;
  46. };
  47. var NONE = function () {
  48. var eq = function (o) {
  49. return o.isNone();
  50. };
  51. var call = function (thunk) {
  52. return thunk();
  53. };
  54. var id = function (n) {
  55. return n;
  56. };
  57. var me = {
  58. fold: function (n, _s) {
  59. return n();
  60. },
  61. is: never,
  62. isSome: never,
  63. isNone: always,
  64. getOr: id,
  65. getOrThunk: call,
  66. getOrDie: function (msg) {
  67. throw new Error(msg || 'error: getOrDie called on none.');
  68. },
  69. getOrNull: constant(null),
  70. getOrUndefined: constant(undefined),
  71. or: id,
  72. orThunk: call,
  73. map: none,
  74. each: noop,
  75. bind: none,
  76. exists: never,
  77. forall: always,
  78. filter: none,
  79. equals: eq,
  80. equals_: eq,
  81. toArray: function () {
  82. return [];
  83. },
  84. toString: constant('none()')
  85. };
  86. return me;
  87. }();
  88. var some = function (a) {
  89. var constant_a = constant(a);
  90. var self = function () {
  91. return me;
  92. };
  93. var bind = function (f) {
  94. return f(a);
  95. };
  96. var me = {
  97. fold: function (n, s) {
  98. return s(a);
  99. },
  100. is: function (v) {
  101. return a === v;
  102. },
  103. isSome: always,
  104. isNone: never,
  105. getOr: constant_a,
  106. getOrThunk: constant_a,
  107. getOrDie: constant_a,
  108. getOrNull: constant_a,
  109. getOrUndefined: constant_a,
  110. or: self,
  111. orThunk: self,
  112. map: function (f) {
  113. return some(f(a));
  114. },
  115. each: function (f) {
  116. f(a);
  117. },
  118. bind: bind,
  119. exists: bind,
  120. forall: bind,
  121. filter: function (f) {
  122. return f(a) ? me : NONE;
  123. },
  124. toArray: function () {
  125. return [a];
  126. },
  127. toString: function () {
  128. return 'some(' + a + ')';
  129. },
  130. equals: function (o) {
  131. return o.is(a);
  132. },
  133. equals_: function (o, elementEq) {
  134. return o.fold(never, function (b) {
  135. return elementEq(a, b);
  136. });
  137. }
  138. };
  139. return me;
  140. };
  141. var from = function (value) {
  142. return value === null || value === undefined ? NONE : some(value);
  143. };
  144. var Option = {
  145. some: some,
  146. none: none,
  147. from: from
  148. };
  149. var fromHtml = function (html, scope) {
  150. var doc = scope || domGlobals.document;
  151. var div = doc.createElement('div');
  152. div.innerHTML = html;
  153. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  154. domGlobals.console.error('HTML does not have a single root node', html);
  155. throw new Error('HTML must have a single root node');
  156. }
  157. return fromDom(div.childNodes[0]);
  158. };
  159. var fromTag = function (tag, scope) {
  160. var doc = scope || domGlobals.document;
  161. var node = doc.createElement(tag);
  162. return fromDom(node);
  163. };
  164. var fromText = function (text, scope) {
  165. var doc = scope || domGlobals.document;
  166. var node = doc.createTextNode(text);
  167. return fromDom(node);
  168. };
  169. var fromDom = function (node) {
  170. if (node === null || node === undefined) {
  171. throw new Error('Node cannot be null or undefined');
  172. }
  173. return { dom: constant(node) };
  174. };
  175. var fromPoint = function (docElm, x, y) {
  176. var doc = docElm.dom();
  177. return Option.from(doc.elementFromPoint(x, y)).map(fromDom);
  178. };
  179. var Element = {
  180. fromHtml: fromHtml,
  181. fromTag: fromTag,
  182. fromText: fromText,
  183. fromDom: fromDom,
  184. fromPoint: fromPoint
  185. };
  186. var isSimpleType = function (type) {
  187. return function (value) {
  188. return typeof value === type;
  189. };
  190. };
  191. var isFunction = isSimpleType('function');
  192. var isSupported = function (dom) {
  193. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  194. };
  195. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  196. var TEXT = 3;
  197. var type = function (element) {
  198. return element.dom().nodeType;
  199. };
  200. var isType = function (t) {
  201. return function (element) {
  202. return type(element) === t;
  203. };
  204. };
  205. var isText = isType(TEXT);
  206. var inBody = function (element) {
  207. var dom = isText(element) ? element.dom().parentNode : element.dom();
  208. return dom !== undefined && dom !== null && dom.ownerDocument.body.contains(dom);
  209. };
  210. var get = function (element, property) {
  211. var dom = element.dom();
  212. var styles = domGlobals.window.getComputedStyle(dom);
  213. var r = styles.getPropertyValue(property);
  214. return r === '' && !inBody(element) ? getUnsafeProperty(dom, property) : r;
  215. };
  216. var getUnsafeProperty = function (dom, property) {
  217. return isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  218. };
  219. var getDirection = function (element) {
  220. return get(element, 'direction') === 'rtl' ? 'rtl' : 'ltr';
  221. };
  222. var getNodeChangeHandler = function (editor, dir) {
  223. return function (api) {
  224. var nodeChangeHandler = function (e) {
  225. var element = Element.fromDom(e.element);
  226. api.setActive(getDirection(element) === dir);
  227. };
  228. editor.on('NodeChange', nodeChangeHandler);
  229. return function () {
  230. return editor.off('NodeChange', nodeChangeHandler);
  231. };
  232. };
  233. };
  234. var register$1 = function (editor) {
  235. editor.ui.registry.addToggleButton('ltr', {
  236. tooltip: 'Left to right',
  237. icon: 'ltr',
  238. onAction: function () {
  239. return editor.execCommand('mceDirectionLTR');
  240. },
  241. onSetup: getNodeChangeHandler(editor, 'ltr')
  242. });
  243. editor.ui.registry.addToggleButton('rtl', {
  244. tooltip: 'Right to left',
  245. icon: 'rtl',
  246. onAction: function () {
  247. return editor.execCommand('mceDirectionRTL');
  248. },
  249. onSetup: getNodeChangeHandler(editor, 'rtl')
  250. });
  251. };
  252. function Plugin () {
  253. global.add('directionality', function (editor) {
  254. register(editor);
  255. register$1(editor);
  256. });
  257. }
  258. Plugin();
  259. }(window));