From 9d09902e7f0eea805436a2f2868ab4a3e61b4648 Mon Sep 17 00:00:00 2001 From: Martin Rosselot Date: Thu, 6 Mar 2025 11:49:47 -0300 Subject: [PATCH 1/2] fix: removes current block instead of prev block if current block is empty --- .../KeyboardShortcutsExtension.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts b/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts index ee4806bf58..f3bdca704b 100644 --- a/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +++ b/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts @@ -292,6 +292,24 @@ export const KeyboardShortcutsExtension = Extension.create<{ throw new Error(`todo`); } + const currentBlockNotTableAndNoContent = + blockInfo.blockContent.node.type.spec.content === "" || + (blockInfo.blockContent.node.type.spec.content === "inline*" && + blockInfo.blockContent.node.childCount === 0); + + if (currentBlockNotTableAndNoContent) { + const pos = state.tr.doc.resolve( + blockInfo.bnBlock.beforePos - 2 + ); + state.tr.setSelection(new TextSelection(pos)); + return chain() + .deleteRange({ + from: blockInfo.bnBlock.beforePos, + to: blockInfo.bnBlock.afterPos, + }) + .run(); + } + const prevBlockNotTableAndNoContent = bottomBlock.blockContent.node.type.spec.content === "" || (bottomBlock.blockContent.node.type.spec.content === From 48cbdd399e947818c3ad119129f00bfa791aa932 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Wed, 19 Mar 2025 19:31:11 +0100 Subject: [PATCH 2/2] Made code more descriptive + small fixes --- .../KeyboardShortcutsExtension.ts | 85 +++++++++++++++---- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts b/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts index f3bdca704b..ae00f7074a 100644 --- a/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +++ b/packages/core/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts @@ -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. @@ -292,24 +359,6 @@ export const KeyboardShortcutsExtension = Extension.create<{ throw new Error(`todo`); } - const currentBlockNotTableAndNoContent = - blockInfo.blockContent.node.type.spec.content === "" || - (blockInfo.blockContent.node.type.spec.content === "inline*" && - blockInfo.blockContent.node.childCount === 0); - - if (currentBlockNotTableAndNoContent) { - const pos = state.tr.doc.resolve( - blockInfo.bnBlock.beforePos - 2 - ); - state.tr.setSelection(new TextSelection(pos)); - return chain() - .deleteRange({ - from: blockInfo.bnBlock.beforePos, - to: blockInfo.bnBlock.afterPos, - }) - .run(); - } - const prevBlockNotTableAndNoContent = bottomBlock.blockContent.node.type.spec.content === "" || (bottomBlock.blockContent.node.type.spec.content ===