"
6 | ],
7 | "description": "a Autosave plugin for Simditor",
8 | "main": "lib/simditor-autosave.js",
9 | "keywords": [
10 | "simditor",
11 | "autosave"
12 | ],
13 | "license": "MIT",
14 | "homepage": "https://github.com/mycolorway/simditor-autosave",
15 | "ignore": [
16 | "**/.*",
17 | "node_modules",
18 | "vendor",
19 | "Gruntfile.coffee",
20 | "package.json"
21 | ],
22 | "dependencies": {
23 | "jquery": "~2.1.4",
24 | "simditor": "2.3.x"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Simditor
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/lib/simditor-autosave.js:
--------------------------------------------------------------------------------
1 | (function (root, factory) {
2 | if (typeof define === 'function' && define.amd) {
3 | // AMD. Register as an anonymous module unless amdModuleId is set
4 | define('simditor-autosave', ["jquery","simple-module","simditor"], function (a0,b1,c2) {
5 | return (root['SimditorAutosave'] = factory(a0,b1,c2));
6 | });
7 | } else if (typeof exports === 'object') {
8 | // Node. Does not work with strict CommonJS, but
9 | // only CommonJS-like environments that support module.exports,
10 | // like Node.
11 | module.exports = factory(require("jquery"),require("simple-module"),require("simditor"));
12 | } else {
13 | root['SimditorAutosave'] = factory(jQuery,SimpleModule,Simditor);
14 | }
15 | }(this, function ($, SimpleModule, Simditor) {
16 |
17 | var SimditorAutosave,
18 | 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; },
19 | hasProp = {}.hasOwnProperty;
20 |
21 | SimditorAutosave = (function(superClass) {
22 | extend(SimditorAutosave, superClass);
23 |
24 | function SimditorAutosave() {
25 | return SimditorAutosave.__super__.constructor.apply(this, arguments);
26 | }
27 |
28 | SimditorAutosave.pluginName = 'Autosave';
29 |
30 | SimditorAutosave.prototype.opts = {
31 | autosave: true,
32 | autosavePath: null
33 | };
34 |
35 | SimditorAutosave.prototype._init = function() {
36 | var currentVal, link, name, val;
37 | this.editor = this._module;
38 | if (!this.opts.autosave) {
39 | return;
40 | }
41 | this.name = typeof this.opts.autosave === 'string' ? this.opts.autosave : 'simditor';
42 | if (this.opts.autosavePath) {
43 | this.path = this.opts.autosavePath;
44 | } else {
45 | link = $("", {
46 | href: location.href
47 | });
48 | name = this.editor.textarea.data('autosave') || this.name;
49 | this.path = "/" + (link[0].pathname.replace(/\/$/g, "").replace(/^\//g, "")) + "/autosave/" + name + "/";
50 | }
51 | if (!this.path) {
52 | return;
53 | }
54 | this.editor.on("valuechanged", (function(_this) {
55 | return function() {
56 | return _this.storage.set(_this.path, _this.editor.getValue());
57 | };
58 | })(this));
59 | this.editor.el.closest('form').on('ajax:success.simditor-' + this.editor.id, (function(_this) {
60 | return function(e) {
61 | return _this.storage.remove(_this.path);
62 | };
63 | })(this));
64 | val = this.storage.get(this.path);
65 | if (!val) {
66 | return;
67 | }
68 | currentVal = this.editor.textarea.val();
69 | if (val === currentVal) {
70 | return;
71 | }
72 | if (this.editor.textarea.is('[data-autosave-confirm]')) {
73 | if (confirm(this.editor.textarea.data('autosave-confirm') || 'Are you sure to restore unsaved changes?')) {
74 | return this.editor.setValue(val);
75 | } else {
76 | return this.storage.remove(this.path);
77 | }
78 | } else {
79 | return this.editor.setValue(val);
80 | }
81 | };
82 |
83 | SimditorAutosave.prototype.storage = {
84 | supported: function() {
85 | var error;
86 | try {
87 | localStorage.setItem('_storageSupported', 'yes');
88 | localStorage.removeItem('_storageSupported');
89 | return true;
90 | } catch (_error) {
91 | error = _error;
92 | return false;
93 | }
94 | },
95 | set: function(key, val, session) {
96 | var storage;
97 | if (session == null) {
98 | session = false;
99 | }
100 | if (!this.supported()) {
101 | return;
102 | }
103 | storage = session ? sessionStorage : localStorage;
104 | return storage.setItem(key, val);
105 | },
106 | get: function(key, session) {
107 | var storage;
108 | if (session == null) {
109 | session = false;
110 | }
111 | if (!this.supported()) {
112 | return;
113 | }
114 | storage = session ? sessionStorage : localStorage;
115 | return storage[key];
116 | },
117 | remove: function(key, session) {
118 | var storage;
119 | if (session == null) {
120 | session = false;
121 | }
122 | if (!this.supported()) {
123 | return;
124 | }
125 | storage = session ? sessionStorage : localStorage;
126 | return storage.removeItem(key);
127 | }
128 | };
129 |
130 | return SimditorAutosave;
131 |
132 | })(SimpleModule);
133 |
134 | Simditor.connect(SimditorAutosave);
135 |
136 | return SimditorAutosave;
137 |
138 | }));
139 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "simditor-autosave",
3 | "description": "A autosave plugin for Simditor",
4 | "version": "1.1.2",
5 | "homepage": "https://github.com/mycolorway/simditor-autosave",
6 | "main": "lib/simditor-autosave.js",
7 | "author": {
8 | "name": "farthinker",
9 | "email": "farthinker@gmail.com"
10 | },
11 | "license": "MIT",
12 | "repository": {
13 | "type": "git",
14 | "url": "git://github.com/mycolorway/simditor-autosave.git"
15 | },
16 | "bugs": {
17 | "url": "https://github.com/mycolorway/simditor-autosave/issues"
18 | },
19 | "devDependencies": {
20 | "grunt": "0.x",
21 | "grunt-contrib-watch": "0.x",
22 | "grunt-contrib-coffee": "0.x",
23 | "grunt-umd": "2.x"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/simditor-autosave.coffee:
--------------------------------------------------------------------------------
1 |
2 | class SimditorAutosave extends SimpleModule
3 |
4 | @pluginName: 'Autosave'
5 |
6 | opts:
7 | autosave: true
8 | autosavePath: null
9 |
10 | _init: () ->
11 | @editor = @_module
12 | return unless @opts.autosave
13 |
14 | @name = if typeof @opts.autosave == 'string' then @opts.autosave else 'simditor'
15 |
16 | if @opts.autosavePath
17 | @path = @opts.autosavePath
18 | else
19 | link = $( "", {href: location.href})
20 | name = @editor.textarea.data('autosave') || @name
21 | @path = "/#{link[0].pathname.replace( /\/$/g, "" ).replace( /^\//g, "" )}/autosave/#{name}/"
22 |
23 | return unless @path
24 |
25 | @editor.on "valuechanged", =>
26 | @storage.set @path, @editor.getValue()
27 |
28 | @editor.el.closest('form').on 'ajax:success.simditor-' + @editor.id, (e) =>
29 | @storage.remove @path
30 |
31 | val = @storage.get @path
32 | return unless val
33 |
34 | currentVal = @editor.textarea.val()
35 | return if val is currentVal
36 |
37 | if @editor.textarea.is('[data-autosave-confirm]')
38 | if confirm(@editor.textarea.data('autosave-confirm') || 'Are you sure to restore unsaved changes?')
39 | @editor.setValue val
40 | else
41 | @storage.remove @path
42 | else
43 | @editor.setValue val
44 |
45 | storage:
46 | supported: () ->
47 | try
48 | localStorage.setItem('_storageSupported', 'yes')
49 | localStorage.removeItem('_storageSupported')
50 | true
51 | catch error
52 | false
53 | set: (key, val, session = false) ->
54 | return unless @.supported()
55 | storage = if session then sessionStorage else localStorage
56 | storage.setItem(key, val)
57 | get: (key, session = false) ->
58 | return unless @.supported()
59 | storage = if session then sessionStorage else localStorage
60 | storage[key]
61 | remove: (key, session = false) ->
62 | return unless @.supported()
63 | storage = if session then sessionStorage else localStorage
64 | storage.removeItem(key);
65 |
66 | Simditor.connect SimditorAutosave
67 |
--------------------------------------------------------------------------------
/tea.yaml:
--------------------------------------------------------------------------------
1 | # https://tea.xyz/what-is-this-file
2 | #
3 | # DO NOT REMOVE OR EDIT THIS WARNING:
4 | #
5 | # This file is auto-generated by the TEA app. It is intended to validate ownership of your repository.
6 | # DO NOT commit this file or accept any PR if you don't know what this is.
7 | # We are aware that spammers will try to use this file to try to profit off others' work.
8 | # We take this very seriously and will take action against any malicious actors.
9 | #
10 | # If you are not the owner of this repository, and someone maliciously opens a commit with this file
11 | # please report it to us at support@tea.xyz.
12 | #
13 | # A constitution without this header is invalid.
14 | ---
15 | version: 2.0.0
16 | codeOwners:
17 | - '0xe189752c4d4C1D404B8aA8ACe008c8Ce14eE520a'
18 | quorum: 1
19 |
20 |
--------------------------------------------------------------------------------