@@ -105,6 +105,7 @@ import {
105105import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState" ;
106106import { useAssetUrl , useAssetUrlState } from "../../state/assets" ;
107107import { resolveWorkspaceRelativeFilePath } from "../files/filePath" ;
108+ import { MARKDOWN_IMAGE_MAX_WIDTH , resolveMarkdownImageDisplaySize } from "./markdownImageSize" ;
108109
109110const 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+
266320function 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 } /> ;
0 commit comments