Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): implement deleteCurrentNode command & fix node joining on Delete key #3192

41 changes: 41 additions & 0 deletions packages/core/src/commands/deleteCurrentNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RawCommands } from '../types'

declare module '@tiptap/core' {
interface Commands<ReturnType> {
deleteCurrentNode: {
/**
* Delete the node that currently has the selection anchor.
*/
deleteCurrentNode: () => ReturnType,
Comment on lines +6 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command should be marked @internal or at least have its behavior documented more clearly. I tried using it thinking it would delete the current node, but the current node was not empty, so it wasn't deleted, leading me to think Tiptap was broken.

}
}
}

export const deleteCurrentNode: RawCommands['deleteCurrentNode'] = () => ({ tr, dispatch }) => {
const { selection } = tr
const currentNode = selection.$anchor.node()

// if there is content inside the current node, break out of this command
if (currentNode.content.size > 0) {
return false
}

const $pos = tr.selection.$anchor

for (let depth = $pos.depth; depth > 0; depth -= 1) {
const node = $pos.node(depth)

if (node.type === currentNode.type) {
if (dispatch) {
const from = $pos.before(depth)
const to = $pos.after(depth)

tr.delete(from, to).scrollIntoView()
}

return true
}
}

return false
}
1 change: 1 addition & 0 deletions packages/core/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './clearContent'
export * from './clearNodes'
export * from './command'
export * from './createParagraphNear'
export * from './deleteCurrentNode'
export * from './deleteNode'
export * from './deleteRange'
export * from './deleteSelection'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/extensions/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const Keymap = Extension.create({

const handleDelete = () => this.editor.commands.first(({ commands }) => [
() => commands.deleteSelection(),
() => commands.deleteCurrentNode(),
() => commands.joinForward(),
() => commands.selectNodeForward(),
])
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,5 @@ export type TextSerializer = (props: {
export type ExtendedRegExpMatchArray = RegExpMatchArray & {
data?: Record<string, any>,
}

export type Dispatch = ((args?: any) => any) | undefined