import clsx from 'clsx'; import React, { useState, useRef, useEffect } from 'react'; import { useTranslation } from '@/hooks/useTranslation'; import ZoomControls from './ZoomControls'; interface TableViewerProps { html: string | null; isDarkMode: boolean; onClose: () => void; } const MIN_SCALE = 0.5; const MAX_SCALE = 4; const ZOOM_SPEED = 0.1; const TableViewer: React.FC = ({ html, isDarkMode, onClose }) => { const _ = useTranslation(); const [scale, setScale] = useState(1); const [position, setPosition] = useState({ x: 0, y: 0 }); const [isDragging, setIsDragging] = useState(false); const [showZoomLabel, setShowZoomLabel] = useState(true); const dragStart = useRef({ x: 0, y: 0 }); const wasDragging = useRef(false); const containerRef = useRef(null); const contentRef = useRef(null); const zoomLabelTimeoutRef = useRef | null>(null); const hideZoomLabelAfterDelay = () => { if (zoomLabelTimeoutRef.current) { clearTimeout(zoomLabelTimeoutRef.current); } setShowZoomLabel(true); zoomLabelTimeoutRef.current = setTimeout(() => { setShowZoomLabel(false); }, 2000); }; const handleZoomIn = () => { const newScale = Math.min(scale + ZOOM_SPEED, MAX_SCALE); setScale(newScale); hideZoomLabelAfterDelay(); }; const handleZoomOut = () => { const newScale = Math.max(scale - ZOOM_SPEED, MIN_SCALE); if (newScale <= 1) { setPosition({ x: 0, y: 0 }); setScale(newScale); } else { setScale(newScale); } hideZoomLabelAfterDelay(); }; const handleResetZoom = () => { setScale(1); setPosition({ x: 0, y: 0 }); hideZoomLabelAfterDelay(); }; const handleKeyDown = (e: React.KeyboardEvent) => { e.stopPropagation(); if (e.key === 'Escape') { onClose(); return; } const isCtrlOrCmd = e.ctrlKey || e.metaKey; if (isCtrlOrCmd) { e.preventDefault(); if (e.key === '=' || e.key === '+') { handleZoomIn(); } else if (e.key === '-' || e.key === '_') { handleZoomOut(); } else if (e.key === '0') { handleResetZoom(); } } }; useEffect(() => { containerRef.current?.focus(); setTimeout(() => { hideZoomLabelAfterDelay(); }, 0); return () => { if (zoomLabelTimeoutRef.current) { clearTimeout(zoomLabelTimeoutRef.current); } }; }, []); const handleWheel = (e: React.WheelEvent) => { const isCtrlOrCmd = e.ctrlKey || e.metaKey; if (!isCtrlOrCmd) { return; } e.preventDefault(); const delta = e.deltaY > 0 ? -ZOOM_SPEED : ZOOM_SPEED; const newScale = Math.min(Math.max(scale + delta, MIN_SCALE), MAX_SCALE); if (newScale <= 1) { setPosition({ x: 0, y: 0 }); setScale(newScale); hideZoomLabelAfterDelay(); return; } setScale(newScale); hideZoomLabelAfterDelay(); }; const handleContentMouseDown = (e: React.MouseEvent) => { if (isDragging || scale <= 1) return; e.stopPropagation(); e.preventDefault(); setIsDragging(true); wasDragging.current = false; dragStart.current = { x: e.clientX - position.x, y: e.clientY - position.y }; }; const handleContentMouseMove = (e: React.MouseEvent) => { if (!isDragging || scale <= 1) return; e.preventDefault(); wasDragging.current = true; const newX = e.clientX - dragStart.current.x; const newY = e.clientY - dragStart.current.y; setPosition({ x: newX, y: newY }); }; const handleContentMouseUp = (e: React.MouseEvent) => { if (isDragging) { e.stopPropagation(); } setIsDragging(false); }; const handleContainerClick = () => { if (wasDragging.current) { wasDragging.current = false; return; } onClose(); }; const handleContentClick = (e: React.MouseEvent) => { e.stopPropagation(); if (wasDragging.current) { wasDragging.current = false; return; } setShowZoomLabel((prev) => !prev); }; const cursorStyle = scale > 1 ? (isDragging ? 'grabbing' : 'grab') : 'default'; if (!html) return null; return (
{ if (e.key === 'Enter' || e.key === ' ') { onClose(); } }} />
{showZoomLabel && (
{Math.round((scale * 100) / 5) * 5}%
)}
); }; export default TableViewer;