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): 完善列表恢复边界

zhangyongjieac52ef8e90ac
diff --git a/apps/desktop/src/renderer/__tests__/composerQuoteDocument.test.ts b/apps/desktop/src/renderer/__tests__/composerQuoteDocument.test.ts
index 25efc769f1..667adac50a 100644
--- a/apps/desktop/src/renderer/__tests__/composerQuoteDocument.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerQuoteDocument.test.ts
@@ -7,6 +7,7 @@ import HardBreak from '@tiptap/extension-hard-break';
import { afterEach, describe, expect, it } from 'vitest';
import { ComposerQuoteNode } from '@/components/new-chat/ComposerQuoteNode';
import {
+ COMPOSER_QUOTE_NODE_TYPE,
appendQuoteToComposerDocument,
composerHistoryEntryToDocument,
prependLegacyQuotesToComposerDocument,
@@ -144,6 +145,36 @@ describe('composerQuoteDocument', () => {
});
});
+ it('keeps a sibling list row separate when it follows a quote segment', () => {
+ expect(
+ quoteSegmentsToComposerDocument([
+ { kind: 'text', text: '- first' },
+ { kind: 'quote', quote: { text: 'quoted' } },
+ { kind: 'text', text: '- second' },
+ ]),
+ ).toEqual({
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'text', text: '- first' },
+ {
+ type: COMPOSER_QUOTE_NODE_TYPE,
+ attrs: {
+ text: 'quoted',
+ sourcePath: null,
+ startLine: null,
+ endLine: null,
+ },
+ },
+ ],
+ },
+ { type: 'paragraph', content: [{ type: 'text', text: '- second' }] },
+ ],
+ });
+ });
+
it('hydrates encoded history rows as quote atoms and leaves unflagged markers as text', () => {
const encoded = `${formatQuoteForSend({ text: 'quoted' })}\n\nreply`;
diff --git a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
index 94d6d42f86..fc0da012a5 100644
--- a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts
@@ -317,6 +317,22 @@ describe('composer structured list input rules', () => {
expect(editor.state.doc.firstChild?.textContent).toBe('```1. ');
});
+ it('keeps hard-break markers literal after a preceding open fence', () => {
+ const editor = makeEditor({
+ type: 'doc',
+ content: [
+ { type: 'paragraph', content: [{ type: 'text', text: '```' }] },
+ { type: 'paragraph', content: [{ type: 'hardBreak' }] },
+ ],
+ });
+ selectDocumentEnd(editor);
+
+ typeThroughInputRules(editor, '1. ');
+
+ expect(editor.state.doc.lastChild?.type.name).toBe('paragraph');
+ expect(editor.state.doc.lastChild?.textContent).toBe('1. ');
+ });
+
it('keeps ordinary list markers literal after a preceding open fence', () => {
const editor = makeEditor({
type: 'doc',
@@ -507,6 +523,33 @@ describe('composer structured list keyboard commands', () => {
expect(editor.getJSON().content).toEqual([{ type: 'paragraph' }]);
});
+ it('treats a whitespace-only CJK marker item as empty on break', () => {
+ const editor = makeEditor({
+ type: 'doc',
+ content: [
+ {
+ type: 'orderedList',
+ attrs: { start: 1, marker: '、', separator: '' },
+ content: [
+ {
+ type: 'listItem',
+ content: [
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: ' ' }],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+ selectDocumentEnd(editor);
+
+ expect(handleStructuredListBreak(editor.view)).toBe(true);
+ expect(editor.getJSON().content).toEqual([{ type: 'paragraph' }]);
+ });
+
it('does not add a task prefix when splitting before the task marker', () => {
const editor = makeEditor({
type: 'doc',
diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts
index 21733d967a..d0d9304041 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts
@@ -13,8 +13,8 @@ import { Selection, TextSelection } from '@tiptap/pm/state';
import type { EditorView } from '@tiptap/pm/view';
const BULLET_MARKER_RE = /^([-+*•])([ \t]+)$/;
-const ORDERED_MARKER_RE = /^([1-9]\d{0,7})([.)])([ \t]+)$/;
-const CJK_ORDERED_MARKER_RE = /^([1-9]\d{0,7})(、)$/;
+const ORDERED_MARKER_RE = /^([1-9]\d{0,8})([.)])([ \t]+)$/;
+const CJK_ORDERED_MARKER_RE = /^([1-9]\d{0,8})(、)$/;
type BulletMarker = '-' | '+' | '*' | '•';
type OrderedMarker = '.' | ')' | '、';
@@ -89,7 +89,7 @@ function plainListParagraphMarker(text: string): PlainListParagraphMarker | null
attrs: { marker: bullet[1], separator: bullet[2] },
};
}
- const ordered = text.match(/^([1-9]\d{0,7})([.)])([ \t]+)/);
+ const ordered = text.match(/^([1-9]\d{0,8})([.)])([ \t]+)/);
if (ordered) {
return {
kind: 'ordered',
@@ -97,7 +97,7 @@ function plainListParagraphMarker(text: string): PlainListParagraphMarker | null
attrs: { start: Number(ordered[1]), marker: ordered[2], separator: ordered[3] },
};
}
- const cjkOrdered = text.match(/^([1-9]\d{0,7})(、)([ \t]*)/);
+ const cjkOrdered = text.match(/^([1-9]\d{0,8})(、)([ \t]*)/);
if (cjkOrdered) {
return {
kind: 'ordered',
@@ -138,6 +138,7 @@ function hardBreakListInputRule(
const hardBreak = paragraph.nodeAt(markerOffset - 1);
if (hardBreak?.type.name !== 'hardBreak') return null;
if (paragraphOffsetIsInsideFence(paragraph, markerOffset)) return null;
+ if (documentFenceStateBefore(state.doc, $markerStart.before(1))) return null;
const before = paragraph.content.cut(0, markerOffset - hardBreak.nodeSize);
const after = paragraph.content.cut(range.to - paragraphStart);
@@ -349,13 +350,23 @@ function selectedListItemDepth(view: EditorView): number | null {
function selectedListItemIsEmpty(view: EditorView, depth: number): boolean {
const item = view.state.selection.$from.node(depth);
+ const paragraph = item.firstChild;
return (
item.childCount === 1 &&
- item.firstChild?.type.name === 'paragraph' &&
- item.firstChild.content.size === 0
+ paragraph?.type.name === 'paragraph' &&
+ paragraph.content.content.every((node) => node.isText && !(node.text ?? '').trim())
);
}
+function liftEmptyStructuredListItem(view: EditorView, itemType: NodeType): boolean {
+ const { $from } = view.state.selection;
+ const paragraph = $from.parent;
+ if (paragraph.type.name === 'paragraph' && paragraph.content.size > 0) {
+ view.dispatch(view.state.tr.delete($from.start(), $from.end()));
+ }
+ return liftListItem(itemType)(view.state, view.dispatch);
+}
+
function selectedListItemIsOnlyTaskParagraph(view: EditorView, depth: number): boolean {
const item = view.state.selection.$from.node(depth);
return item.childCount === 1 && item.firstChild === view.state.selection.$from.parent;
@@ -438,7 +449,7 @@ export function handleStructuredListBreak(view: EditorView): boolean {
return clearTaskPrefixAndLift(view, itemDepth, taskPrefix);
}
if (selectedListItemIsEmpty(view, itemDepth)) {
- return liftListItem(itemType)(state, view.dispatch);
+ return liftEmptyStructuredListItem(view, itemType);
}
const split = splitListItem(itemType)(state, view.dispatch);
if (split && taskPrefix?.caretAtOrAfterPrefix) {
@@ -455,6 +466,10 @@ export function handleStructuredListBackspace(view: EditorView): boolean {
const { $from } = state.selection;
const itemDepth = selectedListItemDepth(view);
const taskPrefix = selectedTaskPrefix(view);
+ const emptyItem = itemDepth !== null && selectedListItemIsEmpty(view, itemDepth);
+ const emptyItemCaret =
+ emptyItem &&
+ ($from.parentOffset === 0 || $from.parentOffset === $from.parent.content.size);
if (
!itemType ||
!state.selection.empty ||
@@ -464,12 +479,12 @@ export function handleStructuredListBackspace(view: EditorView): boolean {
!taskPrefix.caretAtOrAfterPrefix ||
!taskPrefix.caretAtParagraphEnd ||
!selectedListItemIsOnlyTaskParagraph(view, itemDepth)
- : $from.parentOffset !== 0 || !selectedListItemIsEmpty(view, itemDepth))
+ : (!emptyItemCaret && $from.parentOffset !== 0) || !emptyItem)
) {
return false;
}
if (taskPrefix) return clearTaskPrefixAndLift(view, itemDepth, taskPrefix);
- return liftListItem(itemType)(state, view.dispatch);
+ return liftEmptyStructuredListItem(view, itemType);
}
/**
diff --git a/apps/desktop/src/renderer/lib/composerListContinuation.ts b/apps/desktop/src/renderer/lib/composerListContinuation.ts
index 0b570eaf86..2258e1e626 100644
--- a/apps/desktop/src/renderer/lib/composerListContinuation.ts
+++ b/apps/desktop/src/renderer/lib/composerListContinuation.ts
@@ -23,9 +23,9 @@ export interface ListPrefixMatch {
// checkbox 允许行在 `]` 处截止(`(\s+|$)`),这样 "- [ ]" 也能识别成空项退出。
const CHECKBOX_RE = /^(\s*)([-+*])(\s+)\[[ xX]\](\s+|$)/;
const BULLET_RE = /^(\s*)([-+*•])(\s+)/;
-const ORDERED_RE = /^(\s*)(\d{1,8})([.)])(\s+)/;
+const ORDERED_RE = /^(\s*)(\d{1,9})([.)])(\s+)/;
// 中文顿号序号(`1、`)不要求后随空格,符合中文输入习惯。
-const ORDERED_CJK_RE = /^(\s*)(\d{1,8})(、)(\s*)/;
+const ORDERED_CJK_RE = /^(\s*)(\d{1,9})(、)(\s*)/;
const QUOTE_RE = /^(\s*)(>)(\s+)/;
/**
diff --git a/apps/desktop/src/renderer/lib/composerListDocument.ts b/apps/desktop/src/renderer/lib/composerListDocument.ts
index 682f347511..9159c03876 100644
--- a/apps/desktop/src/renderer/lib/composerListDocument.ts
+++ b/apps/desktop/src/renderer/lib/composerListDocument.ts
@@ -17,7 +17,7 @@ interface ComposerLine {
}
const BULLET_RE = /^([-+*•])([ \t]+)/;
-const ORDERED_RE = /^([1-9]\d{0,7})([.)、])([ \t]*)/;
+const ORDERED_RE = /^([1-9]\d{0,8})([.)、])([ \t]*)/;
function inlineNodeText(node: JSONContent): string {
if (node.type === 'text') return node.text ?? '';
diff --git a/apps/desktop/src/renderer/lib/composerQuoteDocument.ts b/apps/desktop/src/renderer/lib/composerQuoteDocument.ts
index f5a1766f56..95ff231b36 100644
--- a/apps/desktop/src/renderer/lib/composerQuoteDocument.ts
+++ b/apps/desktop/src/renderer/lib/composerQuoteDocument.ts
@@ -81,6 +81,8 @@ function isPureLineBreakText(text: string): boolean {
return true;
}
+const LIST_ROW_MARKER_RE = /^(?:[-+*•][ \t]+|[1-9]\d{0,8}[.)][ \t]+|[1-9]\d{0,8}、[ \t]*)/;
+
function normalizeTopLevelQuoteNodes(document: JSONContent | null | undefined): JSONContent[] {
const source =
document?.type === 'doc' && Array.isArray(document.content) ? document.content : [];
@@ -155,6 +157,7 @@ export function quoteSegmentsToComposerDocument(
const content: JSONContent[] = [];
let inlineContent: JSONContent[] = [];
let hasContent = false;
+ let quoteJustEnded = false;
const finishParagraph = () => {
content.push(paragraph(inlineContent));
@@ -165,6 +168,7 @@ export function quoteSegmentsToComposerDocument(
if (segment.kind === 'quote') {
inlineContent.push(quoteNode(segment.quote));
hasContent = true;
+ quoteJustEnded = true;
continue;
}
if (!segment.text) continue;
@@ -180,8 +184,12 @@ export function quoteSegmentsToComposerDocument(
}
const lines = segment.text.split('\n');
lines.forEach((line, index) => {
+ if (quoteJustEnded && line && LIST_ROW_MARKER_RE.test(line) && inlineContent.length > 0) {
+ finishParagraph();
+ }
if (line) inlineContent.push({ type: 'text', text: line });
if (index < lines.length - 1) finishParagraph();
+ if (line) quoteJustEnded = false;
});
}
if (!hasContent) return null;