add insertContentAt command

This commit is contained in:
Philipp Kühn
2021-05-05 13:00:30 +02:00
parent ab3261e12b
commit 9ff7e2400e
3 changed files with 44 additions and 23 deletions

View File

@@ -1,5 +1,3 @@
import createNodeFromContent from '../helpers/createNodeFromContent'
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
import { Command, RawCommands, Content } from '../types'
declare module '@tiptap/core' {
@@ -13,25 +11,6 @@ declare module '@tiptap/core' {
}
}
export const insertContent: RawCommands['insertContent'] = value => ({ tr, dispatch, editor }) => {
if (dispatch) {
const content = createNodeFromContent(value, editor.schema)
if (typeof content === 'string') {
tr.insertText(content)
tr.scrollIntoView()
return true
}
if (!tr.selection.empty) {
tr.deleteSelection()
}
tr.insert(tr.selection.anchor, content)
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
tr.scrollIntoView()
}
return true
export const insertContent: RawCommands['insertContent'] = value => ({ tr, commands }) => {
return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value)
}