Skip to content

fix: Backspace bug when current block is empty and previous block's last child is empty#2610

Open
matthewlipski wants to merge 1 commit intomainfrom
backspace-empty-nested-block-fix
Open

fix: Backspace bug when current block is empty and previous block's last child is empty#2610
matthewlipski wants to merge 1 commit intomainfrom
backspace-empty-nested-block-fix

Conversation

@matthewlipski
Copy link
Copy Markdown
Collaborator

@matthewlipski matthewlipski commented Mar 31, 2026

Summary

This PR fixes an issue with the following case:

Paragraph
  Nested Paragraph
  [Empty Nested Paragraph]
[Empty Paragraph]

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

  • Changed backspace handler case to move selection to the end of the previous block's last descendant, instead of the end of its content.

Impact

N/A

Testing

TODO: Add e2e test?

Screenshots/Video

Checklist

  • Code follows the project's coding standards.
  • Unit tests covering the new feature have been added.
  • All existing tests pass.
  • The documentation has been updated to reflect the new feature

Additional Notes

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of backspace key behavior in the editor when deleting empty inline blocks and moving selection to previous blocks, ensuring cursor positioning works correctly with nested content structures.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
blocknote Ready Ready Preview Mar 31, 2026 4:18pm
blocknote-website Ready Ready Preview Mar 31, 2026 4:18pm

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

📝 Walkthrough

Walkthrough

The Backspace handler now uses getBottomNestedBlockInfo() to locate the deepest nested block container when moving selection after deleting empty inline blocks, preventing incorrect cursor jumps to higher-level blocks in nested structures.

Changes

Cohort / File(s) Summary
Backspace Handler Fix
packages/core/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts
Modified Backspace deletion path to traverse nested block containers and position cursor at the bottom-most nested block instead of the immediate parent, preventing cursor from jumping to top-level blocks when deleting within deeply nested structures.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 When backspace deletes with nested blocks in sight,
No more wild jumps—the cursor lands just right!
Nested containers found, selection set with care,
A rabbity fix that makes deletion fair! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the specific bug fix: backspace behavior when current block is empty and previous block's last child is empty.
Description check ✅ Passed The description covers all major sections including a clear summary of the bug scenario, rationale, specific changes made, and confirms tests were added.
Linked Issues check ✅ Passed The PR directly addresses issue #2566 by fixing the backspace handler to move selection to the previous block's last descendant instead of its content end.
Out of Scope Changes check ✅ Passed All changes are scoped to the KeyboardShortcuts extension's backspace handler and directly address the reported bug in issue #2566.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch backspace-empty-nested-block-fix

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between a850078 and 41c19e0.

📒 Files selected for processing (1)
  • packages/core/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts

Comment on lines +263 to 275
const bottomNestedPrevBlockInfo = getBottomNestedBlockInfo(
state.doc,
prevBlockInfo,
);
if (!bottomNestedPrevBlockInfo.isBlockContainer) {
return false;
}
const blockContentEndPos =
bottomNestedPrevBlockInfo.blockContent.afterPos - 1;

chainedCommands =
chainedCommands.setTextSelection(blockContentStartPos);
chainedCommands.setTextSelection(blockContentEndPos);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 31, 2026

Open in StackBlitz

@blocknote/ariakit

npm i https://pkg.pr.new/@blocknote/ariakit@2610

@blocknote/code-block

npm i https://pkg.pr.new/@blocknote/code-block@2610

@blocknote/core

npm i https://pkg.pr.new/@blocknote/core@2610

@blocknote/mantine

npm i https://pkg.pr.new/@blocknote/mantine@2610

@blocknote/react

npm i https://pkg.pr.new/@blocknote/react@2610

@blocknote/server-util

npm i https://pkg.pr.new/@blocknote/server-util@2610

@blocknote/shadcn

npm i https://pkg.pr.new/@blocknote/shadcn@2610

@blocknote/xl-ai

npm i https://pkg.pr.new/@blocknote/xl-ai@2610

@blocknote/xl-docx-exporter

npm i https://pkg.pr.new/@blocknote/xl-docx-exporter@2610

@blocknote/xl-email-exporter

npm i https://pkg.pr.new/@blocknote/xl-email-exporter@2610

@blocknote/xl-multi-column

npm i https://pkg.pr.new/@blocknote/xl-multi-column@2610

@blocknote/xl-odt-exporter

npm i https://pkg.pr.new/@blocknote/xl-odt-exporter@2610

@blocknote/xl-pdf-exporter

npm i https://pkg.pr.new/@blocknote/xl-pdf-exporter@2610

commit: 41c19e0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting jumps from a block to top of bullet list

2 participants