add insertNode command

This commit is contained in:
Philipp Kühn
2021-04-02 21:55:25 +02:00
parent 4d882af5d7
commit a5e1030e4f
3 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType'
import { Command, RawCommands, AnyObject } from '../types'
declare module '@tiptap/core' {
interface Commands {
insertNode: {
/**
* Insert a node at the current position.
*/
insertNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}
export const insertNode: RawCommands['insertNode'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const { selection } = tr
const type = getNodeType(typeOrName, state.schema)
if (!type) {
return false
}
const node = type.create(attributes)
if (dispatch) {
tr.insert(selection.anchor, node)
}
return true
}