Swap underlying editor correctly when calling setModel on editor element

Previously, when `setModel` was called, we forgot to update the pointer
to the component in the newly supplied editor. This was causing the
element to not update in response to model updates but only as a result
of focus or visibility changes. We suspect this regressed during the
rewrite of the editor rendering layer.

With this commit we will now correctly swap the element's underlying
editor by updating the component pointer on the newly supplied editor.
Also, if the element was already attached to another editor, we will
null out the component reference on it, because one instance of
`TextEditorElement` can only represent one instance of `TextEditor`.
This commit is contained in:
Antonio Scandurra
2017-07-13 15:11:36 +02:00
parent 52bfe3eee5
commit c3f7edc104
2 changed files with 31 additions and 0 deletions

View File

@@ -201,6 +201,33 @@ describe('TextEditorElement', () => {
})
})
describe('::setModel', () => {
describe('when the element does not have an editor yet', () => {
it('uses the supplied one', () => {
const element = buildTextEditorElement({attach: false})
const editor = new TextEditor()
element.setModel(editor)
jasmine.attachToDOM(element)
expect(editor.element).toBe(element)
expect(element.getModel()).toBe(editor)
})
})
describe('when the element already has an editor', () => {
it('unbinds it and then swaps it with the supplied one', async () => {
const element = buildTextEditorElement({attach: true})
const previousEditor = element.getModel()
expect(previousEditor.element).toBe(element)
const newEditor = new TextEditor()
element.setModel(newEditor)
expect(previousEditor.element).not.toBe(element)
expect(newEditor.element).toBe(element)
expect(element.getModel()).toBe(newEditor)
})
})
})
describe('::onDidAttach and ::onDidDetach', () =>
it('invokes callbacks when the element is attached and detached', () => {
const element = buildTextEditorElement({attach: false})

View File

@@ -174,6 +174,10 @@ class TextEditorComponent {
}
update (props) {
if (props.model !== this.props.model) {
this.props.model.component = null
props.model.component = this
}
this.props = props
this.scheduleUpdate()
}