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): 分享图里的表格不再出现滚动条 (#5551)

Lizi13fb0f138592
diff --git a/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts b/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts
index ee787425db..d0edc3c7e3 100644
--- a/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts
+++ b/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts
@@ -263,38 +263,77 @@ describe('assertShareImageReadableSize', () => {
});
describe('expandScrollableBlocks', () => {
- it('只对实际溢出的候选读取样式并展开', () => {
- const el = root('<div class="fits"></div><div class="wide"></div>');
- const fits = el.querySelector<HTMLElement>('.fits')!;
- const wide = el.querySelector<HTMLElement>('.wide')!;
- Object.defineProperties(fits, {
- scrollWidth: { value: 100 },
- clientWidth: { value: 100 },
- scrollHeight: { value: 20 },
- clientHeight: { value: 20 },
- });
- Object.defineProperties(wide, {
- scrollWidth: { value: 200 },
- clientWidth: { value: 100 },
- scrollHeight: { value: 20 },
- clientHeight: { value: 20 },
+ function withSize(
+ el: HTMLElement,
+ size: { scrollWidth: number; clientWidth: number; scrollHeight: number; clientHeight: number },
+ ): void {
+ Object.defineProperties(el, {
+ scrollWidth: { value: size.scrollWidth },
+ clientWidth: { value: size.clientWidth },
+ scrollHeight: { value: size.scrollHeight },
+ clientHeight: { value: size.clientHeight },
});
- const getComputedStyle = vi.spyOn(window, 'getComputedStyle').mockReturnValue({
- overflowX: 'auto',
- overflowY: 'visible',
- } as CSSStyleDeclaration);
-
+ }
+
+ function expandWithOverflow(
+ el: HTMLElement,
+ overflowByClass: Record<string, { overflowX: string; overflowY: string }>,
+ ): void {
+ const getComputedStyle = vi.spyOn(window, 'getComputedStyle').mockImplementation(
+ (target) =>
+ (overflowByClass[(target as HTMLElement).className] ?? {
+ overflowX: 'visible',
+ overflowY: 'visible',
+ }) as CSSStyleDeclaration,
+ );
try {
expandScrollableBlocks(el);
- expect(getComputedStyle).toHaveBeenCalledTimes(1);
} finally {
getComputedStyle.mockRestore();
}
+ }
+
+ it('没溢出的表格滚动容器也去掉滚动,避免产物里冒出系统滚动条', () => {
+ const el = root('<div class="table-scroll"><table></table></div>');
+ const scroller = el.querySelector<HTMLElement>('.table-scroll')!;
+ withSize(scroller, { scrollWidth: 600, clientWidth: 600, scrollHeight: 120, clientHeight: 120 });
+
+ expandWithOverflow(el, { 'table-scroll': { overflowX: 'auto', overflowY: 'auto' } });
+
+ expect(scroller.style.overflowX).toBe('visible');
+ expect(scroller.style.overflowY).toBe('visible');
+ expect(scroller.style.width).toBe('');
+ expect(scroller.style.maxHeight).toBe('');
+ });
+
+ it('横向溢出时按内容宽度展开,且两轴同时改成 visible', () => {
+ const el = root('<div class="wide"></div><div class="plain"></div>');
+ const wide = el.querySelector<HTMLElement>('.wide')!;
+ const plain = el.querySelector<HTMLElement>('.plain')!;
+ withSize(wide, { scrollWidth: 200, clientWidth: 100, scrollHeight: 20, clientHeight: 20 });
+
+ expandWithOverflow(el, { wide: { overflowX: 'auto', overflowY: 'auto' } });
- expect(fits.style.overflowX).toBe('');
expect(wide.style.overflowX).toBe('visible');
+ expect(wide.style.overflowY).toBe('visible');
expect(wide.style.width).toBe('max-content');
expect(wide.style.maxWidth).toBe('none');
+ expect(wide.style.maxHeight).toBe('');
+ expect(plain.style.overflowX).toBe('');
+ });
+
+ it('纵向溢出时解除固定高度与最大高度', () => {
+ const el = root('<pre class="tall"></pre>');
+ const tall = el.querySelector<HTMLElement>('.tall')!;
+ tall.style.height = '96px';
+ withSize(tall, { scrollWidth: 100, clientWidth: 100, scrollHeight: 400, clientHeight: 96 });
+
+ expandWithOverflow(el, { tall: { overflowX: 'visible', overflowY: 'scroll' } });
+
+ expect(tall.style.overflowY).toBe('visible');
+ expect(tall.style.height).toBe('auto');
+ expect(tall.style.maxHeight).toBe('none');
+ expect(tall.style.width).toBe('');
});
});
diff --git a/apps/desktop/src/renderer/lib/shareConversationImage.ts b/apps/desktop/src/renderer/lib/shareConversationImage.ts
index 4c6a094e38..43421dc5f9 100644
--- a/apps/desktop/src/renderer/lib/shareConversationImage.ts
+++ b/apps/desktop/src/renderer/lib/shareConversationImage.ts
@@ -324,31 +324,50 @@ export function assertShareImageReadableSize(root: HTMLElement): void {
}
}
+function isScrollOverflow(value: string): boolean {
+ return value === 'auto' || value === 'scroll' || value === 'overlay';
+}
+
/**
- * 让横向/纵向可滚动的块在产物里完整展开。
+ * 去掉产物里的所有滚动容器,并让溢出的块完整展开。
*
- * 流内的宽表格、长代码行是 `overflow-x:auto` —— 用户能滚动看全。克隆进图片后
- * 滚动条不存在,右侧内容会被**静默裁掉**:接收方既看不到也不知道被裁了。所以
- * 导出时把这些容器改成按内容宽度展开,图会变宽但信息完整
- * (html-to-image 的画布宽取 `scrollWidth`,底色铺满整张画布,溢出区不会透明)。
+ * 流内的宽表格、长代码行是 `overflow-x:auto` —— 用户能滚动看全。图片里没法滚动,
+ * 溢出内容会被**静默裁掉**,所以溢出的容器按内容宽高展开(图会变宽但信息完整;
+ * html-to-image 的画布宽取 `scrollWidth`,底色铺满整张画布,溢出区不会透明)。
*
+ * 没溢出的滚动容器也要改成 visible:html-to-image 会把 `overflow:auto` 连同固定
+ * 像素宽高一起内联进 SVG,却带不走应用的 `::-webkit-scrollbar` 样式。光栅化时只要
+ * 差出亚像素,就会画出系统默认滚动条(Windows 上尤其明显),表格最常见。
+ *
+ * 先读后写:边改样式边读 scrollWidth 会逐个元素强制重排。
* 必须在克隆体挂进 document **之后**调用 —— getComputedStyle 对游离节点无效。
*/
export function expandScrollableBlocks(root: HTMLElement): void {
+ const scrollers: { el: HTMLElement; widen: boolean; lengthen: boolean }[] = [];
for (const el of Array.from(root.querySelectorAll<HTMLElement>('*'))) {
- const overflowsX = el.scrollWidth > el.clientWidth;
- const overflowsY = el.scrollHeight > el.clientHeight;
- if (!overflowsX && !overflowsY) continue;
-
const style = window.getComputedStyle(el);
- if (overflowsX && (style.overflowX === 'auto' || style.overflowX === 'scroll')) {
- el.style.overflowX = 'visible';
+ const scrollsX = isScrollOverflow(style.overflowX);
+ const scrollsY = isScrollOverflow(style.overflowY);
+ if (!scrollsX && !scrollsY) continue;
+ scrollers.push({
+ el,
+ widen: scrollsX && el.scrollWidth > el.clientWidth,
+ lengthen: scrollsY && el.scrollHeight > el.clientHeight,
+ });
+ }
+
+ for (const { el, widen, lengthen } of scrollers) {
+ // 两轴必须一起改:只要另一轴仍是 auto,visible 会被重新计算成 auto。
+ el.style.overflowX = 'visible';
+ el.style.overflowY = 'visible';
+ if (widen) {
// 只解除 overflow 不够:容器仍被父级宽度约束,内容照旧在边界处截断。
el.style.width = 'max-content';
el.style.maxWidth = 'none';
}
- if (overflowsY && (style.overflowY === 'auto' || style.overflowY === 'scroll')) {
- el.style.overflowY = 'visible';
+ if (lengthen) {
+ // 固定高度的容器解除裁剪后,内容会压到下方兄弟节点上。
+ el.style.height = 'auto';
el.style.maxHeight = 'none';
}
}