From 07737f9de0d31abae86d0bcdb41639fbeda6d879 Mon Sep 17 00:00:00 2001 From: Takagi <1103069291@qq.com> Date: Mon, 30 Jun 2025 11:05:30 +0800 Subject: [PATCH] refactor: optimize indentation behavior (#7600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /area editor /kind improvement /milestone 2.21.x #### What this PR does / why we need it: Fixes #7492 优化默认编辑器中缩进的逻辑。现在使用 Tab 对文本进行缩进之后,再按 Backspace 会优先回退缩进。 并且原有的文本会直接删除其上一个节点,现在只有块级节点会被直接删除。 before: ![Kapture 2025-06-27 at 17 53 16](https://github.com/user-attachments/assets/9e4831ec-1e83-4d68-8d83-7dba7090b893) after: ![Kapture 2025-06-27 at 17 57 01](https://github.com/user-attachments/assets/171d5136-5871-4394-ae94-12e159ed861d) #### How to test it? 测试使用 Backspace 是否符合正常逻辑。 #### Does this PR introduce a user-facing change? ```release-note 优化默认编辑器缩进逻辑 ``` --- .../editor/src/extensions/indent/index.ts | 23 +++++++++++++++++++ .../editor/src/extensions/paragraph/index.ts | 5 ++++ 2 files changed, 28 insertions(+) diff --git a/ui/packages/editor/src/extensions/indent/index.ts b/ui/packages/editor/src/extensions/indent/index.ts index 81d310387..d0418460b 100644 --- a/ui/packages/editor/src/extensions/indent/index.ts +++ b/ui/packages/editor/src/extensions/indent/index.ts @@ -30,6 +30,7 @@ type IndentOptions = { }; const Indent = Extension.create({ name: "indent", + priority: 10000, addOptions() { return { @@ -115,6 +116,28 @@ const Indent = Extension.create({ "Shift-Tab": getOutdent(false), "Mod-]": getIndent(), "Mod-[": getOutdent(false), + Backspace: ({ editor }) => { + const { selection } = editor.state; + const { $from } = selection; + + if ($from.parentOffset === 0) { + const node = $from.parent; + + if (node.attrs.lineIndent) { + return editor + .chain() + .focus() + .updateAttributes(node.type.name, { lineIndent: false }) + .run(); + } + + if (node.attrs.indent && node.attrs.indent > 0) { + return getOutdent(false)({ editor }); + } + } + + return false; + }, }; }, diff --git a/ui/packages/editor/src/extensions/paragraph/index.ts b/ui/packages/editor/src/extensions/paragraph/index.ts index 553431ce8..4794e004b 100644 --- a/ui/packages/editor/src/extensions/paragraph/index.ts +++ b/ui/packages/editor/src/extensions/paragraph/index.ts @@ -176,6 +176,11 @@ export function handleDeletePreviousNode( return false; } + const allowGapCursor = nodeBefore.type.spec.allowGapCursor; + if (!allowGapCursor) { + return false; + } + if (deleteNodeByPos($from.doc.resolve(beforePos - 1))(tr)) { dispatch(tr); return true;