@@ -204,31 +204,49 @@ async function encodeCanvas(
204204 return blob ?. type === mimeType ? blob : null ;
205205}
206206
207- const composerThumbnails = new WeakMap < File , Promise < string | null > > ( ) ;
208-
209- export function createComposerImageThumbnail ( file : File ) : Promise < string | null > {
210- const cached = composerThumbnails . get ( file ) ;
207+ const COMPOSER_THUMBNAIL_SIZES = {
208+ square : { width : 256 , height : 256 } ,
209+ // The 208x112 snapshot tile at retina density and 2x app zoom.
210+ snapshot : { width : 832 , height : 448 } ,
211+ } as const ;
212+ export type ComposerImageThumbnailMode = keyof typeof COMPOSER_THUMBNAIL_SIZES ;
213+ const composerThumbnails = new WeakMap <
214+ File ,
215+ Partial < Record < ComposerImageThumbnailMode , Promise < string | null > > >
216+ > ( ) ;
217+
218+ export function createComposerImageThumbnail (
219+ file : File ,
220+ mode : ComposerImageThumbnailMode = "square" ,
221+ ) : Promise < string | null > {
222+ const fileThumbnails = composerThumbnails . get ( file ) ;
223+ const cached = fileThumbnails ?. [ mode ] ;
211224 if ( cached ) return cached ;
212225 const thumbnail = ( async ( ) => {
213226 if ( ! canRecompress ( ) ) return null ;
214227 let bitmap : ImageBitmap | undefined ;
215228 try {
216229 bitmap = await createImageBitmap ( file ) ;
230+ if ( bitmap . width <= 0 || bitmap . height <= 0 ) return null ;
217231 const side = Math . min ( bitmap . width , bitmap . height ) ;
218- if ( side <= 0 ) return null ;
219- const dimension = Math . min ( 256 , side ) ;
220- const surface = createCanvas ( dimension , dimension ) ;
232+ const sourceWidth = mode === "square" ? side : bitmap . width ;
233+ const sourceHeight = mode === "square" ? side : bitmap . height ;
234+ const bounds = COMPOSER_THUMBNAIL_SIZES [ mode ] ;
235+ const scale = Math . min ( 1 , bounds . width / sourceWidth , bounds . height / sourceHeight ) ;
236+ const width = Math . max ( 1 , Math . round ( sourceWidth * scale ) ) ;
237+ const height = Math . max ( 1 , Math . round ( sourceHeight * scale ) ) ;
238+ const surface = createCanvas ( width , height ) ;
221239 if ( ! surface ) return null ;
222240 surface . context . drawImage (
223241 bitmap ,
224- ( bitmap . width - side ) / 2 ,
225- ( bitmap . height - side ) / 2 ,
226- side ,
227- side ,
242+ ( bitmap . width - sourceWidth ) / 2 ,
243+ ( bitmap . height - sourceHeight ) / 2 ,
244+ sourceWidth ,
245+ sourceHeight ,
228246 0 ,
229247 0 ,
230- dimension ,
231- dimension ,
248+ width ,
249+ height ,
232250 ) ;
233251 const blob = await encodeCanvas ( surface . canvas , 1 , "image/png" ) ;
234252 return blob ? await readFileAsDataUrl ( blob ) : null ;
@@ -238,7 +256,7 @@ export function createComposerImageThumbnail(file: File): Promise<string | null>
238256 bitmap ?. close ( ) ;
239257 }
240258 } ) ( ) ;
241- composerThumbnails . set ( file , thumbnail ) ;
259+ composerThumbnails . set ( file , { ... fileThumbnails , [ mode ] : thumbnail } ) ;
242260 return thumbnail ;
243261}
244262
0 commit comments