Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 修复对话框列表长文本换行缩进
zhangyongjied4b2583a4799
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.tsindex b665599f07..a39dcd1932 100644--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts@@ -10,11 +10,12 @@ import HardBreak from '@tiptap/extension-hard-break';import {buildListIndentDecorations,ComposerListIndentDecoration,+ listPrefixIndentStyle,} from '@/components/new-chat/ComposerListIndentDecoration';/*** composer 列表行缩进 decoration:- * - buildListIndentDecorations 的范围计算(hardBreak 分行、多行、前缀即缩进);+ * - buildListIndentDecorations 的范围计算(hardBreak 分行、多行、整行缩进);* - 真实编辑器集成:decoration 渲染进 DOM,打完前缀立即出现、删掉即消失;* - ChatInput 注册 + globals.css 样式存在的接线契约。*/@@ -47,12 +48,21 @@ afterEach(() => {});describe('buildListIndentDecorations', () => {- it('decorates the list prefix of a single-line item', () => {+ it('builds paired hanging-indent variables without embedding user text', () => {+ expect(listPrefixIndentStyle('2. ')).toBe(+ '--composer-list-hang:1.8ch;--composer-list-hang-negative:-1.8ch;display:inline-block;box-sizing:border-box;width:100%;padding-left:calc(1em + var(--composer-list-hang));text-indent:var(--composer-list-hang-negative);vertical-align:top;overflow-wrap:anywhere;word-break:break-all;',+ );+ expect(listPrefixIndentStyle('10、')).toBe(+ '--composer-list-hang:calc(2ch + 1em);--composer-list-hang-negative:calc(-2ch - 1em);display:inline-block;box-sizing:border-box;width:100%;padding-left:calc(1em + var(--composer-list-hang));text-indent:var(--composer-list-hang-negative);vertical-align:top;overflow-wrap:anywhere;word-break:break-all;',+ );+ });++ it('decorates the full content of a single-line item', () => {const ed = makeEditor(['1. test']);const found = buildListIndentDecorations(ed.state.doc).find();expect(found).toHaveLength(1);expect(found[0].from).toBe(1);- expect(found[0].to).toBe(4); // "1. " 三个字符+ expect(found[0].to).toBe(8); // 整行 "1. test"});it('decorates each list line independently across hardBreaks', () => {@@ -61,9 +71,9 @@ describe('buildListIndentDecorations', () => {expect(found).toHaveLength(2);// "intro"(5) + br(1) → "- item" 行起点 offset 6,contentBase 1expect(found[0].from).toBe(7);- expect(found[0].to).toBe(9); // "- "+ expect(found[0].to).toBe(13); // 整行 "- item"expect(found[1].from).toBe(14);- expect(found[1].to).toBe(17); // "2. "+ expect(found[1].to).toBe(18); // 整行 "2. x"});it('decorates a prefix-only line (即时反馈:刚打完 `1. ` 就缩进)', () => {@@ -80,7 +90,12 @@ describe('buildListIndentDecorations', () => {describe('ComposerListIndentDecoration in a real editor', () => {it('renders the indent span into the DOM for list lines', () => {const ed = makeEditor(['1. hello']);- expect(indentSpans(ed)).toEqual(['1. ']);+ expect(indentSpans(ed)).toEqual(['1. hello']);+ expect(+ ed.view.dom+ .querySelector<HTMLElement>('span.composer-list-line-indent')+ ?.style.getPropertyValue('--composer-list-hang'),+ ).toBe('1.8ch');});it('appears the moment the prefix becomes complete, and disappears when broken', () => {@@ -111,6 +126,11 @@ describe('wiring contract', () => {it('globals.css defines the indent class', () => {const css = readFileSync(resolve(__dirname, '..', 'styles', 'globals.css'), 'utf8');expect(css).toContain('.ProseMirror .composer-list-line-indent');- expect(css).toContain('padding-left: 1em;');+ expect(css).toContain('display: inline-block;');+ expect(css).toContain('width: 100%;');+ expect(css).toContain('padding-left: calc(1em + var(--composer-list-hang, 1.25em));');+ expect(css).toContain('text-indent: var(--composer-list-hang-negative, -1.25em);');+ expect(css).toContain('overflow-wrap: anywhere;');+ expect(css).toContain('word-break: break-all;');});});diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsxindex d241754afc..1d091f8d42 100644--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx@@ -1579,8 +1579,8 @@ export function ChatInput({'w-full min-h-[22px] max-h-[186px] overflow-y-auto py-[3px] -my-[3px] pr-[11px]',// tabular-nums:等宽数字。系统字体默认数字是比例宽度("1" 比 "2" 窄),// 多行列表 "1. / 2. / 3." 的点和正文会逐行漂移;等宽数字让前缀宽度- // 一致、列表自然对齐(纯文本 composer 做不了富文本悬挂缩进,这是- // 对齐问题的最小修复)。+ // 一致、列表自然对齐。ComposerListIndentDecoration 会额外为整条列表+ // 行保留换行后的视觉缩进。'text-[15px] leading-[22px] font-normal tabular-nums','text-[var(--chat-input-text)]','focus:outline-none',diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.tsindex 1b2f99f289..dc06aebd08 100644--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts@@ -4,17 +4,18 @@** 行为:当某一"行"(段落内以 hardBreak 划分)以列表 / 待办 / 引用前缀开头* (`1. ` / `- ` / `- [ ] ` / `> ` 等,与列表接续共用 matchListPrefix 判定),- * 给该行前缀包一层 inline decoration span,CSS 加 padding-left → 整行内容- * 右移一格。用户打完 `1. `(空格落下)那一刻缩进立即出现,即"已进入列表- * 状态"的视觉信号;空项退出(前缀被删)时缩进同步消失。+ * 给整条列表行包一层 inline decoration span,并把前缀的估算宽度写入 CSS+ * 变量。CSS 将这条列表行作为独立的换行容器:首行用负 text-indent 把标记+ * 悬挂在左侧,自动换行后的续行则从正文起点开始。用户打完 `1. `(空格落下)+ * 那一刻缩进立即出现,即"已进入列表状态"的视觉信号;空项退出(前缀被删)+ * 时缩进同步消失。** 与 CjkPunctDecoration 相同的设计约束:* - decoration 只是渲染层,doc JSON / 草稿存储 / 发送内容里没有任何痕迹;* - doc 没变直接复用 DecorationSet,变了全量重扫(chat input 文本量小,* 全量成本可忽略,不值得做增量映射);* - IME 组合期不重算,避免 DOM 抖动打断输入法候选框;- * - 纯文本编辑器做不了富文本的悬挂缩进,长行折行后的后续视觉行不缩进,- * 这是已知 trade-off。+ * - 这是纯文本编辑器的视觉缩进,不改变 doc JSON / 发送文本。*/import { Extension } from '@tiptap/core';import { Plugin, PluginKey } from '@tiptap/pm/state';@@ -29,7 +30,48 @@ const PLUGIN_KEY = new PluginKey<DecorationSet>('composerListIndentDecoration');const ATOM_PLACEHOLDER = '\uFFFC';/**- * 扫描 doc,给所有"列表行"的前缀生成 inline decoration。+ * 将列表前缀换算成当前字体下的近似宽度。+ *+ * ChatInput 已启用 tabular-nums,所以数字直接用 1ch;句点、空格、方括号等+ * 窄字符按 0.4ch 估算;中文顿号按全角 1em。正负值成对写入,供 padding-left+ * 与 text-indent 组成悬挂缩进。这里只生成数字和 CSS 单位,不会透传用户文本。+ */+export function listPrefixIndentStyle(prefix: string): string {+ let ch = 0;+ let em = 0;+ for (const char of prefix) {+ if (char >= '0' && char <= '9') {+ ch += 1;+ } else if (char === '、' || char === '\u3000') {+ em += 1;+ } else {+ ch += 0.4;+ }+ }+ const chValue = Number(ch.toFixed(2));+ const emValue = Number(em.toFixed(2));+ const positive =+ emValue > 0 ? `calc(${chValue}ch + ${emValue}em)` : `${chValue}ch`;+ const negative =+ emValue > 0 ? `calc(-${chValue}ch - ${emValue}em)` : `-${chValue}ch`;+ return [+ `--composer-list-hang:${positive}`,+ `--composer-list-hang-negative:${negative}`,+ 'display:inline-block',+ 'box-sizing:border-box',+ 'width:100%',+ 'padding-left:calc(1em + var(--composer-list-hang))',+ 'text-indent:var(--composer-list-hang-negative)',+ 'vertical-align:top',+ 'overflow-wrap:anywhere',+ 'word-break:break-all',+ ]+ .map((declaration) => `${declaration};`)+ .join('');+}++/**+ * 扫描 doc,给所有"列表行"的整行内容生成 inline decoration。* 返回的 from/to 是 doc-level position。导出以便单测直接断言范围。*/export function buildListIndentDecorations(doc: PMNode): DecorationSet {@@ -43,28 +85,38 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {// (text 每字符 1、atom 节点占位符 1)。let lineText = '';let lineStartOffset = 0;+ let lineEndOffset = 0;const flushLine = () => {const match = matchListPrefix(lineText);if (!match) return;const from = contentBase + lineStartOffset;+ const to = contentBase + lineEndOffset;+ const prefix = lineText.slice(0, match.prefixLength);decorations.push(- Decoration.inline(from, from + match.prefixLength, {+ Decoration.inline(from, to, {class: 'composer-list-line-indent',+ style: listPrefixIndentStyle(prefix),}),);};block.nodesBetween(0, block.content.size, (node, pos) => {if (node.type.name === 'hardBreak') {+ // `pos` is the end of the current line in the textblock content.+ lineEndOffset = pos;flushLine();lineText = '';lineStartOffset = pos + node.nodeSize;+ lineEndOffset = lineStartOffset;} else if (node.isText) {lineText += node.text ?? '';+ lineEndOffset = pos + node.nodeSize;} else {lineText += ATOM_PLACEHOLDER;+ lineEndOffset = pos + node.nodeSize;}return false;});+ lineEndOffset = block.content.size;flushLine(); // 段落最后一行return false; // textblock 内部已手动扫过,不再下钻diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.cssindex f2e3325250..9558cbb059 100644--- a/apps/desktop/src/renderer/styles/globals.css+++ b/apps/desktop/src/renderer/styles/globals.css@@ -347,10 +347,18 @@ html:lang(ko) {border-left-color: var(--caret-accent) !important;}/* composer 列表行缩进(ComposerListIndentDecoration):行首列表前缀被识别后,- 整行内容右移一格,作为"已进入列表状态"的视觉反馈(对齐 Claude 原生 App)。- 纯文本编辑器行是 inline 内容,折行后的后续视觉行不缩进(已知 trade-off)。 */+ 整条列表行变成独立的 inline-block 换行容器。padding-left 留出"基础缩进+ + 前缀宽度",首行再用负 text-indent 把列表标记悬挂回基础缩进位置;+ 自动换行后的续行不吃负缩进,因此会与首行正文对齐。 */.ProseMirror .composer-list-line-indent {- padding-left: 1em;+ display: inline-block;+ box-sizing: border-box;+ width: 100%;+ padding-left: calc(1em + var(--composer-list-hang, 1.25em));+ text-indent: var(--composer-list-hang-negative, -1.25em);+ vertical-align: top;+ overflow-wrap: anywhere;+ word-break: break-all;}.ProseMirror .voice-input-draft-replaced {display: none;