diff --git a/static/src/js/app/admin/announcement/announcement.js b/static/src/js/app/admin/announcement/announcement.js index 9b3054d1..debb594d 100644 --- a/static/src/js/app/admin/announcement/announcement.js +++ b/static/src/js/app/admin/announcement/announcement.js @@ -60,6 +60,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "validator", "pag if (!data.code) { bsAlert("修改成功"); vm.isEditing = false; + localStorage.removeItem("/admin/autosave/edit-announcement-editor/"); getPage(1); } else { @@ -97,7 +98,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "validator", "pag } //新建公告表单验证与数据提交 - $("#announcement-form").validator().on('submit', function (e) { + $("#announcement-form").validator().on('submit', function (e) { if (!e.isDefaultPrevented()) { var title = $("#title").val(); var content = createAnnouncementEditor.getValue(); @@ -119,6 +120,7 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "validator", "pag bsAlert("提交成功!"); $("#title").val(""); createAnnouncementEditor.setValue(""); + localStorage.removeItem("/admin/autosave/create-announcement-editor/"); getPage(1); } else { bsAlert(data.data); diff --git a/static/src/js/build.js b/static/src/js/build.js index 12368810..34a05e69 100644 --- a/static/src/js/build.js +++ b/static/src/js/build.js @@ -32,6 +32,7 @@ "simple-module": "lib/simditor/module", "simple-hotkeys": "lib/simditor/hotkeys", "simple-uploader": "lib/simditor/uploader", + "simditor-autosave": "lib/simditor/simditor-autosave", //code mirror 代码编辑器 ->codeMirror _codeMirror: "lib/codeMirror/codemirror", diff --git a/static/src/js/config.js b/static/src/js/config.js index 67d8c994..56b4b8a2 100644 --- a/static/src/js/config.js +++ b/static/src/js/config.js @@ -34,6 +34,7 @@ var require = { "simple-module": "lib/simditor/module", "simple-hotkeys": "lib/simditor/hotkeys", "simple-uploader": "lib/simditor/uploader", + "simditor-autosave": "lib/simditor/simditor-autosave", //code mirror 代码编辑器 ->codeMirror _codeMirror: "lib/codeMirror/codemirror", diff --git a/static/src/js/lib/simditor/simditor-autosave.js b/static/src/js/lib/simditor/simditor-autosave.js new file mode 100644 index 00000000..23f10be8 --- /dev/null +++ b/static/src/js/lib/simditor/simditor-autosave.js @@ -0,0 +1,138 @@ +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module unless amdModuleId is set + define('simditor-autosave', ["jquery","simple-module","simditor"], function (a0,b1,c2) { + return (root['SimditorAutosave'] = factory(a0,b1,c2)); + }); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like environments that support module.exports, + // like Node. + module.exports = factory(require("jquery"),require("simple-module"),require("simditor")); + } else { + root['SimditorAutosave'] = factory(jQuery,SimpleModule,Simditor); + } +}(this, function ($, SimpleModule, Simditor) { + +var SimditorAutosave, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +SimditorAutosave = (function(superClass) { + extend(SimditorAutosave, superClass); + + function SimditorAutosave() { + return SimditorAutosave.__super__.constructor.apply(this, arguments); + } + + SimditorAutosave.pluginName = 'Autosave'; + + SimditorAutosave.prototype.opts = { + autosave: true, + autosavePath: null + }; + + SimditorAutosave.prototype._init = function() { + var currentVal, link, name, val; + this.editor = this._module; + if (!this.opts.autosave) { + return; + } + this.name = typeof this.opts.autosave === 'string' ? this.opts.autosave : 'simditor'; + if (this.opts.autosavePath) { + this.path = this.opts.autosavePath; + } else { + link = $("", { + href: location.href + }); + name = this.editor.textarea.data('autosave') || this.name; + this.path = "/" + (link[0].pathname.replace(/\/$/g, "").replace(/^\//g, "")) + "/autosave/" + name + "/"; + } + if (!this.path) { + return; + } + this.editor.on("valuechanged", (function(_this) { + return function() { + return _this.storage.set(_this.path, _this.editor.getValue()); + }; + })(this)); + this.editor.el.closest('form').on('ajax:success.simditor-' + this.editor.id, (function(_this) { + return function(e) { + return _this.storage.remove(_this.path); + }; + })(this)); + val = this.storage.get(this.path); + if (!val) { + return; + } + currentVal = this.editor.textarea.val(); + if (val === currentVal) { + return; + } + if (this.editor.textarea.is('[data-autosave-confirm]')) { + if (confirm(this.editor.textarea.data('autosave-confirm') || 'Are you sure to restore unsaved changes?')) { + return this.editor.setValue(val); + } else { + return this.storage.remove(this.path); + } + } else { + return this.editor.setValue(val); + } + }; + + SimditorAutosave.prototype.storage = { + supported: function() { + var error; + try { + localStorage.setItem('_storageSupported', 'yes'); + localStorage.removeItem('_storageSupported'); + return true; + } catch (_error) { + error = _error; + return false; + } + }, + set: function(key, val, session) { + var storage; + if (session == null) { + session = false; + } + if (!this.supported()) { + return; + } + storage = session ? sessionStorage : localStorage; + return storage.setItem(key, val); + }, + get: function(key, session) { + var storage; + if (session == null) { + session = false; + } + if (!this.supported()) { + return; + } + storage = session ? sessionStorage : localStorage; + return storage[key]; + }, + remove: function(key, session) { + var storage; + if (session == null) { + session = false; + } + if (!this.supported()) { + return; + } + storage = session ? sessionStorage : localStorage; + return storage.removeItem(key); + } + }; + + return SimditorAutosave; + +})(SimpleModule); + +Simditor.connect(SimditorAutosave); + +return SimditorAutosave; + +})); diff --git a/static/src/js/utils/editor.js b/static/src/js/utils/editor.js index 53a7d557..e3320086 100644 --- a/static/src/js/utils/editor.js +++ b/static/src/js/utils/editor.js @@ -1,4 +1,4 @@ -define("editor", ["simditor"], function(Simditor){ +define("editor", ["simditor", "simditor-autosave"], function(Simditor){ function editor(selector){ return new Simditor({ textarea: $(selector), diff --git a/template/src/admin/announcement/announcement.html b/template/src/admin/announcement/announcement.html index 7bfab36b..24990ce5 100644 --- a/template/src/admin/announcement/announcement.html +++ b/template/src/admin/announcement/announcement.html @@ -38,7 +38,7 @@ ms-duplex="newTitle">
- +
@@ -62,7 +62,7 @@
-