tiptap/packages/tiptap-core/src/commands/insertHTML.ts

19 lines
591 B
TypeScript
Raw Normal View History

2019-12-15 06:54:20 +08:00
import { DOMParser } from 'prosemirror-model'
import { Editor } from '../Editor'
2019-12-17 06:51:18 +08:00
import elementFromString from '../utils/elementFromString'
2019-12-15 06:54:20 +08:00
declare module '../Editor' {
interface Editor {
insertHTML(value: string): Editor,
}
}
export default function insertHTML(next: Function, { state, view }: Editor, value: string): void {
const { selection } = state
2019-12-17 06:51:18 +08:00
const element = elementFromString(value)
2019-12-15 06:54:20 +08:00
const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
const transaction = state.tr.insert(selection.anchor, slice.content)
view.dispatch(transaction)
next()
}