Merge pull request #14695 from atom/jr-editors-live-in-workspace-center

Provide API for observing the active text editor
This commit is contained in:
Jason Rudolph
2017-06-06 10:09:52 -04:00
committed by GitHub
5 changed files with 221 additions and 112 deletions

View File

@@ -4,6 +4,7 @@ const _ = require('underscore-plus')
const {CompositeDisposable} = require('event-kit')
const PaneContainer = require('./pane-container')
const TextEditor = require('./text-editor')
const Grim = require('grim')
const MINIMUM_SIZE = 100
const DEFAULT_INITIAL_SIZE = 300
@@ -384,21 +385,6 @@ module.exports = class Dock {
Section: Event Subscription
*/
// Essential: Invoke the given callback with all current and future text
// editors in the dock.
//
// * `callback` {Function} to be called with current and future text editors.
// * `editor` An {TextEditor} that is present in {::getTextEditors} at the time
// of subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeTextEditors (callback) {
for (const textEditor of this.getTextEditors()) {
callback(textEditor)
}
return this.onDidAddTextEditor(({textEditor}) => callback(textEditor))
}
// Essential: Invoke the given callback with all current and future panes items
// in the dock.
//
@@ -583,18 +569,13 @@ module.exports = class Dock {
return this.paneContainer.getActivePaneItem()
}
// Essential: Get all text editors in the dock.
// Deprecated: Get the active item if it is a {TextEditor}.
//
// Returns an {Array} of {TextEditor}s.
getTextEditors () {
return this.paneContainer.getTextEditors()
}
// Essential: Get the active item if it is an {TextEditor}.
//
// Returns an {TextEditor} or `undefined` if the current active item is not an
// Returns a {TextEditor} or `undefined` if the current active item is not a
// {TextEditor}.
getActiveTextEditor () {
Grim.deprecate('Text editors are not allowed in docks. Use atom.workspace.getActiveTextEditor() instead.')
const activeItem = this.getActivePaneItem()
if (activeItem instanceof TextEditor) { return activeItem }
}

View File

@@ -3628,6 +3628,9 @@ class TextEditor extends Model
})
@component.element
getAllowedLocations: ->
['center']
# Essential: Retrieves the greyed out placeholder of a mini editor.
#
# Returns a {String}.

View File

@@ -216,6 +216,7 @@ module.exports = class Workspace extends Model {
bottom: this.createDock('bottom')
}
this.activePaneContainer = this.paneContainers.center
this.hasActiveTextEditor = false
this.panelContainers = {
top: new PanelContainer({viewRegistry: this.viewRegistry, location: 'top'}),
@@ -296,6 +297,7 @@ module.exports = class Workspace extends Model {
bottom: this.createDock('bottom')
}
this.activePaneContainer = this.paneContainers.center
this.hasActiveTextEditor = false
this.panelContainers = {
top: new PanelContainer({viewRegistry: this.viewRegistry, location: 'top'}),
@@ -371,6 +373,8 @@ module.exports = class Workspace extends Model {
this.paneContainers.center.deserialize(state.paneContainer, deserializerManager)
}
this.hasActiveTextEditor = this.getActiveTextEditor() != null
this.updateWindowTitle()
}
@@ -422,6 +426,16 @@ module.exports = class Workspace extends Model {
this.didChangeActivePaneItem(item)
this.emitter.emit('did-change-active-pane-item', item)
}
if (paneContainer === this.getCenter()) {
const hadActiveTextEditor = this.hasActiveTextEditor
this.hasActiveTextEditor = item instanceof TextEditor
if (this.hasActiveTextEditor || hadActiveTextEditor) {
const itemValue = this.hasActiveTextEditor ? item : undefined
this.emitter.emit('did-change-active-text-editor', itemValue)
}
}
}
didChangeActivePaneItem (item) {
@@ -648,6 +662,18 @@ module.exports = class Workspace extends Model {
return this.emitter.on('did-stop-changing-active-pane-item', callback)
}
// Essential: Invoke the given callback when a text editor becomes the active
// text editor and when there is no longer an active text editor.
//
// * `callback` {Function} to be called when the active text editor changes.
// * `editor` The active {TextEditor} or undefined if there is no longer an
// active text editor.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActiveTextEditor (callback) {
return this.emitter.on('did-change-active-text-editor', callback)
}
// Essential: Invoke the given callback with the current active pane item and
// with all future active pane items in the workspace.
//
@@ -660,6 +686,21 @@ module.exports = class Workspace extends Model {
return this.onDidChangeActivePaneItem(callback)
}
// Essential: Invoke the given callback with the current active text editor
// (if any), with all future active text editors, and when there is no longer
// an active text editor.
//
// * `callback` {Function} to be called when the active text editor changes.
// * `editor` The active {TextEditor} or undefined if there is not an
// active text editor.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActiveTextEditor (callback) {
callback(this.getActiveTextEditor())
return this.onDidChangeActiveTextEditor(callback)
}
// Essential: Invoke the given callback whenever an item is opened. Unlike
// {::onDidAddPaneItem}, observers will be notified for items that are already
// present in the workspace when they are reopened.
@@ -1282,12 +1323,12 @@ module.exports = class Workspace extends Model {
return this.getPaneItems().filter(item => item instanceof TextEditor)
}
// Essential: Get the active item if it is an {TextEditor}.
// Essential: Get the workspace center's active item if it is a {TextEditor}.
//
// Returns an {TextEditor} or `undefined` if the current active item is not an
// {TextEditor}.
// Returns a {TextEditor} or `undefined` if the workspace center's current
// active item is not a {TextEditor}.
getActiveTextEditor () {
const activeItem = this.getActivePaneItem()
const activeItem = this.getCenter().getActivePaneItem()
if (activeItem instanceof TextEditor) { return activeItem }
}