This commit is contained in:
Халимов Рустам
2026-01-10 22:10:37 +03:00
parent 7757de44a2
commit b60d7e6117
2 changed files with 191 additions and 260 deletions
+33 -17
View File
@@ -50,28 +50,32 @@ const App = () => {
} }
}, [config, splits]); }, [config, splits]);
// Error Boundary component for safety // Error Boundary
class ErrorBoundary extends React.Component<{children: React.ReactNode}, {hasError: boolean}> { class ErrorBoundary extends React.Component<{children: React.ReactNode}, {hasError: boolean}> {
state = { hasError: false }; state = { hasError: false };
static getDerivedStateFromError() { return { hasError: true }; } static getDerivedStateFromError() { return { hasError: true }; }
render() { render() {
if (this.state.hasError) return <div className="p-4 text-red-500 flex items-center gap-2"><AlertTriangle/> Ошибка отрисовки. Нажмите "Новый проект".</div>; if (this.state.hasError) return (
<div className="h-full flex flex-col items-center justify-center text-red-400 p-8 border border-red-900 bg-red-900/20 rounded-xl">
<AlertTriangle size={48} className="mb-4"/>
<h2 className="text-xl font-bold mb-2">Произошла ошибка отрисовки</h2>
<button onClick={() => window.location.reload()} className="px-4 py-2 bg-red-600 hover:bg-red-500 text-white rounded-lg font-medium transition-colors">
Перезагрузить страницу
</button>
</div>
);
return this.props.children; return this.props.children;
} }
} }
return ( return (
<div className="min-h-screen flex flex-col font-sans text-gray-100 bg-slate-950"> <div className="min-h-screen flex flex-col font-sans text-gray-100 bg-slate-950 overflow-hidden">
<header className="bg-slate-900 border-b border-slate-800 p-4 shadow-md sticky top-0 z-50"> {/* Header */}
<header className="bg-slate-900 border-b border-slate-800 p-4 shadow-md shrink-0 z-50">
<div className="max-w-7xl mx-auto flex items-center justify-between"> <div className="max-w-7xl mx-auto flex items-center justify-between">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<div className="bg-primary p-2 rounded-lg"> <div className="bg-primary p-2 rounded-lg"><Box className="text-white" size={20} /></div>
<Box className="text-white" size={20} /> <div><h1 className="text-xl font-bold tracking-tight">PrintFit</h1><p className="text-xs text-gray-400">Генератор органайзеров</p></div>
</div>
<div>
<h1 className="text-xl font-bold tracking-tight">PrintFit</h1>
<p className="text-xs text-gray-400">Генератор органайзеров</p>
</div>
</div> </div>
<div className="flex items-center gap-4 text-sm font-medium"> <div className="flex items-center gap-4 text-sm font-medium">
{[1, 2, 3].map((num) => ( {[1, 2, 3].map((num) => (
@@ -84,21 +88,33 @@ const App = () => {
</div> </div>
</header> </header>
<main className="flex-1 max-w-7xl mx-auto w-full p-4 md:p-8 flex flex-col"> {/* Main Content */}
{step === 1 && <div className="max-w-4xl mx-auto w-full animate-fade-in"><ConfigStep config={config} onChange={setConfig} /></div>} <main className="flex-1 max-w-7xl mx-auto w-full p-4 md:p-6 overflow-hidden flex flex-col">
{step === 1 && (
<div className="max-w-4xl mx-auto w-full animate-fade-in overflow-y-auto">
<ConfigStep config={config} onChange={setConfig} />
</div>
)}
{step === 2 && ( {step === 2 && (
<div className="flex-1 flex flex-col min-h-[500px] animate-fade-in"> // ВЕРНУЛИ ЯВНУЮ ВЫСОТУ: h-[calc(100vh-180px)]
<div className="w-full h-[calc(100vh-180px)] animate-fade-in">
<ErrorBoundary> <ErrorBoundary>
<LayoutStep config={config} splits={splits} onChange={setSplits} /> <LayoutStep key="layout-final" config={config} splits={splits} onChange={setSplits} />
</ErrorBoundary> </ErrorBoundary>
</div> </div>
)} )}
{step === 3 && <div className="flex-1 flex flex-col min-h-[600px] animate-fade-in"><PreviewStep parts={parts} config={config} splits={splits} /></div>} {step === 3 && (
// ТАКЖЕ ДЛЯ ШАГА 3
<div className="w-full h-[calc(100vh-180px)] animate-fade-in">
<PreviewStep parts={parts} config={config} splits={splits} />
</div>
)}
</main> </main>
<footer className="bg-slate-900 border-t border-slate-800 p-4 sticky bottom-0 z-50"> {/* Footer */}
<footer className="bg-slate-900 border-t border-slate-800 p-4 shrink-0 z-50">
<div className="max-w-7xl mx-auto flex justify-between items-center"> <div className="max-w-7xl mx-auto flex justify-between items-center">
<button disabled={step === 1} onClick={() => setStep(s => Math.max(1, s - 1))} className="flex items-center gap-2 px-6 py-3 rounded-lg font-semibold bg-slate-800 text-white disabled:opacity-50 disabled:cursor-not-allowed hover:bg-slate-700 transition-colors"><ChevronLeft size={18} /> Назад</button> <button disabled={step === 1} onClick={() => setStep(s => Math.max(1, s - 1))} className="flex items-center gap-2 px-6 py-3 rounded-lg font-semibold bg-slate-800 text-white disabled:opacity-50 disabled:cursor-not-allowed hover:bg-slate-700 transition-colors"><ChevronLeft size={18} /> Назад</button>
<div className="text-sm text-gray-500">{step === 2 && <span className="text-accent font-mono">Ячеек: {parts.length}</span>}</div> <div className="text-sm text-gray-500">{step === 2 && <span className="text-accent font-mono">Ячеек: {parts.length}</span>}</div>
+158 -243
View File
@@ -1,6 +1,6 @@
import React, { useRef, useState, useMemo } from 'react'; import React, { useRef, useState, useMemo } from 'react';
import { AppConfig, LayoutSplits, Partition } from '../types'; import { AppConfig, LayoutSplits, Partition } from '../types';
import { Grid, MousePointer2, Trash2, RotateCcw, X, Move, Plus } from 'lucide-react'; import { Grid, MousePointer2, Trash2, RotateCcw, X, Plus } from 'lucide-react';
interface Props { interface Props {
config: AppConfig; config: AppConfig;
@@ -18,55 +18,47 @@ type DragTarget =
export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => { export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const svgRef = useRef<SVGSVGElement>(null); const svgRef = useRef<SVGSVGElement>(null);
// -- STATE --
const [mode, setMode] = useState<EditMode>('lines'); const [mode, setMode] = useState<EditMode>('lines');
const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
// Dragging const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
const [dragging, setDragging] = useState<DragTarget | null>(null);
const [isButtonHovered, setIsButtonHovered] = useState(false); const [isButtonHovered, setIsButtonHovered] = useState(false);
const [dragging, setDragging] = useState<DragTarget | null>(null);
// Main Grid Hover
const [phantomMainAxis, setPhantomMainAxis] = useState<Axis | null>(null); const [phantomMainAxis, setPhantomMainAxis] = useState<Axis | null>(null);
const [hoveredMainSplit, setHoveredMainSplit] = useState<{ axis: Axis; index: number } | null>(null); const [hoveredMainSplit, setHoveredMainSplit] = useState<{ axis: Axis; index: number } | null>(null);
// Partition Hover
const [hoveredCell, setHoveredCell] = useState<{ i: number; j: number } | null>(null); const [hoveredCell, setHoveredCell] = useState<{ i: number; j: number } | null>(null);
const [hoveredPartition, setHoveredPartition] = useState<{ id: string; cellKey: string } | null>(null); const [hoveredPartition, setHoveredPartition] = useState<{ id: string; cellKey: string } | null>(null);
const [phantomPartition, setPhantomPartition] = useState<{ axis: Axis; offset: number } | null>(null); const [phantomPartition, setPhantomPartition] = useState<{ axis: Axis; offset: number } | null>(null);
// Selection
const [selectedPartitionId, setSelectedPartitionId] = useState<string | null>(null); const [selectedPartitionId, setSelectedPartitionId] = useState<string | null>(null);
// -- SAFE DATA -- // --- Safe Data ---
const safeX = Array.isArray(splits?.x) ? splits.x : []; const safeX = Array.isArray(splits?.x) ? splits.x : [];
const safeY = Array.isArray(splits?.y) ? splits.y : []; const safeY = Array.isArray(splits?.y) ? splits.y : [];
const safePartitions = splits?.partitions || {}; const safePartitions = splits?.partitions || {};
// -- CALCULATIONS -- const drawerW = Math.max(1, config.drawer.width || 300);
// Защита от деления на ноль и NaN const drawerD = Math.max(1, config.drawer.depth || 400);
const safeW = Math.max(1, config.drawer.width || 300); const aspectRatio = drawerD / drawerW;
const safeH = Math.max(1, config.drawer.depth || 400);
const aspectRatio = safeH / safeW;
const viewBoxW = 1000; const viewBoxW = 1000;
const viewBoxH = viewBoxW * aspectRatio; const viewBoxH = viewBoxW * aspectRatio;
const sortedX = useMemo(() => [0, ...safeX, 1].sort((a, b) => a - b), [safeX]); const sortedX = useMemo(() => [0, ...safeX, 1].sort((a, b) => a - b), [safeX]);
const sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]); const sortedY = useMemo(() => [0, ...safeY, 1].sort((a, b) => a - b), [safeY]);
// -- HELPERS -- // --- Helpers ---
const getSelectedPartition = () => { const getSelectedPartition = () => {
if (!selectedPartitionId) return null; if (!selectedPartitionId) return null;
for (const key in safePartitions) { for (const key in safePartitions) {
const part = safePartitions[key].find(p => p.id === selectedPartitionId); const part = safePartitions[key].find(p => p.id === selectedPartitionId);
if (part) return { key, part }; if (part) return { key, part };
} }
return null; return null;
}; };
const selectedData = getSelectedPartition(); const selectedData = getSelectedPartition();
// -- ACTIONS -- // --- Actions ---
const removeMainSplit = (axis: Axis, index: number) => { const removeMainSplit = (axis: Axis, index: number) => {
const newSplits = { ...splits, x: [...safeX], y: [...safeY] }; const newSplits = { ...splits, x: [...safeX], y: [...safeY] };
newSplits[axis] = newSplits[axis].filter((_, i) => i !== index); newSplits[axis] = newSplits[axis].filter((_, i) => i !== index);
@@ -77,33 +69,30 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
}; };
const createPartition = (i: number, j: number, axis: Axis, offset: number) => { const createPartition = (i: number, j: number, axis: Axis, offset: number) => {
const key = `${i}-${j}`; const key = `${i}-${j}`;
const current = safePartitions[key] || []; const current = safePartitions[key] || [];
const newPart: Partition = { const newPart: Partition = {
id: Math.random().toString(36).substr(2, 9), id: Math.random().toString(36).substr(2, 9),
axis, axis, offset, height: config.drawer.height || 80, rounded: false
offset, };
height: config.drawer.height || 80, onChange({ ...splits, partitions: { ...safePartitions, [key]: [...current, newPart] } });
rounded: false setSelectedPartitionId(newPart.id);
};
onChange({ ...splits, partitions: { ...safePartitions, [key]: [...current, newPart] } });
setSelectedPartitionId(newPart.id);
}; };
const updatePartition = (key: string, id: string, updates: Partial<Partition>) => { const updatePartition = (key: string, id: string, updates: Partial<Partition>) => {
const current = safePartitions[key] || []; const current = safePartitions[key] || [];
const updated = current.map(p => p.id === id ? { ...p, ...updates } : p); const updated = current.map(p => p.id === id ? { ...p, ...updates } : p);
onChange({ ...splits, partitions: { ...safePartitions, [key]: updated } }); onChange({ ...splits, partitions: { ...safePartitions, [key]: updated } });
}; };
const removePartition = (key: string, id: string) => { const removePartition = (key: string, id: string) => {
const current = safePartitions[key] || []; const current = safePartitions[key] || [];
onChange({ ...splits, partitions: { ...safePartitions, [key]: current.filter(p => p.id !== id) } }); onChange({ ...splits, partitions: { ...safePartitions, [key]: current.filter(p => p.id !== id) } });
if (selectedPartitionId === id) setSelectedPartitionId(null); if (selectedPartitionId === id) setSelectedPartitionId(null);
setHoveredPartition(null); setHoveredPartition(null);
}; };
// -- MOUSE LOGIC -- // --- Handlers ---
const handleMouseMove = (e: React.MouseEvent) => { const handleMouseMove = (e: React.MouseEvent) => {
if (!svgRef.current) return; if (!svgRef.current) return;
const rect = svgRef.current.getBoundingClientRect(); const rect = svgRef.current.getBoundingClientRect();
@@ -114,101 +103,94 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
setMousePos({ x: nx, y: ny }); setMousePos({ x: nx, y: ny });
if (dragging) { if (dragging) {
if (dragging.type === 'main') { if (dragging.type === 'main') {
const newSplits = { ...splits, x: [...safeX], y: [...safeY] }; const newSplits = { ...splits, x: [...safeX], y: [...safeY] };
const val = dragging.axis === 'x' ? nx : ny; const val = dragging.axis === 'x' ? nx : ny;
newSplits[dragging.axis][dragging.index] = val; newSplits[dragging.axis][dragging.index] = val;
onChange(newSplits); onChange(newSplits);
} else {
const [iStr, jStr] = dragging.cellKey.split('-');
const i = parseInt(iStr); const j = parseInt(jStr);
const cellX1 = sortedX[i]; const cellX2 = sortedX[i+1];
const cellY1 = sortedY[j]; const cellY2 = sortedY[j+1];
let newOffset = 0;
if (dragging.axis === 'x') {
newOffset = (nx - cellX1) / (cellX2 - cellX1);
} else { } else {
newOffset = (ny - cellY1) / (cellY2 - cellY1); const [iStr, jStr] = dragging.cellKey.split('-');
const i = parseInt(iStr); const j = parseInt(jStr);
const cellX1 = sortedX[i]; const cellX2 = sortedX[i+1];
const cellY1 = sortedY[j]; const cellY2 = sortedY[j+1];
let newOffset = 0;
if (dragging.axis === 'x') {
newOffset = (nx - cellX1) / (cellX2 - cellX1);
} else {
newOffset = (ny - cellY1) / (cellY2 - cellY1);
}
newOffset = Math.max(0.05, Math.min(0.95, newOffset));
if (!isNaN(newOffset)) {
updatePartition(dragging.cellKey, dragging.id, { offset: newOffset });
}
} }
newOffset = Math.max(0.05, Math.min(0.95, newOffset)); return;
if (!isNaN(newOffset)) {
updatePartition(dragging.cellKey, dragging.id, { offset: newOffset });
}
}
return;
} }
if (isButtonHovered) return; if (isButtonHovered) return;
if (mode === 'lines') { if (mode === 'lines') {
setHoveredMainSplit(null); setHoveredMainSplit(null);
const SNAP = 0.015; const SNAP = 0.015;
if (nx > SNAP && nx < 1 - SNAP && ny > SNAP && ny < 1 - SNAP) { if (nx > SNAP && nx < 1-SNAP && ny > SNAP && ny < 1-SNAP) {
const closeToX = safeX.some(val => Math.abs(nx - val) < SNAP); const closeToX = safeX.some(val => Math.abs(nx - val) < SNAP);
const closeToY = safeY.some(val => Math.abs(ny - val) < SNAP); const closeToY = safeY.some(val => Math.abs(ny - val) < SNAP);
if (!closeToX && !closeToY) { if (!closeToX && !closeToY) {
const distRight = 1 - nx; const distBottom = 1 - ny; const distRight = 1 - nx; const distBottom = 1 - ny;
setPhantomMainAxis(Math.min(nx, distRight) < Math.min(ny, distBottom) ? 'y' : 'x'); setPhantomMainAxis(Math.min(nx, distRight) < Math.min(ny, distBottom) ? 'y' : 'x');
} else { } else { setPhantomMainAxis(null); }
setPhantomMainAxis(null);
} }
} } else if (mode === 'cells') {
} else { setHoveredPartition(null);
// Cells Mode setPhantomPartition(null);
setHoveredPartition(null); setHoveredCell(null);
setPhantomPartition(null);
setHoveredCell(null);
// Find Cell let cellIdx = null;
let cellIdx = null; for(let i=0; i<sortedX.length-1; i++) {
for (let i = 0; i < sortedX.length - 1; i++) { if (nx >= sortedX[i] && nx <= sortedX[i+1]) {
if (nx >= sortedX[i] && nx <= sortedX[i+1]) { for(let j=0; j<sortedY.length-1; j++) {
for (let j = 0; j < sortedY.length - 1; j++) { if (ny >= sortedY[j] && ny <= sortedY[j+1]) {
if (ny >= sortedY[j] && ny <= sortedY[j+1]) { cellIdx = { i, j };
cellIdx = { i, j }; break;
break; }
}
} }
}
}
}
if (cellIdx) {
setHoveredCell(cellIdx);
const key = `${cellIdx.i}-${cellIdx.j}`;
const parts = safePartitions[key] || [];
const cx1 = sortedX[cellIdx.i]; const cx2 = sortedX[cellIdx.i+1];
const cy1 = sortedY[cellIdx.j]; const cy2 = sortedY[cellIdx.j+1];
const cw = cx2 - cx1; const ch = cy2 - cy1;
const lx = (nx - cx1) / cw;
const ly = (ny - cy1) / ch;
let found = null;
const SNAP = 0.05;
for (const p of parts) {
if (p.axis === 'x') {
if (Math.abs(lx - p.offset) < SNAP * (aspectRatio > 1 ? 1 : aspectRatio)) found = p;
} else {
if (Math.abs(ly - p.offset) < SNAP * (aspectRatio < 1 ? 1 : 1/aspectRatio)) found = p;
}
} }
if (found) { if (cellIdx) {
setHoveredPartition({ id: found.id, cellKey: key }); setHoveredCell(cellIdx);
} else { const key = `${cellIdx.i}-${cellIdx.j}`;
// Phantom logic const parts = safePartitions[key] || [];
const distL = lx; const distR = 1 - lx;
const distT = ly; const distB = 1 - ly; const cx1 = sortedX[cellIdx.i]; const cx2 = sortedX[cellIdx.i+1];
const minX = Math.min(distL, distR); const cy1 = sortedY[cellIdx.j]; const cy2 = sortedY[cellIdx.j+1];
const minY = Math.min(distT, distB); const cw = cx2 - cx1; const ch = cy2 - cy1;
const axis = minX < minY ? 'y' : 'x'; const lx = (nx - cx1) / cw;
const ly = (ny - cy1) / ch;
if (lx > 0.05 && lx < 0.95 && ly > 0.05 && ly < 0.95) {
setPhantomPartition({ axis, offset: axis === 'x' ? lx : ly }); let found = null;
} const SNAP = 0.05;
for (const p of parts) {
if (p.axis === 'x') {
if (Math.abs(lx - p.offset) < SNAP * (aspectRatio > 1 ? 1 : aspectRatio)) found = p;
} else {
if (Math.abs(ly - p.offset) < SNAP * (aspectRatio < 1 ? 1 : 1/aspectRatio)) found = p;
}
}
if (found) {
setHoveredPartition({ id: found.id, cellKey: key });
} else {
const distLeft = lx; const distRight = 1 - lx;
const distTop = ly; const distBottom = 1 - ly;
const minX = Math.min(distLeft, distRight);
const minY = Math.min(distTop, distBottom);
const axis = minX < minY ? 'y' : 'x';
if (lx > 0.05 && lx < 0.95 && ly > 0.05 && ly < 0.95) {
setPhantomPartition({ axis, offset: axis === 'x' ? lx : ly });
}
}
} }
}
} }
}; };
@@ -216,41 +198,40 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
if (isButtonHovered) return; if (isButtonHovered) return;
if (mode === 'lines') { if (mode === 'lines') {
if (hoveredMainSplit) { if (hoveredMainSplit) {
if (e.button === 0) setDragging({ type: 'main', ...hoveredMainSplit }); if (e.button === 0) setDragging({ type: 'main', ...hoveredMainSplit });
else if (e.button === 2) removeMainSplit(hoveredMainSplit.axis, hoveredMainSplit.index); else if (e.button === 2) removeMainSplit(hoveredMainSplit.axis, hoveredMainSplit.index);
} else if (phantomMainAxis) { } else if (phantomMainAxis) {
const val = phantomMainAxis === 'x' ? mousePos.x : mousePos.y; const val = phantomMainAxis === 'x' ? mousePos.x : mousePos.y;
const newSplits = { ...splits, x: [...safeX], y: [...safeY] }; const newSplits = { ...splits, x: [...safeX], y: [...safeY] };
newSplits[phantomMainAxis] = [...newSplits[phantomMainAxis], val]; newSplits[phantomMainAxis] = [...newSplits[phantomMainAxis], val];
onChange(newSplits); onChange(newSplits);
setDragging({ type: 'main', axis: phantomMainAxis, index: newSplits[phantomMainAxis].length - 1 }); setDragging({ type: 'main', axis: phantomMainAxis, index: newSplits[phantomMainAxis].length - 1 });
}
} else {
if (hoveredPartition) {
const key = hoveredPartition.cellKey;
const parts = safePartitions[key] || [];
const part = parts.find(p => p.id === hoveredPartition.id);
if (part) {
if (e.button === 0) {
setDragging({ type: 'partition', cellKey: key, id: part.id, axis: part.axis });
setSelectedPartitionId(part.id);
} else if (e.button === 2) {
removePartition(key, part.id);
}
} }
} else if (hoveredCell && phantomPartition) { } else if (mode === 'cells') {
if (e.button === 0) { if (hoveredPartition) {
createPartition(hoveredCell.i, hoveredCell.j, phantomPartition.axis, phantomPartition.offset); const key = hoveredPartition.cellKey;
const parts = safePartitions[key] || [];
const part = parts.find(p => p.id === hoveredPartition.id);
if (part) {
if (e.button === 0) {
setDragging({ type: 'partition', cellKey: key, id: part.id, axis: part.axis });
setSelectedPartitionId(part.id);
} else if (e.button === 2) {
removePartition(key, part.id);
}
}
} else if (hoveredCell && phantomPartition) {
if (e.button === 0) {
createPartition(hoveredCell.i, hoveredCell.j, phantomPartition.axis, phantomPartition.offset);
}
} else {
setSelectedPartitionId(null);
} }
} else {
setSelectedPartitionId(null);
}
} }
}; };
// -- RENDER HELPERS (Для чистоты кода) -- // --- RENDER HELPERS ---
// Рисуем сетку ячеек отдельно, чтобы не путаться в скобках
const renderCells = () => { const renderCells = () => {
const elements = []; const elements = [];
for (let i = 0; i < sortedX.length - 1; i++) { for (let i = 0; i < sortedX.length - 1; i++) {
@@ -260,7 +241,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const y1 = sortedY[j]; const y1 = sortedY[j];
const y2 = sortedY[j + 1]; const y2 = sortedY[j + 1];
// SAFETY CHECK
if (y2 === undefined || x2 === undefined) continue; if (y2 === undefined || x2 === undefined) continue;
const cellX = x1 * viewBoxW; const cellX = x1 * viewBoxW;
@@ -275,17 +255,12 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
elements.push( elements.push(
<g key={key}> <g key={key}>
{/* Hover Highlight */}
{isHovered && ( {isHovered && (
<rect x={cellX} y={cellY} width={cellW} height={cellH} fill="rgba(16, 185, 129, 0.05)" stroke="#10b981" strokeWidth="2" strokeDasharray="4,4" className="pointer-events-none"/> <rect x={cellX} y={cellY} width={cellW} height={cellH} fill="rgba(16, 185, 129, 0.05)" stroke="#10b981" strokeWidth="2" strokeDasharray="4,4" className="pointer-events-none"/>
)} )}
{/* Selection Highlight */}
{isSelected && mode === 'cells' && ( {isSelected && mode === 'cells' && (
<rect x={cellX} y={cellY} width={cellW} height={cellH} fill="transparent" stroke="#a855f7" strokeWidth="2" className="pointer-events-none opacity-50"/> <rect x={cellX} y={cellY} width={cellW} height={cellH} fill="transparent" stroke="#a855f7" strokeWidth="2" className="pointer-events-none opacity-50"/>
)} )}
{/* Partitions inside this cell */}
{parts.map(p => { {parts.map(p => {
let lx1, ly1, lx2, ly2; let lx1, ly1, lx2, ly2;
if (p.axis === 'x') { if (p.axis === 'x') {
@@ -295,7 +270,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const py = cellY + (cellH * p.offset); const py = cellY + (cellH * p.offset);
lx1 = cellX; ly1 = py; lx2 = cellX + cellW; ly2 = py; lx1 = cellX; ly1 = py; lx2 = cellX + cellW; ly2 = py;
} }
const isPSelected = selectedPartitionId === p.id; const isPSelected = selectedPartitionId === p.id;
const isPHovered = hoveredPartition?.id === p.id; const isPHovered = hoveredPartition?.id === p.id;
const stroke = isPSelected ? "#a855f7" : (isPHovered ? "#d8b4fe" : "#7e22ce"); const stroke = isPSelected ? "#a855f7" : (isPHovered ? "#d8b4fe" : "#7e22ce");
@@ -307,22 +281,12 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
</g> </g>
); );
})} })}
{/* Phantom Partition in this cell */}
{isHovered && phantomPartition && !hoveredPartition && !dragging && ( {isHovered && phantomPartition && !hoveredPartition && !dragging && (
<g className="pointer-events-none opacity-60"> <g className="pointer-events-none opacity-60">
{phantomPartition.axis === 'x' ? ( {phantomPartition.axis === 'x' ? (
<line <line x1={cellX + (cellW * phantomPartition.offset)} y1={cellY} x2={cellX + (cellW * phantomPartition.offset)} y2={cellY + cellH} stroke="#34d399" strokeWidth="3" strokeDasharray="6,6"/>
x1={cellX + (cellW * phantomPartition.offset)} y1={cellY}
x2={cellX + (cellW * phantomPartition.offset)} y2={cellY + cellH}
stroke="#34d399" strokeWidth="3" strokeDasharray="6,6"
/>
) : ( ) : (
<line <line x1={cellX} y1={cellY + (cellH * phantomPartition.offset)} x2={cellX + cellW} y2={cellY + (cellH * phantomPartition.offset)} stroke="#34d399" strokeWidth="3" strokeDasharray="6,6"/>
x1={cellX} y1={cellY + (cellH * phantomPartition.offset)}
x2={cellX + cellW} y2={cellY + (cellH * phantomPartition.offset)}
stroke="#34d399" strokeWidth="3" strokeDasharray="6,6"
/>
)} )}
</g> </g>
)} )}
@@ -341,18 +305,10 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
<h2 className="text-lg font-bold flex items-center gap-2 text-primary"> <h2 className="text-lg font-bold flex items-center gap-2 text-primary">
<Grid size={20} /> 2. Редактор макета <Grid size={20} /> 2. Редактор макета
</h2> </h2>
<div className="flex bg-slate-800 p-1 rounded-lg border border-slate-700"> <div className="flex bg-slate-800 p-1 rounded-lg border border-slate-700">
<button onClick={() => { setMode('lines'); setSelectedPartitionId(null); }} <button onClick={() => { setMode('lines'); setSelectedPartitionId(null); }} className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-xs font-bold transition-all ${mode === 'lines' ? 'bg-primary text-white shadow' : 'text-gray-400 hover:text-gray-200'}`}>Границы</button>
className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-xs font-bold transition-all ${mode === 'lines' ? 'bg-primary text-white shadow' : 'text-gray-400 hover:text-gray-200'}`}> <button onClick={() => setMode('cells')} className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-xs font-bold transition-all ${mode === 'cells' ? 'bg-primary text-white shadow' : 'text-gray-400 hover:text-gray-200'}`}>Внутри ячеек</button>
<Move size={14}/> Границы
</button>
<button onClick={() => setMode('cells')}
className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-xs font-bold transition-all ${mode === 'cells' ? 'bg-primary text-white shadow' : 'text-gray-400 hover:text-gray-200'}`}>
<Grid size={14}/> Внутри ячеек
</button>
</div> </div>
<button onClick={() => onChange({ x: [], y: [], partitions: {} })} className="px-3 py-1.5 text-xs text-red-400 border border-slate-700 rounded hover:bg-slate-800 transition-colors flex items-center gap-1"> <button onClick={() => onChange({ x: [], y: [], partitions: {} })} className="px-3 py-1.5 text-xs text-red-400 border border-slate-700 rounded hover:bg-slate-800 transition-colors flex items-center gap-1">
<RotateCcw size={14} /> Сброс <RotateCcw size={14} /> Сброс
</button> </button>
@@ -368,7 +324,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
</div> </div>
) : ( ) : (
<div className="flex gap-3"> <div className="flex gap-3">
<span><b className="text-green-400">ЛКМ:</b> Стенка</span> <span><b className="text-green-400">ЛКМ в ячейке:</b> Стенка</span>
<span><b className="text-purple-400">Драг:</b> Двигать</span> <span><b className="text-purple-400">Драг:</b> Двигать</span>
<span><b className="text-red-400">2xЛКМ:</b> Удалить</span> <span><b className="text-red-400">2xЛКМ:</b> Удалить</span>
</div> </div>
@@ -381,6 +337,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
<div <div
className="relative shadow-2xl bg-[#1e293b] border border-slate-600 rounded-sm overflow-hidden" className="relative shadow-2xl bg-[#1e293b] border border-slate-600 rounded-sm overflow-hidden"
style={{ style={{
// Удален preserveAspectRatio="none", теперь используем правильный масштабирование
width: aspectRatio > 1 ? 'auto' : '100%', width: aspectRatio > 1 ? 'auto' : '100%',
height: aspectRatio > 1 ? '100%' : 'auto', height: aspectRatio > 1 ? '100%' : 'auto',
aspectRatio: `${1/aspectRatio}`, aspectRatio: `${1/aspectRatio}`,
@@ -389,16 +346,8 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
: (dragging ? 'grabbing' : hoveredPartition ? 'grab' : hoveredCell ? 'crosshair' : 'default'), : (dragging ? 'grabbing' : hoveredPartition ? 'grab' : hoveredCell ? 'crosshair' : 'default'),
}} }}
> >
<svg <svg ref={svgRef} viewBox={`0 0 ${viewBoxW} ${viewBoxH}`} className="w-full h-full touch-none block"
ref={svgRef} onMouseMove={handleMouseMove} onMouseDown={handleMouseDown} onMouseUp={() => setDragging(null)} onMouseLeave={() => setDragging(null)} onContextMenu={(e) => e.preventDefault()}
viewBox={`0 0 ${viewBoxW} ${viewBoxH}`}
className="w-full h-full touch-none block"
preserveAspectRatio="none"
onMouseMove={handleMouseMove}
onMouseDown={handleMouseDown}
onMouseUp={() => setDragging(null)}
onMouseLeave={() => setDragging(null)}
onContextMenu={(e) => e.preventDefault()}
> >
<defs> <defs>
<pattern id="grid" width="50" height="50" patternUnits="userSpaceOnUse"> <pattern id="grid" width="50" height="50" patternUnits="userSpaceOnUse">
@@ -407,17 +356,13 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
</defs> </defs>
<rect width="100%" height="100%" fill="url(#grid)" /> <rect width="100%" height="100%" fill="url(#grid)" />
{/* 1. RENDER CELLS AND PARTITIONS */}
{renderCells()} {renderCells()}
{/* 2. RENDER MAIN GRID LINES */} {/* Main Grid Lines */}
{safeX.map((x, i) => { {safeX.map((x, i) => {
const isHovered = hoveredMainSplit?.axis === 'x' && hoveredMainSplit.index === i; const isHovered = hoveredMainSplit?.axis === 'x' && hoveredMainSplit.index === i;
return ( return (
<g key={`x-${i}`} <g key={`x-${i}`} onMouseMove={(e) => { if(mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredMainSplit({axis: 'x', index: i}); setPhantomMainAxis(null); }}} onDoubleClick={(e) => { e.stopPropagation(); removeMainSplit('x', i); }}>
onMouseMove={(e) => { if(mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredMainSplit({axis: 'x', index: i}); setPhantomMainAxis(null); }}}
onDoubleClick={(e) => { e.stopPropagation(); removeMainSplit('x', i); }}
>
<line x1={x * viewBoxW} y1="0" x2={x * viewBoxW} y2="100%" stroke="transparent" strokeWidth="40" className={mode === 'lines' ? "cursor-col-resize" : ""} /> <line x1={x * viewBoxW} y1="0" x2={x * viewBoxW} y2="100%" stroke="transparent" strokeWidth="40" className={mode === 'lines' ? "cursor-col-resize" : ""} />
<line x1={x * viewBoxW} y1="0" x2={x * viewBoxW} y2="100%" stroke={isHovered ? "#f59e0b" : "#64748b"} strokeWidth={isHovered ? 6 : 4} className="pointer-events-none" /> <line x1={x * viewBoxW} y1="0" x2={x * viewBoxW} y2="100%" stroke={isHovered ? "#f59e0b" : "#64748b"} strokeWidth={isHovered ? 6 : 4} className="pointer-events-none" />
</g> </g>
@@ -426,71 +371,41 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
{safeY.map((y, i) => { {safeY.map((y, i) => {
const isHovered = hoveredMainSplit?.axis === 'y' && hoveredMainSplit.index === i; const isHovered = hoveredMainSplit?.axis === 'y' && hoveredMainSplit.index === i;
return ( return (
<g key={`y-${i}`} <g key={`y-${i}`} onMouseMove={(e) => { if(mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredMainSplit({axis: 'y', index: i}); setPhantomMainAxis(null); }}} onDoubleClick={(e) => { e.stopPropagation(); removeMainSplit('y', i); }}>
onMouseMove={(e) => { if(mode === 'lines' && !dragging) { e.stopPropagation(); setHoveredMainSplit({axis: 'y', index: i}); setPhantomMainAxis(null); }}}
onDoubleClick={(e) => { e.stopPropagation(); removeMainSplit('y', i); }}
>
<line x1="0" y1={y * viewBoxH} x2="100%" y2={y * viewBoxH} stroke="transparent" strokeWidth="40" className={mode === 'lines' ? "cursor-row-resize" : ""} /> <line x1="0" y1={y * viewBoxH} x2="100%" y2={y * viewBoxH} stroke="transparent" strokeWidth="40" className={mode === 'lines' ? "cursor-row-resize" : ""} />
<line x1="0" y1={y * viewBoxH} x2="100%" y2={y * viewBoxH} stroke={isHovered ? "#f59e0b" : "#64748b"} strokeWidth={isHovered ? 6 : 4} className="pointer-events-none" /> <line x1="0" y1={y * viewBoxH} x2="100%" y2={y * viewBoxH} stroke={isHovered ? "#f59e0b" : "#64748b"} strokeWidth={isHovered ? 6 : 4} className="pointer-events-none" />
</g> </g>
); );
})} })}
{/* 3. RENDER MAIN PHANTOMS */} {/* Phantoms */}
{mode === 'lines' && !hoveredMainSplit && !dragging && phantomMainAxis === 'x' && ( {mode === 'lines' && !hoveredMainSplit && !dragging && phantomMainAxis === 'x' && <line x1={mousePos.x * viewBoxW} y1="0" x2={mousePos.x * viewBoxW} y2="100%" stroke="#3b82f6" strokeWidth="4" strokeDasharray="8,8" className="pointer-events-none opacity-50"/>}
<line x1={mousePos.x * viewBoxW} y1="0" x2={mousePos.x * viewBoxW} y2="100%" stroke="#3b82f6" strokeWidth="4" strokeDasharray="8,8" className="pointer-events-none opacity-50"/> {mode === 'lines' && !hoveredMainSplit && !dragging && phantomMainAxis === 'y' && <line x1="0" y1={mousePos.y * viewBoxH} x2="100%" y2={mousePos.y * viewBoxH} stroke="#3b82f6" strokeWidth="4" strokeDasharray="8,8" className="pointer-events-none opacity-50"/>}
)}
{mode === 'lines' && !hoveredMainSplit && !dragging && phantomMainAxis === 'y' && (
<line x1="0" y1={mousePos.y * viewBoxH} x2="100%" y2={mousePos.y * viewBoxH} stroke="#3b82f6" strokeWidth="4" strokeDasharray="8,8" className="pointer-events-none opacity-50"/>
)}
</svg> </svg>
</div> </div>
</div> </div>
{/* --- SIDEBAR --- */} {/* SIDEBAR */}
{mode === 'cells' && selectedData && ( {mode === 'cells' && selectedData && (
<div className="absolute top-0 right-0 bottom-0 w-72 bg-slate-900 border-l border-slate-700 p-4 shadow-2xl flex flex-col z-30 animate-in slide-in-from-right duration-200"> <div className="absolute top-0 right-0 bottom-0 w-72 bg-slate-900 border-l border-slate-700 p-4 shadow-2xl flex flex-col z-30 animate-in slide-in-from-right duration-200">
<div className="flex justify-between items-center mb-6"> <div className="flex justify-between items-center mb-6">
<h3 className="text-sm font-bold text-white flex items-center gap-2"> <h3 className="text-sm font-bold text-white flex items-center gap-2">Настройки стенки</h3>
<Grid size={16} className="text-purple-400"/> Настройки стенки
</h3>
<button onClick={() => setSelectedPartitionId(null)} className="text-gray-400 hover:text-white"><X size={20}/></button> <button onClick={() => setSelectedPartitionId(null)} className="text-gray-400 hover:text-white"><X size={20}/></button>
</div> </div>
<div className="bg-slate-800 p-4 rounded border border-slate-700 space-y-6"> <div className="bg-slate-800 p-4 rounded border border-slate-700 space-y-6">
{/* Height Control */}
<div> <div>
<div className="flex justify-between text-xs text-gray-300 mb-2"> <div className="flex justify-between text-xs text-gray-300 mb-2"><span>Высота</span> <span className="font-mono bg-slate-900 px-1.5 py-0.5 rounded text-xs">{selectedData.part.height} мм</span></div>
<span>Высота</span> <input type="range" min="5" max={config.drawer.height || 100} step="1" value={selectedData.part.height} onChange={(e) => updatePartition(selectedData.key, selectedData.part.id, { height: parseFloat(e.target.value) })} className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-purple-500"/>
<span className="font-mono bg-slate-900 px-1.5 py-0.5 rounded text-xs">{selectedData.part.height} мм</span>
</div>
<input type="range" min="5" max={config.drawer.height || 100} step="1" value={selectedData.part.height}
onChange={(e) => updatePartition(selectedData.key, selectedData.part.id, { height: parseFloat(e.target.value) })}
className="w-full h-1 bg-slate-600 rounded-lg appearance-none cursor-pointer accent-purple-500"
/>
</div> </div>
{/* Rounded Control */}
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<label htmlFor="rounded-check" className="text-xs text-gray-300 cursor-pointer select-none">Скруглить края</label> <label htmlFor="rounded-check" className="text-xs text-gray-300 cursor-pointer select-none">Скруглить края</label>
<input type="checkbox" id="rounded-check" checked={selectedData.part.rounded} <input type="checkbox" id="rounded-check" checked={selectedData.part.rounded} onChange={(e) => updatePartition(selectedData.key, selectedData.part.id, { rounded: e.target.checked })} className="w-4 h-4 rounded bg-slate-700 border-slate-600 text-purple-500 focus:ring-0 cursor-pointer"/>
onChange={(e) => updatePartition(selectedData.key, selectedData.part.id, { rounded: e.target.checked })}
className="w-4 h-4 rounded bg-slate-700 border-slate-600 text-purple-500 focus:ring-0 cursor-pointer"
/>
</div> </div>
<button onClick={() => removePartition(selectedData.key, selectedData.part.id)} className="w-full py-2 bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/30 rounded text-xs flex items-center justify-center gap-2 transition-colors mt-4">
{/* Delete Button */}
<button
onClick={() => removePartition(selectedData.key, selectedData.part.id)}
className="w-full py-2 bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/30 rounded text-xs flex items-center justify-center gap-2 transition-colors mt-4"
>
<Trash2 size={14}/> Удалить <Trash2 size={14}/> Удалить
</button> </button>
</div> </div>
<div className="mt-auto text-[10px] text-gray-500 text-center leading-relaxed">Выделите стенку для настройки.<br/>Двойной клик удаляет её.</div>
<div className="mt-auto text-[10px] text-gray-500 text-center leading-relaxed">
Выделите стенку для настройки.<br/>Двойной клик удаляет её.
</div>
</div> </div>
)} )}
</div> </div>