plugin.js 6.6 KB

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