From 48c7d4bb9805a3dd7aa801bdc215a4a8ee61be87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 16 Dec 2019 23:51:18 +0100 Subject: [PATCH] refactoring --- packages/tiptap-core/src/Editor.ts | 41 ++++++++++--------- .../tiptap-core/src/commands/insertHTML.ts | 7 +--- .../src/utils/elementFromString.ts | 7 ++++ 3 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 packages/tiptap-core/src/utils/elementFromString.ts diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index e907303c..f6d937ad 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -12,6 +12,8 @@ import insertText from './commands/insertText' import insertHTML from './commands/insertHTML' import focus from './commands/focus' +import elementFromString from './utils/elementFromString' + type EditorContent = string | JSON interface EditorOptions { @@ -70,22 +72,6 @@ export class Editor { return this } - registerCommand(name: string, method: Function): Editor { - // @ts-ignore - this[name] = this.chainCommand((...args: any) => { - return new Promise(resolve => { - return method(resolve as Function, this as Editor, ...args as any) - }) - }) - - return this - } - - command(name: string, ...args: any) { - // @ts-ignore - return this[name](...args) - } - private createDocument(content: EditorContent): any { // if (content === null) { // return this.schema.nodeFromJSON(this.options.emptyDocument) @@ -101,10 +87,9 @@ export class Editor { // } if (typeof content === 'string') { - const element = document.createElement('div') - element.innerHTML = content.trim() - - return DOMParser.fromSchema(this.schema).parse(element) + return DOMParser + .fromSchema(this.schema) + .parse(elementFromString(content)) } return false @@ -132,5 +117,21 @@ export class Editor { // this.emitUpdate(transaction) } + + public registerCommand(name: string, method: Function): Editor { + // @ts-ignore + this[name] = this.chainCommand((...args: any) => { + return new Promise(resolve => { + return method(resolve as Function, this as Editor, ...args as any) + }) + }) + + return this + } + + public command(name: string, ...args: any) { + // @ts-ignore + return this[name](...args) + } } diff --git a/packages/tiptap-core/src/commands/insertHTML.ts b/packages/tiptap-core/src/commands/insertHTML.ts index 9fda10cb..535c5da1 100644 --- a/packages/tiptap-core/src/commands/insertHTML.ts +++ b/packages/tiptap-core/src/commands/insertHTML.ts @@ -1,5 +1,6 @@ import { DOMParser } from 'prosemirror-model' import { Editor } from '../Editor' +import elementFromString from '../utils/elementFromString' declare module '../Editor' { interface Editor { @@ -9,14 +10,10 @@ declare module '../Editor' { export default function insertHTML(next: Function, { state, view }: Editor, value: string): void { const { selection } = state - const element = document.createElement('div') - - element.innerHTML = value.trim() - + const element = elementFromString(value) const slice = DOMParser.fromSchema(state.schema).parseSlice(element) const transaction = state.tr.insert(selection.anchor, slice.content) view.dispatch(transaction) - next() } \ No newline at end of file diff --git a/packages/tiptap-core/src/utils/elementFromString.ts b/packages/tiptap-core/src/utils/elementFromString.ts new file mode 100644 index 00000000..ee5f8566 --- /dev/null +++ b/packages/tiptap-core/src/utils/elementFromString.ts @@ -0,0 +1,7 @@ + +export default function elementFromString(value: string): HTMLDivElement { + const element = document.createElement('div') + element.innerHTML = value.trim() + + return element +} \ No newline at end of file