Skip to content

Commit

Permalink
Fix #336: add autosave extension
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Jun 2, 2015
1 parent eeacd82 commit 762428a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/extensions/extra/Autosave/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets */

define(function (require, exports, module) {
"use strict";

/**
* Adds automatic saving to all documents in the editor. A document
* gets queued for saving when it is marked as dirty by the DocumentManager.
* We then wait a certain number of ms until we trigger a save for the document.
* You can change `WRITE_DELAY_MS` below to whatever number you think reasonable.
*/

// Time in ms to wait after a dirtyFlagChange event before autosaving file
var WRITE_DELAY_MS = 15 * 1000;

var FILE_SAVE = brackets.getModule("command/Commands").FILE_SAVE,
SaveCommand = brackets.getModule("command/CommandManager").get(FILE_SAVE),
DocumentManager = brackets.getModule("document/DocumentManager");

// In progress saves
var inProgress = {};

function scheduleSave(doc) {
var path = doc.file.fullPath;

// Skip subsequent attempts to save, if we're already waiting on a save
if(inProgress[path]) {
return;
}

doc.addRef();
inProgress[path] = true;

function finish() {
doc.releaseRef();
delete inProgress[path];
}

setTimeout(function() {
SaveCommand.execute({doc: doc})
.done(finish)
.fail(function(err) {
console.error('[Autosave] failed write for `' + path + '`', err);
finish();
});
}, WRITE_DELAY_MS);
}

// When the editor's document is flagged as having changes, schedule a save
DocumentManager.on("dirtyFlagChange", function(evt, doc) {
scheduleSave(doc);
});
});
3 changes: 2 additions & 1 deletion src/utils/BrambleExtensionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ define(function (require, exports, module) {
var extraExtensions = [
"brackets-cdn-suggestions", // https://github.com/szdc/brackets-cdn-suggestions
"ImageUrlCodeHints",
"HTMLHinter"
"HTMLHinter",
"Autosave"
];

// Disable any extensions we found on the query string's disableExtensions param
Expand Down

0 comments on commit 762428a

Please sign in to comment.