plugin.min.js 7.8 KB
Newer Older
Five-菜鸟级's avatar
init  
Five-菜鸟级 已提交
1 2 3 4 5 6 7 8 9
/*! 
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
* 
*  Version: 5.7.0 (2021-02-10)
*  */
!function(n){var r={};function o(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},o.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},o.n=function(e){var t=e&&e.__esModule?function(){return e["default"]}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s="./src/js/plugins/autosave/plugin.js")}({"./src/js/plugins/autosave/plugin.js":function(module,exports){eval("/**\n  * Copyright (c) Tiny Technologies, Inc. All rights reserved.\n  * Licensed under the LGPL or a commercial license.\n  * For LGPL see License.txt in the project root for license information.\n  * For commercial licenses see https://www.tiny.cloud/\n  *\n  * Version: 5.7.0 (2021-02-10)\n  */\n (function () {\n    'use strict';\n\n    var global = tinymce.util.Tools.resolve('tinymce.PluginManager');\n\n    var eq = function (t) {\n      return function (a) {\n        return t === a;\n      };\n    };\n    var isUndefined = eq(undefined);\n\n    var global$1 = tinymce.util.Tools.resolve('tinymce.util.Delay');\n\n    var global$2 = tinymce.util.Tools.resolve('tinymce.util.LocalStorage');\n\n    var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');\n\n    var fireRestoreDraft = function (editor) {\n      return editor.fire('RestoreDraft');\n    };\n    var fireStoreDraft = function (editor) {\n      return editor.fire('StoreDraft');\n    };\n    var fireRemoveDraft = function (editor) {\n      return editor.fire('RemoveDraft');\n    };\n\n    var parse = function (timeString, defaultTime) {\n      var multiples = {\n        s: 1000,\n        m: 60000\n      };\n      var toParse = timeString || defaultTime;\n      var parsedTime = /^(\\d+)([ms]?)$/.exec('' + toParse);\n      return (parsedTime[2] ? multiples[parsedTime[2]] : 1) * parseInt(toParse, 10);\n    };\n\n    var shouldAskBeforeUnload = function (editor) {\n      return editor.getParam('autosave_ask_before_unload', true);\n    };\n    var getAutoSavePrefix = function (editor) {\n      var location = document.location;\n      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);\n    };\n    var shouldRestoreWhenEmpty = function (editor) {\n      return editor.getParam('autosave_restore_when_empty', false);\n    };\n    var getAutoSaveInterval = function (editor) {\n      return parse(editor.getParam('autosave_interval'), '30s');\n    };\n    var getAutoSaveRetention = function (editor) {\n      return parse(editor.getParam('autosave_retention'), '20m');\n    };\n\n    var isEmpty = function (editor, html) {\n      if (isUndefined(html)) {\n        return editor.dom.isEmpty(editor.getBody());\n      } else {\n        var trimmedHtml = global$3.trim(html);\n        if (trimmedHtml === '') {\n          return true;\n        } else {\n          var fragment = new DOMParser().parseFromString(trimmedHtml, 'text/html');\n          return editor.dom.isEmpty(fragment);\n        }\n      }\n    };\n    var hasDraft = function (editor) {\n      var time = parseInt(global$2.getItem(getAutoSavePrefix(editor) + 'time'), 10) || 0;\n      if (new Date().getTime() - time > getAutoSaveRetention(editor)) {\n        removeDraft(editor, false);\n        return false;\n      }\n      return true;\n    };\n    var removeDraft = function (editor, fire) {\n      var prefix = getAutoSavePrefix(editor);\n      global$2.removeItem(prefix + 'draft');\n      global$2.removeItem(prefix + 'time');\n      if (fire !== false) {\n        fireRemoveDraft(editor);\n      }\n    };\n    var storeDraft = function (editor) {\n      var prefix = getAutoSavePrefix(editor);\n      if (!isEmpty(editor) && editor.isDirty()) {\n        global$2.setItem(prefix + 'draft', editor.getContent({\n          format: 'raw',\n          no_events: true\n        }));\n        global$2.setItem(prefix + 'time', new Date().getTime().toString());\n        fireStoreDraft(editor);\n      }\n    };\n    var restoreDraft = function (editor) {\n      var prefix = getAutoSavePrefix(editor);\n      if (hasDraft(editor)) {\n        editor.setContent(global$2.getItem(prefix + 'draft'), { format: 'raw' });\n        fireRestoreDraft(editor);\n      }\n    };\n    var startStoreDraft = function (editor) {\n      var interval = getAutoSaveInterval(editor);\n      global$1.setEditorInterval(editor, function () {\n        storeDraft(editor);\n      }, interval);\n    };\n    var restoreLastDraft = function (editor) {\n      editor.undoManager.transact(function () {\n        restoreDraft(editor);\n        removeDraft(editor);\n      });\n      editor.focus();\n    };\n\n    var get = function (editor) {\n      return {\n        hasDraft: function () {\n          return hasDraft(editor);\n        },\n        storeDraft: function () {\n          return storeDraft(editor);\n        },\n        restoreDraft: function () {\n          return restoreDraft(editor);\n        },\n        removeDraft: function (fire) {\n          return removeDraft(editor, fire);\n        },\n        isEmpty: function (html) {\n          return isEmpty(editor, html);\n        }\n      };\n    };\n\n    var global$4 = tinymce.util.Tools.resolve('tinymce.EditorManager');\n\n    var setup = function (editor) {\n      editor.editorManager.on('BeforeUnload', function (e) {\n        var msg;\n        global$3.each(global$4.get(), function (editor) {\n          if (editor.plugins.autosave) {\n            editor.plugins.autosave.storeDraft();\n          }\n          if (!msg && editor.isDirty() && shouldAskBeforeUnload(editor)) {\n            msg = editor.translate('You have unsaved changes are you sure you want to navigate away?');\n          }\n        });\n        if (msg) {\n          e.preventDefault();\n          e.returnValue = msg;\n        }\n      });\n    };\n\n    var makeSetupHandler = function (editor) {\n      return function (api) {\n        api.setDisabled(!hasDraft(editor));\n        var editorEventCallback = function () {\n          return api.setDisabled(!hasDraft(editor));\n        };\n        editor.on('StoreDraft RestoreDraft RemoveDraft', editorEventCallback);\n        return function () {\n          return editor.off('StoreDraft RestoreDraft RemoveDraft', editorEventCallback);\n        };\n      };\n    };\n    var register = function (editor) {\n      startStoreDraft(editor);\n      editor.ui.registry.addButton('restoredraft', {\n        tooltip: 'Restore last draft',\n        icon: 'restore-draft',\n        onAction: function () {\n          restoreLastDraft(editor);\n        },\n        onSetup: makeSetupHandler(editor)\n      });\n      editor.ui.registry.addMenuItem('restoredraft', {\n        text: 'Restore last draft',\n        icon: 'restore-draft',\n        onAction: function () {\n          restoreLastDraft(editor);\n        },\n        onSetup: makeSetupHandler(editor)\n      });\n    };\n\n    function Plugin () {\n      global.add('autosave', function (editor) {\n        setup(editor);\n        register(editor);\n        editor.on('init', function () {\n          if (shouldRestoreWhenEmpty(editor) && editor.dom.isEmpty(editor.getBody())) {\n            restoreDraft(editor);\n          }\n        });\n        return get(editor);\n      });\n    }\n\n    Plugin();\n\n}());\n\n\n//# sourceURL=webpack:///./src/js/plugins/autosave/plugin.js?")}});