From c888f13c606f1d902fb781f16a36c5796d0e1297 Mon Sep 17 00:00:00 2001 From: Fedor Katurov <f.katurov@rogii.com> Date: Wed, 14 Dec 2022 09:43:01 +0600 Subject: [PATCH] fixed image preloading on PhotoSwipe modal --- src/containers/dialogs/PhotoSwipe/index.tsx | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/containers/dialogs/PhotoSwipe/index.tsx b/src/containers/dialogs/PhotoSwipe/index.tsx index a66ed514..a0437ec3 100644 --- a/src/containers/dialogs/PhotoSwipe/index.tsx +++ b/src/containers/dialogs/PhotoSwipe/index.tsx @@ -28,26 +28,38 @@ const PhotoSwipe: VFC<PhotoSwipeProps> = observer(({ index, items }) => { new Promise(async (resolve) => { const images = await Promise.all( items.map( - (image) => - new Promise((resolveImage) => { + (file) => + new Promise((resolve) => { + const src = getURL( + file, + isTablet ? imagePresets[900] : imagePresets[1600], + ); + + if (file.metadata?.width && file.metadata.height) { + resolve({ + src, + w: file.metadata.width, + h: file.metadata.height, + }); + + return; + } + const img = new Image(); img.onload = () => { - resolveImage({ - src: getURL( - image, - isTablet ? imagePresets[900] : imagePresets[1600], - ), + resolve({ + src, h: img.naturalHeight, w: img.naturalWidth, }); }; img.onerror = () => { - resolveImage({}); + resolve({}); }; - img.src = getURL(image, imagePresets[1600]); + img.src = getURL(file, imagePresets[1600]); }), ), );