This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
/
FileCommandHandlers.js
554 lines (476 loc) · 20.6 KB
/
FileCommandHandlers.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define: false, $: false, brackets: false, PathUtils: false */
define(function (require, exports, module) {
'use strict';
require("thirdparty/path-utils/path-utils.min");
// Load dependent modules
var CommandManager = require("CommandManager"),
Commands = require("Commands"),
NativeFileSystem = require("NativeFileSystem").NativeFileSystem,
ProjectManager = require("ProjectManager"),
DocumentManager = require("DocumentManager"),
EditorManager = require("EditorManager"),
FileUtils = require("FileUtils"),
Async = require("Async"),
Strings = require("strings"),
PreferencesManager = require("PreferencesManager"),
PerfUtils = require("PerfUtils");
/**
* Handlers for commands related to file handling (opening, saving, etc.)
*/
/** @type {jQueryObject} Container for label shown above editor */
var _title = null;
/** @type {string} Label shown above editor for current document: filename and potentially some of its path */
var _currentTitlePath = null;
function updateTitle() {
var currentDoc = DocumentManager.getCurrentDocument();
if (currentDoc) {
_title.text(_currentTitlePath + (currentDoc.isDirty ? " \u2022" : ""));
} else {
_title.text("");
}
}
function handleCurrentDocumentChange() {
var newDocument = DocumentManager.getCurrentDocument();
if (newDocument) {
var fullPath = newDocument.file.fullPath;
// In the main toolbar, show the project-relative path (if the file is inside the current project)
// or the full absolute path (if it's not in the project).
_currentTitlePath = ProjectManager.makeProjectRelativeIfPossible(fullPath);
} else {
_currentTitlePath = null;
}
// Update title text & "dirty dot" display
updateTitle();
}
function handleDirtyChange(event, changedDoc) {
var currentDoc = DocumentManager.getCurrentDocument();
if (currentDoc && changedDoc.file.fullPath === currentDoc.file.fullPath) {
updateTitle();
}
}
/**
* @private
* Creates a document and displays an editor for the specified file path.
* @param {!string} fullPath
* @return {Deferred} a jQuery Deferred that will be resolved with a new
* document for the specified file path, or rejected if the file can not be read.
*/
function doOpen(fullPath) {
var result = new $.Deferred();
if (!fullPath) {
console.log("doOpen() called without fullPath");
return result.reject();
}
PerfUtils.markStart("Open File: " + fullPath);
result.always(function () {
PerfUtils.addMeasurement("Open File: " + fullPath);
});
var doc = DocumentManager.getDocumentForPath(fullPath);
if (doc) {
// File already open - don't need to load it, just switch to it in the UI
DocumentManager.showInEditor(doc);
result.resolve(doc);
} else {
// File wasn't open before, so we must create a new document for it
var fileEntry = new NativeFileSystem.FileEntry(fullPath);
var docResult = EditorManager.createDocumentAndEditor(fileEntry);
docResult.done(function (doc) {
DocumentManager.showInEditor(doc);
result.resolve(doc);
});
docResult.fail(function (error) {
FileUtils.showFileOpenError(error.code, fullPath);
result.reject();
});
}
return result;
}
/**
* @private
* Used to track the default directory for the file open dialog
*/
var _defaultOpenDialogFullPath = null;
/**
* @private
* Creates a document and displays an editor for the specified file path.
* If no path is specified, a file prompt is provided for input.
* @param {?string} fullPath - The path of the file to open; if it's null we'll prompt for it
* @return {Deferred} a jQuery Deferred that will be resolved with a new
* document for the specified file path, or rejected if the file can not be read.
*/
function _doOpenWithOptionalPath(fullPath) {
var result;
if (!fullPath) {
//first time through, default to the current project path
if (!_defaultOpenDialogFullPath) {
_defaultOpenDialogFullPath = ProjectManager.getProjectRoot().fullPath;
}
// Prompt the user with a dialog
// TODO (issue #117): we're relying on this to not be asynchronous--is that safe?
NativeFileSystem.showOpenDialog(false, false, Strings.OPEN_FILE, _defaultOpenDialogFullPath,
["htm", "html", "js", "css"], function (files) {
if (files.length > 0) {
result = doOpen(files[0])
.done(function updateDefualtOpenDialogFullPath(doc) {
var url = PathUtils.parseUrl(doc.file.fullPath);
//reconstruct the url but use the directory and stop there
_defaultOpenDialogFullPath = url.protocol + url.doubleSlash + url.authority + url.directory;
});
return;
}
});
} else {
result = doOpen(fullPath);
}
if (!result) {
result = (new $.Deferred()).reject();
}
return result;
}
function handleFileOpen(commandData) {
var fullPath = null;
if (commandData) {
fullPath = commandData.fullPath;
}
return _doOpenWithOptionalPath(fullPath)
.always(EditorManager.focusEditor);
}
function handleFileAddToWorkingSet(commandData) {
handleFileOpen(commandData).done(function (doc) {
DocumentManager.addToWorkingSet(doc);
});
}
/**
* @private
* Ensures the suggested file name doesn't already exit.
* @param {string} dir The directory to use
* @param {string} baseFileName The base to start with, "-n" will get appened to make unique
* @param {string} fileExt The file extension
*/
function _getUntitledFileSuggestion(dir, baseFileName, fileExt) {
var result = new $.Deferred();
var suggestedName = baseFileName + fileExt;
var dirEntry = new NativeFileSystem.DirectoryEntry(dir);
result.progress(function attemptNewName(suggestedName, nextIndexToUse) {
if (nextIndexToUse > 99) {
//we've tried this enough
result.reject();
return;
}
//check this name
dirEntry.getFile(
suggestedName,
{},
function successCallback(entry) {
//file exists, notify to the next progress
result.notify(baseFileName + "-" + nextIndexToUse + fileExt, nextIndexToUse + 1);
},
function errorCallback(error) {
//most likely error is FNF, user is better equiped to handle the rest
result.resolve(suggestedName);
}
);
});
//kick it off
result.notify(baseFileName + fileExt, 1);
return result;
}
function handleFileNewInProject() {
// Determine the directory to put the new file
// If a file is currently selected, put it next to it.
// If a directory is currently selected, put it in it.
// If nothing is selected, put it at the root of the project
var baseDir,
selected = ProjectManager.getSelectedItem() || ProjectManager.getProjectRoot();
baseDir = selected.fullPath;
if (selected.isFile) {
baseDir = baseDir.substr(0, baseDir.lastIndexOf("/"));
}
// Create the new node. The createNewItem function does all the heavy work
// of validating file name, creating the new file and selecting.
var deferred = _getUntitledFileSuggestion(baseDir, "Untitled", ".js");
var createWithSuggestedName = function (suggestedName) {
ProjectManager.createNewItem(baseDir, suggestedName, false).pipe(deferred.resolve, deferred.reject, deferred.notify);
};
deferred.done(createWithSuggestedName);
deferred.fail(function createWithDefault() { createWithSuggestedName("Untitled.js"); });
return deferred;
}
function showSaveFileError(code, path) {
return brackets.showModalDialog(
brackets.DIALOG_ID_ERROR,
Strings.ERROR_SAVING_FILE_TITLE,
Strings.format(
Strings.ERROR_SAVING_FILE,
path,
FileUtils.getFileErrorString(code)
)
);
}
/** Note: if there is an error, the Deferred is not rejected until the user has dimissed the dialog */
function doSave(docToSave) {
var result = new $.Deferred();
function handleError(error, fileEntry) {
showSaveFileError(error.code, fileEntry.fullPath)
.always(function () {
result.reject(error);
});
}
if (docToSave && docToSave.isDirty) {
var fileEntry = docToSave.file;
var writeError = false;
fileEntry.createWriter(
function (writer) {
writer.onwriteend = function () {
// Per spec, onwriteend is called after onerror too
if (!writeError) {
docToSave.notifySaved();
result.resolve();
}
};
writer.onerror = function (error) {
writeError = true;
handleError(error, fileEntry);
};
// TODO (issue #241): Blob instead of string
writer.write(docToSave.getText());
},
function (error) {
handleError(error, fileEntry);
}
);
} else {
result.resolve();
}
result.always(function () {
EditorManager.focusEditor();
});
return result;
}
/**
* Saves the given file. If no file specified, assumes the current document.
* @param {?{doc: Document}} commandData Document to close, or null
* @return {$.Deferred}
*/
function handleFileSave(commandData) {
// Default to current document if doc is null
var doc = null;
if (commandData) {
doc = commandData.doc;
}
if (!doc) {
doc = DocumentManager.getCurrentDocument();
}
return doSave(doc);
}
/**
* Saves all unsaved documents. Returns a Promise that will be resolved once ALL the save
* operations have been completed. If ANY save operation fails, an error dialog is immediately
* shown and the other files wait to save until it is dismissed; after all files have been
* processed, the Promise is rejected if any ONE save operation failed.
*
* @return {$.Promise}
*/
function saveAll() {
// 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
return Async.doSequentially(DocumentManager.getWorkingSet(), doSave, false);
}
/**
* Closes the specified document. Prompts user about saving file if document is dirty.
*
* @param {?{doc: Document}} commandData Document to close; assumes the current document if null.
* @param {boolean} promptOnly If true, only displays the relevant confirmation UI and does NOT
* actually close the document. This is useful when chaining file-close together with
* other user prompts that may be cancelable.
* @return {$.Deferred}
*/
function handleFileClose(commandData) {
var doc = null;
if (commandData) {
doc = commandData.doc;
}
// utility function for handleFileClose: closes document & removes from working set
function doClose(doc) {
if (!commandData || !commandData.promptOnly) {
// This selects a different document if the working set has any other options
DocumentManager.closeDocument(doc);
EditorManager.focusEditor();
}
}
var result = new $.Deferred();
// Default to current document if doc is null
if (!doc) {
doc = DocumentManager.getCurrentDocument();
}
// No-op if called when nothing is open; TODO: (issue #273) should command be grayed out instead?
if (!doc) {
return;
}
if (doc.isDirty) {
var filename = PathUtils.parseUrl(doc.file.fullPath).filename;
brackets.showModalDialog(
brackets.DIALOG_ID_SAVE_CLOSE,
Strings.SAVE_CLOSE_TITLE,
Strings.format(Strings.SAVE_CLOSE_MESSAGE, filename)
).done(function (id) {
if (id === brackets.DIALOG_BTN_CANCEL) {
result.reject();
} else if (id === brackets.DIALOG_BTN_OK) {
doSave(doc)
.done(function () {
doClose(doc);
result.resolve();
})
.fail(function () {
result.reject();
});
} else {
// This is the "Don't Save" case--we can just go ahead and close the file.
doClose(doc);
result.resolve();
}
});
result.always(function () {
EditorManager.focusEditor();
});
} else {
// Doc is not dirty, just close
doClose(doc);
EditorManager.focusEditor();
result.resolve();
}
return result;
}
/**
* Closes all open documents; equivalent to calling handleFileClose() for each document, except
* that unsaved changes are confirmed once, in bulk.
* @param {?{promptOnly: boolean}} If true, only displays the relevant confirmation UI and does NOT
* actually close any documents. This is useful when chaining close-all together with
* other user prompts that may be cancelable.
* @return {$.Deferred}
*/
function handleFileCloseAll(commandData) {
var result = new $.Deferred();
var unsavedDocs = DocumentManager.getWorkingSet().filter(function (doc) {
return doc.isDirty;
});
if (unsavedDocs.length === 0) {
// No unsaved changes, so we can proceed without a prompt
result.resolve();
} else if (unsavedDocs.length === 1) {
// Only one unsaved file: show the usual single-file-close confirmation UI
var fileCloseArgs = { doc: unsavedDocs[0], promptOnly: commandData.promptOnly };
handleFileClose(fileCloseArgs).done(function () {
// still need to close any other, non-unsaved documents
result.resolve();
}).fail(function () {
result.reject();
});
} else {
// Multiple unsaved files: show a single bulk prompt listing all files
var message = Strings.SAVE_CLOSE_MULTI_MESSAGE;
message += "<ul>";
unsavedDocs.forEach(function (doc) {
message += "<li>" + ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath) + "</li>";
});
message += "</ul>";
brackets.showModalDialog(
brackets.DIALOG_ID_SAVE_CLOSE,
Strings.SAVE_CLOSE_TITLE,
message
).done(function (id) {
if (id === brackets.DIALOG_BTN_CANCEL) {
result.reject();
} else if (id === brackets.DIALOG_BTN_OK) {
// Save all unsaved files, then if that succeeds, close all
saveAll().done(function () {
result.resolve();
}).fail(function () {
result.reject();
});
} else {
// "Don't Save" case--we can just go ahead and close all files.
result.resolve();
}
});
}
// If all the unsaved-changes confirmations pan out above, then go ahead & close all editors
// NOTE: this still happens before any done() handlers added by our caller, because jQ
// guarantees that handlers run in the order they are added.
result.done(function () {
if (!commandData || !commandData.promptOnly) {
DocumentManager.closeAll();
}
});
return result;
}
/**
* @private - tracks our closing state if we get called again
*/
var _windowGoingAway = false;
/**
* @private
* Common implementation for close/quit/reload which all mostly
* the same except for the final step
*/
function _handleWindowGoingAway(commandData, postCloseHandler) {
if (_windowGoingAway) {
//if we get called back while we're closing, then just return
return (new $.Deferred()).resolve();
}
//prevent the default action of closing the window until we can save all the files
if (commandData && commandData.evt && commandData.evt.cancelable) {
commandData.evt.preventDefault();
}
return CommandManager.execute(Commands.FILE_CLOSE_ALL, { promptOnly: true })
.done(function () {
_windowGoingAway = true;
PreferencesManager.savePreferences();
postCloseHandler();
});
}
/** Confirms any unsaved changes, then closes the window */
function handleFileCloseWindow(commandData) {
return _handleWindowGoingAway(commandData, function () {
window.close();
});
}
/** Closes the window, then quits the app */
function handleFileQuit(commandData) {
return _handleWindowGoingAway(commandData, function () {
brackets.app.quit();
});
// if fail, don't exit: user canceled (or asked us to save changes first, but we failed to do so)
}
/** Does a full reload of the browser window */
function handleFileReload(commandData) {
return _handleWindowGoingAway(commandData, function () {
window.location.reload();
});
}
function init(title) {
_title = title;
// Register global commands
CommandManager.register(Commands.FILE_OPEN, handleFileOpen);
CommandManager.register(Commands.FILE_ADD_TO_WORKING_SET, handleFileAddToWorkingSet);
// TODO: (issue #274) For now, hook up File > New to the "new in project" handler. Eventually
// File > New should open a new blank tab, and handleFileNewInProject should
// be called from a "+" button in the project
CommandManager.register(Commands.FILE_NEW, handleFileNewInProject);
CommandManager.register(Commands.FILE_SAVE, handleFileSave);
CommandManager.register(Commands.FILE_CLOSE, handleFileClose);
CommandManager.register(Commands.FILE_CLOSE_ALL, handleFileCloseAll);
CommandManager.register(Commands.FILE_CLOSE_WINDOW, handleFileCloseWindow);
CommandManager.register(Commands.FILE_QUIT, handleFileQuit);
CommandManager.register(Commands.FILE_RELOAD, handleFileReload);
$(DocumentManager).on("dirtyFlagChange", handleDirtyChange);
$(DocumentManager).on("currentDocumentChange", handleCurrentDocumentChange);
}
// Define public API
exports.init = init;
});