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

22 lines
645 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
type InsertHTMLCommand = (value: string) => Editor
2020-04-22 05:22:27 +08:00
2019-12-15 06:54:20 +08:00
declare module '../Editor' {
interface Editor {
insertHTML: InsertHTMLCommand,
2019-12-15 06:54:20 +08:00
}
}
export default (next: Function, editor: Editor) => (value: string) => {
2020-03-05 04:27:22 +08:00
const { view, state } = editor
2019-12-15 06:54:20 +08:00
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()
}