Files
tiptap/packages/core/src/commands/deleteNode.ts
Philipp Kühn e07a5b625d refactor: Use named exports instead of default exports (#2238)
* use named exports instead of default exports

* fix tests

Co-authored-by: Philipp Kühn <philippkuehn@MacBook-Pro-von-Philipp.local>
2021-12-06 12:00:09 +01:00

37 lines
854 B
TypeScript

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
}