Skip to content

fix: Backspace in empty block deletes previous block #1505

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

Merged
merged 2 commits into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,73 @@ export const KeyboardShortcutsExtension = Extension.create<{

return true;
}),
// Deletes the current block if it's an empty block with inline content,
// and moves the selection to the previous block.
() =>
commands.command(({ state }) => {
const blockInfo = getBlockInfoFromSelection(state);
if (!blockInfo.isBlockContainer) {
return false;
}

const blockEmpty =
blockInfo.blockContent.node.childCount === 0 &&
blockInfo.blockContent.node.type.spec.content === "inline*";

if (blockEmpty) {
const prevBlockInfo = getPrevBlockInfo(
state.doc,
blockInfo.bnBlock.beforePos
);
if (!prevBlockInfo || !prevBlockInfo.isBlockContainer) {
return false;
}

let chainedCommands = chain();

if (
prevBlockInfo.blockContent.node.type.spec.content ===
"tableRow+"
) {
const tableBlockEndPos = blockInfo.bnBlock.beforePos - 1;
const tableBlockContentEndPos = tableBlockEndPos - 1;
const lastRowEndPos = tableBlockContentEndPos - 1;
const lastCellEndPos = lastRowEndPos - 1;
const lastCellParagraphEndPos = lastCellEndPos - 1;

chainedCommands = chainedCommands.setTextSelection(
lastCellParagraphEndPos
);
} else if (
prevBlockInfo.blockContent.node.type.spec.content === ""
) {
const nonEditableBlockContentStartPos =
prevBlockInfo.blockContent.afterPos -
prevBlockInfo.blockContent.node.nodeSize;

chainedCommands = chainedCommands.setNodeSelection(
nonEditableBlockContentStartPos
);
} else {
const blockContentStartPos =
prevBlockInfo.blockContent.afterPos -
prevBlockInfo.blockContent.node.nodeSize;

chainedCommands =
chainedCommands.setTextSelection(blockContentStartPos);
}

return chainedCommands
.deleteRange({
from: blockInfo.bnBlock.beforePos,
to: blockInfo.bnBlock.afterPos,
})
.scrollIntoView()
.run();
}

return false;
}),
// Deletes previous block if it contains no content and isn't a table,
// when the selection is empty and at the start of the block. Moves the
// current block into the deleted block's place.
Expand Down
Loading