From fd1d8728406d7d39452668aacdd78f6c5bd3e76f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 4 Feb 2015 17:14:46 -0800 Subject: [PATCH] Prompt split editors to save on close Previously an editor that was split into multiple panes would not prompt to save correctly when the window was unloading. This adds a new `windowCloseRequested` option passed through from the beforeunload handler to the editor so that it can specially handle this case. Closes #5257 --- spec/window-spec.coffee | 23 ++++++++++++++++++++++- src/pane-container.coffee | 4 ++-- src/pane.coffee | 4 ++-- src/text-editor.coffee | 6 +++++- src/window-event-handler.coffee | 2 +- src/workspace.coffee | 4 ++-- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/spec/window-spec.coffee b/spec/window-spec.coffee index 7e9370d50..1706e042e 100644 --- a/spec/window-spec.coffee +++ b/spec/window-spec.coffee @@ -1,5 +1,7 @@ {$, $$} = require '../src/space-pen-extensions' path = require 'path' +fs = require 'fs-plus' +temp = require 'temp' TextEditor = require '../src/text-editor' WindowEventHandler = require '../src/window-event-handler' @@ -54,7 +56,7 @@ describe "Window", -> jasmine.unspy(TextEditor.prototype, "shouldPromptToSave") beforeUnloadEvent = $.Event(new Event('beforeunload')) - describe "when pane items are are modified", -> + describe "when pane items are modified", -> it "prompts user to save and calls atom.workspace.confirmClose", -> editor = null spyOn(atom.workspace, 'confirmClose').andCallThrough() @@ -92,6 +94,25 @@ describe "Window", -> $(window).trigger(beforeUnloadEvent) expect(atom.confirm).toHaveBeenCalled() + describe "when the same path is modified in multiple panes", -> + it "prompts to save the item", -> + editor = null + filePath = path.join(temp.mkdirSync('atom-file'), 'file.txt') + fs.writeFileSync(filePath, 'hello') + spyOn(atom.workspace, 'confirmClose').andCallThrough() + spyOn(atom, 'confirm').andReturn(0) + + waitsForPromise -> + atom.workspace.open(filePath).then (o) -> editor = o + + runs -> + atom.workspace.getActivePane().splitRight(copyActiveItem: true) + editor.setText('world') + $(window).trigger(beforeUnloadEvent) + expect(atom.workspace.confirmClose).toHaveBeenCalled() + expect(atom.confirm.callCount).toBe 1 + expect(fs.readFileSync(filePath, 'utf8')).toBe 'world' + describe ".unloadEditorWindow()", -> it "saves the serialized state of the window so it can be deserialized after reload", -> workspaceState = atom.workspace.serialize() diff --git a/src/pane-container.coffee b/src/pane-container.coffee index f36169e3b..14fa25a44 100644 --- a/src/pane-container.coffee +++ b/src/pane-container.coffee @@ -152,12 +152,12 @@ class PaneContainer extends Model saveAll: -> pane.saveItems() for pane in @getPanes() - confirmClose: -> + confirmClose: (options) -> allSaved = true for pane in @getPanes() for item in pane.getItems() - unless pane.promptToSaveItem(item) + unless pane.promptToSaveItem(item, options) allSaved = false break diff --git a/src/pane.coffee b/src/pane.coffee index e2e61ca95..8524e3fae 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -437,8 +437,8 @@ class Pane extends Model destroyInactiveItems: -> @destroyItem(item) for item in @getItems() when item isnt @activeItem - promptToSaveItem: (item) -> - return true unless item.shouldPromptToSave?() + promptToSaveItem: (item, options={}) -> + return true unless item.shouldPromptToSave?(options) if typeof item.getURI is 'function' uri = item.getURI() diff --git a/src/text-editor.coffee b/src/text-editor.coffee index d09b8f4dd..32a4ded86 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -641,7 +641,11 @@ class TextEditor extends Model # Determine whether the user should be prompted to save before closing # this editor. - shouldPromptToSave: -> @isModified() and not @buffer.hasMultipleEditors() + shouldPromptToSave: ({windowCloseRequested}={})-> + if windowCloseRequested + @isModified() + else + @isModified() and not @buffer.hasMultipleEditors() ### Section: Reading Text diff --git a/src/window-event-handler.coffee b/src/window-event-handler.coffee index 37c04670b..d2a79b2a4 100644 --- a/src/window-event-handler.coffee +++ b/src/window-event-handler.coffee @@ -52,7 +52,7 @@ class WindowEventHandler @subscribe $(window), 'blur', -> document.body.classList.add('is-blurred') @subscribe $(window), 'beforeunload', => - confirmed = atom.workspace?.confirmClose() + confirmed = atom.workspace?.confirmClose(windowCloseRequested: true) atom.hide() if confirmed and not @reloadRequested and atom.getCurrentWindow().isWebViewFocused() @reloadRequested = false diff --git a/src/workspace.coffee b/src/workspace.coffee index fc2a12cdb..315fa7e94 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -599,8 +599,8 @@ class Workspace extends Model saveAll: -> @paneContainer.saveAll() - confirmClose: -> - @paneContainer.confirmClose() + confirmClose: (options) -> + @paneContainer.confirmClose(options) # Save the active pane item. #