Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 空列表项退格只退出列表不回到上一行 (#5489)

Dasha91af0fd30e2
diff --git a/apps/desktop/src/renderer/components/new-chat/ListComposerTextarea.tsx b/apps/desktop/src/renderer/components/new-chat/ListComposerTextarea.tsx
index a654e6c7d4..d76d9cf020 100644
--- a/apps/desktop/src/renderer/components/new-chat/ListComposerTextarea.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ListComposerTextarea.tsx
@@ -8,7 +8,7 @@ import {
/**
* ListComposerTextarea —— 原生 `<textarea>` 的直替换包装,统一附带 composer
- * 列表输入辅助(Shift/Alt+Enter 序号接续、空列表项一次退格整删),并默认启用
+ * 列表输入辅助(Shift/Alt+Enter 序号接续、空列表项一次退格退出),并默认启用
* `tabular-nums` 让多行序号对齐。
*
* 设计目标是"写一次、到处一行接入":列表逻辑集中在这里(纯逻辑复用
@@ -83,7 +83,7 @@ export const ListComposerTextarea = forwardRef<HTMLTextAreaElement, ListComposer
return;
}
}
- // Backspace(无修饰)— 空列表项整体回删。
+ // Backspace(无修饰)— 空列表项退出(只删前缀、保留换行)。
else if (event.key === 'Backspace' && noHardMods && !event.altKey && !event.shiftKey) {
const edit = computeTextareaBackspace(el.value, selStart, selEnd);
if (edit) {
diff --git a/apps/desktop/src/renderer/lib/__tests__/composerListContinuationEditor.test.ts b/apps/desktop/src/renderer/lib/__tests__/composerListContinuationEditor.test.ts
index 18a2ae8de4..4235ff068a 100644
--- a/apps/desktop/src/renderer/lib/__tests__/composerListContinuationEditor.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/composerListContinuationEditor.test.ts
@@ -105,12 +105,14 @@ describe('applyListContinuation on a real editor', () => {
});
describe('applyListBackspace on a real editor', () => {
- it('deletes the whole empty prefix plus the line break, caret lands at end of previous line', () => {
+ it('deletes only the empty prefix and keeps the line break, caret stays on the emptied line', () => {
const ed = makeEditor(['1. test', '2. ']);
expect(applyListBackspace(ed.view)).toBe(true);
- expect(docText(ed)).toBe('1. test');
- // 光标在 "1. test" 末尾(pos 1 + 7)
- expect(ed.state.selection.from).toBe(8);
+ expect(docText(ed)).toBe('1. test\n');
+ // 光标在空行行首(pos 1 + "1. test"(7) + br(1))
+ expect(ed.state.selection.from).toBe(9);
+ // 空行上再按一次不再拦截,交给浏览器原生退格删掉 hardBreak 回到上一行。
+ expect(applyListBackspace(ed.view)).toBe(false);
});
it('on the first line, deletes only the prefix (exits list, stays on the line)', () => {
@@ -123,7 +125,7 @@ describe('applyListBackspace on a real editor', () => {
it('keeps indentation-aware deletion (indented empty item)', () => {
const ed = makeEditor(['- a', ' - ']);
expect(applyListBackspace(ed.view)).toBe(true);
- expect(docText(ed)).toBe('- a');
+ expect(docText(ed)).toBe('- a\n');
});
it('returns false when the item still has content — normal backspace applies', () => {
@@ -150,6 +152,6 @@ describe('applyListBackspace on a real editor', () => {
// pos = 1 + "1. a"(4) + br(1) + "2. "(3) = 9
ed.commands.setTextSelection(9);
expect(applyListBackspace(ed.view)).toBe(true);
- expect(docText(ed)).toBe('1. a\n3. b');
+ expect(docText(ed)).toBe('1. a\n\n3. b');
});
});
diff --git a/apps/desktop/src/renderer/lib/__tests__/composerListTextarea.test.ts b/apps/desktop/src/renderer/lib/__tests__/composerListTextarea.test.ts
index 15b1adc336..4134efa3bc 100644
--- a/apps/desktop/src/renderer/lib/__tests__/composerListTextarea.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/composerListTextarea.test.ts
@@ -91,11 +91,13 @@ describe('computeTextareaContinuation', () => {
});
describe('computeTextareaBackspace', () => {
- it('空列表项在行尾退格:连同前面的换行整删,回到上一行行尾', () => {
+ it('空列表项在行尾退格:只删前缀、保留换行,停在空行行首', () => {
// "1. a\n2. " 第二行是空项,光标在末尾。
const value = '1. a\n2. ';
const c = value.length;
- expect(computeTextareaBackspace(value, c, c)).toEqual({ value: '1. a', caret: 4 });
+ expect(computeTextareaBackspace(value, c, c)).toEqual({ value: '1. a\n', caret: 5 });
+ // 空行上再退格不再拦截,交给默认退格回到上一行。
+ expect(computeTextareaBackspace('1. a\n', 5, 5)).toBeNull();
});
it('首行空列表项退格:只删前缀', () => {
@@ -117,10 +119,10 @@ describe('computeTextareaBackspace', () => {
expect(backspace('plain|')).toBeNull();
});
- it('空项后紧跟换行(非文本末尾)也整删:非首行连同前面的换行删掉', () => {
+ it('空项后紧跟换行(非文本末尾)也退出:只删前缀、保留前后换行', () => {
// "x\n- \nnext" 光标在第二行(空 bullet)行尾,其后是 \n(非 EOF)。
const value = 'x\n- \nnext';
- expect(computeTextareaBackspace(value, 4, 4)).toEqual({ value: 'x\nnext', caret: 1 });
+ expect(computeTextareaBackspace(value, 4, 4)).toEqual({ value: 'x\n\nnext', caret: 2 });
});
it('有选区 → null', () => {
diff --git a/apps/desktop/src/renderer/lib/composerListContinuation.ts b/apps/desktop/src/renderer/lib/composerListContinuation.ts
index 2258e1e626..0024b81eb5 100644
--- a/apps/desktop/src/renderer/lib/composerListContinuation.ts
+++ b/apps/desktop/src/renderer/lib/composerListContinuation.ts
@@ -157,10 +157,9 @@ export function applyListContinuation(view: EditorView): boolean {
}
/**
- * 空列表项整体回删:当前行只剩前缀(如 "2. ")且光标在行尾时,
- * 一次 Backspace 删掉整个前缀——有上一行则连同前面的换行一起删,光标落到
- * 上一行行尾;是首行则只删前缀(等效退出列表)。其余情况返回 false,调用方
- * 走默认退格。
+ * 空列表项退出:当前行只剩前缀(如 "2. ")且光标在行尾时,一次 Backspace
+ * 只删掉前缀(含缩进)、保留本行换行,与结构化列表的空项退出一致;再按一次
+ * Backspace 才走默认退格回到上一行。其余情况返回 false,调用方走默认退格。
*/
export function applyListBackspace(view: EditorView): boolean {
const { state } = view;
@@ -179,8 +178,6 @@ export function applyListBackspace(view: EditorView): boolean {
if (lineText.slice(match.prefixLength).trim().length > 0) return false;
const lineStartAbs = $from.start() + lineStartOffset;
- // 非首行连同前面的 hardBreak(占 1 个 position)一起删。
- const deleteFrom = lineStartOffset > 0 ? lineStartAbs - 1 : lineStartAbs;
- view.dispatch(state.tr.delete(deleteFrom, $from.pos).scrollIntoView());
+ view.dispatch(state.tr.delete(lineStartAbs, $from.pos).scrollIntoView());
return true;
}
diff --git a/apps/desktop/src/renderer/lib/composerListTextarea.ts b/apps/desktop/src/renderer/lib/composerListTextarea.ts
index ce5161a3d2..83983d38ea 100644
--- a/apps/desktop/src/renderer/lib/composerListTextarea.ts
+++ b/apps/desktop/src/renderer/lib/composerListTextarea.ts
@@ -11,7 +11,7 @@ import { computeListContinuation, matchListPrefix } from './composerListContinua
* 重写逻辑。
*
* 与 tiptap 版的差异(纯文本框固有限制):textarea 做不了逐行 inline 缩进
- * 装饰,所以只提供"序号接续 + 空项整删",没有 ChatInput 那个整行缩进的视觉
+ * 装饰,所以只提供"序号接续 + 空项退出",没有 ChatInput 那个整行缩进的视觉
* 反馈;等宽对齐由消费组件挂 `tabular-nums` class 解决。
*/
@@ -68,9 +68,9 @@ export function computeTextareaContinuation(
}
/**
- * 空列表项整体回删(对齐 Claude / ChatInput):当前行只剩前缀(如 "2. ")且
- * 光标在行尾时,一次 Backspace 删掉整个前缀——非首行连同前面的换行一起删,
- * 光标落到上一行行尾;首行则只删前缀。其余情况返回 null(走默认退格)。
+ * 空列表项退出(对齐 ChatInput):当前行只剩前缀(如 "2. ")且光标在行尾时,
+ * 一次 Backspace 只删掉前缀(含缩进)、保留本行换行,光标停在行首;再按一次
+ * 才走默认退格回到上一行。其余情况返回 null(走默认退格)。
*
* "行尾"= 光标后紧跟换行或已到文本末尾;仅光标折叠时生效。
*/
@@ -93,10 +93,8 @@ export function computeTextareaBackspace(
// 前缀后还有内容 → 正常退格(逐字符删),不整删。
if (line.slice(match.prefixLength).trim().length > 0) return null;
- // 非首行连同前面的换行一起删。
- const deleteFrom = lineStart > 0 ? lineStart - 1 : lineStart;
return {
- value: value.slice(0, deleteFrom) + value.slice(caret),
- caret: deleteFrom,
+ value: value.slice(0, lineStart) + value.slice(caret),
+ caret: lineStart,
};
}