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): avoid unsafe list decoration overlaps

zhangyongjiecad552791664
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
index 00188f5081..0d76e96221 100644
--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
@@ -12,6 +12,7 @@ import {
ComposerListIndentDecoration,
listPrefixIndentStyle,
} from '@/components/new-chat/ComposerListIndentDecoration';
+import { CjkPunctDecoration } from '@/components/new-chat/CjkPunctDecoration';
/**
* composer 列表行缩进 decoration:
@@ -76,18 +77,17 @@ describe('buildListIndentDecorations', () => {
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('skips tab-indented lines instead of guessing a proportional-font tab width', () => {
+ const ed = makeEditor(['\t1. item']);
+ expect(buildListIndentDecorations(ed.state.doc).find()).toHaveLength(0);
});
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(8); // 整行 "1. test"
+ expect(found[0].from).toBe(0);
+ expect(found[0].to).toBe(9);
});
it('decorates each list line independently across hardBreaks', () => {
@@ -141,26 +141,54 @@ 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. hello']);
+ expect(ed.view.dom.querySelector('p.composer-list-block-indent')?.textContent).toBe(
+ '1. hello',
+ );
expect(
ed.view.dom
- .querySelector<HTMLElement>('span.composer-list-line-indent')
+ .querySelector<HTMLElement>('p.composer-list-block-indent')
?.style.getPropertyValue('--composer-list-hang'),
).toBe('1.8ch');
});
+ it('keeps CJK punctuation inside a paragraph-level list wrapper', () => {
+ editor = new Editor({
+ element: document.createElement('div'),
+ extensions: [
+ Document,
+ Paragraph,
+ Text,
+ HardBreak,
+ CjkPunctDecoration,
+ ComposerListIndentDecoration,
+ ],
+ content: {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: '1. 中文,内容。继续' }],
+ },
+ ],
+ },
+ });
+ expect(editor.view.dom.querySelectorAll('p.composer-list-block-indent')).toHaveLength(1);
+ expect(editor.view.dom.querySelectorAll('span.composer-list-line-indent')).toHaveLength(0);
+ expect(editor.view.dom.querySelectorAll('span[style*="font-family"]')).not.toHaveLength(0);
+ });
+
it('appears the moment the prefix becomes complete, and disappears when broken', () => {
const ed = makeEditor(['1.']);
expect(indentSpans(ed)).toHaveLength(0);
// 打出空格,前缀完整 → 缩进立即出现
ed.commands.insertContentAt(ed.state.doc.content.size - 1, ' ');
- expect(indentSpans(ed)).toEqual(['1. ']);
+ expect(ed.view.dom.querySelector('.composer-list-block-indent')?.textContent).toBe('1. ');
// 删掉空格 → 缩进消失
ed.commands.deleteRange({
from: ed.state.doc.content.size - 2,
to: ed.state.doc.content.size - 1,
});
- expect(indentSpans(ed)).toHaveLength(0);
+ expect(ed.view.dom.querySelector('.composer-list-block-indent')).toBeNull();
});
});
@@ -176,13 +204,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-block-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('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 4c15abf29e..b509b61ce5 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
@@ -28,36 +28,26 @@ 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;
+const CJK_PUNCTUATION_RE = /[\u3000-\u303f\uff00-\uffef]/;
/**
* 将列表前缀换算成当前字体下的近似宽度。
*
* ChatInput 已启用 tabular-nums,所以数字直接用 1ch;句点、空格、方括号等
- * 窄字符按 0.4ch 估算;中文顿号按全角 1em。制表符按当前列位置推进到下一个
- * 8 空格 tab stop。这里只生成数字和 CSS 单位,不会透传用户文本。
+ * 窄字符按 0.4ch 估算;中文顿号按全角 1em。这里只生成数字和 CSS 单位,
+ * 不会透传用户文本。含 Tab 的行由调用方跳过,因为比例字体下无法用 ch
+ * 准确复现浏览器的 tab advance。
*/
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));
@@ -91,21 +81,19 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
let lineStartOffset = 0;
let lineEndOffset = 0;
let lineHasInlineAtom = false;
+ const lines: Array<{
+ text: string;
+ start: number;
+ end: number;
+ hasInlineAtom: boolean;
+ }> = [];
const flushLine = () => {
- const match = matchListPrefix(lineText);
- // 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);
- decorations.push(
- Decoration.inline(from, to, {
- class: 'composer-list-line-indent',
- style: listPrefixIndentStyle(prefix),
- }),
- );
+ lines.push({
+ text: lineText,
+ start: lineStartOffset,
+ end: lineEndOffset,
+ hasInlineAtom: lineHasInlineAtom,
+ });
};
block.nodesBetween(0, block.content.size, (node, pos) => {
if (node.type.name === 'hardBreak') {
@@ -129,6 +117,56 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
lineEndOffset = block.content.size;
flushLine(); // 段落最后一行
+ const addLineDecoration = (line: (typeof lines)[number]) => {
+ const match = matchListPrefix(line.text);
+ // 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.
+ // Tab-indented and CJK-punctuated lines are also skipped in the inline
+ // fallback: their rendered width cannot be represented safely by a split
+ // inline wrapper.
+ if (
+ !match ||
+ line.hasInlineAtom ||
+ line.text.slice(0, match.prefixLength).includes('\t') ||
+ CJK_PUNCTUATION_RE.test(line.text)
+ ) {
+ return;
+ }
+ const from = contentBase + line.start;
+ const to = contentBase + line.end;
+ const prefix = line.text.slice(0, match.prefixLength);
+ decorations.push(
+ Decoration.inline(from, to, {
+ class: 'composer-list-line-indent',
+ style: listPrefixIndentStyle(prefix),
+ }),
+ );
+ };
+
+ if (lines.length === 1) {
+ const [line] = lines;
+ const match = line && matchListPrefix(line.text);
+ const prefix = match ? line.text.slice(0, match.prefixLength) : '';
+ // A node decoration stays on the paragraph even when CjkPunctDecoration
+ // adds nested inline spans, so punctuation cannot split the list wrapper.
+ if (
+ line &&
+ match &&
+ !line.hasInlineAtom &&
+ !prefix.includes('\t')
+ ) {
+ decorations.push(
+ Decoration.node(blockPos, blockPos + block.nodeSize, {
+ class: 'composer-list-block-indent',
+ style: listPrefixIndentStyle(prefix),
+ }),
+ );
+ }
+ } else {
+ lines.forEach(addLineDecoration);
+ }
+
return false; // textblock 内部已手动扫过,不再下钻
});
diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.css
index 7fbc787f81..1210d0e511 100644
--- a/apps/desktop/src/renderer/styles/globals.css
+++ b/apps/desktop/src/renderer/styles/globals.css
@@ -346,10 +346,18 @@ html:lang(ko) {
.cm-editor .cm-dropCursor {
border-left-color: var(--caret-accent) !important;
}
-/* composer 列表行缩进(ComposerListIndentDecoration):行首列表前缀被识别后,
- 整条列表行变成独立的 inline-block 换行容器。padding-left 留出"基础缩进
- + 前缀宽度",首行再用负 text-indent 把列表标记悬挂回基础缩进位置;
- 自动换行后的续行不吃负缩进,因此会与首行正文对齐。 */
+/* composer 列表行缩进(ComposerListIndentDecoration):单行列表使用段落级
+ decoration,多行列表的无特殊标点行使用 inline-block 换行容器。padding-left
+ 留出"基础缩进 + 前缀宽度",首行再用负 text-indent 把列表标记悬挂回基础
+ 缩进位置;自动换行后的续行不吃负缩进,因此会与首行正文对齐。 */
+.ProseMirror .composer-list-block-indent {
+ display: 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);
+ overflow-wrap: anywhere;
+}
.ProseMirror span.composer-list-line-indent {
display: inline-block;
box-sizing: border-box;
@@ -358,7 +366,6 @@ html:lang(ko) {
text-indent: var(--composer-list-hang-negative, -1.25em);
vertical-align: top;
overflow-wrap: anywhere;
- tab-size: 8;
}
.ProseMirror .voice-input-draft-replaced {
display: none;