From d7caf47498723c51e53bdab465d08828d7cc1846 Mon Sep 17 00:00:00 2001 From: "pramod.mahajan" Date: Sat, 25 Oct 2025 10:45:52 +0530 Subject: [PATCH] added zoom functionality inside prevw documents --- src/components/Expenses/PreviewDocument.jsx | 132 +++++++++++++++----- 1 file changed, 103 insertions(+), 29 deletions(-) diff --git a/src/components/Expenses/PreviewDocument.jsx b/src/components/Expenses/PreviewDocument.jsx index c9000059..dd756893 100644 --- a/src/components/Expenses/PreviewDocument.jsx +++ b/src/components/Expenses/PreviewDocument.jsx @@ -1,54 +1,128 @@ -import { useState } from "react"; +import { useState, useRef } from "react"; const PreviewDocument = ({ imageUrl }) => { const [loading, setLoading] = useState(true); const [rotation, setRotation] = useState(0); + const [zoom, setZoom] = useState(1); + const [position, setPosition] = useState({ x: 0, y: 0 }); + const [isDragging, setIsDragging] = useState(false); + const [startPos, setStartPos] = useState({ x: 0, y: 0 }); + const containerRef = useRef(null); + + // Zoom handlers + const handleZoomIn = () => setZoom((prev) => Math.min(prev + 0.2, 3)); + const handleZoomOut = () => setZoom((prev) => Math.max(prev - 0.2, 0.5)); + + // Mouse wheel zoom + const handleWheel = (e) => { + e.preventDefault(); + const delta = e.deltaY > 0 ? -0.1 : 0.1; + setZoom((prev) => Math.min(Math.max(prev + delta, 0.5), 3)); + }; + + const handleMouseDown = (e) => { + if (zoom <= 1) return; + setIsDragging(true); + setStartPos({ + x: e.clientX - position.x, + y: e.clientY - position.y, + }); + }; + + const handleMouseMove = (e) => { + if (!isDragging) return; + setPosition({ + x: e.clientX - startPos.x, + y: e.clientY - startPos.y, + }); + }; + + const handleMouseUp = () => setIsDragging(false); + const handleMouseLeave = () => setIsDragging(false); + + const handleReset = () => { + setRotation(0); + setZoom(1); + setPosition({ x: 0, y: 0 }); + }; return ( - <> -
+ <> +
setRotation((prev) => prev + 90)} > + + +
-
- - {loading && ( -
Loading...
- )} -
+
1 ? (isDragging ? "grabbing" : "grab") : "default", + userSelect: "none", + position: "relative", + }} + > + {loading && ( +
+ Loading... +
+ )} Full View setLoading(false)} + style={{ + transform: `translate(${position.x}px, ${position.y}px) rotate(${rotation}deg) scale(${zoom})`, + transition: isDragging ? "none" : "transform 0.3s ease", + objectFit: "contain", + maxWidth: "100%", + maxHeight: "100%", + display: loading ? "none" : "block", + pointerEvents: "none", + }} />
-
+ {/*
-
-
- +
*/} + ); }; export default PreviewDocument; + + +