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,14 +65,15 @@ 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">
{!isPDF && (
<>
<i <i
className="bx bx-rotate-right cursor-pointer fs-4" className="bx bx-rotate-right cursor-pointer fs-4"
onClick={() => setRotation((prev) => prev + 90)} onClick={() => setRotation((prev) => prev + 90)}
@ -106,11 +94,12 @@ const PreviewDocument = ({ files = [] }) => {
onClick={resetAll} onClick={resetAll}
title="Reset" title="Reset"
/> />
</>
)}
</div> </div>
</div> </div>
<div <div
// onWheel={handleWheel}
onMouseDown={handleMouseDown} onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove} onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp} onMouseUp={handleMouseUp}
@ -119,14 +108,32 @@ 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>}
{/* PDF VIEW */}
{isPDF ? (
<iframe
src={"./Expenses.pdf"}
title="PDF Preview"
style={{
width: "100%",
height: "70vh",
border: "none",
}}
onLoad={() => setLoading(false)}
onError={(error)=>{
console.log(error)
}}
/>
) : (
/* IMAGE VIEW */
<img <img
src={currentImage?.preSignedUrl} src={fileUrl}
alt="Preview" alt="Preview"
draggable="false" draggable="false"
style={{ style={{
@ -142,11 +149,14 @@ const PreviewDocument = ({ files = [] }) => {
}} }}
onLoad={() => setLoading(false)} 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