Spaces:
Sleeping
Sleeping
File size: 12,675 Bytes
9ded9e1 a706099 9ded9e1 4a6c290 9ded9e1 4a6c290 9ded9e1 4a6c290 9ded9e1 4a6c290 9ded9e1 a706099 9ded9e1 a706099 9ded9e1 a706099 9ded9e1 a706099 9ded9e1 4a6c290 9ded9e1 a706099 9ded9e1 a706099 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
import { useState, useRef, useEffect } from 'react';
import { Document, Page, pdfjs } from 'react-pdf';
import 'react-pdf/dist/Page/AnnotationLayer.css';
import 'react-pdf/dist/Page/TextLayer.css';
pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js';
const DocumentViewer = ({ selectedFile, documentData, onPageChange }) => {
const pdfContainerRef = useRef(null);
const [numPages, setNumPages] = useState(null);
const [currentPage, setCurrentPage] = useState(1);
const [zoomLevel, setZoomLevel] = useState(1);
const [visiblePages, setVisiblePages] = useState(new Set([1]));
const [containerWidth, setContainerWidth] = useState(0);
// Update container width on mount and resize
useEffect(() => {
const updateContainerWidth = () => {
if (pdfContainerRef.current) {
const width = pdfContainerRef.current.clientWidth;
// Subtract padding and some margin for optimal viewing
const availableWidth = width - 32; // 16px padding on each side
setContainerWidth(availableWidth);
}
};
updateContainerWidth();
window.addEventListener('resize', updateContainerWidth);
return () => window.removeEventListener('resize', updateContainerWidth);
}, []);
// Expose goToPage function to parent component (only once when component mounts)
useEffect(() => {
if (onPageChange) {
onPageChange({ goToPage });
}
}, [onPageChange]);
// Calculate optimal page width
const getPageWidth = () => {
if (!containerWidth) return 600; // Fallback
// Use container width minus some margin, but respect zoom level
const baseWidth = Math.min(containerWidth * 0.99, 2000); // Max 800px for readability
return baseWidth * zoomLevel;
};
// Handle scroll to update current page and track visible pages
const handleScroll = () => {
if (!pdfContainerRef.current || !numPages) return;
const container = pdfContainerRef.current;
const scrollTop = container.scrollTop;
const containerHeight = container.clientHeight;
const totalScrollHeight = container.scrollHeight - containerHeight;
// Calculate which page we're viewing based on scroll position
const scrollPercent = scrollTop / totalScrollHeight;
const newPage = Math.min(Math.floor(scrollPercent * numPages) + 1, numPages);
if (newPage !== currentPage) {
setCurrentPage(newPage);
}
// Track visible pages based on zoom level
const newVisiblePages = new Set();
const visibleRange = Math.max(1, Math.ceil(2 / zoomLevel));
for (let i = Math.max(1, newPage - visibleRange); i <= Math.min(numPages, newPage + visibleRange); i++) {
newVisiblePages.add(i);
}
// Update visible pages if changed
if (newVisiblePages.size !== visiblePages.size ||
![...newVisiblePages].every(page => visiblePages.has(page))) {
setVisiblePages(newVisiblePages);
}
};
// Jump to specific page
const goToPage = (pageNumber) => {
if (!pdfContainerRef.current || !numPages || pageNumber < 1 || pageNumber > numPages) return;
// Update visible pages immediately for target page
const newVisiblePages = new Set();
const visibleRange = Math.max(1, Math.ceil(2 / zoomLevel));
for (let i = Math.max(1, pageNumber - visibleRange); i <= Math.min(numPages, pageNumber + visibleRange); i++) {
newVisiblePages.add(i);
}
setVisiblePages(newVisiblePages);
// Use setTimeout to ensure pages are rendered before scrolling
setTimeout(() => {
const container = pdfContainerRef.current;
if (!container) return;
// Find the target page element by its data attribute or position
const pageElements = container.querySelectorAll('[data-page-number]');
let targetElement = null;
// If we can't find elements by data attribute, calculate position manually
if (pageElements.length === 0) {
// Calculate approximate position based on page height
// Each page has some margin (mb-4 = 16px) plus the actual page height
const containerHeight = container.clientHeight;
const totalContent = container.scrollHeight;
const avgPageHeight = totalContent / numPages;
const targetPosition = (pageNumber - 1) * avgPageHeight;
// Center the page in viewport
const scrollPosition = Math.max(0, targetPosition - containerHeight / 4);
container.scrollTo({
top: scrollPosition,
behavior: 'smooth'
});
} else {
// Find the specific page element
for (const element of pageElements) {
if (parseInt(element.getAttribute('data-page-number')) === pageNumber) {
targetElement = element;
break;
}
}
if (targetElement) {
// Scroll to center the page in viewport
const elementRect = targetElement.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
const scrollOffset = container.scrollTop;
const targetPosition = scrollOffset + elementRect.top - containerRect.top - (container.clientHeight - elementRect.height) / 4;
container.scrollTo({
top: Math.max(0, targetPosition),
behavior: 'smooth'
});
}
}
}, 100); // Small delay to ensure rendering
};
// Zoom controls
const zoomIn = () => setZoomLevel(prev => Math.min(prev + 0.25, 3));
const zoomOut = () => setZoomLevel(prev => Math.max(prev - 0.25, 0.5));
const resetZoom = () => setZoomLevel(1);
if (!selectedFile) {
return (
<div className="bg-white rounded-lg shadow-sm flex items-center justify-center h-full">
<div className="text-center text-gray-500">
<p>No PDF selected</p>
</div>
</div>
);
}
return (
<div className="bg-white rounded-lg shadow-sm flex flex-col relative" style={{ width: '100%', height: '100%' }}>
<div className="sticky top-0 bg-white rounded-t-lg px-6 py-4 border-b border-gray-200 z-10">
<h2 className="text-lg font-semibold text-left text-gray-800">
{documentData?.filename || 'Document'}
</h2>
</div>
{/* PDF Container */}
<div
ref={pdfContainerRef}
className="flex-1 overflow-auto flex justify-center bg-gray-100"
onScroll={handleScroll}
>
<div className="py-4">
<Document
file={selectedFile}
onLoadSuccess={({ numPages }) => setNumPages(numPages)}
>
{/* Render all pages continuously */}
{numPages && Array.from(new Array(numPages), (_, index) => {
const pageNum = index + 1;
const isVisible = visiblePages.has(pageNum);
return (
<div key={pageNum} className="mb-4 flex justify-center" data-page-number={pageNum}>
<Page
pageNumber={pageNum}
width={isVisible ? getPageWidth() : getPageWidth() / zoomLevel}
/>
</div>
);
})}
</Document>
</div>
</div>
{/* Pagination overlay - floating pill */}
{numPages && (
<div className="absolute bottom-4 left-1/2 transform -translate-x-1/2 z-10">
<div className="flex items-center bg-gray-800/90 backdrop-blur-sm rounded-full shadow-lg px-3 py-2 space-x-3">
<button
onClick={() => goToPage(Math.max(currentPage - 1, 1))}
disabled={currentPage <= 1}
className="w-8 h-8 rounded-full bg-gray-600 hover:bg-gray-500 disabled:opacity-30 disabled:cursor-not-allowed flex items-center justify-center transition-colors text-white"
>
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path d="M10 12l-4-4 4-4v8z"/>
</svg>
</button>
<span className="px-3 py-1 text-sm font-medium text-white min-w-[60px] text-center">
{currentPage}/{numPages}
</span>
<button
onClick={() => goToPage(Math.min(currentPage + 1, numPages))}
disabled={currentPage >= numPages}
className="w-8 h-8 rounded-full bg-gray-600 hover:bg-gray-500 disabled:opacity-30 disabled:cursor-not-allowed flex items-center justify-center transition-colors text-white"
>
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path d="M6 4l4 4-4 4V4z"/>
</svg>
</button>
</div>
</div>
)}
{/* Zoom controls overlay - bottom right */}
{numPages && (
<div className="absolute bottom-4 right-4 z-10 flex flex-col items-center space-y-2">
{/* Main zoom pill - vertical */}
<div className="flex flex-col items-center bg-gray-800/90 backdrop-blur-sm rounded-full shadow-lg px-2 py-2 space-y-1">
<button
onClick={zoomIn}
disabled={zoomLevel >= 3}
className="w-6 h-6 rounded-full bg-gray-600 hover:bg-gray-500 disabled:opacity-30 disabled:cursor-not-allowed flex items-center justify-center transition-colors text-white"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
<path d="M8 4v4H4v1h4v4h1V9h4V8H9V4z"/>
</svg>
</button>
<button
onClick={zoomOut}
disabled={zoomLevel <= 0.5}
className="w-6 h-6 rounded-full bg-gray-600 hover:bg-gray-500 disabled:opacity-30 disabled:cursor-not-allowed flex items-center justify-center transition-colors text-white"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
<path d="M4 8h8v1H4z"/>
</svg>
</button>
</div>
{/* Reset button below */}
<button
onClick={resetZoom}
className="w-10 h-10 bg-gray-700 hover:bg-gray-500 backdrop-blur-sm rounded-full shadow-lg flex items-center justify-center text-white transition-colors"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" stroke="currentColor" strokeWidth="0.5">
<path d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z" strokeWidth="1"/>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"/>
</svg>
</button>
</div>
)}
</div>
);
};
export default DocumentViewer; |