add insertHTML command
This commit is contained in:
@@ -9,6 +9,7 @@ import {addListNodes} from "prosemirror-schema-list"
|
||||
import {exampleSetup} from "prosemirror-example-setup"
|
||||
|
||||
import insertText from './commands/insertText'
|
||||
import insertHTML from './commands/insertHTML'
|
||||
import focus from './commands/focus'
|
||||
|
||||
interface EditorOptions {
|
||||
@@ -36,6 +37,7 @@ export class Editor {
|
||||
this.view = this.createView()
|
||||
this.registerCommand('focus', focus)
|
||||
this.registerCommand('insertText', insertText)
|
||||
this.registerCommand('insertHTML', insertHTML)
|
||||
}
|
||||
|
||||
get state() {
|
||||
|
||||
22
packages/tiptap-core/src/commands/insertHTML.ts
Normal file
22
packages/tiptap-core/src/commands/insertHTML.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { DOMParser } from 'prosemirror-model'
|
||||
import { Editor } from '../Editor'
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
insertHTML(value: string): 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 slice = DOMParser.fromSchema(state.schema).parseSlice(element)
|
||||
const transaction = state.tr.insert(selection.anchor, slice.content)
|
||||
|
||||
view.dispatch(transaction)
|
||||
|
||||
next()
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
import { Editor } from '../Editor'
|
||||
|
||||
declare module "../Editor" {
|
||||
declare module '../Editor' {
|
||||
interface Editor {
|
||||
insertText(text: string): Editor,
|
||||
insertText(value: string): Editor,
|
||||
}
|
||||
}
|
||||
|
||||
export default function insertText(next: Function, { state, view }: Editor, text: string): void {
|
||||
view.dispatch(state.tr.insertText(text))
|
||||
export default function insertText(next: Function, { state, view }: Editor, value: string): void {
|
||||
const transaction = state.tr.insertText(value)
|
||||
|
||||
view.dispatch(transaction)
|
||||
next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user