diff --git a/js/dialog.js b/js/dialog.js index 3fd7a95..a84819e 100644 --- a/js/dialog.js +++ b/js/dialog.js @@ -1,4 +1,4 @@ -define(["dom2"], function() { +define(["editor", "dom2"], function(editor) { return function(text, buttons, callback) { if (typeof buttons == "function" || typeof buttons == "undefined") { @@ -30,6 +30,7 @@ define(["dom2"], function() { modal.remove(); var value = JSON.parse(this.value); if (callback) callback(value); + editor.focus(); }; buttons.forEach(function(options) { @@ -42,13 +43,18 @@ define(["dom2"], function() { } button.innerHTML = options.label; button.value = options.value; + if (options.focus) { + button.className = "default"; + } buttonRow.append(button); button.on("click", clickButton); }); setTimeout(function() { //ensure focus, even from palette (which normally refocuses editor) - modal.find("button").focus(); + var button = modal.find("button.default"); + if (!button) button = modal.find("button"); + button.focus(); }); } diff --git a/js/fileManager.js b/js/fileManager.js index 9198925..5f2ea3f 100644 --- a/js/fileManager.js +++ b/js/fileManager.js @@ -53,6 +53,29 @@ define([ sessions.renderTabs(); }); }); + command.on("session:check-file", function() { + var tab = sessions.getCurrent(); + if (!tab.file) return; + tab.file.entry.file(function(entry) { + if (tab.modifiedAt && entry.lastModifiedDate > tab.modifiedAt) { + if (tab.modified) { + dialog( + "This file has been modified since the last time it was saved. Would you like to reload?", + [{label: "Reload", value: true}, {label: "Cancel", value: false, focus: true}], + function(confirmed) { + if (confirmed) { + command.fire("session:revert-file"); + } else { + tab.modifiedAt = new Date(); + } + } + ); + } else { + command.fire("session:revert-file"); + } + } + }); + }); command.on("session:open-settings-file", function(name) { Settings.load(name, function() { diff --git a/js/sessions.js b/js/sessions.js index e1fd53c..3dd9075 100644 --- a/js/sessions.js +++ b/js/sessions.js @@ -19,6 +19,7 @@ define([ if (file) { session.file = file; session.fileName = file.entry.name; + session.modifiedAt = new Date(); } else { session.fileName = "untitled.txt"; } @@ -46,6 +47,7 @@ define([ var whenOpen = function() { self.file.write(content, c); self.modified = false; + self.modifiedAt = new Date(); renderTabs(); }; @@ -233,6 +235,7 @@ define([ var raiseTab = function(index) { var tab = tabs[index]; tab.raise(); + command.fire("session:check-file"); }; var switchTab = function(shift) {