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): 修复长数字字母列表标记断行

zhangyongjie97b61ed8751d
diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
index a0262d3a4d..837fe87361 100644
--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts
@@ -94,6 +94,27 @@ describe('buildListIndentDecorations', () => {
expect(found[0].to).toBe(9);
});
+ it('marks long digit and letter runs for scoped emergency breaking', () => {
+ const ed = makeEditor([
+ '2. 221241412423532235235325235212414',
+ '3. abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
+ ]);
+ expect(ed.view.dom.querySelector('p.composer-list-block-indent')).toBeNull();
+ expect(
+ Array.from(ed.view.dom.querySelectorAll('span.composer-list-long-run-marker')).map(
+ (node) => node.textContent,
+ ),
+ ).toEqual(['2. ', '3. ']);
+ expect(
+ Array.from(ed.view.dom.querySelectorAll('span.composer-list-long-run-body')).map(
+ (node) => node.textContent,
+ ),
+ ).toEqual([
+ '221241412423532235235325235212414',
+ 'abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
+ ]);
+ });
+
it('decorates each list line independently across hardBreaks', () => {
const ed = makeEditor(['intro', '- item', '2. x']);
const found = buildListIndentDecorations(ed.state.doc).find();
@@ -194,6 +215,16 @@ describe('ComposerListIndentDecoration in a real editor', () => {
expect(indentSpans(ed)).toEqual(['2. plain']);
});
+ it('keeps hanging indent when CJK punctuation belongs only to the list marker', () => {
+ const ed = makeEditor(['intro', '1、中文正文会在输入框边界自然换行']);
+ expect(
+ Array.from(ed.view.dom.querySelectorAll('span.composer-list-prefix-indent')).map(
+ (node) => node.textContent,
+ ),
+ ).toEqual([]);
+ expect(indentSpans(ed)).toEqual(['1、中文正文会在输入框边界自然换行']);
+ });
+
it('keeps slash-command pills inline by falling back to prefix-only decoration', () => {
editor = new Editor({
element: document.createElement('div'),
@@ -227,6 +258,40 @@ describe('ComposerListIndentDecoration in a real editor', () => {
expect(indentSpans(editor)).toEqual(['2. plain']);
});
+ it('keeps hanging indent for slash paths and unknown commands without pills', () => {
+ editor = new Editor({
+ element: document.createElement('div'),
+ extensions: [
+ Document,
+ Paragraph,
+ Text,
+ HardBreak,
+ SlashCommandDecoration,
+ ComposerListIndentDecoration,
+ ],
+ content: {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'text', text: '- inspect /usr/local/bin before continuing' },
+ { type: 'hardBreak' },
+ { type: 'text', text: '2. try /unknown later' },
+ ],
+ },
+ ],
+ },
+ });
+ setSlashCommandRoster(editor, [{ name: 'foo', description: 'test command' }]);
+ expect(editor.view.dom.querySelectorAll('span.composer-list-prefix-indent')).toHaveLength(0);
+ expect(editor.view.dom.querySelectorAll('span.slash-cmd-pill')).toHaveLength(0);
+ expect(indentSpans(editor)).toEqual([
+ '- inspect /usr/local/bin before continuing',
+ '2. try /unknown later',
+ ]);
+ });
+
it('appears the moment the prefix becomes complete, and disappears when broken', () => {
const ed = makeEditor(['1.']);
expect(indentSpans(ed)).toHaveLength(0);
@@ -262,6 +327,14 @@ describe('wiring contract', () => {
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).not.toContain('word-break: break-all;');
+ expect(css).toContain('.ProseMirror .composer-list-long-run-indent');
+ expect(css).toContain('word-break: break-all;');
+ expect(css).toContain('.ProseMirror .composer-list-long-run-marker');
+ expect(css).toContain('.ProseMirror .composer-list-long-run-body');
+ expect(css).toContain('white-space: nowrap;');
+ const regularIndentRule = css.match(
+ /\.ProseMirror span\.composer-list-line-indent \{([\s\S]*?)\n\}/,
+ )?.[1];
+ expect(regularIndentRule).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 c56d7531bb..e2d1baf146 100644
--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts
@@ -23,13 +23,39 @@ import { Decoration, DecorationSet } from '@tiptap/pm/view';
import type { EditorState, Transaction } from '@tiptap/pm/state';
import type { Node as PMNode } from '@tiptap/pm/model';
import { matchListPrefix } from '@/lib/composerListContinuation';
+import {
+ findSlashCommandMatches,
+ getSlashCommandRoster,
+ getSlashCommandRosterUpdate,
+ type SlashCommandMatch,
+} from './SlashCommandDecoration';
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+/;
+const LONG_ALPHANUMERIC_RUN_RE = /[A-Za-z0-9]{12,}/;
+
+function addLongRunDecoration(
+ decorations: Decoration[],
+ from: number,
+ to: number,
+ prefixLength: number,
+ prefix: string,
+): void {
+ const prefixTo = from + prefixLength;
+ decorations.push(
+ Decoration.inline(from, prefixTo, {
+ class: 'composer-list-long-run-marker',
+ style: listPrefixIndentStyle(prefix),
+ }),
+ Decoration.inline(prefixTo, to, {
+ class: 'composer-list-long-run-body composer-list-long-run-indent',
+ style: listPrefixIndentStyle(prefix),
+ }),
+ );
+}
/**
* 将列表前缀换算成当前字体下的近似宽度。
@@ -69,7 +95,10 @@ export function listPrefixIndentStyle(prefix: string): string {
* 扫描 doc,给所有"列表行"的整行内容生成 inline decoration。
* 返回的 from/to 是 doc-level position。导出以便单测直接断言范围。
*/
-export function buildListIndentDecorations(doc: PMNode): DecorationSet {
+export function buildListIndentDecorations(
+ doc: PMNode,
+ slashCommandMatches: ReadonlyArray<Pick<SlashCommandMatch, 'from' | 'to'>> = [],
+): DecorationSet {
const decorations: Decoration[] = [];
doc.descendants((block, blockPos) => {
@@ -128,10 +157,14 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
const from = contentBase + line.start;
const to = contentBase + line.end;
const prefix = line.text.slice(0, match.prefixLength);
+ const body = line.text.slice(match.prefixLength);
+ const overlapsSlashCommandPill = slashCommandMatches.some(
+ (slashMatch) => slashMatch.from < to && slashMatch.to > from,
+ );
const hasOverlappingInlineDecoration =
line.hasInlineAtom ||
- CJK_PUNCTUATION_RE.test(line.text) ||
- SLASH_COMMAND_RUN_RE.test(line.text.slice(match.prefixLength));
+ CJK_PUNCTUATION_RE.test(body) ||
+ overlapsSlashCommandPill;
if (hasOverlappingInlineDecoration) {
// Keep list-mode feedback without wrapping an inline decoration range.
// Prefix-only styling cannot be split by CJK punctuation, slash-command
@@ -143,6 +176,10 @@ export function buildListIndentDecorations(doc: PMNode): DecorationSet {
);
return;
}
+ if (LONG_ALPHANUMERIC_RUN_RE.test(body)) {
+ addLongRunDecoration(decorations, from, to, match.prefixLength, prefix);
+ return;
+ }
decorations.push(
Decoration.inline(from, to, {
class: 'composer-list-line-indent',
@@ -157,13 +194,33 @@ 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 && !prefix.includes('\t') && !line.hasInlineAtom) {
+ if (
+ line &&
+ match &&
+ !prefix.includes('\t') &&
+ !line.hasInlineAtom &&
+ !LONG_ALPHANUMERIC_RUN_RE.test(line.text.slice(match.prefixLength))
+ ) {
decorations.push(
Decoration.node(blockPos, blockPos + block.nodeSize, {
class: 'composer-list-block-indent',
style: listPrefixIndentStyle(prefix),
}),
);
+ } else if (
+ line &&
+ match &&
+ !prefix.includes('\t') &&
+ !line.hasInlineAtom &&
+ LONG_ALPHANUMERIC_RUN_RE.test(line.text.slice(match.prefixLength))
+ ) {
+ addLongRunDecoration(
+ decorations,
+ contentBase + line.start,
+ contentBase + line.end,
+ match.prefixLength,
+ prefix,
+ );
} else if (line && match && !prefix.includes('\t')) {
decorations.push(
Decoration.inline(
@@ -194,11 +251,24 @@ export const ComposerListIndentDecoration = Extension.create({
key: PLUGIN_KEY,
state: {
init(_config, state: EditorState) {
- return buildListIndentDecorations(state.doc);
+ const roster = getSlashCommandRoster(state);
+ return buildListIndentDecorations(
+ state.doc,
+ findSlashCommandMatches(state.doc, roster),
+ );
},
- apply(tr: Transaction, old: DecorationSet) {
- if (!tr.docChanged) return old;
- return buildListIndentDecorations(tr.doc);
+ apply(
+ tr: Transaction,
+ old: DecorationSet,
+ oldState: EditorState,
+ ) {
+ const rosterUpdate = getSlashCommandRosterUpdate(tr);
+ if (!tr.docChanged && rosterUpdate === undefined) return old;
+ const roster = rosterUpdate ?? getSlashCommandRoster(oldState);
+ return buildListIndentDecorations(
+ tr.doc,
+ findSlashCommandMatches(tr.doc, roster),
+ );
},
},
props: {
diff --git a/apps/desktop/src/renderer/components/new-chat/SlashCommandDecoration.ts b/apps/desktop/src/renderer/components/new-chat/SlashCommandDecoration.ts
index 7a548dc3d7..1c76736cfd 100644
--- a/apps/desktop/src/renderer/components/new-chat/SlashCommandDecoration.ts
+++ b/apps/desktop/src/renderer/components/new-chat/SlashCommandDecoration.ts
@@ -19,7 +19,7 @@ import type { UnifiedCommand } from '@/lib/slashCommands';
const PLUGIN_KEY = new PluginKey<SlashCommandPluginState>('slashCommandDecoration');
const META_KEY = 'slashCommandDecoration';
-type SlashCommandRoster = ReadonlyArray<Pick<UnifiedCommand, 'name' | 'description'>>;
+export type SlashCommandRoster = ReadonlyArray<Pick<UnifiedCommand, 'name' | 'description'>>;
interface SlashCommandPluginState {
commands: SlashCommandRoster;
@@ -138,11 +138,21 @@ export function setSlashCommandRoster(editor: Editor | null, commands: SlashComm
editor.view.dispatch(editor.state.tr.setMeta(META_KEY, commands));
}
+/** Return the command roster currently used by the decoration plugin. */
+export function getSlashCommandRoster(state: EditorState): SlashCommandRoster {
+ return PLUGIN_KEY.getState(state)?.commands ?? [];
+}
+
+/** Return a roster update carried by this transaction, if present. */
+export function getSlashCommandRosterUpdate(
+ tr: Transaction,
+): SlashCommandRoster | undefined {
+ return tr.getMeta(META_KEY) as SlashCommandRoster | undefined;
+}
+
/** Return the exact slash runs currently decorated by this editor's roster. */
export function getDecoratedSlashCommandMatches(editor: Editor): SlashCommandMatch[] {
- const current = PLUGIN_KEY.getState(editor.state);
- if (!current) return [];
- return findSlashCommandMatches(editor.state.doc, current.commands);
+ return findSlashCommandMatches(editor.state.doc, getSlashCommandRoster(editor.state));
}
/** 导出供 ProseMirror state 层测试 roster、退格与 legacy 草稿迁移。 */
diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.css
index b39c369bae..b34a4a70d6 100644
--- a/apps/desktop/src/renderer/styles/globals.css
+++ b/apps/desktop/src/renderer/styles/globals.css
@@ -370,6 +370,29 @@ html:lang(ko) {
vertical-align: top;
overflow-wrap: anywhere;
}
+/* 只对没有自然断点的长数字/字母串启用强制断字。普通英文句子仍由
+ overflow-wrap:anywhere 保持自然单词边界,避免全局 break-all 造成逐字竖排。 */
+.ProseMirror .composer-list-long-run-indent {
+ word-break: break-all;
+}
+.ProseMirror .composer-list-long-run-marker {
+ display: inline-block;
+ box-sizing: border-box;
+ width: calc(1em + var(--composer-list-hang, 1.25em));
+ padding-left: 1em;
+ overflow: visible;
+ white-space: nowrap;
+ vertical-align: top;
+}
+.ProseMirror .composer-list-long-run-body {
+ display: inline-block;
+ box-sizing: border-box;
+ width: calc(100% - 1em - var(--composer-list-hang, 1.25em));
+ max-width: calc(100% - 1em - var(--composer-list-hang, 1.25em));
+ overflow-wrap: anywhere;
+ word-break: break-all;
+ vertical-align: top;
+}
.ProseMirror .voice-input-draft-replaced {
display: none;
}