forked from Vhornets/brackets-builder
-
Notifications
You must be signed in to change notification settings - Fork 7
/
save.js
67 lines (60 loc) · 2.72 KB
/
save.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */
/**
* From: brackets-autosave-files-on-window-blur Author: martypenner
* Autosaves all files when leaving Brackets, in the style of PHPStorm/WebStorm.
*
* The functions are essentially copied from document/DocumentCommandHandlers.js. The only
* modification is a check if the current document in the loop is untitled (i.e. hasn't
* been saved to a permanent disk location). If it is, don't bother trying to save it.
*/
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule('command/CommandManager'),
Commands = brackets.getModule('command/Commands'),
DocumentManager = brackets.getModule('document/DocumentManager'),
Async = brackets.getModule('utils/Async');
/** Unique token used to indicate user-driven cancellation of Save As (as opposed to file IO error) */
var USER_CANCELED = {
userCanceled: true
};
function saveFileList(fileList) {
// Do in serial because doSave shows error UI for each file, and we don't want to stack
// multiple dialogs on top of each other
var userCanceled = false,
filesAfterSave = [];
return Async.doSequentially(
fileList,
function (file) {
// Abort remaining saves if user canceled any Save As dialog
if (userCanceled) {
return (new $.Deferred()).reject().promise();
}
var doc = DocumentManager.getOpenDocumentForPath(file.fullPath);
if (doc && !doc.isUntitled()) {
var savePromise = CommandManager.execute(Commands.FILE_SAVE, {
doc: doc
});
savePromise
.done(function (newFile) {
filesAfterSave.push(newFile);
})
.fail(function (error) {
if (error === USER_CANCELED) {
userCanceled = true;
}
});
return savePromise;
} else {
// working set entry that was never actually opened - ignore
filesAfterSave.push(file);
return (new $.Deferred()).resolve().promise();
}
},
false // if any save fails, continue trying to save other files anyway; then reject at end
).then(function () {
return filesAfterSave;
});
}
module.exports = {saveFileList: saveFileList};
});