import React, { useState, useEffect, useRef, useCallback } from "react" import ModalPopupComponent from "@mwater/react-library/lib/ModalPopupComponent" import { Image } from "./RotationAwareImageComponent" import { ImageManager } from "./formContext" import { LocalizeString } from "ez-localize" const MIN_ZOOM = 1 const MAX_ZOOM = 8 const ZOOM_STEP = 1.5 const WHEEL_ZOOM_STEP = 1.15 export interface ImagePopupComponentProps { /** Manager for loading image URLs */ imageManager: ImageManager /** The image object */ image: Image /** Called when remove is requested */ onRemove?: () => void /** Called when set as cover is requested */ onSetCover?: () => void /** Called when rotate is requested */ onRotate?: () => void /** Called when popup should close */ onClose: () => void /** Localization function */ T: LocalizeString /** Extra content to render in the toolbar (e.g. a "View Row" button) */ extraToolbarContent?: React.ReactNode } /** Displays an image in a popup with controls for removing, rotating, zooming, downloading, or setting as cover */ function ImagePopupComponent(props: ImagePopupComponentProps) { const { imageManager, image, onRemove, onSetCover, onRotate, onClose, T, extraToolbarContent } = props const [url, setUrl] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(false) const [zoomLevel, setZoomLevel] = useState(MIN_ZOOM) const [panOffset, setPanOffset] = useState({ x: 0, y: 0 }) const [isDragging, setIsDragging] = useState(false) const loadSeqRef = useRef(0) const containerRef = useRef(null) const dragStartRef = useRef({ x: 0, y: 0 }) const panStartRef = useRef({ x: 0, y: 0 }) useEffect(() => { loadSeqRef.current += 1 const seq = loadSeqRef.current setLoading(true) setError(false) imageManager.getImageUrl( image.id, (loadedUrl) => { if (seq !== loadSeqRef.current) return setUrl(loadedUrl) setLoading(false) }, () => { if (seq !== loadSeqRef.current) return setError(true) setLoading(false) } ) }, [image.id, imageManager]) const clampZoom = (z: number) => Math.min(Math.max(z, MIN_ZOOM), MAX_ZOOM) const applyZoom = useCallback((newZoom: number) => { const clamped = clampZoom(newZoom) setZoomLevel(clamped) if (clamped <= MIN_ZOOM) { setPanOffset({ x: 0, y: 0 }) } }, []) const handleZoomIn = () => applyZoom(zoomLevel * ZOOM_STEP) const handleZoomOut = () => applyZoom(zoomLevel / ZOOM_STEP) const handleZoomReset = () => applyZoom(MIN_ZOOM) useEffect(() => { const container = containerRef.current if (!container || !url) return const onWheel = (e: WheelEvent) => { e.preventDefault() const factor = e.deltaY < 0 ? WHEEL_ZOOM_STEP : 1 / WHEEL_ZOOM_STEP const newZoom = clampZoom(zoomLevel * factor) setZoomLevel(newZoom) if (newZoom <= MIN_ZOOM) { setPanOffset({ x: 0, y: 0 }) } } container.addEventListener("wheel", onWheel, { passive: false }) return () => container.removeEventListener("wheel", onWheel) }, [url, zoomLevel]) const handleMouseDown = (e: React.MouseEvent) => { if (zoomLevel <= MIN_ZOOM) return e.preventDefault() setIsDragging(true) dragStartRef.current = { x: e.clientX, y: e.clientY } panStartRef.current = { ...panOffset } } const handleMouseMove = (e: React.MouseEvent) => { if (!isDragging) return setPanOffset({ x: panStartRef.current.x + (e.clientX - dragStartRef.current.x), y: panStartRef.current.y + (e.clientY - dragStartRef.current.y) }) } const handleMouseUp = (e: React.MouseEvent) => { if (!isDragging) return setIsDragging(false) } const handleImageClick = (e: React.MouseEvent) => { const wasDrag = Math.abs(e.clientX - dragStartRef.current.x) > 3 || Math.abs(e.clientY - dragStartRef.current.y) > 3 if (!wasDrag) { handleZoomIn() } } const handleDownload = () => { if (url) { window.open(url, "_blank") } } const rotationDeg = image.rotation || 0 const isSwappedAspect = rotationDeg === 90 || rotationDeg === 270 const isZoomed = zoomLevel > MIN_ZOOM const toolbar = (
{onSetCover && ( )} {onRemove && ( )} {onRotate && ( )}
{extraToolbarContent}
) let content if (loading) { content = (
) } else if (error || !url) { content =
{T("Error loading image")}
} else { content = (
setIsDragging(false)} > {image.caption
) } return ( {content} ) } export default ImagePopupComponent