plugin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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$4 = 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 = 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('string');
  35. var isFunction = isSimpleType('function');
  36. var noop = function () {
  37. };
  38. var constant = function (value) {
  39. return function () {
  40. return value;
  41. };
  42. };
  43. var identity = function (x) {
  44. return x;
  45. };
  46. function curry(fn) {
  47. var initialArgs = [];
  48. for (var _i = 1; _i < arguments.length; _i++) {
  49. initialArgs[_i - 1] = arguments[_i];
  50. }
  51. return function () {
  52. var restArgs = [];
  53. for (var _i = 0; _i < arguments.length; _i++) {
  54. restArgs[_i] = arguments[_i];
  55. }
  56. var all = initialArgs.concat(restArgs);
  57. return fn.apply(null, all);
  58. };
  59. }
  60. var never = constant(false);
  61. var always = constant(true);
  62. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  63. var global$2 = tinymce.util.Tools.resolve('tinymce.util.XHR');
  64. var getCreationDateClasses = function (editor) {
  65. return editor.getParam('template_cdate_classes', 'cdate');
  66. };
  67. var getModificationDateClasses = function (editor) {
  68. return editor.getParam('template_mdate_classes', 'mdate');
  69. };
  70. var getSelectedContentClasses = function (editor) {
  71. return editor.getParam('template_selected_content_classes', 'selcontent');
  72. };
  73. var getPreviewReplaceValues = function (editor) {
  74. return editor.getParam('template_preview_replace_values');
  75. };
  76. var getContentStyle = function (editor) {
  77. return editor.getParam('content_style', '', 'string');
  78. };
  79. var shouldUseContentCssCors = function (editor) {
  80. return editor.getParam('content_css_cors', false, 'boolean');
  81. };
  82. var getTemplateReplaceValues = function (editor) {
  83. return editor.getParam('template_replace_values');
  84. };
  85. var getTemplates = function (editor) {
  86. return editor.getParam('templates');
  87. };
  88. var getCdateFormat = function (editor) {
  89. return editor.getParam('template_cdate_format', editor.translate('%Y-%m-%d'));
  90. };
  91. var getMdateFormat = function (editor) {
  92. return editor.getParam('template_mdate_format', editor.translate('%Y-%m-%d'));
  93. };
  94. var getBodyClassFromHash = function (editor) {
  95. var bodyClass = editor.getParam('body_class', '', 'hash');
  96. return bodyClass[editor.id] || '';
  97. };
  98. var getBodyClass = function (editor) {
  99. var bodyClass = editor.getParam('body_class', '', 'string');
  100. if (bodyClass.indexOf('=') === -1) {
  101. return bodyClass;
  102. } else {
  103. return getBodyClassFromHash(editor);
  104. }
  105. };
  106. var addZeros = function (value, len) {
  107. value = '' + value;
  108. if (value.length < len) {
  109. for (var i = 0; i < len - value.length; i++) {
  110. value = '0' + value;
  111. }
  112. }
  113. return value;
  114. };
  115. var getDateTime = function (editor, fmt, date) {
  116. if (date === void 0) {
  117. date = new Date();
  118. }
  119. var daysShort = 'Sun Mon Tue Wed Thu Fri Sat Sun'.split(' ');
  120. var daysLong = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' ');
  121. var monthsShort = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
  122. var monthsLong = 'January February March April May June July August September October November December'.split(' ');
  123. fmt = fmt.replace('%D', '%m/%d/%Y');
  124. fmt = fmt.replace('%r', '%I:%M:%S %p');
  125. fmt = fmt.replace('%Y', '' + date.getFullYear());
  126. fmt = fmt.replace('%y', '' + date.getYear());
  127. fmt = fmt.replace('%m', addZeros(date.getMonth() + 1, 2));
  128. fmt = fmt.replace('%d', addZeros(date.getDate(), 2));
  129. fmt = fmt.replace('%H', '' + addZeros(date.getHours(), 2));
  130. fmt = fmt.replace('%M', '' + addZeros(date.getMinutes(), 2));
  131. fmt = fmt.replace('%S', '' + addZeros(date.getSeconds(), 2));
  132. fmt = fmt.replace('%I', '' + ((date.getHours() + 11) % 12 + 1));
  133. fmt = fmt.replace('%p', '' + (date.getHours() < 12 ? 'AM' : 'PM'));
  134. fmt = fmt.replace('%B', '' + editor.translate(monthsLong[date.getMonth()]));
  135. fmt = fmt.replace('%b', '' + editor.translate(monthsShort[date.getMonth()]));
  136. fmt = fmt.replace('%A', '' + editor.translate(daysLong[date.getDay()]));
  137. fmt = fmt.replace('%a', '' + editor.translate(daysShort[date.getDay()]));
  138. fmt = fmt.replace('%%', '%');
  139. return fmt;
  140. };
  141. var createTemplateList = function (editor, callback) {
  142. return function () {
  143. var templateList = getTemplates(editor);
  144. if (isFunction(templateList)) {
  145. templateList(callback);
  146. } else if (isString(templateList)) {
  147. global$2.send({
  148. url: templateList,
  149. success: function (text) {
  150. callback(JSON.parse(text));
  151. }
  152. });
  153. } else {
  154. callback(templateList);
  155. }
  156. };
  157. };
  158. var replaceTemplateValues = function (html, templateValues) {
  159. global$3.each(templateValues, function (v, k) {
  160. if (isFunction(v)) {
  161. v = v(k);
  162. }
  163. html = html.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
  164. });
  165. return html;
  166. };
  167. var replaceVals = function (editor, scope) {
  168. var dom = editor.dom, vl = getTemplateReplaceValues(editor);
  169. global$3.each(dom.select('*', scope), function (e) {
  170. global$3.each(vl, function (v, k) {
  171. if (dom.hasClass(e, k)) {
  172. if (isFunction(v)) {
  173. v(e);
  174. }
  175. }
  176. });
  177. });
  178. };
  179. var hasClass = function (n, c) {
  180. return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
  181. };
  182. var insertTemplate = function (editor, _ui, html) {
  183. var dom = editor.dom;
  184. var sel = editor.selection.getContent();
  185. html = replaceTemplateValues(html, getTemplateReplaceValues(editor));
  186. var el = dom.create('div', null, html);
  187. var n = dom.select('.mceTmpl', el);
  188. if (n && n.length > 0) {
  189. el = dom.create('div', null);
  190. el.appendChild(n[0].cloneNode(true));
  191. }
  192. global$3.each(dom.select('*', el), function (n) {
  193. if (hasClass(n, getCreationDateClasses(editor).replace(/\s+/g, '|'))) {
  194. n.innerHTML = getDateTime(editor, getCdateFormat(editor));
  195. }
  196. if (hasClass(n, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
  197. n.innerHTML = getDateTime(editor, getMdateFormat(editor));
  198. }
  199. if (hasClass(n, getSelectedContentClasses(editor).replace(/\s+/g, '|'))) {
  200. n.innerHTML = sel;
  201. }
  202. });
  203. replaceVals(editor, el);
  204. editor.execCommand('mceInsertContent', false, el.innerHTML);
  205. editor.addVisual();
  206. };
  207. var none = function () {
  208. return NONE;
  209. };
  210. var NONE = function () {
  211. var call = function (thunk) {
  212. return thunk();
  213. };
  214. var id = identity;
  215. var me = {
  216. fold: function (n, _s) {
  217. return n();
  218. },
  219. isSome: never,
  220. isNone: always,
  221. getOr: id,
  222. getOrThunk: call,
  223. getOrDie: function (msg) {
  224. throw new Error(msg || 'error: getOrDie called on none.');
  225. },
  226. getOrNull: constant(null),
  227. getOrUndefined: constant(undefined),
  228. or: id,
  229. orThunk: call,
  230. map: none,
  231. each: noop,
  232. bind: none,
  233. exists: never,
  234. forall: always,
  235. filter: function () {
  236. return none();
  237. },
  238. toArray: function () {
  239. return [];
  240. },
  241. toString: constant('none()')
  242. };
  243. return me;
  244. }();
  245. var some = function (a) {
  246. var constant_a = constant(a);
  247. var self = function () {
  248. return me;
  249. };
  250. var bind = function (f) {
  251. return f(a);
  252. };
  253. var me = {
  254. fold: function (n, s) {
  255. return s(a);
  256. },
  257. isSome: always,
  258. isNone: never,
  259. getOr: constant_a,
  260. getOrThunk: constant_a,
  261. getOrDie: constant_a,
  262. getOrNull: constant_a,
  263. getOrUndefined: constant_a,
  264. or: self,
  265. orThunk: self,
  266. map: function (f) {
  267. return some(f(a));
  268. },
  269. each: function (f) {
  270. f(a);
  271. },
  272. bind: bind,
  273. exists: bind,
  274. forall: bind,
  275. filter: function (f) {
  276. return f(a) ? me : NONE;
  277. },
  278. toArray: function () {
  279. return [a];
  280. },
  281. toString: function () {
  282. return 'some(' + a + ')';
  283. }
  284. };
  285. return me;
  286. };
  287. var from = function (value) {
  288. return value === null || value === undefined ? NONE : some(value);
  289. };
  290. var Optional = {
  291. some: some,
  292. none: none,
  293. from: from
  294. };
  295. var map = function (xs, f) {
  296. var len = xs.length;
  297. var r = new Array(len);
  298. for (var i = 0; i < len; i++) {
  299. var x = xs[i];
  300. r[i] = f(x, i);
  301. }
  302. return r;
  303. };
  304. var findUntil = function (xs, pred, until) {
  305. for (var i = 0, len = xs.length; i < len; i++) {
  306. var x = xs[i];
  307. if (pred(x, i)) {
  308. return Optional.some(x);
  309. } else if (until(x, i)) {
  310. break;
  311. }
  312. }
  313. return Optional.none();
  314. };
  315. var find = function (xs, pred) {
  316. return findUntil(xs, pred, never);
  317. };
  318. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  319. var global = tinymce.util.Tools.resolve('tinymce.util.Promise');
  320. var hasOwnProperty = Object.hasOwnProperty;
  321. var get = function (obj, key) {
  322. return has(obj, key) ? Optional.from(obj[key]) : Optional.none();
  323. };
  324. var has = function (obj, key) {
  325. return hasOwnProperty.call(obj, key);
  326. };
  327. var entitiesAttr = {
  328. '"': '&quot;',
  329. '<': '&lt;',
  330. '>': '&gt;',
  331. '&': '&amp;',
  332. '\'': '&#039;'
  333. };
  334. var htmlEscape = function (html) {
  335. return html.replace(/["'<>&]/g, function (match) {
  336. return get(entitiesAttr, match).getOr(match);
  337. });
  338. };
  339. var getPreviewContent = function (editor, html) {
  340. if (html.indexOf('<html>') === -1) {
  341. var contentCssEntries_1 = '';
  342. var contentStyle = getContentStyle(editor);
  343. var cors_1 = shouldUseContentCssCors(editor) ? ' crossorigin="anonymous"' : '';
  344. global$3.each(editor.contentCSS, function (url) {
  345. contentCssEntries_1 += '<link type="text/css" rel="stylesheet" href="' + editor.documentBaseURI.toAbsolute(url) + '"' + cors_1 + '>';
  346. });
  347. if (contentStyle) {
  348. contentCssEntries_1 += '<style type="text/css">' + contentStyle + '</style>';
  349. }
  350. var bodyClass = getBodyClass(editor);
  351. var encode = editor.dom.encode;
  352. var isMetaKeyPressed = global$1.mac ? 'e.metaKey' : 'e.ctrlKey && !e.altKey';
  353. var preventClicksOnLinksScript = '<script>' + 'document.addEventListener && document.addEventListener("click", function(e) {' + 'for (var elm = e.target; elm; elm = elm.parentNode) {' + 'if (elm.nodeName === "A" && !(' + isMetaKeyPressed + ')) {' + 'e.preventDefault();' + '}' + '}' + '}, false);' + '</script> ';
  354. var directionality = editor.getBody().dir;
  355. var dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
  356. html = '<!DOCTYPE html>' + '<html>' + '<head>' + '<base href="' + encode(editor.documentBaseURI.getURI()) + '">' + contentCssEntries_1 + preventClicksOnLinksScript + '</head>' + '<body class="' + encode(bodyClass) + '"' + dirAttr + '>' + html + '</body>' + '</html>';
  357. }
  358. return replaceTemplateValues(html, getPreviewReplaceValues(editor));
  359. };
  360. var open = function (editor, templateList) {
  361. var createTemplates = function () {
  362. if (!templateList || templateList.length === 0) {
  363. var message = editor.translate('No templates defined.');
  364. editor.notificationManager.open({
  365. text: message,
  366. type: 'info'
  367. });
  368. return Optional.none();
  369. }
  370. return Optional.from(global$3.map(templateList, function (template, index) {
  371. var isUrlTemplate = function (t) {
  372. return t.url !== undefined;
  373. };
  374. return {
  375. selected: index === 0,
  376. text: template.title,
  377. value: {
  378. url: isUrlTemplate(template) ? Optional.from(template.url) : Optional.none(),
  379. content: !isUrlTemplate(template) ? Optional.from(template.content) : Optional.none(),
  380. description: template.description
  381. }
  382. };
  383. }));
  384. };
  385. var createSelectBoxItems = function (templates) {
  386. return map(templates, function (t) {
  387. return {
  388. text: t.text,
  389. value: t.text
  390. };
  391. });
  392. };
  393. var findTemplate = function (templates, templateTitle) {
  394. return find(templates, function (t) {
  395. return t.text === templateTitle;
  396. });
  397. };
  398. var loadFailedAlert = function (api) {
  399. editor.windowManager.alert('Could not load the specified template.', function () {
  400. return api.focus('template');
  401. });
  402. };
  403. var getTemplateContent = function (t) {
  404. return new global(function (resolve, reject) {
  405. t.value.url.fold(function () {
  406. return resolve(t.value.content.getOr(''));
  407. }, function (url) {
  408. return global$2.send({
  409. url: url,
  410. success: function (html) {
  411. resolve(html);
  412. },
  413. error: function (e) {
  414. reject(e);
  415. }
  416. });
  417. });
  418. });
  419. };
  420. var onChange = function (templates, updateDialog) {
  421. return function (api, change) {
  422. if (change.name === 'template') {
  423. var newTemplateTitle = api.getData().template;
  424. findTemplate(templates, newTemplateTitle).each(function (t) {
  425. api.block('Loading...');
  426. getTemplateContent(t).then(function (previewHtml) {
  427. updateDialog(api, t, previewHtml);
  428. }).catch(function () {
  429. updateDialog(api, t, '');
  430. api.disable('save');
  431. loadFailedAlert(api);
  432. });
  433. });
  434. }
  435. };
  436. };
  437. var onSubmit = function (templates) {
  438. return function (api) {
  439. var data = api.getData();
  440. findTemplate(templates, data.template).each(function (t) {
  441. getTemplateContent(t).then(function (previewHtml) {
  442. editor.execCommand('mceInsertTemplate', false, previewHtml);
  443. api.close();
  444. }).catch(function () {
  445. api.disable('save');
  446. loadFailedAlert(api);
  447. });
  448. });
  449. };
  450. };
  451. var openDialog = function (templates) {
  452. var selectBoxItems = createSelectBoxItems(templates);
  453. var buildDialogSpec = function (bodyItems, initialData) {
  454. return {
  455. title: 'Insert Template',
  456. size: 'large',
  457. body: {
  458. type: 'panel',
  459. items: bodyItems
  460. },
  461. initialData: initialData,
  462. buttons: [
  463. {
  464. type: 'cancel',
  465. name: 'cancel',
  466. text: 'Cancel'
  467. },
  468. {
  469. type: 'submit',
  470. name: 'save',
  471. text: 'Save',
  472. primary: true
  473. }
  474. ],
  475. onSubmit: onSubmit(templates),
  476. onChange: onChange(templates, updateDialog)
  477. };
  478. };
  479. var updateDialog = function (dialogApi, template, previewHtml) {
  480. var content = getPreviewContent(editor, previewHtml);
  481. var bodyItems = [
  482. {
  483. type: 'selectbox',
  484. name: 'template',
  485. label: 'Templates',
  486. items: selectBoxItems
  487. },
  488. {
  489. type: 'htmlpanel',
  490. html: '<p aria-live="polite">' + htmlEscape(template.value.description) + '</p>'
  491. },
  492. {
  493. label: 'Preview',
  494. type: 'iframe',
  495. name: 'preview',
  496. sandboxed: false
  497. }
  498. ];
  499. var initialData = {
  500. template: template.text,
  501. preview: content
  502. };
  503. dialogApi.unblock();
  504. dialogApi.redial(buildDialogSpec(bodyItems, initialData));
  505. dialogApi.focus('template');
  506. };
  507. var dialogApi = editor.windowManager.open(buildDialogSpec([], {
  508. template: '',
  509. preview: ''
  510. }));
  511. dialogApi.block('Loading...');
  512. getTemplateContent(templates[0]).then(function (previewHtml) {
  513. updateDialog(dialogApi, templates[0], previewHtml);
  514. }).catch(function () {
  515. updateDialog(dialogApi, templates[0], '');
  516. dialogApi.disable('save');
  517. loadFailedAlert(dialogApi);
  518. });
  519. };
  520. var optTemplates = createTemplates();
  521. optTemplates.each(openDialog);
  522. };
  523. var showDialog = function (editor) {
  524. return function (templates) {
  525. open(editor, templates);
  526. };
  527. };
  528. var register$1 = function (editor) {
  529. editor.addCommand('mceInsertTemplate', curry(insertTemplate, editor));
  530. editor.addCommand('mceTemplate', createTemplateList(editor, showDialog(editor)));
  531. };
  532. var setup = function (editor) {
  533. editor.on('PreProcess', function (o) {
  534. var dom = editor.dom, dateFormat = getMdateFormat(editor);
  535. global$3.each(dom.select('div', o.node), function (e) {
  536. if (dom.hasClass(e, 'mceTmpl')) {
  537. global$3.each(dom.select('*', e), function (e) {
  538. if (dom.hasClass(e, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
  539. e.innerHTML = getDateTime(editor, dateFormat);
  540. }
  541. });
  542. replaceVals(editor, e);
  543. }
  544. });
  545. });
  546. };
  547. var register = function (editor) {
  548. var onAction = function () {
  549. return editor.execCommand('mceTemplate');
  550. };
  551. editor.ui.registry.addButton('template', {
  552. icon: 'template',
  553. tooltip: 'Insert template',
  554. onAction: onAction
  555. });
  556. editor.ui.registry.addMenuItem('template', {
  557. icon: 'template',
  558. text: 'Insert template...',
  559. onAction: onAction
  560. });
  561. };
  562. function Plugin () {
  563. global$4.add('template', function (editor) {
  564. register(editor);
  565. register$1(editor);
  566. setup(editor);
  567. });
  568. }
  569. Plugin();
  570. }());