Skip to content

Commit 9cd16c0

Browse files
fix: Backspace in empty block deletes previous block (#1505)
* fix: removes current block instead of prev block if current block is empty * Made code more descriptive + small fixes --------- Co-authored-by: Matthew Lipski <[email protected]>
1 parent 7d8c79c commit 9cd16c0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Diff for: packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts

+67
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,73 @@ export const KeyboardShortcutsExtension = Extension.create<{
260260

261261
return true;
262262
}),
263+
// Deletes the current block if it's an empty block with inline content,
264+
// and moves the selection to the previous block.
265+
() =>
266+
commands.command(({ state }) => {
267+
const blockInfo = getBlockInfoFromSelection(state);
268+
if (!blockInfo.isBlockContainer) {
269+
return false;
270+
}
271+
272+
const blockEmpty =
273+
blockInfo.blockContent.node.childCount === 0 &&
274+
blockInfo.blockContent.node.type.spec.content === "inline*";
275+
276+
if (blockEmpty) {
277+
const prevBlockInfo = getPrevBlockInfo(
278+
state.doc,
279+
blockInfo.bnBlock.beforePos
280+
);
281+
if (!prevBlockInfo || !prevBlockInfo.isBlockContainer) {
282+
return false;
283+
}
284+
285+
let chainedCommands = chain();
286+
287+
if (
288+
prevBlockInfo.blockContent.node.type.spec.content ===
289+
"tableRow+"
290+
) {
291+
const tableBlockEndPos = blockInfo.bnBlock.beforePos - 1;
292+
const tableBlockContentEndPos = tableBlockEndPos - 1;
293+
const lastRowEndPos = tableBlockContentEndPos - 1;
294+
const lastCellEndPos = lastRowEndPos - 1;
295+
const lastCellParagraphEndPos = lastCellEndPos - 1;
296+
297+
chainedCommands = chainedCommands.setTextSelection(
298+
lastCellParagraphEndPos
299+
);
300+
} else if (
301+
prevBlockInfo.blockContent.node.type.spec.content === ""
302+
) {
303+
const nonEditableBlockContentStartPos =
304+
prevBlockInfo.blockContent.afterPos -
305+
prevBlockInfo.blockContent.node.nodeSize;
306+
307+
chainedCommands = chainedCommands.setNodeSelection(
308+
nonEditableBlockContentStartPos
309+
);
310+
} else {
311+
const blockContentStartPos =
312+
prevBlockInfo.blockContent.afterPos -
313+
prevBlockInfo.blockContent.node.nodeSize;
314+
315+
chainedCommands =
316+
chainedCommands.setTextSelection(blockContentStartPos);
317+
}
318+
319+
return chainedCommands
320+
.deleteRange({
321+
from: blockInfo.bnBlock.beforePos,
322+
to: blockInfo.bnBlock.afterPos,
323+
})
324+
.scrollIntoView()
325+
.run();
326+
}
327+
328+
return false;
329+
}),
263330
// Deletes previous block if it contains no content and isn't a table,
264331
// when the selection is empty and at the start of the block. Moves the
265332
// current block into the deleted block's place.

0 commit comments

Comments
 (0)