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): preserve list feedback around inline pills

zhangyongjieb82f7820ea99
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
index 0d76e96221..a0262d3a4d 100644
--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
@@ -13,6 +13,10 @@ import {
listPrefixIndentStyle,
} from '@/components/new-chat/ComposerListIndentDecoration';
import { CjkPunctDecoration } from '@/components/new-chat/CjkPunctDecoration';
+import {
+ setSlashCommandRoster,
+ SlashCommandDecoration,
+} from '@/components/new-chat/SlashCommandDecoration';
/**
* composer 列表行缩进 decoration:
@@ -111,7 +115,7 @@ describe('buildListIndentDecorations', () => {
expect(buildListIndentDecorations(ed.state.doc).find()).toHaveLength(0);
});
- it('leaves lines containing inline atoms untouched so chips keep their inline geometry', () => {
+ it('uses prefix-only fallback so inline atoms keep their geometry', () => {
editor = new Editor({
element: document.createElement('div'),
extensions: [Document, Paragraph, Text, HardBreak, TestAtom, ComposerListIndentDecoration],
@@ -129,7 +133,10 @@ describe('buildListIndentDecorations', () => {
],
},
});
- expect(buildListIndentDecorations(editor.state.doc).find()).toHaveLength(0);
+ expect(buildListIndentDecorations(editor.state.doc).find()).toHaveLength(1);
+ expect(
+ editor.view.dom.querySelector('span.composer-list-prefix-indent')?.textContent,
+ ).toBe('- ');
expect(
editor.view.dom
.querySelector('[data-test-atom]')
@@ -177,6 +184,49 @@ describe('ComposerListIndentDecoration in a real editor', () => {
expect(editor.view.dom.querySelectorAll('span[style*="font-family"]')).not.toHaveLength(0);
});
+ it('uses prefix-only fallback for CJK punctuation in hardBreak-separated list lines', () => {
+ const ed = makeEditor(['- 中文,内容', '2. plain']);
+ expect(
+ Array.from(ed.view.dom.querySelectorAll('span.composer-list-prefix-indent')).map(
+ (node) => node.textContent,
+ ),
+ ).toEqual(['- ']);
+ expect(indentSpans(ed)).toEqual(['2. plain']);
+ });
+
+ it('keeps slash-command pills inline by falling back to prefix-only decoration', () => {
+ editor = new Editor({
+ element: document.createElement('div'),
+ extensions: [
+ Document,
+ Paragraph,
+ Text,
+ HardBreak,
+ SlashCommandDecoration,
+ ComposerListIndentDecoration,
+ ],
+ content: {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'text', text: '- /foo details' },
+ { type: 'hardBreak' },
+ { type: 'text', text: '2. plain' },
+ ],
+ },
+ ],
+ },
+ });
+ setSlashCommandRoster(editor, [{ name: 'foo', description: 'test command' }]);
+ expect(
+ editor.view.dom.querySelector('span.composer-list-prefix-indent')?.textContent,
+ ).toBe('- ');
+ expect(editor.view.dom.querySelector('span.slash-cmd-pill')?.textContent).toBe('/foo');
+ expect(indentSpans(editor)).toEqual(['2. plain']);
+ });
+
it('appears the moment the prefix becomes complete, and disappears when broken', () => {
const ed = makeEditor(['1.']);
expect(indentSpans(ed)).toHaveLength(0);
@@ -205,6 +255,7 @@ 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 .composer-list-prefix-indent');
expect(css).toContain('.ProseMirror span.composer-list-line-indent');
expect(css).toContain('display: inline-block;');
expect(css).toContain('width: 100%;');
diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
index b509b61ce5..c56d7531bb 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
@@ -29,6 +29,7 @@ const PLUGIN_KEY = new PluginKey<DecorationSet>('composerListIndentDecoration');
/** 行内一个非文本 inline 节点(mention chip 等)的占位符,与 applyListContinuation 一致。 */
const ATOM_PLACEHOLDER = '\uFFFC';
const CJK_PUNCTUATION_RE = /[\u3000-\u303f\uff00-\uffef]/;
+const SLASH_COMMAND_RUN_RE = /(?:^|\s)\/\S+/;
/**
* 将列表前缀换算成当前字体下的近似宽度。
@@ -120,22 +121,28 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
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)
- ) {
+ // Mixed decoration ranges therefore use a prefix-only fallback below.
+ if (!match || line.text.slice(0, match.prefixLength).includes('\t')) {
return;
}
const from = contentBase + line.start;
const to = contentBase + line.end;
const prefix = line.text.slice(0, match.prefixLength);
+ const hasOverlappingInlineDecoration =
+ line.hasInlineAtom ||
+ CJK_PUNCTUATION_RE.test(line.text) ||
+ SLASH_COMMAND_RUN_RE.test(line.text.slice(match.prefixLength));
+ if (hasOverlappingInlineDecoration) {
+ // Keep list-mode feedback without wrapping an inline decoration range.
+ // Prefix-only styling cannot be split by CJK punctuation, slash-command
+ // pills, or inline atoms, and is safer than producing multiple blocks.
+ decorations.push(
+ Decoration.inline(from, from + match.prefixLength, {
+ class: 'composer-list-prefix-indent',
+ }),
+ );
+ return;
+ }
decorations.push(
Decoration.inline(from, to, {
class: 'composer-list-line-indent',
@@ -150,18 +157,23 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
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')
- ) {
+ if (line && match && !prefix.includes('\t') && !line.hasInlineAtom) {
decorations.push(
Decoration.node(blockPos, blockPos + block.nodeSize, {
class: 'composer-list-block-indent',
style: listPrefixIndentStyle(prefix),
}),
);
+ } else if (line && match && !prefix.includes('\t')) {
+ decorations.push(
+ Decoration.inline(
+ contentBase + line.start,
+ contentBase + line.start + match.prefixLength,
+ {
+ class: 'composer-list-prefix-indent',
+ },
+ ),
+ );
}
} else {
lines.forEach(addLineDecoration);
diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.css
index 1210d0e511..b39c369bae 100644
--- a/apps/desktop/src/renderer/styles/globals.css
+++ b/apps/desktop/src/renderer/styles/globals.css
@@ -358,6 +358,9 @@ html:lang(ko) {
text-indent: var(--composer-list-hang-negative, -1.25em);
overflow-wrap: anywhere;
}
+.ProseMirror .composer-list-prefix-indent {
+ padding-left: 1em;
+}
.ProseMirror span.composer-list-line-indent {
display: inline-block;
box-sizing: border-box;