Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 收窄列表 fallback 断行与字体范围 (#629)
zhangyongjie5dd2292c2387
diff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex c6d5a12ee4..574be64da8 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -126,7 +126,7 @@"electron-window-state": "^5.0.3","fix-path": "^5.0.0","gray-matter": "^4.0.3",- "harmonyos-sans-sc-webfont-splitted": "^1.1.0",+ "harmonyos-sans-sc-webfont-splitted": "1.1.0","highlight.js": "^11.11.1","html-to-image": "^1.11.13","i18next": "^26.1.0",diff --git a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.tsindex 711b14ff38..5ccb77dc33 100644--- a/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts+++ b/apps/desktop/src/renderer/__tests__/composerListIndentDecoration.test.ts@@ -1,5 +1,5 @@// @vitest-environment jsdom-import { readFileSync } from 'node:fs';+import { existsSync, readFileSync } from 'node:fs';import { resolve } from 'node:path';import { afterEach, describe, expect, it, vi } from 'vitest';import { Editor, Node as TiptapNode } from '@tiptap/core';@@ -97,6 +97,14 @@ describe('buildListIndentDecorations', () => {);});+ it('reserves a deterministic 8ch slot for every indented tab', () => {+ expect(listPrefixIndentStyle(' \t1. ')).toContain('--composer-list-hang:10.6ch;');+ expect(listPrefixIndentStyle('\t\t1. ')).toContain('--composer-list-hang:17.8ch;');+ expect(listPrefixIndentStyle('1、\t')).toContain(+ '--composer-list-hang:calc(9ch + 1em);',+ );+ });+it('decorates the full content of a single-line item', () => {const ed = makeEditor(['1. test']);const found = buildListIndentDecorations(ed.state.doc).find();@@ -757,7 +765,10 @@ describe('ComposerListIndentDecoration in a real editor', () => {expect(container).not.toBeNull();expect(unindentedRows).toHaveLength(1);expect(unindentedRows?.[0]?.textContent).toBe('有新增游戏数的用户数、占全部用户比例。');- expect(unindentedRows?.[0]?.classList.contains('composer-list-cjk-font')).toBe(true);+ expect(unindentedRows?.[0]?.classList.contains('composer-list-cjk-font')).toBe(false);+ expect(+ unindentedRows?.[0]?.classList.contains('composer-list-cjk-punctuation-font'),+ ).toBe(true);expect(unindentedRows?.[0]?.querySelectorAll('span[style*="font-family"]')).toHaveLength(0);});@@ -800,7 +811,43 @@ describe('ComposerListIndentDecoration in a real editor', () => {const fallbackContainerRule = css.match(/\.ProseMirror \.composer-list-fallback-container \{([\s\S]*?)\n\}/,)?.[1];- expect(fallbackContainerRule).toContain('word-break: break-all;');+ const fallbackLongRunRule = css.match(+ /\.ProseMirror \.composer-list-fallback-long-run-body \{([\s\S]*?)\n\}/,+ )?.[1];+ expect(fallbackContainerRule).not.toContain('word-break: break-all;');+ expect(fallbackLongRunRule).toContain('word-break: break-all;');++ 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. 中文标点。' },+ { type: 'hardBreak' },+ { type: 'text', text: '2. abcdefghijklmnopqrstuvwxyz' },+ { type: 'hardBreak' },+ { type: 'text', text: '3. alpha ordinaryword omega' },+ ],+ },+ ],+ },+ });+ const longRunBodies = editor.view.dom.querySelectorAll(+ '.composer-list-fallback-long-run-body',+ );+ expect(longRunBodies).toHaveLength(1);+ expect(longRunBodies[0]?.textContent).toBe('abcdefghijklmnopqrstuvwxyz');});it('keeps hanging indent for slash paths and unknown commands without pills', () => {@@ -883,8 +930,47 @@ describe('wiring contract', () => {expect(css).toContain('.ProseMirror .composer-list-long-run-marker');expect(css).toContain('.ProseMirror .composer-list-long-run-body');expect(css).toContain('.ProseMirror .composer-list-tab-indent');- expect(css).toContain('tab-size: 8;');- expect(css).toContain('white-space: nowrap;');+ expect(css).toContain('.ProseMirror .composer-list-cjk-punctuation-font');+ expect(css).toContain("font-family: 'Cindy CJK Punctuation Local'");+ expect(css).toContain("font-family: 'Cindy CJK Punctuation Bundled'");+ expect(css).toContain('unicode-range: U+3000-303F, U+FF00-FFEF;');+ const punctuationFontCss = css.slice(+ css.indexOf("font-family: 'Cindy CJK Punctuation Local'"),+ css.indexOf('.ProseMirror .composer-list-cjk-punctuation-font'),+ );+ expect(punctuationFontCss.match(/font-style: normal;/g)).toHaveLength(5);+ expect(punctuationFontCss.match(/font-weight: 400;/g)).toHaveLength(5);+ const bundledPunctuationFonts = [+ 'Regular_256855.woff2',+ 'Regular_312071.woff2',+ 'Regular_ac4458.woff2',+ 'Regular_ea8896.woff2',+ ];+ bundledPunctuationFonts.forEach((filename) => {+ expect(css).toContain(`harmonyos-sans-sc-webfont-splitted/dist/${filename}`);+ expect(+ existsSync(+ resolve(+ __dirname,+ '..',+ '..',+ '..',+ '..',+ '..',+ 'node_modules',+ 'harmonyos-sans-sc-webfont-splitted',+ 'dist',+ filename,+ ),+ ),+ ).toBe(true);+ });+ const desktopPackage = JSON.parse(+ readFileSync(resolve(__dirname, '..', '..', '..', 'package.json'), 'utf8'),+ ) as { dependencies?: Record<string, string> };+ expect(desktopPackage.dependencies?.['harmonyos-sans-sc-webfont-splitted']).toBe('1.1.0');+ expect(css).toContain('tab-size: 8ch;');+ expect(css).toContain('white-space: pre;');const fallbackPrefixRule = css.match(/\.ProseMirror \.composer-list-fallback-prefix \{([\s\S]*?)\n\}/,)?.[1];diff --git a/apps/desktop/src/renderer/components/new-chat/CjkPunctDecoration.ts b/apps/desktop/src/renderer/components/new-chat/CjkPunctDecoration.tsindex 48afb721f2..b73b2bc32a 100644--- a/apps/desktop/src/renderer/components/new-chat/CjkPunctDecoration.ts+++ b/apps/desktop/src/renderer/components/new-chat/CjkPunctDecoration.ts@@ -125,10 +125,10 @@ function listLineRanges(match: matchListPrefix(line.text),}));// ComposerListIndentDecoration switches the entire paragraph to its- // prefix-only fallback when any list row contains an atom or a recognized- // slash pill. In that mode even otherwise-plain sibling rows do not have a- // complete hanging wrapper, so their punctuation must remain available to- // this plugin as well.+ // prefix-only fallback when any list row contains an atom, a recognized+ // slash pill, voice replacement, or CJK punctuation. Its unindented sibling+ // wrapper must remain one inline-block, so those rows opt out of nested+ // punctuation spans and use a Unicode-scoped composite font class instead.const hasFallbackLine = lineMatches.some(({ line, match }) => {if (!match) return false;const overlapsSlashCommandPill = slashCommandMatches.some(@@ -220,10 +220,11 @@ function buildDecorations(const from = pos + m.index;const to = from + m[0].length;if (listRanges.some((range) => from >= range.from && to <= range.to)) {- // ComposerListIndentDecoration owns the wrapping container for list- // rows. An overlapping inline font decoration would split that- // container at every punctuation boundary; list rows opt into the- // same CJK font stack through `.composer-list-cjk-font` instead.+ // ComposerListIndentDecoration owns the wrapping container for these+ // ranges. An overlapping inline font decoration would split fixed-width+ // boxes at punctuation boundaries; marker boxes use+ // `.composer-list-cjk-font`, while unindented fallback rows use the+ // Unicode-scoped `.composer-list-cjk-punctuation-font`.continue;}decorations.push(diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.tsindex d051fcf6e0..3089b169c1 100644--- a/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListIndentDecoration.ts@@ -73,6 +73,10 @@ function listPrefixIndentValues(prefix: string): ListIndentValues {if (char >= '0' && char <= '9') {ch += 1;} else if (char === '\t') {+ // Reserve a full 8ch slot for each tab. The browser's native tab advance+ // is at most 8ch, so this remains safe when an earlier full-width marker+ // contributes `em` that cannot participate in deterministic ch-only+ // tab-stop arithmetic.ch += TAB_SIZE;} else if (char === '、' || char === '\u3000') {em += 1;@@ -247,7 +251,7 @@ export function buildListIndentDecorations(Decoration.inline(contentBase + line.start, contentBase + line.end, {class: ['composer-list-fallback-unindented',- hasCjkPunctuation ? 'composer-list-cjk-font' : '',+ hasCjkPunctuation ? 'composer-list-cjk-punctuation-font' : '',].filter(Boolean).join(' '),@@ -281,6 +285,13 @@ export function buildListIndentDecorations(style: listPrefixIndentStyle(prefix),}),);+ if (LONG_ALPHANUMERIC_BODY_RE.test(body) && !hasTabPrefix) {+ decorations.push(+ Decoration.inline(from + match.prefixLength, to, {+ class: 'composer-list-fallback-long-run-body',+ }),+ );+ }return;}if (LONG_ALPHANUMERIC_BODY_RE.test(body) && !hasTabPrefix) {diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.cssindex 67fa17cdc4..2d4a5cb2e4 100644--- a/apps/desktop/src/renderer/styles/globals.css+++ b/apps/desktop/src/renderer/styles/globals.css@@ -367,16 +367,19 @@ html:lang(ko) {width: 100%;padding-left: calc(1em + var(--composer-list-fallback-indent, 1.25em));overflow-wrap: anywhere;- word-break: break-all;}.ProseMirror .composer-list-fallback-prefix {display: inline-block;box-sizing: border-box;width: var(--composer-list-fallback-indent, 1.25em);margin-left: calc(0px - var(--composer-list-fallback-indent, 1.25em));- white-space: nowrap;+ white-space: pre;vertical-align: top;}+.ProseMirror .composer-list-fallback-long-run-body {+ overflow-wrap: anywhere;+ word-break: break-all;+}.ProseMirror .composer-list-fallback-unindented {display: inline-block;box-sizing: border-box;@@ -403,7 +406,71 @@ html:lang(ko) {/* Keep the browser's tab advance aligned with the deterministic prefix-widthestimate. Do not measure and rewrite decoration DOM from a plugin view:ProseMirror's DOMObserver may restore that style and enter an update loop. */- tab-size: 8;+ tab-size: 8ch;+}+/* The fallback unindented row must remain one inline-block; character-scoped+ ProseMirror decorations would split it into several full-width fragments.+ The local face covers the same ranges as CjkPunctDecoration, while the+ bundled faces cover the punctuation glyphs HarmonyOS actually ships. Latin+ text and digits continue through the configured UI font. Keep the asset+ names aligned with the exact-pinned font package. */+@font-face {+ font-family: 'Cindy CJK Punctuation Local';+ src:+ local('PingFang SC'),+ local('Hiragino Sans GB'),+ local('Microsoft YaHei UI'),+ local('Microsoft YaHei'),+ local('Noto Sans CJK SC'),+ local('Source Han Sans SC');+ font-style: normal;+ font-weight: 400;+ font-display: swap;+ unicode-range: U+3000-303F, U+FF00-FFEF;+}+@font-face {+ font-family: 'Cindy CJK Punctuation Bundled';+ src: url('../../../../../node_modules/harmonyos-sans-sc-webfont-splitted/dist/Regular_256855.woff2')+ format('woff2');+ font-style: normal;+ font-weight: 400;+ font-display: swap;+ unicode-range: U+3001-3002, U+3008-3011, U+3014-3017;+}+@font-face {+ font-family: 'Cindy CJK Punctuation Bundled';+ src: url('../../../../../node_modules/harmonyos-sans-sc-webfont-splitted/dist/Regular_312071.woff2')+ format('woff2');+ font-style: normal;+ font-weight: 400;+ font-display: swap;+ unicode-range:+ U+3000, U+3003, U+3005-3007, U+3012-3013, U+3018-301B, U+301D-301E, U+3021-3029, U+303E;+}+@font-face {+ font-family: 'Cindy CJK Punctuation Bundled';+ src: url('../../../../../node_modules/harmonyos-sans-sc-webfont-splitted/dist/Regular_ac4458.woff2')+ format('woff2');+ font-style: normal;+ font-weight: 400;+ font-display: swap;+ unicode-range:+ U+FF01-FF03, U+FF05-FF0A, U+FF0C-FF1B, U+FF1F-FF3D, U+FF3F, U+FF41-FF5B, U+FF5D, U+FF5F-FF60,+ U+FFE6;+}+@font-face {+ font-family: 'Cindy CJK Punctuation Bundled';+ src: url('../../../../../node_modules/harmonyos-sans-sc-webfont-splitted/dist/Regular_ea8896.woff2')+ format('woff2');+ font-style: normal;+ font-weight: 400;+ font-display: swap;+ unicode-range: U+FF04, U+FF0B, U+FF1C-FF1E, U+FF3E, U+FF40, U+FF5C, U+FF5E, U+FFE0-FFE5;+}+.ProseMirror .composer-list-cjk-punctuation-font {+ font-family:+ 'Cindy CJK Punctuation Local', 'Cindy CJK Punctuation Bundled',+ var(--app-font-ui, var(--app-font-ui-default));}.ProseMirror .composer-list-cjk-font {font-family:diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f0a1c05eb7..f861723e41 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -297,7 +297,7 @@ importers:specifier: ^4.0.3version: 4.0.3harmonyos-sans-sc-webfont-splitted:- specifier: ^1.1.0+ specifier: 1.1.0version: 1.1.0(patch_hash=272e3ded46880ee2f9eaff39327b8465de6df4072271de03caf73950e6e57822)(@emnapi/[email protected])(@emnapi/[email protected])highlight.js:specifier: ^11.11.1