refactoring
This commit is contained in:
@@ -12,6 +12,8 @@ import insertText from './commands/insertText'
|
|||||||
import insertHTML from './commands/insertHTML'
|
import insertHTML from './commands/insertHTML'
|
||||||
import focus from './commands/focus'
|
import focus from './commands/focus'
|
||||||
|
|
||||||
|
import elementFromString from './utils/elementFromString'
|
||||||
|
|
||||||
type EditorContent = string | JSON
|
type EditorContent = string | JSON
|
||||||
|
|
||||||
interface EditorOptions {
|
interface EditorOptions {
|
||||||
@@ -70,22 +72,6 @@ export class Editor {
|
|||||||
return this
|
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 {
|
private createDocument(content: EditorContent): any {
|
||||||
// if (content === null) {
|
// if (content === null) {
|
||||||
// return this.schema.nodeFromJSON(this.options.emptyDocument)
|
// return this.schema.nodeFromJSON(this.options.emptyDocument)
|
||||||
@@ -101,10 +87,9 @@ export class Editor {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
if (typeof content === 'string') {
|
if (typeof content === 'string') {
|
||||||
const element = document.createElement('div')
|
return DOMParser
|
||||||
element.innerHTML = content.trim()
|
.fromSchema(this.schema)
|
||||||
|
.parse(elementFromString(content))
|
||||||
return DOMParser.fromSchema(this.schema).parse(element)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
@@ -133,4 +118,20 @@ export class Editor {
|
|||||||
// this.emitUpdate(transaction)
|
// 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 { DOMParser } from 'prosemirror-model'
|
||||||
import { Editor } from '../Editor'
|
import { Editor } from '../Editor'
|
||||||
|
import elementFromString from '../utils/elementFromString'
|
||||||
|
|
||||||
declare module '../Editor' {
|
declare module '../Editor' {
|
||||||
interface Editor {
|
interface Editor {
|
||||||
@@ -9,14 +10,10 @@ declare module '../Editor' {
|
|||||||
|
|
||||||
export default function insertHTML(next: Function, { state, view }: Editor, value: string): void {
|
export default function insertHTML(next: Function, { state, view }: Editor, value: string): void {
|
||||||
const { selection } = state
|
const { selection } = state
|
||||||
const element = document.createElement('div')
|
const element = elementFromString(value)
|
||||||
|
|
||||||
element.innerHTML = value.trim()
|
|
||||||
|
|
||||||
const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
|
const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
|
||||||
const transaction = state.tr.insert(selection.anchor, slice.content)
|
const transaction = state.tr.insert(selection.anchor, slice.content)
|
||||||
|
|
||||||
view.dispatch(transaction)
|
view.dispatch(transaction)
|
||||||
|
|
||||||
next()
|
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