From 0626a7aea11b23805b1f1e99b3c594e1b1575635 Mon Sep 17 00:00:00 2001 From: CJ <72030708+cj12312021@users.noreply.github.com> Date: Tue, 5 Mar 2024 03:37:39 +0100 Subject: [PATCH] fix lightbox display modes (#4644) --- ui/v2.5/src/hooks/Lightbox/LightboxImage.tsx | 68 ++++++++++++++------ 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/ui/v2.5/src/hooks/Lightbox/LightboxImage.tsx b/ui/v2.5/src/hooks/Lightbox/LightboxImage.tsx index 9581c9bd9..0f2b9df8a 100644 --- a/ui/v2.5/src/hooks/Lightbox/LightboxImage.tsx +++ b/ui/v2.5/src/hooks/Lightbox/LightboxImage.tsx @@ -98,8 +98,11 @@ export const LightboxImage: React.FC = ({ const [moving, setMoving] = useState(false); const [positionX, setPositionX] = useState(0); const [positionY, setPositionY] = useState(0); + const [imageWidth, setImageWidth] = useState(width); + const [imageHeight, setImageHeight] = useState(height); const [boxWidth, setBoxWidth] = useState(0); const [boxHeight, setBoxHeight] = useState(0); + const dimensionsProvided = width > 0 && height > 0; const mouseDownEvent = useRef(); const resetPositionRef = useRef(resetPosition); @@ -137,41 +140,62 @@ export const LightboxImage: React.FC = ({ }, 250); }, [container]); + useEffect(() => { + if (dimensionsProvided) { + return; + } + let mounted = true; + const img = new Image(); + function onLoad() { + if (mounted) { + setImageWidth(img.width); + setImageHeight(img.height); + } + } + + img.onload = onLoad; + img.src = src; + + return () => { + mounted = false; + }; + }, [src, dimensionsProvided]); + const minMaxY = useCallback( (appliedZoom: number) => { let minY, maxY: number; - const inBounds = appliedZoom * height <= boxHeight; + const inBounds = appliedZoom * imageHeight <= boxHeight; // NOTE: I don't even know how these work, but they do if (!inBounds) { - if (height > boxHeight) { + if (imageHeight > boxHeight) { minY = - (appliedZoom * height - height) / 2 - - appliedZoom * height + + (appliedZoom * imageHeight - imageHeight) / 2 - + appliedZoom * imageHeight + boxHeight; - maxY = (appliedZoom * height - height) / 2; + maxY = (appliedZoom * imageHeight - imageHeight) / 2; } else { - minY = (boxHeight - appliedZoom * height) / 2; - maxY = (appliedZoom * height - boxHeight) / 2; + minY = (boxHeight - appliedZoom * imageHeight) / 2; + maxY = (appliedZoom * imageHeight - boxHeight) / 2; } } else { - minY = Math.min((boxHeight - height) / 2, 0); + minY = Math.min((boxHeight - imageHeight) / 2, 0); maxY = minY; } return [minY, maxY]; }, - [height, boxHeight] + [imageHeight, boxHeight] ); const calculateInitialPosition = useCallback( (appliedZoom: number) => { // Center image from container's center - const newPositionX = Math.min((boxWidth - width) / 2, 0); + const newPositionX = Math.min((boxWidth - imageWidth) / 2, 0); let newPositionY: number; if (displayMode === GQL.ImageLightboxDisplayMode.FitXy) { - newPositionY = Math.min((boxHeight - height) / 2, 0); + newPositionY = Math.min((boxHeight - imageHeight) / 2, 0); } else { // otherwise, align image with container const [minY, maxY] = minMaxY(appliedZoom); @@ -184,16 +208,24 @@ export const LightboxImage: React.FC = ({ return [newPositionX, newPositionY]; }, - [displayMode, boxWidth, width, boxHeight, height, alignBottom, minMaxY] + [ + displayMode, + boxWidth, + imageWidth, + boxHeight, + imageHeight, + alignBottom, + minMaxY, + ] ); useEffect(() => { // don't set anything until we have the dimensions - if (!width || !height || !boxWidth || !boxHeight) { + if (!imageWidth || !imageHeight || !boxWidth || !boxHeight) { return; } - if (!scaleUp && width < boxWidth && height < boxHeight) { + if (!scaleUp && imageWidth < boxWidth && imageHeight < boxHeight) { setDefaultZoom(1); setPositionX(0); setPositionY(0); @@ -202,8 +234,8 @@ export const LightboxImage: React.FC = ({ // set initial zoom level based on options const newZoom = calculateDefaultZoom( - width, - height, + imageWidth, + imageHeight, boxWidth, boxHeight, displayMode, @@ -223,8 +255,8 @@ export const LightboxImage: React.FC = ({ scrollAttempts.current = -scrollAttemptsBeforeChange; } }, [ - width, - height, + imageWidth, + imageHeight, boxWidth, boxHeight, displayMode,