diff --git a/spec/workspace-center-spec.js b/spec/workspace-center-spec.js new file mode 100644 index 000000000..7c01078c3 --- /dev/null +++ b/spec/workspace-center-spec.js @@ -0,0 +1,31 @@ +/** @babel */ + +const TextEditor = require('../src/text-editor') + +import {it, fit, ffit, fffit, beforeEach, afterEach} from './async-spec-helpers' + +describe('WorkspaceCenter', () => { + describe('.observeTextEditors()', () => { + it('invokes the observer with current and future text editors', () => { + const workspaceCenter = atom.workspace.getCenter() + const pane = workspaceCenter.getActivePane() + const observed = [] + + const editorAddedBeforeRegisteringObserver = new TextEditor() + const nonEditorItemAddedBeforeRegisteringObserver = document.createElement('div') + pane.activateItem(editorAddedBeforeRegisteringObserver) + pane.activateItem(nonEditorItemAddedBeforeRegisteringObserver) + + workspaceCenter.observeTextEditors(editor => observed.push(editor)) + + const editorAddedAfterRegisteringObserver = new TextEditor() + const nonEditorItemAddedAfterRegisteringObserver = document.createElement('div') + pane.activateItem(editorAddedAfterRegisteringObserver) + pane.activateItem(nonEditorItemAddedAfterRegisteringObserver) + + expect(observed).toEqual( + [editorAddedBeforeRegisteringObserver, editorAddedAfterRegisteringObserver] + ) + }) + }) +}) diff --git a/src/workspace-center.js b/src/workspace-center.js index 56d821265..1972c5c59 100644 --- a/src/workspace-center.js +++ b/src/workspace-center.js @@ -226,6 +226,25 @@ module.exports = class WorkspaceCenter { return this.paneContainer.onDidDestroyPaneItem(callback) } + // Extended: Invoke the given callback when a text editor is added to the + // workspace center. + // + // * `callback` {Function} to be called when panes are added. + // * `event` {Object} with the following keys: + // * `textEditor` {TextEditor} that was added. + // * `pane` {Pane} containing the added text editor. + // * `index` {Number} indicating the index of the added text editor in its + // pane. + // + // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidAddTextEditor (callback) { + return this.onDidAddPaneItem(({item, pane, index}) => { + if (item instanceof TextEditor) { + callback({textEditor: item, pane, index}) + } + }) + } + /* Section: Pane Items */