import React, { useState, useRef, useCallback } from 'react'; import { Icon } from '@iconify/react'; import { motion } from 'framer-motion'; import FilePreview from './FilePreview'; interface ZoomablePreviewProps { url: string; filename: string; } export default function ZoomablePreview({ url, filename }: ZoomablePreviewProps) { const [zoom, setZoom] = useState(1); const [position, setPosition] = useState({ x: 0, y: 0 }); const [isDragging, setIsDragging] = useState(false); const [dragStart, setDragStart] = useState({ x: 0, y: 0 }); const containerRef = useRef(null); const zoomLevels = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3]; const currentZoomIndex = zoomLevels.findIndex(level => Math.abs(level - zoom) < 0.01); const handleZoomIn = useCallback(() => { const nextIndex = Math.min(currentZoomIndex + 1, zoomLevels.length - 1); setZoom(zoomLevels[nextIndex]); }, [currentZoomIndex]); const handleZoomOut = useCallback(() => { const prevIndex = Math.max(currentZoomIndex - 1, 0); setZoom(zoomLevels[prevIndex]); }, [currentZoomIndex]); const handleZoomReset = useCallback(() => { setZoom(1); setPosition({ x: 0, y: 0 }); }, []); const handleMouseDown = useCallback((e: React.MouseEvent) => { if (zoom > 1) { setIsDragging(true); setDragStart({ x: e.clientX - position.x, y: e.clientY - position.y }); } }, [zoom, position]); const handleMouseMove = useCallback((e: React.MouseEvent) => { if (isDragging && zoom > 1) { setPosition({ x: e.clientX - dragStart.x, y: e.clientY - dragStart.y }); } }, [isDragging, dragStart, zoom]); const handleMouseUp = useCallback(() => { setIsDragging(false); }, []); const handleWheel = useCallback((e: React.WheelEvent) => { e.preventDefault(); const delta = e.deltaY > 0 ? -1 : 1; const newZoomIndex = Math.max(0, Math.min(zoomLevels.length - 1, currentZoomIndex + delta)); setZoom(zoomLevels[newZoomIndex]); // Reset position when zooming out to 100% or less if (zoomLevels[newZoomIndex] <= 1) { setPosition({ x: 0, y: 0 }); } }, [currentZoomIndex]); return (
{/* Zoom Controls */}
= zoomLevels.length - 1} title="Zoom In" >
{Math.round(zoom * 100)}%
{/* Zoom Indicator */} {zoom !== 1 && (
{zoom > 1 ? 'Click and drag to pan' : ''}
)} {/* Preview Container */}
1 ? (isDragging ? 'grabbing' : 'grab') : 'default' }} >
{/* Usage Hint */}
Scroll to zoom • Click and drag to pan
); }