diff --git a/e2e/agentMarkdown.spec.ts b/e2e/agentMarkdown.spec.ts index 2a221ffef7..b227e27467 100644 --- a/e2e/agentMarkdown.spec.ts +++ b/e2e/agentMarkdown.spec.ts @@ -214,7 +214,7 @@ test("styles quotes, rules, images and task lists", async ({ page }) => { expect(await css(reply, "blockquote", "border-left-width")).toBe("3px"); expect(await css(reply, "hr", "border-left-width")).toBe("0px"); expect(await css(reply, "hr", "border-top-width")).toBe("1px"); - expect(await css(reply, "img", "max-width")).toBe("100%"); + expect(await css(reply, "img", "max-height")).toBe("360px"); // A checkbox stands in for the bullet. expect(await css(reply, "li:has(> input)", "list-style-type")).toBe("none"); }); @@ -246,6 +246,47 @@ test("loads local reply images, and web ones when asked", async ({ page }) => { expect(fetched).toBe(1); }); +test("fits reply images within the chat without stretching them or enlarging small images", async ({ page }) => { + const [tall, wide, small] = await page.evaluate(() => + [ + [480, 1280], + [1600, 160], + [80, 40], + ].map(([w, h]) => { + const canvas = document.createElement("canvas"); + canvas.width = w!; + canvas.height = h!; + canvas.getContext("2d")!.fillRect(0, 0, w!, h!); + return canvas.toDataURL(); + }), + ); + const reply = await openReply(page, `![Tall](${tall})\n\n![Wide](${wide})\n\n![Small](${small})`); + const image = (name: string) => reply.getByRole("img", { name, exact: true }); + await expect.poll(() => image("Tall").evaluate((img: HTMLImageElement) => img.naturalWidth)).toBe(480); + await expect.poll(() => image("Wide").evaluate((img: HTMLImageElement) => img.naturalWidth)).toBe(1600); + await expect.poll(() => image("Small").evaluate((img: HTMLImageElement) => img.naturalWidth)).toBe(80); + + const sizes = () => + reply.evaluate((el) => ({ + width: el.clientWidth, + images: [...el.querySelectorAll("img")].map((img) => ({ + w: img.getBoundingClientRect().width, + h: img.getBoundingClientRect().height, + ratio: img.naturalWidth / img.naturalHeight, + })), + })); + for (const zoom of [1, 2]) { + await reply.evaluate((el, zoom) => el.style.setProperty("--markdown-zoom-scale", String(zoom)), zoom); + const { width, images } = await sizes(); + for (const { w, h, ratio } of images) { + expect(w).toBeLessThanOrEqual(width + 1); + expect(h).toBeLessThanOrEqual(360); + expect(w / h).toBeCloseTo(ratio, 1); + } + expect(images[2]!.w).toBe(80 * zoom); + } +}); + test("copies reply Markdown with the keyboard and keeps code copying separate", async ({ page, context }) => { await context.grantPermissions(["clipboard-read", "clipboard-write"]); const reply = await openReply(page, REPLY); diff --git a/src/features/agents/Session.module.css b/src/features/agents/Session.module.css index 4a8b17e9d6..385e1f6948 100644 --- a/src/features/agents/Session.module.css +++ b/src/features/agents/Session.module.css @@ -894,11 +894,14 @@ border-top: 1px solid var(--border); } .markdown :global(img) { - max-width: calc(100% * var(--markdown-zoom-scale, 1)); + --image-preview-height: min(360px, 50vh); + max-width: min(100%, var(--image-fit-width, 100%)); + max-height: var(--image-preview-height); + height: auto; + object-fit: contain; } .markdown :global(img[data-natural-width]) { - width: calc(min(var(--image-width), 100%) * var(--markdown-zoom-scale, 1)); - height: auto; + width: calc(var(--image-width) * var(--markdown-zoom-scale, 1)); } .loadImage { padding: 2px 8px; diff --git a/src/features/agents/markdownImages.ts b/src/features/agents/markdownImages.ts index 9592134195..2cb206ae89 100644 --- a/src/features/agents/markdownImages.ts +++ b/src/features/agents/markdownImages.ts @@ -51,22 +51,23 @@ export function loadImages(root: HTMLElement, cwd: string, imagesState: MessageI let alive = true; const held: HTMLButtonElement[] = []; const images = [...root.querySelectorAll("img")]; + const sizeImage = (img: HTMLImageElement, size: { w: number; h: number }) => { + img.style.setProperty("--image-fit-width", `calc(var(--image-preview-height) * ${size.w / size.h})`); + return applyImageZoomWidth(img, size); + }; const bindUrl = (img: HTMLImageElement, url: string, asyncLoad: boolean) => { const svgSize = svgDataUrlSize(url); - if (svgSize && applyImageZoomWidth(img, svgSize) && asyncLoad) imagesState.onLoaded?.(); + if (svgSize && sizeImage(img, svgSize) && asyncLoad) imagesState.onLoaded?.(); img.onload = () => { if (!alive) return; - if (!img.hasAttribute("data-natural-width") && img.naturalWidth > 0 && img.naturalHeight > 0) { - if (applyImageZoomWidth(img, { w: img.naturalWidth, h: img.naturalHeight }) && asyncLoad) { - imagesState.onLoaded?.(); - } - } else if (asyncLoad) { - imagesState.onLoaded?.(); + if (!svgSize && img.naturalWidth > 0 && img.naturalHeight > 0) { + sizeImage(img, { w: img.naturalWidth, h: img.naturalHeight }); } + if (asyncLoad) imagesState.onLoaded?.(); }; img.src = url; - if (!img.hasAttribute("data-natural-width") && img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) { - applyImageZoomWidth(img, { w: img.naturalWidth, h: img.naturalHeight }); + if (!svgSize && img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) { + sizeImage(img, { w: img.naturalWidth, h: img.naturalHeight }); } }; for (const img of images) {