tinymce_plugins_autosave.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import {
  2. __commonJS
  3. } from "./chunk-2LSFTFF7.js";
  4. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/autosave/plugin.js
  5. var require_plugin = __commonJS({
  6. "node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/autosave/plugin.js"() {
  7. (function() {
  8. "use strict";
  9. var global$4 = tinymce.util.Tools.resolve("tinymce.PluginManager");
  10. var eq = function(t) {
  11. return function(a) {
  12. return t === a;
  13. };
  14. };
  15. var isUndefined = eq(void 0);
  16. var global$3 = tinymce.util.Tools.resolve("tinymce.util.Delay");
  17. var global$2 = tinymce.util.Tools.resolve("tinymce.util.LocalStorage");
  18. var global$1 = tinymce.util.Tools.resolve("tinymce.util.Tools");
  19. var fireRestoreDraft = function(editor) {
  20. return editor.fire("RestoreDraft");
  21. };
  22. var fireStoreDraft = function(editor) {
  23. return editor.fire("StoreDraft");
  24. };
  25. var fireRemoveDraft = function(editor) {
  26. return editor.fire("RemoveDraft");
  27. };
  28. var parse = function(timeString, defaultTime) {
  29. var multiples = {
  30. s: 1e3,
  31. m: 6e4
  32. };
  33. var toParse = timeString || defaultTime;
  34. var parsedTime = /^(\d+)([ms]?)$/.exec("" + toParse);
  35. return (parsedTime[2] ? multiples[parsedTime[2]] : 1) * parseInt(toParse, 10);
  36. };
  37. var shouldAskBeforeUnload = function(editor) {
  38. return editor.getParam("autosave_ask_before_unload", true);
  39. };
  40. var getAutoSavePrefix = function(editor) {
  41. var location = document.location;
  42. return editor.getParam("autosave_prefix", "tinymce-autosave-{path}{query}{hash}-{id}-").replace(/{path}/g, location.pathname).replace(/{query}/g, location.search).replace(/{hash}/g, location.hash).replace(/{id}/g, editor.id);
  43. };
  44. var shouldRestoreWhenEmpty = function(editor) {
  45. return editor.getParam("autosave_restore_when_empty", false);
  46. };
  47. var getAutoSaveInterval = function(editor) {
  48. return parse(editor.getParam("autosave_interval"), "30s");
  49. };
  50. var getAutoSaveRetention = function(editor) {
  51. return parse(editor.getParam("autosave_retention"), "20m");
  52. };
  53. var isEmpty = function(editor, html) {
  54. if (isUndefined(html)) {
  55. return editor.dom.isEmpty(editor.getBody());
  56. } else {
  57. var trimmedHtml = global$1.trim(html);
  58. if (trimmedHtml === "") {
  59. return true;
  60. } else {
  61. var fragment = new DOMParser().parseFromString(trimmedHtml, "text/html");
  62. return editor.dom.isEmpty(fragment);
  63. }
  64. }
  65. };
  66. var hasDraft = function(editor) {
  67. var time = parseInt(global$2.getItem(getAutoSavePrefix(editor) + "time"), 10) || 0;
  68. if ((/* @__PURE__ */ new Date()).getTime() - time > getAutoSaveRetention(editor)) {
  69. removeDraft(editor, false);
  70. return false;
  71. }
  72. return true;
  73. };
  74. var removeDraft = function(editor, fire) {
  75. var prefix = getAutoSavePrefix(editor);
  76. global$2.removeItem(prefix + "draft");
  77. global$2.removeItem(prefix + "time");
  78. if (fire !== false) {
  79. fireRemoveDraft(editor);
  80. }
  81. };
  82. var storeDraft = function(editor) {
  83. var prefix = getAutoSavePrefix(editor);
  84. if (!isEmpty(editor) && editor.isDirty()) {
  85. global$2.setItem(prefix + "draft", editor.getContent({
  86. format: "raw",
  87. no_events: true
  88. }));
  89. global$2.setItem(prefix + "time", (/* @__PURE__ */ new Date()).getTime().toString());
  90. fireStoreDraft(editor);
  91. }
  92. };
  93. var restoreDraft = function(editor) {
  94. var prefix = getAutoSavePrefix(editor);
  95. if (hasDraft(editor)) {
  96. editor.setContent(global$2.getItem(prefix + "draft"), { format: "raw" });
  97. fireRestoreDraft(editor);
  98. }
  99. };
  100. var startStoreDraft = function(editor) {
  101. var interval = getAutoSaveInterval(editor);
  102. global$3.setEditorInterval(editor, function() {
  103. storeDraft(editor);
  104. }, interval);
  105. };
  106. var restoreLastDraft = function(editor) {
  107. editor.undoManager.transact(function() {
  108. restoreDraft(editor);
  109. removeDraft(editor);
  110. });
  111. editor.focus();
  112. };
  113. var get = function(editor) {
  114. return {
  115. hasDraft: function() {
  116. return hasDraft(editor);
  117. },
  118. storeDraft: function() {
  119. return storeDraft(editor);
  120. },
  121. restoreDraft: function() {
  122. return restoreDraft(editor);
  123. },
  124. removeDraft: function(fire) {
  125. return removeDraft(editor, fire);
  126. },
  127. isEmpty: function(html) {
  128. return isEmpty(editor, html);
  129. }
  130. };
  131. };
  132. var global = tinymce.util.Tools.resolve("tinymce.EditorManager");
  133. var setup = function(editor) {
  134. editor.editorManager.on("BeforeUnload", function(e) {
  135. var msg;
  136. global$1.each(global.get(), function(editor2) {
  137. if (editor2.plugins.autosave) {
  138. editor2.plugins.autosave.storeDraft();
  139. }
  140. if (!msg && editor2.isDirty() && shouldAskBeforeUnload(editor2)) {
  141. msg = editor2.translate("You have unsaved changes are you sure you want to navigate away?");
  142. }
  143. });
  144. if (msg) {
  145. e.preventDefault();
  146. e.returnValue = msg;
  147. }
  148. });
  149. };
  150. var makeSetupHandler = function(editor) {
  151. return function(api) {
  152. api.setDisabled(!hasDraft(editor));
  153. var editorEventCallback = function() {
  154. return api.setDisabled(!hasDraft(editor));
  155. };
  156. editor.on("StoreDraft RestoreDraft RemoveDraft", editorEventCallback);
  157. return function() {
  158. return editor.off("StoreDraft RestoreDraft RemoveDraft", editorEventCallback);
  159. };
  160. };
  161. };
  162. var register = function(editor) {
  163. startStoreDraft(editor);
  164. editor.ui.registry.addButton("restoredraft", {
  165. tooltip: "Restore last draft",
  166. icon: "restore-draft",
  167. onAction: function() {
  168. restoreLastDraft(editor);
  169. },
  170. onSetup: makeSetupHandler(editor)
  171. });
  172. editor.ui.registry.addMenuItem("restoredraft", {
  173. text: "Restore last draft",
  174. icon: "restore-draft",
  175. onAction: function() {
  176. restoreLastDraft(editor);
  177. },
  178. onSetup: makeSetupHandler(editor)
  179. });
  180. };
  181. function Plugin() {
  182. global$4.add("autosave", function(editor) {
  183. setup(editor);
  184. register(editor);
  185. editor.on("init", function() {
  186. if (shouldRestoreWhenEmpty(editor) && editor.dom.isEmpty(editor.getBody())) {
  187. restoreDraft(editor);
  188. }
  189. });
  190. return get(editor);
  191. });
  192. }
  193. Plugin();
  194. })();
  195. }
  196. });
  197. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/autosave/index.js
  198. require_plugin();
  199. //# sourceMappingURL=tinymce_plugins_autosave.js.map