import { useCallback, useEffect, useRef, useState } from 'react' import { ChevronLeft, Crop, Maximize2 } from 'lucide-react' import { clampCropRect, computeDisplayLayout, cropToDisplay, defaultCropRect, type CropRect, type DisplayLayout, } from '../lib/cropImage' import { t } from '../lib/i18n' import type { Lang } from '../lib/types' type Handle = 'move' | 'nw' | 'ne' | 'sw' | 'se' interface Props { lang: Lang imageSrc: string naturalW: number naturalH: number onBack: () => void onConfirm: (crop: CropRect) => void onUseFull: () => void } export function StepCrop({ lang, imageSrc, naturalW, naturalH, onBack, onConfirm, onUseFull, }: Props) { const containerRef = useRef(null) const [layout, setLayout] = useState(null) const [crop, setCrop] = useState(() => defaultCropRect(naturalW, naturalH)) const dragRef = useRef<{ handle: Handle startX: number startY: number startCrop: CropRect } | null>(null) const layoutRef = useRef(layout) layoutRef.current = layout const naturalRef = useRef({ w: naturalW, h: naturalH }) naturalRef.current = { w: naturalW, h: naturalH } const measure = useCallback(() => { const el = containerRef.current if (!el) return const next = computeDisplayLayout(el.clientWidth, el.clientHeight, naturalW, naturalH) if (next.width > 0 && next.height > 0) setLayout(next) }, [naturalW, naturalH]) useEffect(() => { measure() const el = containerRef.current if (!el) return const ro = new ResizeObserver(measure) ro.observe(el) return () => ro.disconnect() }, [measure]) useEffect(() => { setCrop(defaultCropRect(naturalW, naturalH)) }, [naturalW, naturalH, imageSrc]) const displayCrop = layout ? cropToDisplay(crop, layout, naturalW, naturalH) : null const startDrag = (handle: Handle, clientX: number, clientY: number) => { dragRef.current = { handle, startX: clientX, startY: clientY, startCrop: crop } } const onPointerMove = useCallback((clientX: number, clientY: number) => { const drag = dragRef.current const layoutNow = layoutRef.current if (!drag || !layoutNow) return const { w: naturalWNow, h: naturalHNow } = naturalRef.current const dx = ((clientX - drag.startX) / layoutNow.width) * naturalWNow const dy = ((clientY - drag.startY) / layoutNow.height) * naturalHNow const s = drag.startCrop let next: CropRect switch (drag.handle) { case 'move': next = { ...s, x: s.x + dx, y: s.y + dy } break case 'nw': next = { x: s.x + dx, y: s.y + dy, width: s.width - dx, height: s.height - dy, } break case 'ne': next = { x: s.x, y: s.y + dy, width: s.width + dx, height: s.height - dy, } break case 'sw': next = { x: s.x + dx, y: s.y, width: s.width - dx, height: s.height + dy, } break case 'se': next = { x: s.x, y: s.y, width: s.width + dx, height: s.height + dy, } break } setCrop(clampCropRect(next, naturalWNow, naturalHNow)) }, []) const endDrag = () => { dragRef.current = null } useEffect(() => { const onMove = (e: PointerEvent) => onPointerMove(e.clientX, e.clientY) const onUp = () => endDrag() window.addEventListener('pointermove', onMove) window.addEventListener('pointerup', onUp) window.addEventListener('pointercancel', onUp) return () => { window.removeEventListener('pointermove', onMove) window.removeEventListener('pointerup', onUp) window.removeEventListener('pointercancel', onUp) } }, [onPointerMove]) const handleCorner = (corner: Exclude) => ( e: React.PointerEvent, ) => { e.stopPropagation() e.currentTarget.setPointerCapture(e.pointerId) startDrag(corner, e.clientX, e.clientY) } return (

{t(lang, 'cropTitle')}

{t(lang, 'cropHint')}

{displayCrop && (
{ e.currentTarget.setPointerCapture(e.pointerId) startDrag('move', e.clientX, e.clientY) }} >
{Array.from({ length: 9 }).map((_, i) => (
))}
{(['nw', 'ne', 'sw', 'se'] as const).map((corner) => { const pos = corner === 'nw' ? 'left-0 top-0 -translate-x-1/2 -translate-y-1/2' : corner === 'ne' ? 'right-0 top-0 translate-x-1/2 -translate-y-1/2' : corner === 'sw' ? 'left-0 bottom-0 -translate-x-1/2 translate-y-1/2' : 'right-0 bottom-0 translate-x-1/2 translate-y-1/2' return (
) })}
)}

{Math.round(crop.width)} × {Math.round(crop.height)}

) }