-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): implement deleteCurrentNode command & fix node joining on …
…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
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters