From a799fc1490924253505850e7266ec43f16272511 Mon Sep 17 00:00:00 2001 From: Wxw-Gu <84499436+Wxw-Gu@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:37:05 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E5=BC=95=E7=94=A8=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/RichMessageBubble.tsx | 10 +- src/renderer/src/styles/chat.scss | 12 ++- tests/component/quote-bubble.test.tsx | 92 +++++++++++++++++++ .../quoted-text-overflow-contract.test.ts | 76 +++++++++++++++ 4 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 tests/component/quote-bubble.test.tsx create mode 100644 tests/unit/quoted-text-overflow-contract.test.ts diff --git a/src/renderer/src/components/RichMessageBubble.tsx b/src/renderer/src/components/RichMessageBubble.tsx index 1372445..8059283 100644 --- a/src/renderer/src/components/RichMessageBubble.tsx +++ b/src/renderer/src/components/RichMessageBubble.tsx @@ -382,12 +382,18 @@ function QuoteBubble({ }): JSX.Element { const quotedText = data.quotedContent || data.content || '[引用消息]' const replyText = data.content || data.title || '' - const quotedSender = data.quotedSender || data.sender || '' + // 空白 sender 不渲染:否则只剩下 `.quoted-sender::after` 的冒号,看着像乱码。 + const quotedSender = String(data.quotedSender || data.sender || '').trim() const hasQuotedImage = Boolean(data.quotedImageMd5 || data.quotedImageDatName) + // 引用框里只放一行(超出用省略号),全文交给 hover 的 title 兜底。 + const quotedTitle = quotedSender ? `${quotedSender}: ${quotedText}` : quotedText return (
-
+
{quotedSender && {quotedSender}} {hasQuotedImage ? ( ({ + getCachedLoadedImage: vi.fn(() => undefined), + requestImage: vi.fn(() => Promise.resolve({ data: '', isThumbnail: true })) +})) + +import { RichMessageBubble } from '../../src/renderer/src/components/RichMessageBubble' + +/** 合成 fixture:正文刻意写长,才能触发引用框的单行截断。名字用仓库通用占位名。 */ +const LONG_QUOTED = [ + '群发言统计', + '以下当前群成员在统计区间内的本地已归档消息中没有发言记录,这段正文刻意写长,用来触发引用框的单行省略', + '1. 张三', + '2. 李四', + '3. 王五' +].join('\n') + +describe('引用气泡 — 截断之外必须能把全文交出来', () => { + it('引用框的 title 带完整引用内容,hover 就能看到被省略的部分', () => { + const { container } = render( + + ) + + const box = container.querySelector('.quoted-message') + expect(box).not.toBeNull() + const title = box!.getAttribute('title') || '' + // 单行省略只是排版,title 里必须是**未截断**的原文 + expect(title).toContain('1. 张三') + expect(title).toContain('3. 王五') + expect(title).not.toContain('…') + }) + + it('有名字时 title 里也带上名字', () => { + const { container } = render( + + ) + + expect(container.querySelector('.quoted-sender')?.textContent).toBe('张三') + expect(container.querySelector('.quoted-message')?.getAttribute('title')).toBe( + `张三: ${LONG_QUOTED}` + ) + }) + + it('空白 sender 不渲染名字,避免只剩一个冒号', () => { + const { container } = render( + + ) + + expect(container.querySelector('.quoted-sender')).toBeNull() + expect(container.querySelector('.quoted-message')?.getAttribute('title')).toBe('被引用') + }) + + it('图片引用同样带 title,且不渲染正文 span', () => { + const { container } = render( + + ) + + const box = container.querySelector('.quoted-message') + expect(box).toHaveClass('quoted-message-image') + expect(box?.getAttribute('title')).toBe('张三: [图片]') + expect(container.querySelector('.quoted-text')).toBeNull() + }) +}) diff --git a/tests/unit/quoted-text-overflow-contract.test.ts b/tests/unit/quoted-text-overflow-contract.test.ts new file mode 100644 index 0000000..a166711 --- /dev/null +++ b/tests/unit/quoted-text-overflow-contract.test.ts @@ -0,0 +1,76 @@ +import { readFileSync } from 'fs' +import { resolve } from 'path' +import { describe, expect, it } from 'vitest' + +/** + * 引用内容(`.quoted-text`)的收缩不变量。 + * + * 被引用的正文常见形态是「多行长文本被折成一行」——TraceMemo 自己发进群里的 + * 发言统计、导出任务日志都是这种。`.quoted-message` 是 flex 容器, + * `.quote-message` 把引用框的宽度上限卡在 320px。 + * + * 于是 `.quoted-text` 一旦写着 `flex: 0 0 auto`,它就会按 max-content 参与布局、 + * 一步都不肯缩:文本先越出引用框,再越出气泡,直接画到聊天背景上。 + * + * jsdom 没有排版引擎,量不出「文本溢出容器」,所以这里锁的是**让溢出不可能发生的 + * 那几条声明**:可收缩(flex-shrink: 1 + min-width: 0)+ 单行省略。 + */ + +const chatStyles = readFileSync(resolve('src/renderer/src/styles/chat.scss'), 'utf8') + +/** 去掉注释,避免注释里的示例声明被当成真声明。 */ +const withoutComments = chatStyles.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^[ \t]*\/\/.*$/gm, '') + +/** 取出顶层选择器的声明块,返回声明文本。 */ +const ruleBody = (css: string, selector: string): string => { + const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + // 必须锚在行首且不留前导空格:`.quoted-message` 既是顶层选择器, + // 也是 `.wechat-message-row { &.mine { … } }` 里嵌着的子选择器的后缀。 + const match = new RegExp(`^${escaped}[ \\t]*\\{`, 'm').exec(css) + if (!match) throw new Error(`selector not found: ${selector}`) + const open = css.indexOf('{', match.index) + const close = css.indexOf('}', open) + if (open < 0 || close < 0) throw new Error(`unterminated rule: ${selector}`) + return css.slice(open + 1, close) +} + +describe('引用气泡 — 引用内容不得撑破引用框', () => { + const body = ruleBody(withoutComments, '.quoted-text') + + it('引用内容必须可收缩', () => { + expect(body, 'flex: 0 0 auto 会让长引用按 max-content 撑破引用框').not.toMatch(/flex:\s*0\s+0/) + expect(body).toMatch(/flex:\s*0\s+1\s+auto/) + expect(body, 'flex 项不给 min-width: 0 就缩不到内容宽度以下').toMatch(/min-width:\s*0/) + }) + + it('引用内容单行省略,与 `.quoted-sender` 和微信的引用样式一致', () => { + expect(body).toMatch(/white-space:\s*nowrap/) + expect(body).toMatch(/overflow:\s*hidden/) + expect(body).toMatch(/text-overflow:\s*ellipsis/) + }) + + it('引用框的宽度上限由容器决定,不受引用正文长短影响', () => { + expect(ruleBody(withoutComments, '.quote-message')).toMatch(/max-width:\s*[\d.]+px/) + expect(ruleBody(withoutComments, '.quoted-message')).toMatch(/max-width:\s*100%/) + }) +}) + +describe('引用气泡 — 名字优先,正文让位', () => { + const body = ruleBody(withoutComments, '.quoted-sender') + + it('名字不参与收缩', () => { + // flex 项按比例收缩:名字一旦可缩,长引用会把它压成半个字(只剩一个冒号) + expect(body, 'flex-shrink: 1 会把名字压成一个字').not.toMatch(/flex:\s*0\s+1/) + expect(body).toMatch(/flex:\s*0\s+0/) + }) + + it('名字有宽度上限,不能把正文挤没', () => { + expect(body).toMatch(/max-width:\s*[\d.]+%/) + }) + + it('名字自带省略号,超长名字自己截断而不是撑破引用框', () => { + expect(body).toMatch(/overflow:\s*hidden/) + expect(body).toMatch(/text-overflow:\s*ellipsis/) + expect(body).toMatch(/white-space:\s*nowrap/) + }) +})