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
Esse commit está contido em:
@@ -1,5 +1,7 @@
|
|||||||
{$, $$} = require '../src/space-pen-extensions'
|
{$, $$} = require '../src/space-pen-extensions'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
|
fs = require 'fs-plus'
|
||||||
|
temp = require 'temp'
|
||||||
TextEditor = require '../src/text-editor'
|
TextEditor = require '../src/text-editor'
|
||||||
WindowEventHandler = require '../src/window-event-handler'
|
WindowEventHandler = require '../src/window-event-handler'
|
||||||
|
|
||||||
@@ -54,7 +56,7 @@ describe "Window", ->
|
|||||||
jasmine.unspy(TextEditor.prototype, "shouldPromptToSave")
|
jasmine.unspy(TextEditor.prototype, "shouldPromptToSave")
|
||||||
beforeUnloadEvent = $.Event(new Event('beforeunload'))
|
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", ->
|
it "prompts user to save and calls atom.workspace.confirmClose", ->
|
||||||
editor = null
|
editor = null
|
||||||
spyOn(atom.workspace, 'confirmClose').andCallThrough()
|
spyOn(atom.workspace, 'confirmClose').andCallThrough()
|
||||||
@@ -92,6 +94,25 @@ describe "Window", ->
|
|||||||
$(window).trigger(beforeUnloadEvent)
|
$(window).trigger(beforeUnloadEvent)
|
||||||
expect(atom.confirm).toHaveBeenCalled()
|
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()", ->
|
describe ".unloadEditorWindow()", ->
|
||||||
it "saves the serialized state of the window so it can be deserialized after reload", ->
|
it "saves the serialized state of the window so it can be deserialized after reload", ->
|
||||||
workspaceState = atom.workspace.serialize()
|
workspaceState = atom.workspace.serialize()
|
||||||
|
|||||||
@@ -152,12 +152,12 @@ class PaneContainer extends Model
|
|||||||
saveAll: ->
|
saveAll: ->
|
||||||
pane.saveItems() for pane in @getPanes()
|
pane.saveItems() for pane in @getPanes()
|
||||||
|
|
||||||
confirmClose: ->
|
confirmClose: (options) ->
|
||||||
allSaved = true
|
allSaved = true
|
||||||
|
|
||||||
for pane in @getPanes()
|
for pane in @getPanes()
|
||||||
for item in pane.getItems()
|
for item in pane.getItems()
|
||||||
unless pane.promptToSaveItem(item)
|
unless pane.promptToSaveItem(item, options)
|
||||||
allSaved = false
|
allSaved = false
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -437,8 +437,8 @@ class Pane extends Model
|
|||||||
destroyInactiveItems: ->
|
destroyInactiveItems: ->
|
||||||
@destroyItem(item) for item in @getItems() when item isnt @activeItem
|
@destroyItem(item) for item in @getItems() when item isnt @activeItem
|
||||||
|
|
||||||
promptToSaveItem: (item) ->
|
promptToSaveItem: (item, options={}) ->
|
||||||
return true unless item.shouldPromptToSave?()
|
return true unless item.shouldPromptToSave?(options)
|
||||||
|
|
||||||
if typeof item.getURI is 'function'
|
if typeof item.getURI is 'function'
|
||||||
uri = item.getURI()
|
uri = item.getURI()
|
||||||
|
|||||||
@@ -641,7 +641,11 @@ class TextEditor extends Model
|
|||||||
|
|
||||||
# Determine whether the user should be prompted to save before closing
|
# Determine whether the user should be prompted to save before closing
|
||||||
# this editor.
|
# this editor.
|
||||||
shouldPromptToSave: -> @isModified() and not @buffer.hasMultipleEditors()
|
shouldPromptToSave: ({windowCloseRequested}={})->
|
||||||
|
if windowCloseRequested
|
||||||
|
@isModified()
|
||||||
|
else
|
||||||
|
@isModified() and not @buffer.hasMultipleEditors()
|
||||||
|
|
||||||
###
|
###
|
||||||
Section: Reading Text
|
Section: Reading Text
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class WindowEventHandler
|
|||||||
@subscribe $(window), 'blur', -> document.body.classList.add('is-blurred')
|
@subscribe $(window), 'blur', -> document.body.classList.add('is-blurred')
|
||||||
|
|
||||||
@subscribe $(window), 'beforeunload', =>
|
@subscribe $(window), 'beforeunload', =>
|
||||||
confirmed = atom.workspace?.confirmClose()
|
confirmed = atom.workspace?.confirmClose(windowCloseRequested: true)
|
||||||
atom.hide() if confirmed and not @reloadRequested and atom.getCurrentWindow().isWebViewFocused()
|
atom.hide() if confirmed and not @reloadRequested and atom.getCurrentWindow().isWebViewFocused()
|
||||||
@reloadRequested = false
|
@reloadRequested = false
|
||||||
|
|
||||||
|
|||||||
@@ -599,8 +599,8 @@ class Workspace extends Model
|
|||||||
saveAll: ->
|
saveAll: ->
|
||||||
@paneContainer.saveAll()
|
@paneContainer.saveAll()
|
||||||
|
|
||||||
confirmClose: ->
|
confirmClose: (options) ->
|
||||||
@paneContainer.confirmClose()
|
@paneContainer.confirmClose(options)
|
||||||
|
|
||||||
# Save the active pane item.
|
# Save the active pane item.
|
||||||
#
|
#
|
||||||
|
|||||||
Referência em uma Nova Issue
Bloquear um usuário