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): 保留列表格式与草稿状态

zhangyongjiee699a4218d04
diff --git a/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts b/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
index d91dce7ac2..7885b5ed59 100644
--- a/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
@@ -129,6 +129,28 @@ describe('composer list document normalization', () => {
});
});
+ it('keeps optional spaces after CJK ordered markers in item text', () => {
+ expect(plainTextToComposerDocument('2、项目\n3、 项目')).toEqual({
+ type: 'doc',
+ content: [
+ {
+ type: 'orderedList',
+ attrs: { start: 2, marker: '、' },
+ content: [
+ {
+ type: 'listItem',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: '项目' }] }],
+ },
+ {
+ type: 'listItem',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: ' 项目' }] }],
+ },
+ ],
+ },
+ ],
+ });
+ });
+
it('leaves existing structured content unchanged', () => {
const document = {
type: 'doc' as const,
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
index 1a60a4f055..9dcb71caab 100644
--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
@@ -48,7 +48,7 @@ const TestAtom = TiptapNode.create({
},
});
-function makeEditor(lines: string[], structuredLists = false): Editor {
+function makeEditor(lines: string[]): Editor {
const content: Array<Record<string, unknown>> = [];
lines.forEach((line, i) => {
if (i > 0) content.push({ type: 'hardBreak' });
@@ -62,7 +62,7 @@ function makeEditor(lines: string[], structuredLists = false): Editor {
Text,
HardBreak,
TestAtom,
- ComposerListIndentDecoration.configure({ structuredLists }),
+ ComposerListIndentDecoration,
],
content: { type: 'doc', content: [{ type: 'paragraph', content }] },
});
@@ -82,9 +82,9 @@ afterEach(() => {
});
describe('buildListIndentDecorations', () => {
- it('leaves promotable top-level rows to structured-list normalization', () => {
- const ed = makeEditor(['1. one', ' - nested', '> quote'], true);
- expect(indentSpans(ed)).toEqual([' - nested', '> quote']);
+ it('keeps fallback decoration for rows that have not been promoted yet', () => {
+ const ed = makeEditor(['1. one', ' - nested', '> quote']);
+ expect(indentSpans(ed)).toEqual(['1. one', ' - nested', '> quote']);
});
it('builds paired hanging-indent variables without embedding user text', () => {
@@ -921,7 +921,7 @@ describe('wiring contract', () => {
"import { ComposerListIndentDecoration } from './ComposerListIndentDecoration';",
);
expect(src).toMatch(
- /CjkPunctDecoration,\s*\n\s*ComposerListIndentDecoration\.configure\(\{ structuredLists: true \}\),/,
+ /CjkPunctDecoration,\s*\n\s*ComposerListIndentDecoration,/,
);
});
diff --git a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
index 505f2efeae..ab8d6d413b 100644
--- a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
@@ -613,6 +613,30 @@ describe('composer structured list serialization', () => {
expect(serializeEditorContent(editor).text).toBe('10. parent\n - child');
});
+ it('preserves the optional space after CJK ordered markers', () => {
+ const editor = makeEditor({
+ type: 'doc',
+ content: [
+ {
+ type: 'orderedList',
+ attrs: { start: 2, marker: '、' },
+ content: [
+ {
+ type: 'listItem',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: '项目' }] }],
+ },
+ {
+ type: 'listItem',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: ' 项目' }] }],
+ },
+ ],
+ },
+ ],
+ });
+
+ expect(serializeEditorContent(editor).text).toBe('2、项目\n3、 项目');
+ });
+
it('preserves nested markers and projects atom ranges into wire offsets', () => {
const href = 'cindy://session/session-a?message=message-a';
const editor = makeEditor({
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 178659cd80..5bdc8bffd0 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -1379,7 +1379,7 @@ export function ChatInput({
enabled: window.electronAPI.platform === 'win32',
}),
CjkPunctDecoration,
- ComposerListIndentDecoration.configure({ structuredLists: true }),
+ ComposerListIndentDecoration,
VoiceInputDraftDecoration,
MentionDragCaretDecoration,
GhostCommandDecoration,
@@ -2716,8 +2716,11 @@ export function ChatInput({
setBrowserComments(draft.browserComments ?? []);
// 同值外部写入不做全量 setContent,避免把用户停在中段的光标弹到末尾、
// 打断 IME 组合。appendQuoteToDraft 会改变正文文档,自然走下方 setContent。
+ const normalizedDraftText = draft.text
+ ? normalizeComposerDocumentJSON(draft.text)
+ : null;
const textUnchanged =
- JSON.stringify(draft.text ?? null) ===
+ JSON.stringify(normalizedDraftText) ===
JSON.stringify(editor.isEmpty ? null : editor.getJSON());
if (textUnchanged) {
if (!editor.isFocused) editor.commands.focus();
@@ -2725,8 +2728,8 @@ export function ChatInput({
}
isRestoringRef.current = true;
try {
- if (draft.text) {
- editor.commands.setContent(normalizeComposerDocumentJSON(draft.text));
+ if (normalizedDraftText) {
+ editor.commands.setContent(normalizedDraftText);
editor.commands.focus('end');
} else {
editor.commands.clearContent();
diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
index f68e9e462e..9b6bde7ae9 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
@@ -50,10 +50,6 @@ type ListDecorationPluginState = {
type CompositionMeta = 'suspend' | 'resume';
-interface ComposerListIndentOptions {
- structuredLists: boolean;
-}
-
const PLUGIN_STATE_KEY = new PluginKey<ListDecorationPluginState>(
'composerListIndentDecorationState',
);
@@ -65,9 +61,6 @@ const CJK_PUNCTUATION_RE = /[\u3000-\u303f\uff00-\uffef]/;
// 普通句子里偶然出现长 token 时,交给 overflow-wrap:anywhere,保留空格
// 的自然断词行为,避免把整句变成逐字断行。
const LONG_ALPHANUMERIC_BODY_RE = /^\s*[A-Za-z0-9]{12,}\s*$/;
-const PROMOTABLE_TOP_LEVEL_LIST_RE =
- /^(?:[-+*•][ \t]+|\d{1,6}[.)][ \t]+|\d{1,6}、[ \t]*)/;
-
interface ListIndentValues {
ch: number;
em: number;
@@ -156,7 +149,6 @@ export function buildListIndentDecorations(
doc: PMNode,
slashCommandMatches: ReadonlyArray<Pick<SlashCommandMatch, 'from' | 'to'>> = [],
voiceReplacementRange: VoiceInputReplacementRange | null = null,
- structuredLists = false,
): DecorationSet {
const decorations: Decoration[] = [];
@@ -209,16 +201,7 @@ export function buildListIndentDecorations(
flushLine(); // 段落最后一行
const compatibilityMatch = (line: (typeof lines)[number]) => {
- const match = matchListPrefix(line.text);
- if (
- match &&
- structuredLists &&
- !line.hasInlineAtom &&
- PROMOTABLE_TOP_LEVEL_LIST_RE.test(line.text)
- ) {
- return null;
- }
- return match;
+ return matchListPrefix(line.text);
};
const lineMatches = lines.map((line) => ({
line,
@@ -418,18 +401,11 @@ export function buildListIndentDecorations(
return DecorationSet.create(doc, decorations);
}
-export const ComposerListIndentDecoration = Extension.create<ComposerListIndentOptions>({
+export const ComposerListIndentDecoration = Extension.create({
name: 'composerListIndentDecoration',
- addOptions() {
- return {
- structuredLists: false,
- };
- },
-
addProseMirrorPlugins() {
let resumeTimer: ReturnType<typeof setTimeout> | null = null;
- const structuredLists = this.options.structuredLists;
return [
new Plugin<ListDecorationPluginState>({
@@ -442,7 +418,6 @@ export const ComposerListIndentDecoration = Extension.create<ComposerListIndentO
state.doc,
findSlashCommandMatches(state.doc, roster),
null,
- structuredLists,
),
suspendedForComposition: false,
};
@@ -475,7 +450,6 @@ export const ComposerListIndentDecoration = Extension.create<ComposerListIndentO
tr.doc,
findSlashCommandMatches(tr.doc, roster),
voiceReplacement.range,
- structuredLists,
),
suspendedForComposition: false,
};
diff --git a/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts b/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts
index 5ccafe80be..26a44420ac 100644
--- a/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts
+++ b/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts
@@ -234,7 +234,9 @@ export function serializeEditorContent(editor: Editor): SerializedComposerConten
listNode.forEach((item, itemOffset, itemIndex) => {
if (item.type.name !== 'listItem') return;
const itemPosition = listPosition + 1 + itemOffset;
- const itemMarker = isOrdered ? `${start + itemIndex}${marker} ` : '- ';
+ const itemMarker = isOrdered
+ ? `${start + itemIndex}${marker}${marker === '、' ? '' : ' '}`
+ : '- ';
const itemIndent = `${indent}${' '.repeat(itemMarker.length)}`;
let firstParagraph = true;
diff --git a/apps/desktop/src/renderer/lib/composerListDocument.ts b/apps/desktop/src/renderer/lib/composerListDocument.ts
index 045b7c3024..d56855a4fa 100644
--- a/apps/desktop/src/renderer/lib/composerListDocument.ts
+++ b/apps/desktop/src/renderer/lib/composerListDocument.ts
@@ -82,7 +82,11 @@ function parseListMarker(text: string): ListMarker | null {
if (marker !== '、' && ordered[3].length === 0) return null;
return {
kind: 'ordered',
- prefixLength: ordered[0].length,
+ // CJK ordered markers are valid with or without a following space.
+ // Keep that space in the item body so serialization preserves the
+ // user's original form without adding another list attribute.
+ prefixLength:
+ marker === '、' ? ordered[1].length + marker.length : ordered[0].length,
marker,
start: Number(ordered[1]),
};