fix: Backspace bug when current block is empty and previous block's last child is empty#2610
fix: Backspace bug when current block is empty and previous block's last child is empty#2610matthewlipski wants to merge 1 commit intomainfrom
Conversation
…last child is empty
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe Backspace handler now uses Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@packages/core/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts`:
- Around line 263-275: The selection mode check is using prevBlockInfo while the
target position is computed from bottomNestedPrevBlockInfo, causing wrong branch
choices; update the branching that decides between node vs text selection to
inspect bottomNestedPrevBlockInfo (e.g., check
bottomNestedPrevBlockInfo.isBlockContainer or
bottomNestedPrevBlockInfo.node.spec.content === "" as appropriate) before
calling chainedCommands.setTextSelection, so the selection decision matches the
actual deepest-target node determined by getBottomNestedBlockInfo.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: bbe0b0eb-e082-40b7-8797-49a18a2f5af4
📒 Files selected for processing (1)
packages/core/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts
| const bottomNestedPrevBlockInfo = getBottomNestedBlockInfo( | ||
| state.doc, | ||
| prevBlockInfo, | ||
| ); | ||
| if (!bottomNestedPrevBlockInfo.isBlockContainer) { | ||
| return false; | ||
| } | ||
| const blockContentEndPos = | ||
| bottomNestedPrevBlockInfo.blockContent.afterPos - 1; | ||
|
|
||
| chainedCommands = | ||
| chainedCommands.setTextSelection(blockContentStartPos); | ||
| chainedCommands.setTextSelection(blockContentEndPos); | ||
| } |
There was a problem hiding this comment.
Selection mode is derived from the wrong block type after switching to bottom-nested targeting.
At Line 274, the target position is computed from bottomNestedPrevBlockInfo, but the node-vs-text selection branching still uses prevBlockInfo earlier. This can incorrectly call setTextSelection when the deepest descendant is a non-inline block (spec.content === ""), causing wrong cursor behavior.
💡 Proposed fix
- } else {
- const bottomNestedPrevBlockInfo = getBottomNestedBlockInfo(
- state.doc,
- prevBlockInfo,
- );
- if (!bottomNestedPrevBlockInfo.isBlockContainer) {
- return false;
- }
- const blockContentEndPos =
- bottomNestedPrevBlockInfo.blockContent.afterPos - 1;
-
- chainedCommands =
- chainedCommands.setTextSelection(blockContentEndPos);
- }
+ } else {
+ const bottomNestedPrevBlockInfo = getBottomNestedBlockInfo(
+ state.doc,
+ prevBlockInfo,
+ );
+ if (!bottomNestedPrevBlockInfo.isBlockContainer) {
+ return false;
+ }
+
+ const targetContentSpec =
+ bottomNestedPrevBlockInfo.blockContent.node.type.spec.content;
+
+ chainedCommands =
+ targetContentSpec === ""
+ ? chainedCommands.setNodeSelection(
+ bottomNestedPrevBlockInfo.blockContent.beforePos,
+ )
+ : chainedCommands.setTextSelection(
+ bottomNestedPrevBlockInfo.blockContent.afterPos - 1,
+ );
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@packages/core/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts`
around lines 263 - 275, The selection mode check is using prevBlockInfo while
the target position is computed from bottomNestedPrevBlockInfo, causing wrong
branch choices; update the branching that decides between node vs text selection
to inspect bottomNestedPrevBlockInfo (e.g., check
bottomNestedPrevBlockInfo.isBlockContainer or
bottomNestedPrevBlockInfo.node.spec.content === "" as appropriate) before
calling chainedCommands.setTextSelection, so the selection decision matches the
actual deepest-target node determined by getBottomNestedBlockInfo.
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/mantine
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
commit: |
Summary
This PR fixes an issue with the following case:
When the cursor is in the empty paragraph at the end, pressing backspace will delete it and move the cursor to the end of the first paragraph. The intended behaviour is for the cursor to move to the empty nested paragraph.
Closes #2566
Rationale
This is a bug and should be fixed.
Changes
Impact
N/A
Testing
TODO: Add e2e test?
Screenshots/Video
Checklist
Additional Notes
Summary by CodeRabbit