From d4a9c0ee09fabfb7ca7eac13c321b326b74ea62a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 6 May 2019 17:46:05 +0200 Subject: [PATCH] add state getter --- packages/tiptap/src/Editor.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/tiptap/src/Editor.js b/packages/tiptap/src/Editor.js index bc35201a..5db9c5e4 100644 --- a/packages/tiptap/src/Editor.js +++ b/packages/tiptap/src/Editor.js @@ -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() {