Add Workspace::onWillDestroyPaneItem

It is needed by the autosave package which previously relied on DOM
bubbling to coalesce events from various panes.
This commit is contained in:
Nathan Sobo 2014-12-01 10:39:09 -07:00
parent 3d3521962c
commit 7dc9d0c8ce
4 changed files with 53 additions and 6 deletions

View File

@ -148,3 +148,30 @@ describe "PaneContainer", ->
saved = container.confirmClose()
expect(saved).toBeFalsy()
expect(atom.confirm).toHaveBeenCalled()
describe "::onWillDestroyPaneItem() and ::onDidDestroyPaneItem", ->
it "invokes the given callbacks when an item will be destroyed on any pane", ->
container = new PaneContainer
pane1 = container.getRoot()
item1 = new Object
item2 = new Object
item3 = new Object
pane1.addItem(item1)
events = []
container.onWillDestroyPaneItem (event) -> events.push(['will', event])
container.onDidDestroyPaneItem (event) -> events.push(['did', event])
pane2 = pane1.splitRight(items: [item2, item3])
pane1.destroyItem(item1)
pane2.destroyItem(item3)
pane2.destroyItem(item2)
expect(events).toEqual [
['will', {item: item1, pane: pane1, index: 0}]
['did', {item: item1, pane: pane1, index: 0}]
['will', {item: item3, pane: pane2, index: 1}]
['did', {item: item3, pane: pane2, index: 1}]
['will', {item: item2, pane: pane2, index: 0}]
['did', {item: item2, pane: pane2, index: 0}]
]

View File

@ -112,6 +112,9 @@ class PaneContainer extends Model
fn(@getActivePaneItem())
@onDidChangeActivePaneItem(fn)
onWillDestroyPaneItem: (fn) ->
@emitter.on 'will-destroy-pane-item', fn
onDidDestroyPaneItem: (fn) ->
@emitter.on 'did-destroy-pane-item', fn
@ -193,8 +196,11 @@ class PaneContainer extends Model
destroyEmptyPanes: ->
pane.destroy() for pane in @getPanes() when pane.items.length is 0
paneItemDestroyed: (item) ->
@emitter.emit 'did-destroy-pane-item', item
willDestroyPaneItem: (event) ->
@emitter.emit 'will-destroy-pane-item', event
didDestroyPaneItem: (event) ->
@emitter.emit 'did-destroy-pane-item', event
didAddPane: (pane) ->
@emitter.emit 'did-add-pane', pane

View File

@ -371,7 +371,7 @@ class Pane extends Model
@items.splice(index, 1)
@emit 'item-removed', item, index, destroyed
@emitter.emit 'did-remove-item', {item, index, destroyed}
@container?.paneItemDestroyed(item) if destroyed
@container?.didDestroyPaneItem({item, index, pane: this}) if destroyed
@destroy() if @items.length is 0 and atom.config.get('core.destroyEmptyPanes')
# Public: Move the given item to the given index.
@ -410,6 +410,7 @@ class Pane extends Model
if index isnt -1
@emit 'before-item-destroyed', item
@emitter.emit 'will-destroy-item', {item, index}
@container?.willDestroyPaneItem({item, index, pane: this})
if @promptToSaveItem(item)
@removeItem(item, true)
item.destroy?()

View File

@ -47,7 +47,7 @@ class Workspace extends Model
viewRegistry = atom.views
@paneContainer ?= new PaneContainer({viewRegistry})
@paneContainer.onDidDestroyPaneItem(@onPaneItemDestroyed)
@paneContainer.onDidDestroyPaneItem(@didDestroyPaneItem)
@panelContainers =
top: new PanelContainer({viewRegistry, location: 'top'})
@ -271,7 +271,7 @@ class Workspace extends Model
# Extended: Invoke the given callback when a pane item is added to the
# workspace.
#
# * `callback` {Function} to be called when panes are added.
# * `callback` {Function} to be called when pane items are added.
# * `event` {Object} with the following keys:
# * `item` The added pane item.
# * `pane` {Pane} containing the added item.
@ -280,6 +280,19 @@ class Workspace extends Model
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPaneItem: (callback) -> @paneContainer.onDidAddPaneItem(callback)
# Extended: Invoke the given callback when a pane item is about to be
# destroyed, before the user is prompted to save it.
#
# * `callback` {Function} to be called before pane items are destroyed.
# * `event` {Object} with the following keys:
# * `item` The item to be destroyed.
# * `pane` {Pane} containing the item to be destroyed.
# * `index` {Number} indicating the index of the item to be destroyed in
# its pane.
#
# Returns a {Disposable} on which `.dispose` can be called to unsubscribe.
onWillDestroyPaneItem: (callback) -> @paneContainer.onWillDestroyPaneItem(callback)
# Extended: Invoke the given callback when a text editor is added to the
# workspace.
#
@ -605,7 +618,7 @@ class Workspace extends Model
_.remove(@destroyedItemUris, uri)
# Adds the destroyed item's uri to the list of items to reopen.
onPaneItemDestroyed: (item) =>
didDestroyPaneItem: ({item}) =>
if uri = item.getUri?()
@destroyedItemUris.push(uri)