fix(web): size the chat image slot from server-reported dimensions (#10200)
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
1 parent
7451d17a69
commit
d0f855bfa9
3 files changed
+67
-5
No files matched your search
@@ -1235,9 +1235,15 @@ function markdownImageCopy(alt: string, src: string, title: string | undefined):
|
||||
return ``;
|
||||
}
|
||||
|
||||
/**
|
||||
* `maxHeightRem` folds a height cap into the width bound: `max-height` alone
|
||||
* would not feed back through `aspect-ratio` once `width` is definite, so a
|
||||
* tall image would keep a box wider than the picture it draws.
|
||||
*/
|
||||
function authoredImageSizeStyle(
|
||||
width: string | number | undefined,
|
||||
height: string | number | undefined,
|
||||
maxHeightRem = 30,
|
||||
): CSSProperties | undefined {
|
||||
const parsedWidth = Number(width);
|
||||
const parsedHeight = Number(height);
|
||||
@@ -1248,7 +1254,7 @@ function authoredImageSizeStyle(
|
||||
width: parsedWidth,
|
||||
height: "auto",
|
||||
aspectRatio: `${parsedWidth} / ${parsedHeight}`,
|
||||
maxWidth: `min(100%, 30rem, ${(30 * parsedWidth) / parsedHeight}rem)`,
|
||||
maxWidth: `min(100%, 30rem, ${(maxHeightRem * parsedWidth) / parsedHeight}rem)`,
|
||||
};
|
||||
}
|
||||
if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
|
||||
@@ -1522,6 +1528,8 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
|
||||
readonly srcFragment?: string;
|
||||
/** Reserve a slot while loading; off for images that share a line with text. */
|
||||
readonly standalone?: boolean | undefined;
|
||||
/** Caps the box height in rem while keeping the image's ratio; 30 by default. */
|
||||
readonly maxHeightRem?: number | undefined;
|
||||
readonly style?: CSSProperties | undefined;
|
||||
readonly workspaceRoot?: string | undefined;
|
||||
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
|
||||
@@ -1538,6 +1546,18 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
|
||||
const reference = path ? mediaFileReference(path, props.workspaceRoot) : undefined;
|
||||
const relativePath = reference?.relativePath;
|
||||
const src = assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null;
|
||||
// The server reads the pixel size from the file header, so the slot can be
|
||||
// the image's final box instead of a 16:9 guess. An authored size wins; a
|
||||
// caller's height cap shrinks the box while keeping the ratio.
|
||||
const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
|
||||
const maxHeightRem = props.maxHeightRem ?? 30;
|
||||
const style =
|
||||
props.style ??
|
||||
(knownSize
|
||||
? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
|
||||
: maxHeightRem !== 30
|
||||
? { maxHeight: `${maxHeightRem}rem` }
|
||||
: undefined);
|
||||
const actionsSource: MediaActionSource = {
|
||||
kind: props.kind ?? "image",
|
||||
name: props.alt || (props.kind ?? "image"),
|
||||
@@ -1581,7 +1601,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
|
||||
copyMarkdown={props.copyMarkdown}
|
||||
standalone={props.standalone ?? true}
|
||||
className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}
|
||||
style={props.style}
|
||||
style={style}
|
||||
actionsSource={actionsSource}
|
||||
onImageExpand={props.onImageExpand}
|
||||
/>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
const testState = vi.hoisted(() => ({
|
||||
resources: [] as Array<unknown>,
|
||||
assetState: "success" as "success" | "loading" | "failure",
|
||||
imageDimensions: undefined as { width: number; height: number } | undefined,
|
||||
}));
|
||||
|
||||
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
|
||||
@@ -14,7 +15,11 @@ vi.mock("../assets/assetUrls", () => ({
|
||||
testState.resources.push(resource);
|
||||
if (testState.assetState === "loading") return { _tag: "Loading" };
|
||||
if (testState.assetState === "failure") return { _tag: "Failure" };
|
||||
return { _tag: "Success", url: "https://signed.test/workspace-image.svg" };
|
||||
return {
|
||||
_tag: "Success",
|
||||
url: "https://signed.test/workspace-image.svg",
|
||||
...(testState.imageDimensions ? { imageDimensions: testState.imageDimensions } : {}),
|
||||
};
|
||||
},
|
||||
}));
|
||||
vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
|
||||
@@ -42,7 +47,7 @@ vi.mock("~/lib/openPullRequestLink", () => ({
|
||||
useOpenChangeRequestLink: () => vi.fn(),
|
||||
}));
|
||||
|
||||
import ChatMarkdown from "./ChatMarkdown";
|
||||
import ChatMarkdown, { ChatMarkdownAssetImage } from "./ChatMarkdown";
|
||||
import { FileMarkdownPreview } from "./files/FileMarkdownPreview";
|
||||
|
||||
const threadRef = {
|
||||
@@ -92,6 +97,7 @@ describe("ChatMarkdown workspace images", () => {
|
||||
beforeEach(() => {
|
||||
testState.resources = [];
|
||||
testState.assetState = "success";
|
||||
testState.imageDimensions = undefined;
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -245,6 +251,42 @@ describe("ChatMarkdown workspace images", () => {
|
||||
expect(html).toContain('<span id="user-content-diagram"');
|
||||
});
|
||||
|
||||
it("sizes the slot from server-reported dimensions so a portrait image never grows", () => {
|
||||
testState.imageDimensions = { width: 720, height: 1400 };
|
||||
|
||||
const style = firstInlineStyle(render(""));
|
||||
|
||||
expect(style).toMatchObject({ width: "720px", "aspect-ratio": "720 / 1400" });
|
||||
});
|
||||
|
||||
it("folds a caller's height cap into the width bound so the ratio holds", () => {
|
||||
testState.imageDimensions = { width: 720, height: 1400 };
|
||||
|
||||
const html = renderToStaticMarkup(
|
||||
<ChatMarkdownAssetImage
|
||||
environmentId={threadRef.environmentId}
|
||||
resource={{ _tag: "media-file", threadId: threadRef.threadId, path: "/shot.png" }}
|
||||
alt="shot"
|
||||
maxHeightRem={16}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(firstInlineStyle(html)).toMatchObject({
|
||||
"aspect-ratio": "720 / 1400",
|
||||
"max-width": `min(100%, 30rem, ${(16 * 720) / 1400}rem)`,
|
||||
});
|
||||
});
|
||||
|
||||
it("lets an authored size override server-reported dimensions", () => {
|
||||
testState.imageDimensions = { width: 720, height: 1400 };
|
||||
|
||||
const style = firstInlineStyle(
|
||||
render('<img src=".t3/workspace-image.svg" alt="sized" width="96" height="128">'),
|
||||
);
|
||||
|
||||
expect(style).toMatchObject({ width: "96px", "aspect-ratio": "96 / 128" });
|
||||
});
|
||||
|
||||
it("reserves a slot for an image that is alone in a list item", () => {
|
||||
expect(render("- ")).toContain("aspect-video");
|
||||
});
|
||||
|
||||
@@ -3342,7 +3342,7 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {
|
||||
alt={viewedImage.alt}
|
||||
srcFragment={viewedImage.srcFragment}
|
||||
workspaceRoot={workspaceRoot}
|
||||
style={{ maxHeight: "16rem" }}
|
||||
maxHeightRem={16}
|
||||
onImageExpand={onImageExpand}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user