style: 引用消息文本溢出

This commit is contained in:
Wxw-Gu committed 2026-10-15 15:37:05 +08:00
1 parent 0d78ad3f54
commit a799fc1490
4 files changed
+186 -4

No files matched your search

@@ -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 (
<div className="quote-message">
<div className={`quoted-message${hasQuotedImage ? ' quoted-message-image' : ''}`}>
<div
className={`quoted-message${hasQuotedImage ? ' quoted-message-image' : ''}`}
title={quotedTitle}
>
{quotedSender && <span className="quoted-sender">{quotedSender}</span>}
{hasQuotedImage ? (
<ImageBubble
+10 -2
View File
@@ -1739,8 +1739,11 @@
}
.quoted-sender {
flex: 0 1 auto;
// 名字优先:两个 flex 项都按比例收缩时,长引用会把名字压成半个字(只剩一个冒号),
// 所以名字固定不参与收缩,只用 max-width 限制它最多占六成,剩下的留给正文。
flex: 0 0 auto;
min-width: 0;
max-width: 60%;
font-weight: 600;
overflow: hidden;
text-overflow: ellipsis;
@@ -1752,7 +1755,12 @@
}
.quoted-text {
flex: 0 0 auto;
// 收缩全部落在正文上(名字已固定),单行省略;全文由引用框的 title 兜底。
flex: 0 1 auto;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.quote-reply {
+92
View File
@@ -0,0 +1,92 @@
import { render } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
// 图片引用会挂载 ImageBubble,这里不需要真的去取图。
vi.mock('../../src/renderer/src/components/image-loader', () => ({
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(
<RichMessageBubble
contentData={{ type: 'quote', content: '👍👍', quotedContent: LONG_QUOTED }}
/>
)
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(
<RichMessageBubble
contentData={{
type: 'quote',
content: '👍👍',
quotedSender: '张三',
quotedContent: LONG_QUOTED
}}
/>
)
expect(container.querySelector('.quoted-sender')?.textContent).toBe('张三')
expect(container.querySelector('.quoted-message')?.getAttribute('title')).toBe(
`张三: ${LONG_QUOTED}`
)
})
it('空白 sender 不渲染名字,避免只剩一个冒号', () => {
const { container } = render(
<RichMessageBubble
contentData={{
type: 'quote',
content: '好的',
quotedSender: ' ',
quotedContent: '被引用'
}}
/>
)
expect(container.querySelector('.quoted-sender')).toBeNull()
expect(container.querySelector('.quoted-message')?.getAttribute('title')).toBe('被引用')
})
it('图片引用同样带 title,且不渲染正文 span', () => {
const { container } = render(
<RichMessageBubble
contentData={{
type: 'quote',
content: '这张图',
quotedSender: '张三',
quotedContent: '[图片]',
quotedImageMd5: 'fixture-image',
quotedImageDatName: 'fixture.dat'
}}
sessionId="fixture-session"
/>
)
const box = container.querySelector('.quoted-message')
expect(box).toHaveClass('quoted-message-image')
expect(box?.getAttribute('title')).toBe('张三: [图片]')
expect(container.querySelector('.quoted-text')).toBeNull()
})
})
@@ -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/)
})
})