@@ -204,49 +204,31 @@ async function encodeCanvas(
204204 return blob ?. type === mimeType ? blob : null ;
205205}
206206
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 ] ;
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 ) ;
224211 if ( cached ) return cached ;
225212 const thumbnail = ( async ( ) => {
226213 if ( ! canRecompress ( ) ) return null ;
227214 let bitmap : ImageBitmap | undefined ;
228215 try {
229216 bitmap = await createImageBitmap ( file ) ;
230- if ( bitmap . width <= 0 || bitmap . height <= 0 ) return null ;
231217 const side = Math . min ( bitmap . width , bitmap . height ) ;
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 ) ;
218+ if ( side <= 0 ) return null ;
219+ const dimension = Math . min ( 256 , side ) ;
220+ const surface = createCanvas ( dimension , dimension ) ;
239221 if ( ! surface ) return null ;
240222 surface . context . drawImage (
241223 bitmap ,
242- ( bitmap . width - sourceWidth ) / 2 ,
243- ( bitmap . height - sourceHeight ) / 2 ,
244- sourceWidth ,
245- sourceHeight ,
224+ ( bitmap . width - side ) / 2 ,
225+ ( bitmap . height - side ) / 2 ,
226+ side ,
227+ side ,
246228 0 ,
247229 0 ,
248- width ,
249- height ,
230+ dimension ,
231+ dimension ,
250232 ) ;
251233 const blob = await encodeCanvas ( surface . canvas , 1 , "image/png" ) ;
252234 return blob ? await readFileAsDataUrl ( blob ) : null ;
@@ -256,7 +238,7 @@ export function createComposerImageThumbnail(
256238 bitmap ?. close ( ) ;
257239 }
258240 } ) ( ) ;
259- composerThumbnails . set ( file , { ... fileThumbnails , [ mode ] : thumbnail } ) ;
241+ composerThumbnails . set ( file , thumbnail ) ;
260242 return thumbnail ;
261243}
262244
0 commit comments