From 9c50cf26dff9802b17ba239dcda14845ab722ed5 Mon Sep 17 00:00:00 2001 From: makhnatkin Date: Fri, 10 Oct 2025 12:57:56 +0200 Subject: [PATCH] fix: keyboard navigation in nested block containers --- src/extensions/behavior/Selection/commands.ts | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/extensions/behavior/Selection/commands.ts b/src/extensions/behavior/Selection/commands.ts index 93cef1c88..6ab82fa13 100644 --- a/src/extensions/behavior/Selection/commands.ts +++ b/src/extensions/behavior/Selection/commands.ts @@ -114,11 +114,48 @@ export function findFakeParaPosClosestToPos( if (dir === 'before') { if (isFirstChild || !isTextblock(parent.child(index - 1))) { - return $pos.doc.resolve($pos.before(depth)); + const $target = $pos.doc.resolve($pos.before(depth)); + + const prevNode = $target.nodeBefore; + const nextNode = $target.nodeAfter; + if (prevNode && nextNode) { + const prevIsBlockContainer = + prevNode.isBlock && prevNode.type.spec.content?.includes('block'); + const nextIsBlockContainer = + nextNode.isBlock && nextNode.type.spec.content?.includes('block'); + + if (prevIsBlockContainer && nextIsBlockContainer) { + // skip this depth, try next one + continue; + } + } + + return $target; } } else if (dir === 'after') { if (isLastChild || !isTextblock(parent.child(index + 1))) { - return $pos.doc.resolve($pos.after(depth)); + const $target = $pos.doc.resolve($pos.after(depth)); + + const prevNode = $target.nodeBefore; + const nextNode = $target.nodeAfter; + + if (!nextNode && isLastChild && depth > 1) { + continue; + } + + if (prevNode && nextNode && isLastChild && depth > 1) { + const prevIsBlockContainer = + prevNode.isBlock && prevNode.type.spec.content?.includes('block'); + const nextIsBlockContainer = + nextNode.isBlock && nextNode.type.spec.content?.includes('block'); + + if (prevIsBlockContainer && nextIsBlockContainer) { + // skip this depth, try next one + continue; + } + } + + return $target; } }