plugin.js 19 KB

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