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): 处理列表缩进审查反馈

zhangyongjieb6746ee43dfa
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
index a39dcd1932..00188f5081 100644
--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
@@ -2,7 +2,7 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
-import { Editor } from '@tiptap/core';
+import { Editor, Node as TiptapNode } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
@@ -22,6 +22,22 @@ import {
let editor: Editor | null = null;
+const TestAtom = TiptapNode.create({
+ name: 'testAtom',
+ inline: true,
+ group: 'inline',
+ atom: true,
+ selectable: true,
+
+ parseHTML() {
+ return [{ tag: 'span[data-test-atom]' }];
+ },
+
+ renderHTML() {
+ return ['span', { 'data-test-atom': '' }, 'chip'];
+ },
+});
+
function makeEditor(lines: string[]): Editor {
const content: Array<Record<string, unknown>> = [];
lines.forEach((line, i) => {
@@ -30,7 +46,7 @@ function makeEditor(lines: string[]): Editor {
});
editor = new Editor({
element: document.createElement('div'),
- extensions: [Document, Paragraph, Text, HardBreak, ComposerListIndentDecoration],
+ extensions: [Document, Paragraph, Text, HardBreak, TestAtom, ComposerListIndentDecoration],
content: { type: 'doc', content: [{ type: 'paragraph', content }] },
});
return editor;
@@ -49,12 +65,21 @@ afterEach(() => {
describe('buildListIndentDecorations', () => {
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;',
- );
+ const latinStyle = listPrefixIndentStyle('2. ');
+ expect(latinStyle).toContain('--composer-list-hang:1.8ch;');
+ expect(latinStyle).toContain('--composer-list-hang-negative:-1.8ch;');
+ expect(latinStyle).not.toContain('2. ');
+
+ const cjkStyle = listPrefixIndentStyle('10、');
+ expect(cjkStyle).toContain('--composer-list-hang:calc(2ch + 1em);');
+ expect(cjkStyle).toContain('--composer-list-hang-negative:calc(-2ch - 1em);');
+ expect(cjkStyle).not.toContain('10、');
+ });
+
+ it('measures tabs using the configured tab stop instead of punctuation width', () => {
+ const style = listPrefixIndentStyle('\t1. ');
+ expect(style).toContain('--composer-list-hang:9.8ch;');
+ expect(style).toContain('--composer-list-hang-negative:-9.8ch;');
});
it('decorates the full content of a single-line item', () => {
@@ -85,6 +110,32 @@ describe('buildListIndentDecorations', () => {
const ed = makeEditor(['hello world', '3.14159']);
expect(buildListIndentDecorations(ed.state.doc).find()).toHaveLength(0);
});
+
+ it('leaves lines containing inline atoms untouched so chips keep their inline geometry', () => {
+ editor = new Editor({
+ element: document.createElement('div'),
+ extensions: [Document, Paragraph, Text, HardBreak, TestAtom, ComposerListIndentDecoration],
+ content: {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'text', text: '- before ' },
+ { type: 'testAtom' },
+ { type: 'text', text: ' after' },
+ ],
+ },
+ ],
+ },
+ });
+ expect(buildListIndentDecorations(editor.state.doc).find()).toHaveLength(0);
+ expect(
+ editor.view.dom
+ .querySelector('[data-test-atom]')
+ ?.classList.contains('composer-list-line-indent'),
+ ).toBe(false);
+ });
});
describe('ComposerListIndentDecoration in a real editor', () => {
@@ -125,12 +176,13 @@ 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('.ProseMirror span.composer-list-line-indent');
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;');
+ expect(css).toContain('tab-size: 8;');
+ expect(css).not.toContain('word-break: break-all;');
});
});
diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
index dc06aebd08..4c15abf29e 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
@@ -14,7 +14,7 @@
* - decoration 只是渲染层,doc JSON / 草稿存储 / 发送内容里没有任何痕迹;
* - doc 没变直接复用 DecorationSet,变了全量重扫(chat input 文本量小,
* 全量成本可忽略,不值得做增量映射);
- * - IME 组合期不重算,避免 DOM 抖动打断输入法候选框;
+ * - 只在 doc 发生变化时重算,view.update 不参与 decoration 计算;
* - 这是纯文本编辑器的视觉缩进,不改变 doc JSON / 发送文本。
*/
import { Extension } from '@tiptap/core';
@@ -29,23 +29,35 @@ const PLUGIN_KEY = new PluginKey<DecorationSet>('composerListIndentDecoration');
/** 行内一个非文本 inline 节点(mention chip 等)的占位符,与 applyListContinuation 一致。 */
const ATOM_PLACEHOLDER = '\uFFFC';
+/** 浏览器默认 tab stop 是 8 个空格,并与 CSS 的 tab-size 保持一致。 */
+const TAB_SIZE_CH = 8;
+
/**
* 将列表前缀换算成当前字体下的近似宽度。
*
* ChatInput 已启用 tabular-nums,所以数字直接用 1ch;句点、空格、方括号等
- * 窄字符按 0.4ch 估算;中文顿号按全角 1em。正负值成对写入,供 padding-left
- * 与 text-indent 组成悬挂缩进。这里只生成数字和 CSS 单位,不会透传用户文本。
+ * 窄字符按 0.4ch 估算;中文顿号按全角 1em。制表符按当前列位置推进到下一个
+ * 8 空格 tab stop。这里只生成数字和 CSS 单位,不会透传用户文本。
*/
export function listPrefixIndentStyle(prefix: string): string {
let ch = 0;
let em = 0;
+ let columns = 0;
for (const char of prefix) {
if (char >= '0' && char <= '9') {
ch += 1;
+ columns += 1;
} else if (char === '、' || char === '\u3000') {
em += 1;
+ columns += 2;
+ } else if (char === '\t') {
+ const remainder = columns % TAB_SIZE_CH;
+ const advance = remainder === 0 ? TAB_SIZE_CH : TAB_SIZE_CH - remainder;
+ ch += advance;
+ columns += advance;
} else {
ch += 0.4;
+ columns += 0.4;
}
}
const chValue = Number(ch.toFixed(2));
@@ -57,14 +69,6 @@ export function listPrefixIndentStyle(prefix: string): string {
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('');
@@ -86,9 +90,13 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
let lineText = '';
let lineStartOffset = 0;
let lineEndOffset = 0;
+ let lineHasInlineAtom = false;
const flushLine = () => {
const match = matchListPrefix(lineText);
- if (!match) return;
+ // Inline decorations apply their attributes to every covered inline node.
+ // A mixed text/atom line would split the full-width wrapper around the atom
+ // and turn chips into blocks, so leave those lines untouched.
+ if (!match || lineHasInlineAtom) return;
const from = contentBase + lineStartOffset;
const to = contentBase + lineEndOffset;
const prefix = lineText.slice(0, match.prefixLength);
@@ -107,12 +115,14 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
lineText = '';
lineStartOffset = pos + node.nodeSize;
lineEndOffset = lineStartOffset;
+ lineHasInlineAtom = false;
} else if (node.isText) {
lineText += node.text ?? '';
lineEndOffset = pos + node.nodeSize;
} else {
lineText += ATOM_PLACEHOLDER;
lineEndOffset = pos + node.nodeSize;
+ lineHasInlineAtom = true;
}
return false;
});
diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.css
index 9558cbb059..7fbc787f81 100644
--- a/apps/desktop/src/renderer/styles/globals.css
+++ b/apps/desktop/src/renderer/styles/globals.css
@@ -350,7 +350,7 @@ html:lang(ko) {
整条列表行变成独立的 inline-block 换行容器。padding-left 留出"基础缩进
+ 前缀宽度",首行再用负 text-indent 把列表标记悬挂回基础缩进位置;
自动换行后的续行不吃负缩进,因此会与首行正文对齐。 */
-.ProseMirror .composer-list-line-indent {
+.ProseMirror span.composer-list-line-indent {
display: inline-block;
box-sizing: border-box;
width: 100%;
@@ -358,7 +358,7 @@ html:lang(ko) {
text-indent: var(--composer-list-hang-negative, -1.25em);
vertical-align: top;
overflow-wrap: anywhere;
- word-break: break-all;
+ tab-size: 8;
}
.ProseMirror .voice-input-draft-replaced {
display: none;