tinymce_plugins_template.js 20 KB

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