import React, { ChangeEvent, useEffect, useRef, useState } from "react"; import DragableBox from "./DragableBox"; import { Coordinates } from "~/types"; type ContainerDimensions = { height: number; width: number; } export default function CropSelector() { const [firstDragable, setFirstDragable] = useState({ x: 0, y: 0 }); const [secondDragable, setSecondDragable] = useState({ x: 100, y: 100 }); const [selectorPosition, setSelectorPosition] = useState({ x: 0, y: 0 }); const [containerSize, setContainerSize] = useState({ height: 0, width: 0 }); const [userInputs, setUserInputs] = useState({ height: 100, width: 100 }); const containerRef = useRef(null); useEffect(() => { setSelectorPosition({ x: Math.min(firstDragable.x, secondDragable.x), y: Math.min(firstDragable.y, secondDragable.y) }); }, [firstDragable, secondDragable]); useEffect(() => { if (!containerRef.current) return; const resizeObserver = new ResizeObserver(entries => { const entry = entries[0]; setContainerSize({ width: entry.contentRect.width, height: entry.contentRect.height }); }); resizeObserver.observe(containerRef.current); return () => resizeObserver.disconnect(); }, [containerRef]); const selectorWidth = Math.abs(secondDragable.x - firstDragable.x); const selectorHeight = Math.abs(secondDragable.y - firstDragable.y); const handleSelectorDrag = (newPosition: Coordinates) => { const deltaX = newPosition.x - selectorPosition.x; const deltaY = newPosition.y - selectorPosition.y; const newFirstX = Math.min(firstDragable.x, secondDragable.x) + deltaX; const newFirstY = Math.min(firstDragable.y, secondDragable.y) + deltaY; const newSecondX = Math.max(firstDragable.x, secondDragable.x) + deltaX; const newSecondY = Math.max(firstDragable.y, secondDragable.y) + deltaY; // Ensure the selector stays within container bounds if (newFirstX >= 0 && newSecondX <= containerSize.width && newFirstY >= 0 && newSecondY <= containerSize.height) { setFirstDragable({ x: newFirstX, y: newFirstY }); setSecondDragable({ x: newSecondX, y: newSecondY }); } }; const handleDimensionChange = ( dimension: 'width' | 'height', event: React.ChangeEvent ) => { const value = Math.max(0, parseInt(event.target.value) || 0); const maxValue = dimension === 'width' ? containerSize.width : containerSize.height; const clampedValue = Math.min(value, maxValue); setUserInputs(prev => ({ ...prev, [dimension]: clampedValue })); // Update selector dimensions if (dimension === 'width') { const newX = Math.min(firstDragable.x, containerSize.width - clampedValue); setFirstDragable(prev => ({ ...prev, x: newX })); setSecondDragable(prev => ({ ...prev, x: newX + clampedValue })); } else { const newY = Math.min(firstDragable.y, containerSize.height - clampedValue); setFirstDragable(prev => ({ ...prev, y: newY })); setSecondDragable(prev => ({ ...prev, y: newY + clampedValue })); } }; // Update user inputs when dragging useEffect(() => { setUserInputs({ width: Math.round(selectorWidth), height: Math.round(selectorHeight) }); }, [selectorWidth, selectorHeight]); return (
) => handleDimensionChange('width', e)} className="w-24" />
handleDimensionChange('height', e)} className="w-24" />
); }