Mercurial Hosting > sceditor
comparison src/development/plugins/autosave.js @ 0:4c4fc447baea
start with sceditor-3.1.1
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 04 Aug 2022 15:21:29 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c4fc447baea |
---|---|
1 /** | |
2 * SCEditor AutoSave Plugin | |
3 * http://www.sceditor.com/ | |
4 * | |
5 * Copyright (C) 2017, Sam Clarke (samclarke.com) | |
6 * | |
7 * SCEditor is licensed under the MIT license: | |
8 * http://www.opensource.org/licenses/mit-license.php | |
9 * | |
10 * @author Sam Clarke | |
11 */ | |
12 (function (sceditor) { | |
13 'use strict'; | |
14 | |
15 var defaultKey = 'sce-autodraft-' + location.pathname + location.search; | |
16 | |
17 function clear(key) { | |
18 localStorage.removeItem(key || defaultKey); | |
19 } | |
20 | |
21 sceditor.plugins.autosave = function () { | |
22 var base = this; | |
23 var editor; | |
24 var isLoading = false; | |
25 var storageKey = defaultKey; | |
26 // 86400000 = 24 hrs (24 * 60 * 60 * 1000) | |
27 var expires = 86400000; | |
28 var saveHandler = function (value) { | |
29 localStorage.setItem(storageKey, JSON.stringify(value)); | |
30 }; | |
31 var loadHandler = function () { | |
32 return JSON.parse(localStorage.getItem(storageKey)); | |
33 }; | |
34 | |
35 function gc() { | |
36 for (var i = 0; i < localStorage.length; i++) { | |
37 var key = localStorage.key(i); | |
38 | |
39 if (/^sce\-autodraft\-/.test(key)) { | |
40 var item = JSON.parse(localStorage.getItem(storageKey)); | |
41 if (item && item.time < Date.now() - expires) { | |
42 clear(key); | |
43 } | |
44 } | |
45 } | |
46 } | |
47 | |
48 base.init = function () { | |
49 editor = this; | |
50 var opts = editor.opts && editor.opts.autosave || {}; | |
51 | |
52 saveHandler = opts.save || saveHandler; | |
53 loadHandler = opts.load || loadHandler; | |
54 storageKey = opts.storageKey || storageKey; | |
55 expires = opts.expires || expires; | |
56 | |
57 gc(); | |
58 }; | |
59 | |
60 base.signalReady = function () { | |
61 // Add submit event listener to clear autosave | |
62 var parent = editor.getContentAreaContainer(); | |
63 while (parent) { | |
64 if (/form/i.test(parent.nodeName)) { | |
65 parent.addEventListener( | |
66 'submit', clear.bind(null, storageKey), true | |
67 ); | |
68 break; | |
69 } | |
70 | |
71 parent = parent.parentNode; | |
72 } | |
73 | |
74 var state = loadHandler(); | |
75 if (state) { | |
76 isLoading = true; | |
77 editor.sourceMode(state.sourceMode); | |
78 editor.val(state.value, false); | |
79 editor.focus(); | |
80 | |
81 if (state.sourceMode) { | |
82 editor.sourceEditorCaret(state.caret); | |
83 } else { | |
84 editor.getRangeHelper().restoreRange(); | |
85 } | |
86 isLoading = false; | |
87 } else { | |
88 saveHandler({ | |
89 caret: this.sourceEditorCaret(), | |
90 sourceMode: this.sourceMode(), | |
91 value: editor.val(null, false), | |
92 time: Date.now() | |
93 }); | |
94 } | |
95 }; | |
96 | |
97 base.signalValuechangedEvent = function (e) { | |
98 if (!isLoading) { | |
99 saveHandler({ | |
100 caret: this.sourceEditorCaret(), | |
101 sourceMode: this.sourceMode(), | |
102 value: e.detail.rawValue, | |
103 time: Date.now() | |
104 }); | |
105 } | |
106 }; | |
107 }; | |
108 | |
109 sceditor.plugins.autosave.clear = clear; | |
110 }(sceditor)); |