refactoring
This commit is contained in:
@@ -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
|
||||
@@ -133,4 +118,20 @@ 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
7
packages/tiptap-core/src/utils/elementFromString.ts
Normal file
7
packages/tiptap-core/src/utils/elementFromString.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
export default function elementFromString(value: string): HTMLDivElement {
|
||||
const element = document.createElement('div')
|
||||
element.innerHTML = value.trim()
|
||||
|
||||
return element
|
||||
}
|
||||
Reference in New Issue
Block a user