plugin.js 7.3 KB

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