Fix select line
This commit is contained in:
@@ -13,18 +13,12 @@ type Axis = 'x' | 'y';
|
||||
export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
const svgRef = useRef<SVGSVGElement>(null);
|
||||
|
||||
// State for interaction
|
||||
// State
|
||||
const [phantomAxis, setPhantomAxis] = useState<Axis | null>(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);
|
||||
|
||||
// Constants
|
||||
// Увеличил порог с 0.02 до 0.03, чтобы линию было легче поймать
|
||||
const SNAP_THRESHOLD = 0.03;
|
||||
const viewBoxW = 1000;
|
||||
const aspectRatio = config.drawer.depth / config.drawer.width;
|
||||
const viewBoxH = viewBoxW * aspectRatio;
|
||||
@@ -32,8 +26,8 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
const sortedX = useMemo(() => [0, ...splits.x, 1].sort((a, b) => a - b), [splits.x]);
|
||||
const sortedY = useMemo(() => [0, ...splits.y, 1].sort((a, b) => a - b), [splits.y]);
|
||||
|
||||
// Handlers
|
||||
const handleMouseMove = (e: React.MouseEvent) => {
|
||||
// --- Глобальный обработчик (для пустого места и перетаскивания) ---
|
||||
const handleGlobalMouseMove = (e: React.MouseEvent) => {
|
||||
if (!svgRef.current) return;
|
||||
const rect = svgRef.current.getBoundingClientRect();
|
||||
const nx = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
|
||||
@@ -41,6 +35,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
|
||||
setMousePos({ x: nx, y: ny });
|
||||
|
||||
// 1. Если тащим - обновляем позицию
|
||||
if (dragging) {
|
||||
const newSplits = {
|
||||
x: [...splits.x],
|
||||
@@ -52,71 +47,69 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// ИСПРАВЛЕНИЕ: Если мышка на кнопке удаления, не пересчитываем hover,
|
||||
// чтобы кнопка не исчезла под курсором.
|
||||
if (isButtonHovered) return;
|
||||
// 2. Если мы здесь, значит мышка НЕ на существующей линии
|
||||
// (иначе событие было бы перехвачено в handleSplitHover)
|
||||
setHoveredSplit(null);
|
||||
|
||||
// Hover logic
|
||||
let foundSplit = null;
|
||||
// 3. Логика Фантомной линии (создание новой)
|
||||
const SNAP_THRESHOLD = 0.02;
|
||||
|
||||
// Check X splits
|
||||
splits.x.forEach((val, idx) => {
|
||||
if (Math.abs(nx - val) < SNAP_THRESHOLD) foundSplit = { axis: 'x' as Axis, index: idx };
|
||||
});
|
||||
// Проверяем, не слишком ли мы близко к краям или другим линиям (чтобы не спамить)
|
||||
const closeToX = splits.x.some(val => Math.abs(nx - val) < SNAP_THRESHOLD);
|
||||
const closeToY = splits.y.some(val => Math.abs(ny - val) < SNAP_THRESHOLD);
|
||||
const closeToEdgeX = nx < SNAP_THRESHOLD || nx > (1 - SNAP_THRESHOLD);
|
||||
const closeToEdgeY = ny < SNAP_THRESHOLD || ny > (1 - SNAP_THRESHOLD);
|
||||
|
||||
// Check Y splits
|
||||
if (!foundSplit) {
|
||||
splits.y.forEach((val, idx) => {
|
||||
if (Math.abs(ny - val) < SNAP_THRESHOLD) foundSplit = { axis: 'y' as Axis, index: idx };
|
||||
});
|
||||
const distLeft = nx;
|
||||
const distRight = 1 - nx;
|
||||
const distTop = ny;
|
||||
const distBottom = 1 - ny;
|
||||
|
||||
const minXDist = Math.min(distLeft, distRight);
|
||||
const minYDist = Math.min(distTop, distBottom);
|
||||
|
||||
// Определяем ось по близости к краю
|
||||
let potentialAxis: Axis = minXDist < minYDist ? 'y' : 'x';
|
||||
|
||||
let valid = true;
|
||||
if (potentialAxis === 'x') {
|
||||
if (closeToX || closeToEdgeX) valid = false;
|
||||
} else {
|
||||
if (closeToY || closeToEdgeY) valid = false;
|
||||
}
|
||||
|
||||
setHoveredSplit(foundSplit);
|
||||
|
||||
// Phantom line logic
|
||||
if (!foundSplit) {
|
||||
const closeToX = splits.x.some(val => Math.abs(nx - val) < SNAP_THRESHOLD);
|
||||
const closeToY = splits.y.some(val => Math.abs(ny - val) < SNAP_THRESHOLD);
|
||||
const closeToEdgeX = nx < SNAP_THRESHOLD || nx > (1 - SNAP_THRESHOLD);
|
||||
const closeToEdgeY = ny < SNAP_THRESHOLD || ny > (1 - SNAP_THRESHOLD);
|
||||
|
||||
const distLeft = nx;
|
||||
const distRight = 1 - nx;
|
||||
const distTop = ny;
|
||||
const distBottom = 1 - ny;
|
||||
|
||||
const minXDist = Math.min(distLeft, distRight);
|
||||
const minYDist = Math.min(distTop, distBottom);
|
||||
|
||||
let potentialAxis: Axis = minXDist < minYDist ? 'y' : 'x';
|
||||
|
||||
let valid = true;
|
||||
if (potentialAxis === 'x') {
|
||||
if (closeToX || closeToEdgeX) valid = false;
|
||||
} else {
|
||||
if (closeToY || closeToEdgeY) valid = false;
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
setPhantomAxis(potentialAxis);
|
||||
} else {
|
||||
setPhantomAxis(null);
|
||||
}
|
||||
if (valid) {
|
||||
setPhantomAxis(potentialAxis);
|
||||
} else {
|
||||
setPhantomAxis(null);
|
||||
}
|
||||
};
|
||||
|
||||
// --- Обработчик наведения на КОНКРЕТНУЮ линию ---
|
||||
const handleSplitHover = (e: React.MouseEvent, axis: Axis, index: number) => {
|
||||
// Если мы тащим линию, позволяем событию всплыть до глобального обработчика,
|
||||
// чтобы он посчитал координаты.
|
||||
if (dragging) return;
|
||||
|
||||
// Если просто водим мышкой - блокируем всплытие,
|
||||
// чтобы глобальный обработчик не думал, что мы в пустоте.
|
||||
e.stopPropagation();
|
||||
|
||||
setHoveredSplit({ axis, index });
|
||||
setPhantomAxis(null); // Убираем фантом, раз мы на линии
|
||||
};
|
||||
|
||||
const handleMouseDown = (e: React.MouseEvent) => {
|
||||
// Если нажали на кнопку, событие там обработается (stopPropagation)
|
||||
// Здесь обрабатываем только клик по пустому месту или начало драга линии
|
||||
if (hoveredSplit && !isButtonHovered) {
|
||||
// Клик по существующей линии (hoveredSplit уже установлен через onMouseMove линии)
|
||||
if (hoveredSplit) {
|
||||
if (e.button === 0) {
|
||||
setDragging(hoveredSplit);
|
||||
} else if (e.button === 2) {
|
||||
removeSplit(hoveredSplit.axis, hoveredSplit.index);
|
||||
}
|
||||
} else if (phantomAxis && !isButtonHovered) {
|
||||
}
|
||||
// Клик по пустому месту (создание)
|
||||
else if (phantomAxis) {
|
||||
const val = phantomAxis === 'x' ? mousePos.x : mousePos.y;
|
||||
const newSplits = { ...splits };
|
||||
newSplits[phantomAxis] = [...newSplits[phantomAxis], val];
|
||||
@@ -135,7 +128,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
onChange(newSplits);
|
||||
setHoveredSplit(null);
|
||||
setDragging(null);
|
||||
setIsButtonHovered(false); // Сбрасываем блокировку
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -155,7 +147,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
<div className="flex flex-col h-full select-none">
|
||||
<div className="flex-1 bg-slate-800/30 rounded-lg p-6 flex flex-col items-center justify-center relative overflow-hidden border border-slate-700/50">
|
||||
|
||||
{/* Instruction Box */}
|
||||
<div className="absolute top-4 left-4 z-10 bg-slate-900/90 p-3 rounded-lg backdrop-blur border border-slate-700 shadow-xl max-w-[200px] pointer-events-none">
|
||||
<div className="flex items-center gap-2 font-bold text-gray-100 mb-2 text-sm">
|
||||
<MousePointer2 size={14} className="text-primary"/> Инструкция
|
||||
@@ -167,7 +158,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
{/* Rulers */}
|
||||
<div className="w-full flex justify-between px-8 mb-1 max-w-[900px]">
|
||||
<span className="text-xs text-slate-500 font-mono">0</span>
|
||||
<span className="text-xs text-slate-500 font-mono">{config.drawer.width} мм</span>
|
||||
@@ -179,7 +169,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
<span className="text-xs text-slate-500 font-mono" style={{writingMode: 'vertical-rl'}}>{config.drawer.depth} мм</span>
|
||||
</div>
|
||||
|
||||
{/* Main Interactive SVG */}
|
||||
<div
|
||||
className="relative shadow-2xl bg-[#1e293b] border border-slate-600 rounded-sm overflow-hidden"
|
||||
style={{
|
||||
@@ -194,7 +183,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
ref={svgRef}
|
||||
viewBox={`0 0 ${viewBoxW} ${viewBoxH}`}
|
||||
className="w-full h-full touch-none"
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseMove={handleGlobalMouseMove}
|
||||
onMouseDown={handleMouseDown}
|
||||
onMouseUp={handleMouseUp}
|
||||
onMouseLeave={handleMouseUp}
|
||||
@@ -207,7 +196,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="url(#grid)" />
|
||||
|
||||
{/* --- Cell Dimensions Labels --- */}
|
||||
{/* --- Labels --- */}
|
||||
{sortedX.slice(0, -1).map((x1, i) => {
|
||||
const x2 = sortedX[i + 1];
|
||||
return sortedY.slice(0, -1).map((y1, j) => {
|
||||
@@ -244,7 +233,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
});
|
||||
})}
|
||||
|
||||
{/* --- Existing X Lines (Vertical) --- */}
|
||||
{/* --- X Lines (Vertical) --- */}
|
||||
{splits.x.map((x, i) => {
|
||||
const isHovered = hoveredSplit?.axis === 'x' && hoveredSplit.index === i;
|
||||
const isDragging = dragging?.axis === 'x' && dragging.index === i;
|
||||
@@ -252,26 +241,28 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
const width = isHovered || isDragging ? 8 : 4;
|
||||
|
||||
return (
|
||||
<g key={`x-${i}`} onDoubleClick={() => removeSplit('x', i)}>
|
||||
<g
|
||||
key={`x-${i}`}
|
||||
onDoubleClick={() => removeSplit('x', i)}
|
||||
// ВАЖНО: Событие вешаем на группу
|
||||
onMouseMove={(e) => handleSplitHover(e, 'x', i)}
|
||||
>
|
||||
{/* Невидимая широкая зона захвата (80 единиц!) */}
|
||||
<line
|
||||
x1={x * viewBoxW} y1={0}
|
||||
x2={x * viewBoxW} y2={viewBoxH}
|
||||
stroke="transparent" strokeWidth="60"
|
||||
className="cursor-col-resize hover:stroke-white/5"
|
||||
stroke="transparent" strokeWidth="80"
|
||||
className="cursor-col-resize"
|
||||
/>
|
||||
{/* Видимая линия */}
|
||||
<line
|
||||
x1={x * viewBoxW} y1={0}
|
||||
x2={x * viewBoxW} y2={viewBoxH}
|
||||
stroke={color} strokeWidth={width}
|
||||
className="transition-all duration-150"
|
||||
className="transition-all duration-150 pointer-events-none"
|
||||
/>
|
||||
{(isHovered || isDragging) && (
|
||||
<g
|
||||
transform={`translate(${x * viewBoxW}, 40)`}
|
||||
onClick={(e) => { e.stopPropagation(); removeSplit('x', i); }}
|
||||
onMouseEnter={() => setIsButtonHovered(true)} // Блокируем скрытие
|
||||
onMouseLeave={() => setIsButtonHovered(false)}
|
||||
>
|
||||
<g transform={`translate(${x * viewBoxW}, 40)`} onClick={(e) => { e.stopPropagation(); removeSplit('x', i); }}>
|
||||
<circle r="16" fill="#ef4444" className="cursor-pointer hover:scale-110 transition-transform shadow-lg"/>
|
||||
<Trash2 size={16} color="white" x={-8} y={-8} className="pointer-events-none"/>
|
||||
</g>
|
||||
@@ -280,7 +271,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
);
|
||||
})}
|
||||
|
||||
{/* --- Existing Y Lines (Horizontal) --- */}
|
||||
{/* --- Y Lines (Horizontal) --- */}
|
||||
{splits.y.map((y, i) => {
|
||||
const isHovered = hoveredSplit?.axis === 'y' && hoveredSplit.index === i;
|
||||
const isDragging = dragging?.axis === 'y' && dragging.index === i;
|
||||
@@ -288,26 +279,25 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
const width = isHovered || isDragging ? 8 : 4;
|
||||
|
||||
return (
|
||||
<g key={`y-${i}`} onDoubleClick={() => removeSplit('y', i)}>
|
||||
<g
|
||||
key={`y-${i}`}
|
||||
onDoubleClick={() => removeSplit('y', i)}
|
||||
onMouseMove={(e) => handleSplitHover(e, 'y', i)}
|
||||
>
|
||||
<line
|
||||
x1={0} y1={y * viewBoxH}
|
||||
x2={viewBoxW} y2={y * viewBoxH}
|
||||
stroke="transparent" strokeWidth="60"
|
||||
className="cursor-row-resize hover:stroke-white/5"
|
||||
stroke="transparent" strokeWidth="80"
|
||||
className="cursor-row-resize"
|
||||
/>
|
||||
<line
|
||||
x1={0} y1={y * viewBoxH}
|
||||
x2={viewBoxW} y2={y * viewBoxH}
|
||||
stroke={color} strokeWidth={width}
|
||||
className="transition-all duration-150"
|
||||
className="transition-all duration-150 pointer-events-none"
|
||||
/>
|
||||
{(isHovered || isDragging) && (
|
||||
<g
|
||||
transform={`translate(40, ${y * viewBoxH})`}
|
||||
onClick={(e) => { e.stopPropagation(); removeSplit('y', i); }}
|
||||
onMouseEnter={() => setIsButtonHovered(true)} // Блокируем скрытие
|
||||
onMouseLeave={() => setIsButtonHovered(false)}
|
||||
>
|
||||
<g transform={`translate(40, ${y * viewBoxH})`} onClick={(e) => { e.stopPropagation(); removeSplit('y', i); }}>
|
||||
<circle r="16" fill="#ef4444" className="cursor-pointer hover:scale-110 transition-transform shadow-lg"/>
|
||||
<Trash2 size={16} color="white" x={-8} y={-8} className="pointer-events-none"/>
|
||||
</g>
|
||||
@@ -317,26 +307,26 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
|
||||
})}
|
||||
|
||||
{/* --- Phantom Lines --- */}
|
||||
{!hoveredSplit && !dragging && !isButtonHovered && phantomAxis === 'x' && (
|
||||
<g>
|
||||
{!hoveredSplit && !dragging && phantomAxis === 'x' && (
|
||||
<g className="pointer-events-none">
|
||||
<line
|
||||
x1={mousePos.x * viewBoxW} y1="0"
|
||||
x2={mousePos.x * viewBoxW} y2="100%"
|
||||
stroke="#3b82f6" strokeWidth="4" strokeDasharray="12,8"
|
||||
className="pointer-events-none opacity-60"
|
||||
className="opacity-60"
|
||||
/>
|
||||
<g transform={`translate(${mousePos.x * viewBoxW}, ${viewBoxH/2})`}>
|
||||
<circle r="3" fill="#3b82f6" />
|
||||
</g>
|
||||
</g>
|
||||
)}
|
||||
{!hoveredSplit && !dragging && !isButtonHovered && phantomAxis === 'y' && (
|
||||
<g>
|
||||
{!hoveredSplit && !dragging && phantomAxis === 'y' && (
|
||||
<g className="pointer-events-none">
|
||||
<line
|
||||
x1="0" y1={mousePos.y * viewBoxH}
|
||||
x2="100%" y2={mousePos.y * viewBoxH}
|
||||
stroke="#3b82f6" strokeWidth="4" strokeDasharray="12,8"
|
||||
className="pointer-events-none opacity-60"
|
||||
className="opacity-60"
|
||||
/>
|
||||
<g transform={`translate(${viewBoxW/2}, ${mousePos.y * viewBoxH})`}>
|
||||
<circle r="3" fill="#3b82f6" />
|
||||
|
||||
Reference in New Issue
Block a user