plugin.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.2.0 (2020-02-13)
  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 Direction = { setDir: setDir };
  28. var register = function (editor) {
  29. editor.addCommand('mceDirectionLTR', function () {
  30. Direction.setDir(editor, 'ltr');
  31. });
  32. editor.addCommand('mceDirectionRTL', function () {
  33. Direction.setDir(editor, 'rtl');
  34. });
  35. };
  36. var Commands = { register: register };
  37. var noop = function () {
  38. };
  39. var constant = function (value) {
  40. return function () {
  41. return value;
  42. };
  43. };
  44. var never = constant(false);
  45. var always = constant(true);
  46. var none = function () {
  47. return NONE;
  48. };
  49. var NONE = function () {
  50. var eq = function (o) {
  51. return o.isNone();
  52. };
  53. var call = function (thunk) {
  54. return thunk();
  55. };
  56. var id = function (n) {
  57. return n;
  58. };
  59. var me = {
  60. fold: function (n, s) {
  61. return n();
  62. },
  63. is: never,
  64. isSome: never,
  65. isNone: always,
  66. getOr: id,
  67. getOrThunk: call,
  68. getOrDie: function (msg) {
  69. throw new Error(msg || 'error: getOrDie called on none.');
  70. },
  71. getOrNull: constant(null),
  72. getOrUndefined: constant(undefined),
  73. or: id,
  74. orThunk: call,
  75. map: none,
  76. each: noop,
  77. bind: none,
  78. exists: never,
  79. forall: always,
  80. filter: none,
  81. equals: eq,
  82. equals_: eq,
  83. toArray: function () {
  84. return [];
  85. },
  86. toString: constant('none()')
  87. };
  88. if (Object.freeze) {
  89. Object.freeze(me);
  90. }
  91. return me;
  92. }();
  93. var some = function (a) {
  94. var constant_a = constant(a);
  95. var self = function () {
  96. return me;
  97. };
  98. var bind = function (f) {
  99. return f(a);
  100. };
  101. var me = {
  102. fold: function (n, s) {
  103. return s(a);
  104. },
  105. is: function (v) {
  106. return a === v;
  107. },
  108. isSome: always,
  109. isNone: never,
  110. getOr: constant_a,
  111. getOrThunk: constant_a,
  112. getOrDie: constant_a,
  113. getOrNull: constant_a,
  114. getOrUndefined: constant_a,
  115. or: self,
  116. orThunk: self,
  117. map: function (f) {
  118. return some(f(a));
  119. },
  120. each: function (f) {
  121. f(a);
  122. },
  123. bind: bind,
  124. exists: bind,
  125. forall: bind,
  126. filter: function (f) {
  127. return f(a) ? me : NONE;
  128. },
  129. toArray: function () {
  130. return [a];
  131. },
  132. toString: function () {
  133. return 'some(' + a + ')';
  134. },
  135. equals: function (o) {
  136. return o.is(a);
  137. },
  138. equals_: function (o, elementEq) {
  139. return o.fold(never, function (b) {
  140. return elementEq(a, b);
  141. });
  142. }
  143. };
  144. return me;
  145. };
  146. var from = function (value) {
  147. return value === null || value === undefined ? NONE : some(value);
  148. };
  149. var Option = {
  150. some: some,
  151. none: none,
  152. from: from
  153. };
  154. var fromHtml = function (html, scope) {
  155. var doc = scope || domGlobals.document;
  156. var div = doc.createElement('div');
  157. div.innerHTML = html;
  158. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  159. domGlobals.console.error('HTML does not have a single root node', html);
  160. throw new Error('HTML must have a single root node');
  161. }
  162. return fromDom(div.childNodes[0]);
  163. };
  164. var fromTag = function (tag, scope) {
  165. var doc = scope || domGlobals.document;
  166. var node = doc.createElement(tag);
  167. return fromDom(node);
  168. };
  169. var fromText = function (text, scope) {
  170. var doc = scope || domGlobals.document;
  171. var node = doc.createTextNode(text);
  172. return fromDom(node);
  173. };
  174. var fromDom = function (node) {
  175. if (node === null || node === undefined) {
  176. throw new Error('Node cannot be null or undefined');
  177. }
  178. return { dom: constant(node) };
  179. };
  180. var fromPoint = function (docElm, x, y) {
  181. var doc = docElm.dom();
  182. return Option.from(doc.elementFromPoint(x, y)).map(fromDom);
  183. };
  184. var Element = {
  185. fromHtml: fromHtml,
  186. fromTag: fromTag,
  187. fromText: fromText,
  188. fromDom: fromDom,
  189. fromPoint: fromPoint
  190. };
  191. var typeOf = function (x) {
  192. if (x === null) {
  193. return 'null';
  194. }
  195. var t = typeof x;
  196. if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  197. return 'array';
  198. }
  199. if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  200. return 'string';
  201. }
  202. return t;
  203. };
  204. var isType = function (type) {
  205. return function (value) {
  206. return typeOf(value) === type;
  207. };
  208. };
  209. var isFunction = isType('function');
  210. var nativeSlice = Array.prototype.slice;
  211. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  212. return nativeSlice.call(x);
  213. };
  214. var isSupported = function (dom) {
  215. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  216. };
  217. var ATTRIBUTE = domGlobals.Node.ATTRIBUTE_NODE;
  218. var CDATA_SECTION = domGlobals.Node.CDATA_SECTION_NODE;
  219. var COMMENT = domGlobals.Node.COMMENT_NODE;
  220. var DOCUMENT = domGlobals.Node.DOCUMENT_NODE;
  221. var DOCUMENT_TYPE = domGlobals.Node.DOCUMENT_TYPE_NODE;
  222. var DOCUMENT_FRAGMENT = domGlobals.Node.DOCUMENT_FRAGMENT_NODE;
  223. var ELEMENT = domGlobals.Node.ELEMENT_NODE;
  224. var TEXT = domGlobals.Node.TEXT_NODE;
  225. var PROCESSING_INSTRUCTION = domGlobals.Node.PROCESSING_INSTRUCTION_NODE;
  226. var ENTITY_REFERENCE = domGlobals.Node.ENTITY_REFERENCE_NODE;
  227. var ENTITY = domGlobals.Node.ENTITY_NODE;
  228. var NOTATION = domGlobals.Node.NOTATION_NODE;
  229. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  230. var type = function (element) {
  231. return element.dom().nodeType;
  232. };
  233. var isType$1 = function (t) {
  234. return function (element) {
  235. return type(element) === t;
  236. };
  237. };
  238. var isText = isType$1(TEXT);
  239. var inBody = function (element) {
  240. var dom = isText(element) ? element.dom().parentNode : element.dom();
  241. return dom !== undefined && dom !== null && dom.ownerDocument.body.contains(dom);
  242. };
  243. var get = function (element, property) {
  244. var dom = element.dom();
  245. var styles = domGlobals.window.getComputedStyle(dom);
  246. var r = styles.getPropertyValue(property);
  247. var v = r === '' && !inBody(element) ? getUnsafeProperty(dom, property) : r;
  248. return v === null ? undefined : v;
  249. };
  250. var getUnsafeProperty = function (dom, property) {
  251. return isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  252. };
  253. var getDirection = function (element) {
  254. return get(element, 'direction') === 'rtl' ? 'rtl' : 'ltr';
  255. };
  256. var getNodeChangeHandler = function (editor, dir) {
  257. return function (api) {
  258. var nodeChangeHandler = function (e) {
  259. var element = Element.fromDom(e.element);
  260. api.setActive(getDirection(element) === dir);
  261. };
  262. editor.on('NodeChange', nodeChangeHandler);
  263. return function () {
  264. return editor.off('NodeChange', nodeChangeHandler);
  265. };
  266. };
  267. };
  268. var register$1 = function (editor) {
  269. editor.ui.registry.addToggleButton('ltr', {
  270. tooltip: 'Left to right',
  271. icon: 'ltr',
  272. onAction: function () {
  273. return editor.execCommand('mceDirectionLTR');
  274. },
  275. onSetup: getNodeChangeHandler(editor, 'ltr')
  276. });
  277. editor.ui.registry.addToggleButton('rtl', {
  278. tooltip: 'Right to left',
  279. icon: 'rtl',
  280. onAction: function () {
  281. return editor.execCommand('mceDirectionRTL');
  282. },
  283. onSetup: getNodeChangeHandler(editor, 'rtl')
  284. });
  285. };
  286. var Buttons = { register: register$1 };
  287. function Plugin () {
  288. global.add('directionality', function (editor) {
  289. Commands.register(editor);
  290. Buttons.register(editor);
  291. });
  292. }
  293. Plugin();
  294. }(window));