Subversion Repositories oidplus

Rev

Rev 1422 | View as "text/javascript" | Blame | Compare with Previous | Last modification | View Log | RSS feed

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