allow view pdf file

This commit is contained in:
pramod.mahajan 2025-12-05 11:38:54 +05:30
parent 427d728efc
commit 25bb7d1f58
2 changed files with 75 additions and 67 deletions

View File

@ -1,3 +1,4 @@
import { error } from "pdf-lib";
import { useState, useRef, useEffect } from "react"; import { useState, useRef, useEffect } from "react";
const PreviewDocument = ({ files = [] }) => { const PreviewDocument = ({ files = [] }) => {
@ -15,9 +16,11 @@ const PreviewDocument = ({ files = [] }) => {
const MIN_ZOOM = 0.4; const MIN_ZOOM = 0.4;
const MAX_ZOOM = 3; const MAX_ZOOM = 3;
const currentImage = images[index]; const currentFile = images[index];
const fileUrl = currentFile?.preSignedUrl;
const isPDF = fileUrl?.toLowerCase().endsWith(".pdf");
// Reset on image change
useEffect(() => { useEffect(() => {
setRotation(0); setRotation(0);
setScale(1); setScale(1);
@ -25,8 +28,8 @@ const PreviewDocument = ({ files = [] }) => {
setLoading(true); setLoading(true);
}, [index]); }, [index]);
const zoomIn = () => setScale((prev) => Math.min(prev + 0.2, MAX_ZOOM)); const zoomIn = () => !isPDF && setScale((prev) => Math.min(prev + 0.2, MAX_ZOOM));
const zoomOut = () => setScale((prev) => Math.max(prev - 0.2, MIN_ZOOM)); const zoomOut = () => !isPDF && setScale((prev) => Math.max(prev - 0.2, MIN_ZOOM));
const resetAll = () => { const resetAll = () => {
setRotation(0); setRotation(0);
@ -42,24 +45,8 @@ const PreviewDocument = ({ files = [] }) => {
if (index > 0) setIndex((i) => i - 1); if (index > 0) setIndex((i) => i - 1);
}; };
const handleWheel = (e) => {
e.preventDefault();
if (e.ctrlKey) {
const delta = e.deltaY > 0 ? -0.1 : 0.1;
setScale((prev) => {
let next = prev + delta;
if (next < MIN_ZOOM) next = MIN_ZOOM;
if (next > MAX_ZOOM) next = MAX_ZOOM;
return next;
});
} else {
if (e.deltaY > 0) nextImage();
else prevImage();
}
};
const handleMouseDown = (e) => { const handleMouseDown = (e) => {
if (isPDF) return;
setDragging(true); setDragging(true);
startPos.current = { startPos.current = {
x: e.clientX - position.x, x: e.clientX - position.x,
@ -68,7 +55,7 @@ const PreviewDocument = ({ files = [] }) => {
}; };
const handleMouseMove = (e) => { const handleMouseMove = (e) => {
if (!dragging) return; if (!dragging || isPDF) return;
setPosition({ setPosition({
x: e.clientX - startPos.current.x, x: e.clientX - startPos.current.x,
@ -78,39 +65,41 @@ const PreviewDocument = ({ files = [] }) => {
const handleMouseUp = () => setDragging(false); const handleMouseUp = () => setDragging(false);
const handleDoubleClick = () => resetAll(); const handleDoubleClick = () => !isPDF && resetAll();
return ( return (
<> <>
{/* Top Controls */} {/* Controls */}
<div className="d-flex justify-content-start align-items-center mb-2"> <div className="d-flex justify-content-start align-items-center mb-2">
{/* Left */}
<div className="d-flex gap-3"> <div className="d-flex gap-3">
<i {!isPDF && (
className="bx bx-rotate-right cursor-pointer fs-4" <>
onClick={() => setRotation((prev) => prev + 90)} <i
title="Rotate" className="bx bx-rotate-right cursor-pointer fs-4"
/> onClick={() => setRotation((prev) => prev + 90)}
<i title="Rotate"
className="bx bx-zoom-in cursor-pointer fs-4" />
onClick={zoomIn} <i
title="Zoom In" className="bx bx-zoom-in cursor-pointer fs-4"
/> onClick={zoomIn}
<i title="Zoom In"
className="bx bx-zoom-out cursor-pointer fs-4" />
onClick={zoomOut} <i
title="Zoom Out" className="bx bx-zoom-out cursor-pointer fs-4"
/> onClick={zoomOut}
<i title="Zoom Out"
className="bx bx-reset cursor-pointer fs-4" />
onClick={resetAll} <i
title="Reset" className="bx bx-reset cursor-pointer fs-4"
/> onClick={resetAll}
title="Reset"
/>
</>
)}
</div> </div>
</div> </div>
<div <div
// onWheel={handleWheel}
onMouseDown={handleMouseDown} onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove} onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp} onMouseUp={handleMouseUp}
@ -119,34 +108,55 @@ const PreviewDocument = ({ files = [] }) => {
className="position-relative d-flex justify-content-center align-items-center bg-light-secondary overflow-hidden" className="position-relative d-flex justify-content-center align-items-center bg-light-secondary overflow-hidden"
style={{ style={{
minHeight: "70vh", minHeight: "70vh",
userSelect: "none", userSelect: "none",
borderRadius: "10px", borderRadius: "10px",
}} }}
> >
{loading && <div className="text-secondary">Loading...</div>} {loading && <div className="text-secondary">Loading...</div>}
<img {/* PDF VIEW */}
src={currentImage?.preSignedUrl} {isPDF ? (
alt="Preview" <iframe
draggable="false" src={"./Expenses.pdf"}
style={{ title="PDF Preview"
maxHeight: "60vh", style={{
display: loading ? "none" : "block", width: "100%",
transform: ` height: "70vh",
translate(${position.x}px, ${position.y}px) border: "none",
scale(${scale}) }}
rotate(${rotation}deg) onLoad={() => setLoading(false)}
`, onError={(error)=>{
transition: dragging ? "none" : "transform 0.2s ease", console.log(error)
cursor: dragging ? "grabbing" : "grab", }}
}} />
onLoad={() => setLoading(false)} ) : (
/> /* IMAGE VIEW */
<img
src={fileUrl}
alt="Preview"
draggable="false"
style={{
maxHeight: "60vh",
display: loading ? "none" : "block",
transform: `
translate(${position.x}px, ${position.y}px)
scale(${scale})
rotate(${rotation}deg)
`,
transition: dragging ? "none" : "transform 0.2s ease",
cursor: dragging ? "grabbing" : "grab",
}}
onLoad={() => setLoading(false)}
/>
)}
</div> </div>
<div className="d-flex justify-content-between"> <div className="d-flex justify-content-between">
<div className="text-center text-muted mt-2 small"> <div className="text-center text-muted mt-2 small">
Scroll = change image | Double click = reset Scroll = change file | Double click = reset (images only)
</div> </div>
<div className="d-flex align-items-center gap-2"> <div className="d-flex align-items-center gap-2">
<i <i

View File

@ -390,12 +390,10 @@ const tdsPercentage = Number(watch("tdsPercentage")) || 0;
key={doc.documentId} key={doc.documentId}
className="d-flex align-items-center cusor-pointer" className="d-flex align-items-center cusor-pointer"
onClick={() => { onClick={() => {
if (isImage) {
setDocumentView({ setDocumentView({
IsOpen: true, IsOpen: true,
Images: data?.documents, Images: data?.documents,
}); });
}
}} }}
> >
<i <i