From cd4670fb6769f319431a458efbc491ffdb1cd7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sun, 29 Mar 2020 00:21:28 +0100 Subject: [PATCH] refactoring --- packages/tiptap-core/src/Editor.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index 717a6f18f..f7f6233c2 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -68,6 +68,10 @@ export class Editor extends EventEmitter { return (...args: any) => command(...args) } + public get state() { + return this.view.state + } + public registerCommand(name: string, callback: Command): Editor { if (this.commands[name]) { throw new Error(`tiptap: command '${name}' is already defined.`) @@ -125,12 +129,7 @@ export class Editor extends EventEmitter { } private createDocument(content: EditorContent, parseOptions: any = {}): any { - if (content === null) { - // this.options.emptyDocument - return this.createDocument('') - } - - if (typeof content === 'object') { + if (content && typeof content === 'object') { try { return this.schema.nodeFromJSON(content) } catch (error) { @@ -145,15 +144,18 @@ export class Editor extends EventEmitter { .parse(elementFromString(content), parseOptions) } - return false + return this.createDocument('') + } + + private storeSelection() { + const { from, to } = this.state.selection + this.selection = { from, to } } private dispatchTransaction(transaction: any): void { const state = this.state.apply(transaction) this.view.updateState(state) - - const { from, to } = this.state.selection - this.selection = { from, to } + this.storeSelection() // this.setActiveNodesAndMarks() @@ -171,10 +173,6 @@ export class Editor extends EventEmitter { // this.emitUpdate(transaction) } - public get state() { - return this.view.state - } - public setContent(content: EditorContent = '', emitUpdate: Boolean = false, parseOptions: any = {}) { const { doc, tr } = this.state const document = this.createDocument(content, parseOptions)