fix guide line in safari

This commit is contained in:
hamid zarghami
2026-04-29 14:31:40 +03:30
parent c5702ec6b3
commit caf9fca227
2 changed files with 134 additions and 18 deletions
+105 -13
View File
@@ -24,6 +24,10 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
const layerRef = useRef<Konva.Layer>(null); const layerRef = useRef<Konva.Layer>(null);
const activeGuideIdsRef = useRef<string[]>([]); const activeGuideIdsRef = useRef<string[]>([]);
const stageWrapRef = useRef<HTMLDivElement>(null); const stageWrapRef = useRef<HTMLDivElement>(null);
const draggingGuideRef = useRef<{
id: string;
orientation: Guide["orientation"];
} | null>(null);
const { const {
tool, tool,
@@ -57,6 +61,7 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
orientation: Guide["orientation"]; orientation: Guide["orientation"];
position: number; position: number;
} | null>(null); } | null>(null);
const cleanupRulerDragRef = useRef<(() => void) | null>(null);
useEffect(() => { useEffect(() => {
if (stageRef.current) { if (stageRef.current) {
@@ -107,6 +112,7 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
}; };
const handleGuideDragEnd = (id: string, position: number, shouldRemove: boolean) => { const handleGuideDragEnd = (id: string, position: number, shouldRemove: boolean) => {
draggingGuideRef.current = null;
if (shouldRemove) { if (shouldRemove) {
removeGuide(id); removeGuide(id);
return; return;
@@ -114,6 +120,51 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
handleGuidePositionChange(id, position); handleGuidePositionChange(id, position);
}; };
useEffect(() => {
const forceFinishGuideDrag = () => {
const draggingGuide = draggingGuideRef.current;
if (!draggingGuide) return;
const stage = stageRef.current;
if (!stage) return;
const node = stage.findOne(`#${draggingGuide.id}`) as Konva.Node | null;
if (!node) {
draggingGuideRef.current = null;
return;
}
if (node.isDragging()) {
node.stopDrag();
}
const nextPosition =
draggingGuide.orientation === "vertical"
? node.x()
: node.y();
const shouldRemove =
draggingGuide.orientation === "vertical"
? nextPosition < 0 || nextPosition > stageSize.width
: nextPosition < 0 || nextPosition > stageSize.height;
handleGuideDragEnd(draggingGuide.id, nextPosition, shouldRemove);
};
window.addEventListener("pointerup", forceFinishGuideDrag);
window.addEventListener("mouseup", forceFinishGuideDrag);
window.addEventListener("touchend", forceFinishGuideDrag);
window.addEventListener("pointercancel", forceFinishGuideDrag);
window.addEventListener("blur", forceFinishGuideDrag);
return () => {
window.removeEventListener("pointerup", forceFinishGuideDrag);
window.removeEventListener("mouseup", forceFinishGuideDrag);
window.removeEventListener("touchend", forceFinishGuideDrag);
window.removeEventListener("pointercancel", forceFinishGuideDrag);
window.removeEventListener("blur", forceFinishGuideDrag);
};
}, [stageSize.height, stageSize.width]);
useEffect(() => { useEffect(() => {
const handleDeleteGuide = (e: KeyboardEvent) => { const handleDeleteGuide = (e: KeyboardEvent) => {
if (!selectedGuideId) return; if (!selectedGuideId) return;
@@ -138,8 +189,21 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
}; };
}, [selectedGuideId]); }, [selectedGuideId]);
const startRulerGuideDrag = (orientation: Guide["orientation"]) => { useEffect(() => {
const updateFromPointer = (event: MouseEvent) => { return () => {
cleanupRulerDragRef.current?.();
cleanupRulerDragRef.current = null;
};
}, []);
const startRulerGuideDrag = (
orientation: Guide["orientation"],
pointerId: number | null,
) => {
cleanupRulerDragRef.current?.();
const updateFromPointer = (event: PointerEvent) => {
if (pointerId !== null && event.pointerId !== pointerId) return null;
const rect = stageWrapRef.current?.getBoundingClientRect(); const rect = stageWrapRef.current?.getBoundingClientRect();
if (!rect) return null; if (!rect) return null;
const rawPos = const rawPos =
@@ -149,7 +213,7 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
return rawPos; return rawPos;
}; };
const updateDraft = (event: MouseEvent) => { const updateDraft = (event: PointerEvent) => {
const pointerPos = updateFromPointer(event); const pointerPos = updateFromPointer(event);
if (pointerPos === null) return; if (pointerPos === null) return;
const boundedPos = const boundedPos =
@@ -159,14 +223,24 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
setDraftGuide({ orientation, position: boundedPos }); setDraftGuide({ orientation, position: boundedPos });
}; };
const handleMouseMove = (event: MouseEvent) => { const handlePointerMove = (event: PointerEvent) => {
updateDraft(event); updateDraft(event);
}; };
const handleMouseUp = (event: MouseEvent) => { const cleanup = () => {
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
window.removeEventListener("pointercancel", handlePointerCancel);
window.removeEventListener("blur", handleBlur);
cleanupRulerDragRef.current = null;
};
const handlePointerUp = (event: PointerEvent) => {
if (pointerId !== null && event.pointerId !== pointerId) return;
const rect = stageWrapRef.current?.getBoundingClientRect(); const rect = stageWrapRef.current?.getBoundingClientRect();
if (!rect) { if (!rect) {
setDraftGuide(null); setDraftGuide(null);
cleanup();
return; return;
} }
@@ -187,12 +261,26 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
} }
setDraftGuide(null); setDraftGuide(null);
window.removeEventListener("mousemove", handleMouseMove); cleanup();
window.removeEventListener("mouseup", handleMouseUp);
}; };
window.addEventListener("mousemove", handleMouseMove); const handlePointerCancel = (event: PointerEvent) => {
window.addEventListener("mouseup", handleMouseUp, { once: true }); if (pointerId !== null && event.pointerId !== pointerId) return;
setDraftGuide(null);
cleanup();
};
const handleBlur = () => {
setDraftGuide(null);
cleanup();
};
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
window.addEventListener("pointercancel", handlePointerCancel);
window.addEventListener("blur", handleBlur);
cleanupRulerDragRef.current = cleanup;
}; };
const handleDragMove = (e: Konva.KonvaEventObject<DragEvent>) => { const handleDragMove = (e: Konva.KonvaEventObject<DragEvent>) => {
@@ -233,18 +321,18 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
<div <div
className="absolute -top-6 left-0 h-6 bg-slate-100 border-b border-slate-300 cursor-col-resize" className="absolute -top-6 left-0 h-6 bg-slate-100 border-b border-slate-300 cursor-col-resize"
style={{ width: stageSize.width * finalScale }} style={{ width: stageSize.width * finalScale }}
onMouseDown={(e) => { onPointerDown={(e) => {
e.preventDefault(); e.preventDefault();
startRulerGuideDrag("vertical"); startRulerGuideDrag("vertical", e.pointerId);
}} }}
title="Drag to create vertical guide" title="Drag to create vertical guide"
/> />
<div <div
className="absolute top-0 -left-6 w-6 bg-slate-100 border-r border-slate-300 cursor-row-resize" className="absolute top-0 -left-6 w-6 bg-slate-100 border-r border-slate-300 cursor-row-resize"
style={{ height: stageSize.height * finalScale }} style={{ height: stageSize.height * finalScale }}
onMouseDown={(e) => { onPointerDown={(e) => {
e.preventDefault(); e.preventDefault();
startRulerGuideDrag("horizontal"); startRulerGuideDrag("horizontal", e.pointerId);
}} }}
title="Drag to create horizontal guide" title="Drag to create horizontal guide"
/> />
@@ -300,6 +388,10 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
selectedGuideId={selectedGuideId} selectedGuideId={selectedGuideId}
draftGuide={draftGuide} draftGuide={draftGuide}
onGuideSelect={setSelectedGuideId} onGuideSelect={setSelectedGuideId}
onGuideDragStart={(id, orientation) => {
draggingGuideRef.current = { id, orientation };
setSelectedGuideId(id);
}}
onGuideDragEnd={handleGuideDragEnd} onGuideDragEnd={handleGuideDragEnd}
/> />
</Stage> </Stage>
@@ -9,6 +9,7 @@ type GuidesLayerProps = {
selectedGuideId: string | null; selectedGuideId: string | null;
draftGuide: { orientation: Guide["orientation"]; position: number } | null; draftGuide: { orientation: Guide["orientation"]; position: number } | null;
onGuideSelect: (id: string) => void; onGuideSelect: (id: string) => void;
onGuideDragStart: (id: string, orientation: Guide["orientation"]) => void;
onGuideDragEnd: (id: string, position: number, shouldRemove: boolean) => void; onGuideDragEnd: (id: string, position: number, shouldRemove: boolean) => void;
}; };
@@ -20,8 +21,23 @@ const GuidesLayer = ({
selectedGuideId, selectedGuideId,
draftGuide, draftGuide,
onGuideSelect, onGuideSelect,
onGuideDragStart,
onGuideDragEnd, onGuideDragEnd,
}: GuidesLayerProps) => { }: GuidesLayerProps) => {
const finalizeGuideDrag = (
guideId: string,
orientation: Guide["orientation"],
x: number,
y: number,
) => {
const nextPosition = orientation === "vertical" ? x : y;
const shouldRemove =
orientation === "vertical"
? nextPosition < 0 || nextPosition > stageWidth
: nextPosition < 0 || nextPosition > stageHeight;
onGuideDragEnd(guideId, nextPosition, shouldRemove);
};
return ( return (
<Layer> <Layer>
{guides.map((guide) => { {guides.map((guide) => {
@@ -46,13 +62,21 @@ const GuidesLayer = ({
isVertical ? { x: pos.x, y: 0 } : { x: 0, y: pos.y } isVertical ? { x: pos.x, y: 0 } : { x: 0, y: pos.y }
} }
onMouseDown={() => onGuideSelect(guide.id)} onMouseDown={() => onGuideSelect(guide.id)}
onDragStart={() => onGuideDragStart(guide.id, guide.orientation)}
onDragMove={(e) => {
const nativeEvent = e.evt as MouseEvent | PointerEvent | TouchEvent;
// Safari can miss dragend/mouseup; if no pressed pointer exists, end drag immediately.
if ("buttons" in nativeEvent && nativeEvent.buttons === 0) {
const node = e.target;
if (node.isDragging()) {
node.stopDrag();
}
finalizeGuideDrag(guide.id, guide.orientation, node.x(), node.y());
}
}}
onDragEnd={(e) => { onDragEnd={(e) => {
const node = e.target; const node = e.target;
const nextPosition = isVertical ? node.x() : node.y(); finalizeGuideDrag(guide.id, guide.orientation, node.x(), node.y());
const shouldRemove = isVertical
? nextPosition < 0 || nextPosition > stageWidth
: nextPosition < 0 || nextPosition > stageHeight;
onGuideDragEnd(guide.id, nextPosition, shouldRemove);
}} }}
/> />
); );