add state getter

This commit is contained in:
Philipp Kühn 2019-05-06 17:46:05 +02:00
parent e4dd55d08b
commit d4a9c0ee09

View File

@ -71,7 +71,6 @@ export default class Editor extends Emitter {
this.keymaps = this.createKeymaps()
this.inputRules = this.createInputRules()
this.pasteRules = this.createPasteRules()
this.state = this.createState()
this.view = this.createView()
this.commands = this.createCommands()
this.setActiveNodesAndMarks()
@ -118,6 +117,10 @@ export default class Editor extends Emitter {
]
}
get state() {
return this.view ? this.view.state : null
}
createExtensions() {
return new ExtensionManager([
...this.builtInExtensions,
@ -233,7 +236,7 @@ export default class Editor extends Emitter {
createView() {
const view = new EditorView(this.element, {
state: this.state,
state: this.createState(),
handlePaste: (...args) => { this.emit('paste', ...args) },
handleDrop: (...args) => { this.emit('drop', ...args) },
dispatchTransaction: this.dispatchTransaction.bind(this),
@ -300,8 +303,8 @@ export default class Editor extends Emitter {
}
dispatchTransaction(transaction) {
this.state = this.state.apply(transaction)
this.view.updateState(this.state)
const newState = this.state.apply(transaction)
this.view.updateState(newState)
this.setActiveNodesAndMarks()
if (!transaction.docChanged) {
@ -365,13 +368,13 @@ export default class Editor extends Emitter {
}
setContent(content = {}, emitUpdate = false, parseOptions) {
this.state = EditorState.create({
const newState = EditorState.create({
schema: this.state.schema,
doc: this.createDocument(content, parseOptions),
plugins: this.state.plugins,
})
this.view.updateState(this.state)
this.view.updateState(newState)
if (emitUpdate) {
this.emitUpdate()
@ -426,10 +429,10 @@ export default class Editor extends Emitter {
return
}
this.state = this.state.reconfigure({
const newState = this.state.reconfigure({
plugins: this.state.plugins.concat([plugin]),
})
this.view.updateState(this.state)
this.view.updateState(newState)
}
destroy() {