Skip to content

Commit 89c3c1b

Browse files
committed
fix(mobile): preserve markdown image dimensions
1 parent 4e00471 commit 89c3c1b

3 files changed

Lines changed: 191 additions & 45 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import {
105105
import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
106106
import { useAssetUrl, useAssetUrlState } from "../../state/assets";
107107
import { resolveWorkspaceRelativeFilePath } from "../files/filePath";
108+
import { MARKDOWN_IMAGE_MAX_WIDTH, resolveMarkdownImageDisplaySize } from "./markdownImageSize";
108109

109110
const WIDE_MARKDOWN_BLOCK_OPTIONS = {
110111
includeOrderedLists: Platform.OS === "android",
@@ -196,30 +197,43 @@ function MessageAttachmentImage(props: {
196197
);
197198
}
198199

199-
/** Markdown image whose src is a workspace file — loads through a signed asset URL. */
200-
function ThreadMarkdownImage(props: {
201-
readonly environmentId: EnvironmentId;
202-
readonly threadId: ThreadId;
203-
readonly path: string;
200+
function ThreadMarkdownImageView(props: {
201+
readonly uri: string | null;
202+
readonly unavailable: boolean;
204203
readonly alt: string | null;
205204
readonly onPressImage: (uri: string) => void;
206205
}) {
207206
const codeBackground = useThemeColor("--color-md-code-bg");
207+
const [availableWidth, setAvailableWidth] = useState(0);
208+
const [sourceSize, setSourceSize] = useState<{ width: number; height: number } | null>(null);
208209
const [failedUri, setFailedUri] = useState<string | null>(null);
209-
const assetUrl = useAssetUrlState(props.environmentId, {
210-
_tag: "workspace-file",
211-
threadId: props.threadId,
212-
path: props.path,
213-
});
214-
const uri = assetUrl._tag === "Success" ? assetUrl.url : null;
215-
const failed = assetUrl._tag === "Failure" || (uri !== null && failedUri === uri);
210+
211+
useEffect(() => {
212+
setSourceSize(null);
213+
setFailedUri(null);
214+
}, [props.uri]);
215+
216+
const displaySize =
217+
sourceSize === null
218+
? null
219+
: resolveMarkdownImageDisplaySize({
220+
sourceWidth: sourceSize.width,
221+
sourceHeight: sourceSize.height,
222+
availableWidth,
223+
});
224+
const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);
225+
const placeholderWidth =
226+
availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
216227

217228
return (
218-
<View style={{ gap: 6 }}>
219-
{uri === null || failed ? (
229+
<View
230+
onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}
231+
style={{ alignSelf: "stretch", gap: 6 }}
232+
>
233+
{props.uri === null || failed ? (
220234
<View
221235
style={{
222-
width: "100%",
236+
width: placeholderWidth,
223237
aspectRatio: 16 / 9,
224238
borderRadius: 10,
225239
backgroundColor: codeBackground,
@@ -238,20 +252,36 @@ function ThreadMarkdownImage(props: {
238252
accessibilityRole="imagebutton"
239253
accessibilityLabel={props.alt ?? "Markdown image"}
240254
activeOpacity={0.7}
241-
onPress={() => props.onPressImage(uri)}
255+
onPress={() => props.onPressImage(props.uri!)}
256+
style={{ alignSelf: "flex-start" }}
242257
>
243-
<Image
244-
source={{ uri }}
245-
resizeMode="contain"
246-
accessible={false}
247-
onError={() => setFailedUri(uri)}
258+
<View
248259
style={{
249-
width: "100%",
250-
aspectRatio: 16 / 9,
260+
...(displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 }),
251261
borderRadius: 10,
252262
backgroundColor: codeBackground,
263+
alignItems: "center",
264+
justifyContent: "center",
265+
overflow: "hidden",
253266
}}
254-
/>
267+
>
268+
<Image
269+
source={{ uri: props.uri }}
270+
resizeMode="contain"
271+
accessible={false}
272+
onLoad={(event) => {
273+
const { width, height } = event.nativeEvent.source;
274+
setSourceSize({ width, height });
275+
}}
276+
onError={() => setFailedUri(props.uri)}
277+
style={{
278+
width: "100%",
279+
height: "100%",
280+
opacity: displaySize === null ? 0 : 1,
281+
}}
282+
/>
283+
{displaySize === null ? <ActivityIndicator style={StyleSheet.absoluteFill} /> : null}
284+
</View>
255285
</TouchableOpacity>
256286
)}
257287
{props.alt ? (
@@ -263,28 +293,38 @@ function ThreadMarkdownImage(props: {
263293
);
264294
}
265295

296+
/** Markdown image whose src is a workspace file — loads through a signed asset URL. */
297+
function ThreadMarkdownImage(props: {
298+
readonly environmentId: EnvironmentId;
299+
readonly threadId: ThreadId;
300+
readonly path: string;
301+
readonly alt: string | null;
302+
readonly onPressImage: (uri: string) => void;
303+
}) {
304+
const assetUrl = useAssetUrlState(props.environmentId, {
305+
_tag: "workspace-file",
306+
threadId: props.threadId,
307+
path: props.path,
308+
});
309+
310+
return (
311+
<ThreadMarkdownImageView
312+
uri={assetUrl._tag === "Success" ? assetUrl.url : null}
313+
unavailable={assetUrl._tag === "Failure"}
314+
alt={props.alt}
315+
onPressImage={props.onPressImage}
316+
/>
317+
);
318+
}
319+
266320
function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
267-
const codeBackground = useThemeColor("--color-md-code-bg");
268321
return (
269-
<View style={{ gap: 6 }}>
270-
<View
271-
style={{
272-
width: "100%",
273-
aspectRatio: 16 / 9,
274-
borderRadius: 10,
275-
backgroundColor: codeBackground,
276-
alignItems: "center",
277-
justifyContent: "center",
278-
}}
279-
>
280-
<Text className="text-xs text-foreground-muted">Image unavailable</Text>
281-
</View>
282-
{props.alt ? (
283-
<Text selectable className="text-xs text-foreground-muted">
284-
{props.alt}
285-
</Text>
286-
) : null}
287-
</View>
322+
<ThreadMarkdownImageView
323+
uri={null}
324+
unavailable
325+
alt={props.alt}
326+
onPressImage={() => undefined}
327+
/>
288328
);
289329
}
290330

@@ -1530,7 +1570,14 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
15301570
(image) => {
15311571
const imageSource = classifyMarkdownImageSource(image.href, props.workspaceRoot ?? null);
15321572
if (imageSource._tag === "Direct") {
1533-
return null;
1573+
return (
1574+
<ThreadMarkdownImageView
1575+
uri={imageSource.uri}
1576+
unavailable={false}
1577+
alt={image.alt}
1578+
onPressImage={(uri) => setExpandedImage({ uri })}
1579+
/>
1580+
);
15341581
}
15351582
if (imageSource._tag === "Blocked") {
15361583
return <ThreadMarkdownImageUnavailable alt={image.alt} />;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import {
4+
MARKDOWN_IMAGE_MAX_HEIGHT,
5+
MARKDOWN_IMAGE_MAX_WIDTH,
6+
resolveMarkdownImageDisplaySize,
7+
} from "./markdownImageSize";
8+
9+
describe("resolveMarkdownImageDisplaySize", () => {
10+
it("keeps small images at their intrinsic size", () => {
11+
expect(
12+
resolveMarkdownImageDisplaySize({
13+
sourceWidth: 96,
14+
sourceHeight: 96,
15+
availableWidth: 332,
16+
}),
17+
).toEqual({ width: 96, height: 96 });
18+
});
19+
20+
it("fits wide images to the available chat width", () => {
21+
expect(
22+
resolveMarkdownImageDisplaySize({
23+
sourceWidth: 960,
24+
sourceHeight: 540,
25+
availableWidth: 332,
26+
}),
27+
).toEqual({ width: 332, height: 186.75 });
28+
});
29+
30+
it("caps wide images at 480 points on larger screens", () => {
31+
expect(
32+
resolveMarkdownImageDisplaySize({
33+
sourceWidth: 960,
34+
sourceHeight: 540,
35+
availableWidth: 900,
36+
}),
37+
).toEqual({ width: MARKDOWN_IMAGE_MAX_WIDTH, height: 270 });
38+
});
39+
40+
it("caps tall images by height without changing their aspect ratio", () => {
41+
expect(
42+
resolveMarkdownImageDisplaySize({
43+
sourceWidth: 400,
44+
sourceHeight: 800,
45+
availableWidth: 332,
46+
}),
47+
).toEqual({ width: 240, height: MARKDOWN_IMAGE_MAX_HEIGHT });
48+
});
49+
50+
it("rejects dimensions that cannot produce a stable layout", () => {
51+
expect(
52+
resolveMarkdownImageDisplaySize({ sourceWidth: 0, sourceHeight: 100, availableWidth: 332 }),
53+
).toBeNull();
54+
expect(
55+
resolveMarkdownImageDisplaySize({
56+
sourceWidth: 100,
57+
sourceHeight: Number.NaN,
58+
availableWidth: 332,
59+
}),
60+
).toBeNull();
61+
});
62+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const MARKDOWN_IMAGE_MAX_WIDTH = 480;
2+
export const MARKDOWN_IMAGE_MAX_HEIGHT = 480;
3+
4+
export interface MarkdownImageDisplaySize {
5+
readonly width: number;
6+
readonly height: number;
7+
}
8+
9+
/** Keeps small images intrinsic while fitting larger images inside the chat viewport. */
10+
export function resolveMarkdownImageDisplaySize(input: {
11+
readonly sourceWidth: number;
12+
readonly sourceHeight: number;
13+
readonly availableWidth: number;
14+
}): MarkdownImageDisplaySize | null {
15+
if (
16+
!Number.isFinite(input.sourceWidth) ||
17+
!Number.isFinite(input.sourceHeight) ||
18+
!Number.isFinite(input.availableWidth) ||
19+
input.sourceWidth <= 0 ||
20+
input.sourceHeight <= 0 ||
21+
input.availableWidth <= 0
22+
) {
23+
return null;
24+
}
25+
26+
const scale = Math.min(
27+
1,
28+
input.availableWidth / input.sourceWidth,
29+
MARKDOWN_IMAGE_MAX_WIDTH / input.sourceWidth,
30+
MARKDOWN_IMAGE_MAX_HEIGHT / input.sourceHeight,
31+
);
32+
33+
return {
34+
width: input.sourceWidth * scale,
35+
height: input.sourceHeight * scale,
36+
};
37+
}

0 commit comments

Comments
 (0)