diff --git a/src/components/LayoutStep.tsx b/src/components/LayoutStep.tsx index d4b1fc8..1d66ced 100644 --- a/src/components/LayoutStep.tsx +++ b/src/components/LayoutStep.tsx @@ -12,25 +12,23 @@ type EditMode = 'lines' | 'cells'; type Axis = 'x' | 'y'; export const LayoutStep: React.FC = ({ config, splits, onChange }) => { - // DEBUG LOGGING - console.log("LayoutStep Render. Config:", config, "Splits:", splits); + // Логируем рендер для отладки + console.log("LayoutStep Render"); const svgRef = useRef(null); const [mode, setMode] = useState('lines'); + // States const [phantomAxis, setPhantomAxis] = useState(null); const [mousePos, setMousePos] = useState({ x: 0, y: 0 }); const [hoveredSplit, setHoveredSplit] = useState<{ axis: Axis; index: number } | null>(null); const [dragging, setDragging] = useState<{ axis: Axis; index: number } | null>(null); const [isButtonHovered, setIsButtonHovered] = useState(false); + + // Editor State const [editingCell, setEditingCell] = useState<{ i: number, j: number } | null>(null); - // DATA SAFETY CHECKS - if (!config || !config.drawer) { - console.error("LayoutStep: Config is missing!"); - return
Config Error: Missing configuration
; - } - + // --- ЗАЩИТА ДАННЫХ --- const safeX = Array.isArray(splits?.x) ? splits.x : []; const safeY = Array.isArray(splits?.y) ? splits.y : []; const safePartitions = splits?.partitions || {}; @@ -42,23 +40,20 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { const aspectRatio = depth / width; const viewBoxH = viewBoxW * aspectRatio; - // LOG CALCULATIONS - if (isNaN(viewBoxH) || !isFinite(viewBoxH)) { - console.error("LayoutStep: Invalid Dimensions", { width, depth, aspectRatio }); - return
Error: Invalid Dimensions ({width}x{depth})
; - } - const sortedX = useMemo(() => [0, ...safeX, 1].sort((a, b) => a - b), [safeX]); const sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]); - // Handlers + // --- ЛОГИКА --- const addPartition = (axis: 'x' | 'y') => { if (!editingCell) return; const key = `${editingCell.i}-${editingCell.j}`; const current = safePartitions[key] || []; const newPart: Partition = { id: Math.random().toString(36).substr(2, 9), - axis, offset: 0.5, height: config.drawer.height || 80, rounded: false + axis, + offset: 0.5, + height: config.drawer.height || 80, + rounded: false }; onChange({ ...splits, partitions: { ...safePartitions, [key]: [...current, newPart] } }); }; @@ -78,6 +73,7 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { onChange({ ...splits, partitions: { ...safePartitions, [key]: current.filter(p => p.id !== id) } }); }; + // --- MOUSE HANDLERS --- const handleGlobalMouseMove = (e: React.MouseEvent) => { if (!svgRef.current) return; const rect = svgRef.current.getBoundingClientRect(); @@ -85,6 +81,7 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { const nx = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width)); const ny = Math.max(0, Math.min(1, (e.clientY - rect.top) / rect.height)); + setMousePos({ x: nx, y: ny }); if (mode === 'lines') { @@ -97,15 +94,18 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { } if (isButtonHovered) return; setHoveredSplit(null); - - const SNAP = 0.02; + + const SNAP = 0.02; if (nx > SNAP && nx < 1-SNAP && ny > SNAP && ny < 1-SNAP) { const closeToX = safeX.some(val => Math.abs(nx - val) < SNAP); const closeToY = safeY.some(val => Math.abs(ny - val) < SNAP); + if (!closeToX && !closeToY) { const distRight = 1 - nx; const distBottom = 1 - ny; setPhantomAxis(Math.min(nx, distRight) < Math.min(ny, distBottom) ? 'y' : 'x'); - } else { setPhantomAxis(null); } + } else { + setPhantomAxis(null); + } } } }; @@ -138,15 +138,27 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { return (
+ + {/* HEADER */}

2. Макет

+
- - + +
- + +
@@ -154,7 +166,7 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => {
- {mode === 'lines' ? 'Границы' : 'Ячейки'} + {mode === 'lines' ? 'Режим: Границы' : 'Режим: Ячейки'}

{mode === 'lines' ? 'Клик: создать. Драг: двигать.' : 'Кликни по ячейке для настройки.'}

@@ -181,11 +193,17 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { + {/* --- ИСПРАВЛЕННЫЙ БЛОК ОТРИСОВКИ ЯЧЕЕК --- */} {sortedX.slice(0, -1).map((x1, i) => { const x2 = sortedX[i + 1]; return sortedY.slice(0, -1).map((y1, j) => { - const cellX = x1 * viewBoxW; const cellY = y1 * viewBoxH; - const cellW = (x2 - x1) * viewBoxW; const cellH = (y2 - y1) * viewBoxH; + const y2 = sortedY[j + 1]; // <--- ВОТ ЭТОЙ СТРОКИ НЕ ХВАТАЛО! + + const cellX = x1 * viewBoxW; + const cellY = y1 * viewBoxH; + const cellW = (x2 - x1) * viewBoxW; + const cellH = (y2 - y1) * viewBoxH; + const isSelected = editingCell?.i === i && editingCell?.j === j; const parts = safePartitions[`${i}-${j}`] || []; @@ -211,6 +229,7 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { }); })} + {/* GRID LINES X */} {safeX.map((x, i) => ( { if (mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredSplit({ axis: 'x', index: i }); setPhantomAxis(null); } }}> @@ -224,6 +243,7 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => { ))} + {/* GRID LINES Y */} {safeY.map((y, i) => ( { if (mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredSplit({ axis: 'y', index: i }); setPhantomAxis(null); } }}> @@ -243,34 +263,56 @@ export const LayoutStep: React.FC = ({ config, splits, onChange }) => {
+ {/* --- EDITOR PANEL --- */} {mode === 'cells' && editingCell && (

Редактор ячейки

+
- - + +
+
{(safePartitions[`${editingCell.i}-${editingCell.j}`] || []).map((p, idx) => (
- Стенка #{idx+1} ({p.axis === 'x' ? 'Верт' : 'Гориз'}) + + Стенка #{idx+1} ({p.axis === 'x' ? 'Верт' : 'Гориз'}) +
-
Позиция {(p.offset * 100).toFixed(0)}%
- updatePartition(p.id, { offset: parseFloat(e.target.value) })} className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-purple-500"/> +
+ Позиция {(p.offset * 100).toFixed(0)}% +
+ updatePartition(p.id, { offset: parseFloat(e.target.value) })} + className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-purple-500" + />
-
Высота {p.height} мм
- updatePartition(p.id, { height: parseFloat(e.target.value) })} className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-blue-500"/> +
+ Высота {p.height} мм +
+ updatePartition(p.id, { height: parseFloat(e.target.value) })} + className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-blue-500" + />
- updatePartition(p.id, { rounded: e.target.checked })} className="rounded bg-slate-700 border-slate-600 text-purple-500 focus:ring-0"/> + updatePartition(p.id, { rounded: e.target.checked })} + className="rounded bg-slate-700 border-slate-600 text-purple-500 focus:ring-0" + />