Skip to content

Commit

Permalink
fix: Implement minor Lightbox component improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
freshavocado7 authored and MoritzWeber0 committed Jun 27, 2024
1 parent d9f707a commit 41bcd95
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions frontend/src/components/Lightbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Printer, XIcon } from 'lucide-react';
import { LightboxButton } from './LightboxButton';

export const Lightbox = ({ onClose, imageSource }) => {
const [adjustedWidth, setAdjustedWidth] = useState('100%');
const [adjustedHeight, setAdjustedHeight] = useState('100%');
const [isClicked, setIsClicked] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -45,27 +43,12 @@ export const Lightbox = ({ onClose, imageSource }) => {
};
}, [onClose]);

useEffect(() => {
if (imageSource) {
const controlBar = document.getElementById('control-bar');
const heightMatch = imageSource.match(/height="(\d+)"/);
const widthMatch = imageSource.match(/width="(\d+)"/);
const imgHeight = heightMatch ? heightMatch[1] : 'defaultHeight';
const imgWidth = widthMatch ? widthMatch[1] : 'defaultWidth';

if (imgHeight + controlBar.clientHeight > window.innerHeight) {
let widthScale = window.innerWidth / imgWidth;
let heightScale =
(window.innerHeight - controlBar.clientHeight - 40) / imgHeight;
const scalingFactor = Math.min(heightScale, widthScale);

let adjustedHeight = imgHeight * scalingFactor;
let adjustedWidth = imgWidth * scalingFactor;
setAdjustedHeight(adjustedHeight);
setAdjustedWidth(adjustedWidth);
}
}
}, [imageSource]);
const parser = new DOMParser();
const doc = parser.parseFromString(imageSource, 'image/svg+xml');
const svg = doc.querySelector('svg');
svg.setAttribute('height', 'calc(100vh - 72px)');
svg.setAttribute('width', '100%');
const modifiedSvgString = new XMLSerializer().serializeToString(doc);

return (
<div className="fixed inset-0 z-50 flex items-center justify-center">
Expand All @@ -88,19 +71,15 @@ export const Lightbox = ({ onClose, imageSource }) => {
</div>
<div className="mt-2 flex h-full w-full overflow-visible pt-16">
{imageSource && (
<TransformWrapper
options={{ limitToBounds: true, centerContent: true }}>
<TransformWrapper wheel={{ smoothStep: 0.005 }}>
<TransformComponent>
<div
dangerouslySetInnerHTML={{ __html: imageSource }}
className={`
select-text overflow-visible
${isClicked ? 'cursor-grabbing' : 'cursor-grab'}`}
onMouseDown={() => setIsClicked(true)}
style={{
width: adjustedWidth,
height: adjustedHeight
}}></div>
dangerouslySetInnerHTML={{ __html: modifiedSvgString }}
className={
'select-text overflow-visible' +
(isClicked ? 'cursor-grabbing' : 'cursor-grab')
}
onMouseDown={() => setIsClicked(true)}></div>
</TransformComponent>
</TransformWrapper>
)}
Expand Down

0 comments on commit 41bcd95

Please sign in to comment.