Skip to content

Commit

Permalink
fix(core): implement deleteCurrentNode command & fix node joining on …
Browse files Browse the repository at this point in the history
…Delete key (#3192)

* fix(core): 🐛 fix delete on empty nodes joining next node incorrectly

This PR implements a "deleteCurrentNode" action in combination with registering this command inside the keymap for the delete key. This way, we editor will check, if the current node is empty before joining - if the current node is empty, the node will be removed. Joining will still work if the current node is not empty and the selection is at the end of the current node

2924

* refactor(core): ♻️ remove unnecessary return on handleDelete keymap
  • Loading branch information
bdbch authored Nov 25, 2022
1 parent 28d436f commit ef8a1a2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
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,
}
}
}

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

0 comments on commit ef8a1a2

Please sign in to comment.