From 145b868a4402b37ff932c1a4671fcc6007684370 Mon Sep 17 00:00:00 2001 From: landaiqing Date: Sat, 21 Jun 2025 00:34:22 +0800 Subject: [PATCH] :bug: Fixed the issue of deleting blocks --- .../views/editor/extensions/codeblock/commands.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/editor/extensions/codeblock/commands.ts b/frontend/src/views/editor/extensions/codeblock/commands.ts index 2a0a15b..3b6482a 100644 --- a/frontend/src/views/editor/extensions/codeblock/commands.ts +++ b/frontend/src/views/editor/extensions/codeblock/commands.ts @@ -282,12 +282,21 @@ export const deleteBlock = (options: EditorOptions): Command => ({ state, dispat if (blockIndex === blocks.length - 1) { // 如果是最后一个块,将光标移到前一个块的末尾 - newCursorPos = blocks[blockIndex - 1].content.to; + // 需要计算删除后的位置 + const prevBlock = blocks[blockIndex - 1]; + newCursorPos = prevBlock.content.to; } else { // 否则移到下一个块的开始 - newCursorPos = blocks[blockIndex + 1].content.from; + // 需要计算删除后的位置,下一个块会向前移动 + const nextBlock = blocks[blockIndex + 1]; + const blockLength = block.range.to - block.range.from; + newCursorPos = nextBlock.content.from - blockLength; } + // 确保光标位置在有效范围内 + const docLengthAfterDelete = state.doc.length - (block.range.to - block.range.from); + newCursorPos = Math.max(0, Math.min(newCursorPos, docLengthAfterDelete)); + dispatch(state.update({ changes: { from: block.range.from,