/
autosave.js
215 lines (178 loc) · 6.57 KB
/
autosave.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Auto-save Module for the Cloud9 IDE. A simple plug-in that auto-saves files
* the user is working on and potentially restores them in case the user
* accidentally leaves the editor without saving.
*
* @author Sergi Mansilla <sergi AT ajax DOT org>
* @copyright 2011, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
"use strict";
var ide = require("core/ide");
var ext = require("core/ext");
var fs = require("ext/filesystem/filesystem");
var markup = require("text!ext/autosave/autosave.xml");
var INTERVAL = 60000;
var FILE_SUFFIX = "c9save";
var getTempPath = function(originalPath) {
var pathLeafs = originalPath.split("/");
var last = pathLeafs.length - 1;
pathLeafs[last] = "." + pathLeafs[last] + "." + FILE_SUFFIX;
return pathLeafs.join("/");
};
var removeFile = function(path) {
fs.exists(path, function(exists) {
if (exists)
fs.remove(path);
});
};
module.exports = ext.register("ext/autosave/autosave", {
dev : "Ajax.org",
name : "Save",
alone : true,
type : ext.GENERAL,
markup : markup,
deps : [fs],
offline : true,
nodes : [],
saveBuffer: {},
hook : function(){
if (!tabEditors)
return;
var self = this;
// This is the main interval. Whatever it happens, every `INTERVAL`
// milliseconds, the plugin will attempt to save every file that is
// open and dirty.
// We might want to detect user "bursts" in writing and autosave after
// those happen. Left as an exercise for the reader.
this.autoSaveInterval = setInterval(function() {
self.doAutoSave();
}, INTERVAL);
ide.addEventListener("afteropenfile", function(data) {
if (!data || !data.doc)
return;
var node = data.doc.getNode();
var dateOriginal = new Date(node.getAttribute("modifieddate"));
var bkpPath = getTempPath(node.getAttribute("path"));
// If there is already a backup file
fs.exists(bkpPath, function(exists) {
if (!exists)
return;
fs.list(bkpPath, function(xml, depth, fileObj) {
var date;
if (fileObj.data.parentNode.lastModified)
date = new Date(fileObj.data.parentNode.lastModified);
// If the date of the backed up file is newer than the file we
// are trying to open, present the user with a choice dialog
if (date && date.getTime() > dateOriginal.getTime()) {
ext.initExtension(self);
fs.readFile(bkpPath, function(contents) {
// Set up some state into the window itself. Not great,
// but easiest way and not too awful
winNewerSave.restoredContents = contents;
winNewerSave.doc = data.doc;
winNewerSave.path = bkpPath;
winNewerSave.show();
});
}
});
});
});
// Remove any temporary file after the user saves willingly.
ide.addEventListener("afterfilesave", function(obj) {
removeFile(getTempPath(obj.node.getAttribute("path")));
});
},
init : function() {
var resetWinAndHide = function() {
winNewerSave.restoredContents = null;
winNewerSave.doc = null;
winNewerSave.path = null;
winNewerSave.hide();
};
winNewerSave.onafterrender = function(){
btnRestoreYes.addEventListener("click", function() {
var contents = winNewerSave.restoredContents;
winNewerSave.doc && winNewerSave.doc.setValue(contents);
winNewerSave.path && removeFile(winNewerSave.path);
resetWinAndHide();
});
btnRestoreNo.addEventListener("click", function() {
// It is understood that if the user doesn't want to restore
// contents from the previous file the first time, he will
// never want to.
winNewerSave.path && removeFile(winNewerSave.path);
resetWinAndHide();
});
};
},
doAutoSave: function() {
var pages = tabEditors.getPages();
for (var i = 0, len = pages.length; i < len; i++) {
this.saveTmp(pages[i]);
}
},
saveTmp : function(page) {
if (!page || !page.$at)
page = tabEditors.getPage();
if (!page)
return;
ext.initExtension(this);
// Check to see if the page has been actually modified since the last
// save.
var model = page.getModel();
if (model && model.data.getAttribute("changed") !== "1")
return;
var doc = page.$doc;
var node = doc.getNode();
if (/* for now */ node.getAttribute("newfile") ||
node.getAttribute("debug")) {
return;
}
// Check if we're already saving!
var saving = parseInt(node.getAttribute("saving"), 10);
var path = node.getAttribute("path");
if (saving) {
this.saveBuffer[path] = page;
return;
}
apf.xmldb.setAttribute(node, "saving", "1");
var pathLeafs = path.split("/");
var fileName = pathLeafs.pop();
var dirName = pathLeafs.join("/");
fileName = getTempPath(fileName);
var self = this;
var bkpPath = dirName + "/" + fileName;
var value = doc.getValue();
fs.saveFile(bkpPath, value, function(data, state, extra) {
if (state != apf.SUCCESS)
return;
apf.xmldb.removeAttribute(node, "saving");
if (self.saveBuffer[path]) {
delete self.saveBuffer[path];
self.saveTmp(page);
}
});
return false;
},
enable : function(){
this.nodes.each(function(item){
item.enable();
});
},
disable : function(){
this.nodes.each(function(item){
item.disable();
});
},
destroy : function(){
if (this.autoSaveInterval)
clearInterval(this.autoSaveInterval);
this.nodes.each(function(item){
item.destroy(true, true);
});
this.nodes = [];
}
});
});