plugin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.5.1 (2020-10-01)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var unique = 0;
  13. var generate = function (prefix) {
  14. var date = new Date();
  15. var time = date.getTime();
  16. var random = Math.floor(Math.random() * 1000000000);
  17. unique++;
  18. return prefix + '_' + random + unique + String(time);
  19. };
  20. var createTableHtml = function (cols, rows) {
  21. var x, y, html;
  22. html = '<table data-mce-id="mce" style="width: 100%">';
  23. html += '<tbody>';
  24. for (y = 0; y < rows; y++) {
  25. html += '<tr>';
  26. for (x = 0; x < cols; x++) {
  27. html += '<td><br></td>';
  28. }
  29. html += '</tr>';
  30. }
  31. html += '</tbody>';
  32. html += '</table>';
  33. return html;
  34. };
  35. var getInsertedElement = function (editor) {
  36. var elms = editor.dom.select('*[data-mce-id]');
  37. return elms[0];
  38. };
  39. var insertTableHtml = function (editor, cols, rows) {
  40. editor.undoManager.transact(function () {
  41. editor.insertContent(createTableHtml(cols, rows));
  42. var tableElm = getInsertedElement(editor);
  43. tableElm.removeAttribute('data-mce-id');
  44. var cellElm = editor.dom.select('td,th', tableElm);
  45. editor.selection.setCursorLocation(cellElm[0], 0);
  46. });
  47. };
  48. var insertTable = function (editor, cols, rows) {
  49. editor.plugins.table ? editor.plugins.table.insertTable(cols, rows) : insertTableHtml(editor, cols, rows);
  50. };
  51. var insertBlob = function (editor, base64, blob) {
  52. var blobCache = editor.editorUpload.blobCache;
  53. var blobInfo = blobCache.create(generate('mceu'), blob, base64);
  54. blobCache.add(blobInfo);
  55. editor.insertContent(editor.dom.createHTML('img', { src: blobInfo.blobUri() }));
  56. };
  57. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  58. var blobToBase64 = function (blob) {
  59. return new global$1(function (resolve) {
  60. var reader = new FileReader();
  61. reader.onloadend = function () {
  62. resolve(reader.result.split(',')[1]);
  63. };
  64. reader.readAsDataURL(blob);
  65. });
  66. };
  67. var global$2 = tinymce.util.Tools.resolve('tinymce.Env');
  68. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  69. var pickFile = function (editor) {
  70. return new global$1(function (resolve) {
  71. var fileInput = document.createElement('input');
  72. fileInput.type = 'file';
  73. fileInput.accept = 'image/*';
  74. fileInput.style.position = 'fixed';
  75. fileInput.style.left = '0';
  76. fileInput.style.top = '0';
  77. fileInput.style.opacity = '0.001';
  78. document.body.appendChild(fileInput);
  79. var changeHandler = function (e) {
  80. resolve(Array.prototype.slice.call(e.target.files));
  81. };
  82. fileInput.addEventListener('change', changeHandler);
  83. var cancelHandler = function (e) {
  84. var cleanup = function () {
  85. resolve([]);
  86. fileInput.parentNode.removeChild(fileInput);
  87. };
  88. if (global$2.os.isAndroid() && e.type !== 'remove') {
  89. global$3.setEditorTimeout(editor, cleanup, 0);
  90. } else {
  91. cleanup();
  92. }
  93. editor.off('focusin remove', cancelHandler);
  94. };
  95. editor.on('focusin remove', cancelHandler);
  96. fileInput.click();
  97. });
  98. };
  99. var setupButtons = function (editor) {
  100. editor.ui.registry.addButton('quickimage', {
  101. icon: 'image',
  102. tooltip: 'Insert image',
  103. onAction: function () {
  104. pickFile(editor).then(function (files) {
  105. if (files.length > 0) {
  106. var blob_1 = files[0];
  107. blobToBase64(blob_1).then(function (base64) {
  108. insertBlob(editor, base64, blob_1);
  109. });
  110. }
  111. });
  112. }
  113. });
  114. editor.ui.registry.addButton('quicktable', {
  115. icon: 'table',
  116. tooltip: 'Insert table',
  117. onAction: function () {
  118. insertTable(editor, 2, 2);
  119. }
  120. });
  121. };
  122. var noop = function () {
  123. };
  124. var constant = function (value) {
  125. return function () {
  126. return value;
  127. };
  128. };
  129. var never = constant(false);
  130. var always = constant(true);
  131. var none = function () {
  132. return NONE;
  133. };
  134. var NONE = function () {
  135. var eq = function (o) {
  136. return o.isNone();
  137. };
  138. var call = function (thunk) {
  139. return thunk();
  140. };
  141. var id = function (n) {
  142. return n;
  143. };
  144. var me = {
  145. fold: function (n, _s) {
  146. return n();
  147. },
  148. is: never,
  149. isSome: never,
  150. isNone: always,
  151. getOr: id,
  152. getOrThunk: call,
  153. getOrDie: function (msg) {
  154. throw new Error(msg || 'error: getOrDie called on none.');
  155. },
  156. getOrNull: constant(null),
  157. getOrUndefined: constant(undefined),
  158. or: id,
  159. orThunk: call,
  160. map: none,
  161. each: noop,
  162. bind: none,
  163. exists: never,
  164. forall: always,
  165. filter: none,
  166. equals: eq,
  167. equals_: eq,
  168. toArray: function () {
  169. return [];
  170. },
  171. toString: constant('none()')
  172. };
  173. return me;
  174. }();
  175. var some = function (a) {
  176. var constant_a = constant(a);
  177. var self = function () {
  178. return me;
  179. };
  180. var bind = function (f) {
  181. return f(a);
  182. };
  183. var me = {
  184. fold: function (n, s) {
  185. return s(a);
  186. },
  187. is: function (v) {
  188. return a === v;
  189. },
  190. isSome: always,
  191. isNone: never,
  192. getOr: constant_a,
  193. getOrThunk: constant_a,
  194. getOrDie: constant_a,
  195. getOrNull: constant_a,
  196. getOrUndefined: constant_a,
  197. or: self,
  198. orThunk: self,
  199. map: function (f) {
  200. return some(f(a));
  201. },
  202. each: function (f) {
  203. f(a);
  204. },
  205. bind: bind,
  206. exists: bind,
  207. forall: bind,
  208. filter: function (f) {
  209. return f(a) ? me : NONE;
  210. },
  211. toArray: function () {
  212. return [a];
  213. },
  214. toString: function () {
  215. return 'some(' + a + ')';
  216. },
  217. equals: function (o) {
  218. return o.is(a);
  219. },
  220. equals_: function (o, elementEq) {
  221. return o.fold(never, function (b) {
  222. return elementEq(a, b);
  223. });
  224. }
  225. };
  226. return me;
  227. };
  228. var from = function (value) {
  229. return value === null || value === undefined ? NONE : some(value);
  230. };
  231. var Optional = {
  232. some: some,
  233. none: none,
  234. from: from
  235. };
  236. var typeOf = function (x) {
  237. var t = typeof x;
  238. if (x === null) {
  239. return 'null';
  240. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  241. return 'array';
  242. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  243. return 'string';
  244. } else {
  245. return t;
  246. }
  247. };
  248. var isType = function (type) {
  249. return function (value) {
  250. return typeOf(value) === type;
  251. };
  252. };
  253. var isSimpleType = function (type) {
  254. return function (value) {
  255. return typeof value === type;
  256. };
  257. };
  258. var eq = function (t) {
  259. return function (a) {
  260. return t === a;
  261. };
  262. };
  263. var isString = isType('string');
  264. var isObject = isType('object');
  265. var isArray = isType('array');
  266. var isBoolean = isSimpleType('boolean');
  267. var isUndefined = eq(undefined);
  268. var isFunction = isSimpleType('function');
  269. function ClosestOrAncestor (is, ancestor, scope, a, isRoot) {
  270. return is(scope, a) ? Optional.some(scope) : isFunction(isRoot) && isRoot(scope) ? Optional.none() : ancestor(scope, a, isRoot);
  271. }
  272. var ELEMENT = 1;
  273. var fromHtml = function (html, scope) {
  274. var doc = scope || document;
  275. var div = doc.createElement('div');
  276. div.innerHTML = html;
  277. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  278. console.error('HTML does not have a single root node', html);
  279. throw new Error('HTML must have a single root node');
  280. }
  281. return fromDom(div.childNodes[0]);
  282. };
  283. var fromTag = function (tag, scope) {
  284. var doc = scope || document;
  285. var node = doc.createElement(tag);
  286. return fromDom(node);
  287. };
  288. var fromText = function (text, scope) {
  289. var doc = scope || document;
  290. var node = doc.createTextNode(text);
  291. return fromDom(node);
  292. };
  293. var fromDom = function (node) {
  294. if (node === null || node === undefined) {
  295. throw new Error('Node cannot be null or undefined');
  296. }
  297. return { dom: node };
  298. };
  299. var fromPoint = function (docElm, x, y) {
  300. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  301. };
  302. var SugarElement = {
  303. fromHtml: fromHtml,
  304. fromTag: fromTag,
  305. fromText: fromText,
  306. fromDom: fromDom,
  307. fromPoint: fromPoint
  308. };
  309. var is = function (element, selector) {
  310. var dom = element.dom;
  311. if (dom.nodeType !== ELEMENT) {
  312. return false;
  313. } else {
  314. var elem = dom;
  315. if (elem.matches !== undefined) {
  316. return elem.matches(selector);
  317. } else if (elem.msMatchesSelector !== undefined) {
  318. return elem.msMatchesSelector(selector);
  319. } else if (elem.webkitMatchesSelector !== undefined) {
  320. return elem.webkitMatchesSelector(selector);
  321. } else if (elem.mozMatchesSelector !== undefined) {
  322. return elem.mozMatchesSelector(selector);
  323. } else {
  324. throw new Error('Browser lacks native selectors');
  325. }
  326. }
  327. };
  328. var Global = typeof window !== 'undefined' ? window : Function('return this;')();
  329. var name = function (element) {
  330. var r = element.dom.nodeName;
  331. return r.toLowerCase();
  332. };
  333. var ancestor = function (scope, predicate, isRoot) {
  334. var element = scope.dom;
  335. var stop = isFunction(isRoot) ? isRoot : never;
  336. while (element.parentNode) {
  337. element = element.parentNode;
  338. var el = SugarElement.fromDom(element);
  339. if (predicate(el)) {
  340. return Optional.some(el);
  341. } else if (stop(el)) {
  342. break;
  343. }
  344. }
  345. return Optional.none();
  346. };
  347. var closest = function (scope, predicate, isRoot) {
  348. var is = function (s, test) {
  349. return test(s);
  350. };
  351. return ClosestOrAncestor(is, ancestor, scope, predicate, isRoot);
  352. };
  353. var ancestor$1 = function (scope, selector, isRoot) {
  354. return ancestor(scope, function (e) {
  355. return is(e, selector);
  356. }, isRoot);
  357. };
  358. var closest$1 = function (scope, selector, isRoot) {
  359. var is$1 = function (element, selector) {
  360. return is(element, selector);
  361. };
  362. return ClosestOrAncestor(is$1, ancestor$1, scope, selector, isRoot);
  363. };
  364. var validDefaultOrDie = function (value, predicate) {
  365. if (predicate(value)) {
  366. return true;
  367. }
  368. throw new Error('Default value doesn\'t match requested type.');
  369. };
  370. var items = function (value, defaultValue) {
  371. if (isArray(value) || isObject(value)) {
  372. throw new Error('expected a string but found: ' + value);
  373. }
  374. if (isUndefined(value)) {
  375. return defaultValue;
  376. }
  377. if (isBoolean(value)) {
  378. return value === false ? '' : defaultValue;
  379. }
  380. return value;
  381. };
  382. var getToolbarItemsOr_ = function (predicate) {
  383. return function (editor, name, defaultValue) {
  384. validDefaultOrDie(defaultValue, predicate);
  385. var value = editor.getParam(name, defaultValue);
  386. return items(value, defaultValue);
  387. };
  388. };
  389. var getToolbarItemsOr = getToolbarItemsOr_(isString);
  390. var getTextSelectionToolbarItems = function (editor) {
  391. return getToolbarItemsOr(editor, 'quickbars_selection_toolbar', 'bold italic | quicklink h2 h3 blockquote');
  392. };
  393. var getInsertToolbarItems = function (editor) {
  394. return getToolbarItemsOr(editor, 'quickbars_insert_toolbar', 'quickimage quicktable');
  395. };
  396. var getImageToolbarItems = function (editor) {
  397. return getToolbarItemsOr(editor, 'quickbars_image_toolbar', 'alignleft aligncenter alignright');
  398. };
  399. var addToEditor = function (editor) {
  400. var insertToolbarItems = getInsertToolbarItems(editor);
  401. if (insertToolbarItems.trim().length > 0) {
  402. editor.ui.registry.addContextToolbar('quickblock', {
  403. predicate: function (node) {
  404. var sugarNode = SugarElement.fromDom(node);
  405. var textBlockElementsMap = editor.schema.getTextBlockElements();
  406. var isRoot = function (elem) {
  407. return elem.dom === editor.getBody();
  408. };
  409. return closest$1(sugarNode, 'table', isRoot).fold(function () {
  410. return closest(sugarNode, function (elem) {
  411. return name(elem) in textBlockElementsMap && editor.dom.isEmpty(elem.dom);
  412. }, isRoot).isSome();
  413. }, function () {
  414. return false;
  415. });
  416. },
  417. items: insertToolbarItems,
  418. position: 'line',
  419. scope: 'editor'
  420. });
  421. }
  422. };
  423. var addToEditor$1 = function (editor) {
  424. var isEditable = function (node) {
  425. return editor.dom.getContentEditableParent(node) !== 'false';
  426. };
  427. var isImage = function (node) {
  428. return node.nodeName === 'IMG' || node.nodeName === 'FIGURE' && /image/i.test(node.className);
  429. };
  430. var imageToolbarItems = getImageToolbarItems(editor);
  431. if (imageToolbarItems.trim().length > 0) {
  432. editor.ui.registry.addContextToolbar('imageselection', {
  433. predicate: isImage,
  434. items: imageToolbarItems,
  435. position: 'node'
  436. });
  437. }
  438. var textToolbarItems = getTextSelectionToolbarItems(editor);
  439. if (textToolbarItems.trim().length > 0) {
  440. editor.ui.registry.addContextToolbar('textselection', {
  441. predicate: function (node) {
  442. return !isImage(node) && !editor.selection.isCollapsed() && isEditable(node);
  443. },
  444. items: textToolbarItems,
  445. position: 'selection',
  446. scope: 'editor'
  447. });
  448. }
  449. };
  450. function Plugin () {
  451. global.add('quickbars', function (editor) {
  452. setupButtons(editor);
  453. addToEditor(editor);
  454. addToEditor$1(editor);
  455. });
  456. }
  457. Plugin();
  458. }());