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

@ -166,6 +166,7 @@ Have a look at all of the core commands listed below. They should give you a goo
| ----------------------- | --------------------------------------------------------- | ------------------------------------ |
| .clearNodes() | Normalize nodes to a simple paragraph. | [More](/api/commands/clear-nodes) |
| .createParagraphNear() | Create a paragraph nearby. | [More](/api/commands/create-paragraph-near) |
| .deleteNode() | Delete a node. | [More](/api/commands/delete-node) |
| .extendMarkRange() | Extends the text selection to the current mark. | [More](/api/commands/extend-mark-range) |
| .exitCode() | Exit from a code block. | [More](/api/commands/exit-code) |
| .joinBackward() | Join two nodes backward. | [More](/api/commands/join-backward) |

View File

@ -0,0 +1,3 @@
# deleteNode
<ContentMissing />

View File

@ -140,6 +140,9 @@
- title: createParagraphNear
link: /api/commands/create-paragraph-near
type: draft
- title: deleteNode
link: /api/commands/delete-node
type: draft
- title: deleteRange
link: /api/commands/delete-range
type: draft

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
}

View File

@ -4,6 +4,7 @@ import * as clearContent from '../commands/clearContent'
import * as clearNodes from '../commands/clearNodes'
import * as command from '../commands/command'
import * as createParagraphNear from '../commands/createParagraphNear'
import * as deleteNode from '../commands/deleteNode'
import * as deleteRange from '../commands/deleteRange'
import * as deleteSelection from '../commands/deleteSelection'
import * as enter from '../commands/enter'
@ -54,6 +55,7 @@ export { clearContent }
export { clearNodes }
export { command }
export { createParagraphNear }
export { deleteNode }
export { deleteRange }
export { deleteSelection }
export { enter }
@ -109,6 +111,7 @@ export const Commands = Extension.create({
...clearNodes,
...command,
...createParagraphNear,
...deleteNode,
...deleteRange,
...deleteSelection,
...enter,