feat: add deleteNode command

This commit is contained in:
Philipp Kühn
2021-07-28 11:19:42 +02:00
parent f25a65fb19
commit 73f1c50bca
5 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType'
import { RawCommands } from '../types'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
deleteNode: {
/**
* Delete a node.
*/
deleteNode: (typeOrName: string | NodeType) => ReturnType,
}
}
}
export const deleteNode: RawCommands['deleteNode'] = typeOrName => ({ tr, state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
const $pos = tr.selection.$anchor
for (let depth = $pos.depth; depth > 0; depth -= 1) {
const node = $pos.node(depth)
if (node.type === type) {
if (dispatch) {
const from = $pos.before(depth)
const to = $pos.after(depth)
tr.delete(from, to).scrollIntoView()
}
return true
}
}
return false
}