diff --git a/src/components/GroupsOverlay.tsx b/src/components/GroupsOverlay.tsx index 43cee3e2..bd31db96 100644 --- a/src/components/GroupsOverlay.tsx +++ b/src/components/GroupsOverlay.tsx @@ -30,8 +30,58 @@ interface GroupBackgroundProps { // Renders just the group background - displayed below nodes (z-index 1) function GroupBackground({ groupId }: GroupBackgroundProps) { - const { groups } = useWorkflowStore(); + const { groups, updateGroup, moveGroupNodes } = useWorkflowStore(); const group = groups[groupId]; + const { zoom } = useViewport(); + const [isDragging, setIsDragging] = useState(false); + const dragStartRef = useRef<{ x: number; y: number } | null>(null); + + const handleBackgroundMouseDown = useCallback( + (e: React.MouseEvent) => { + if (e.button !== 0) return; + + e.stopPropagation(); + e.preventDefault(); + setIsDragging(true); + dragStartRef.current = { x: e.clientX, y: e.clientY }; + }, + [] + ); + + useEffect(() => { + if (!isDragging || !group) return; + + const handleMouseMove = (e: MouseEvent) => { + if (!dragStartRef.current) return; + + const deltaX = (e.clientX - dragStartRef.current.x) / zoom; + const deltaY = (e.clientY - dragStartRef.current.y) / zoom; + + if (Math.abs(deltaX) > 2 || Math.abs(deltaY) > 2) { + updateGroup(groupId, { + position: { + x: group.position.x + deltaX, + y: group.position.y + deltaY, + }, + }); + moveGroupNodes(groupId, { x: deltaX, y: deltaY }); + dragStartRef.current = { x: e.clientX, y: e.clientY }; + } + }; + + const handleMouseUp = () => { + setIsDragging(false); + dragStartRef.current = null; + }; + + window.addEventListener("mousemove", handleMouseMove); + window.addEventListener("mouseup", handleMouseUp); + + return () => { + window.removeEventListener("mousemove", handleMouseMove); + window.removeEventListener("mouseup", handleMouseUp); + }; + }, [isDragging, group, groupId, moveGroupNodes, updateGroup, zoom]); if (!group) return null; @@ -39,7 +89,8 @@ function GroupBackground({ groupId }: GroupBackgroundProps) { return (
); @@ -531,7 +582,7 @@ export function GroupBackgroundsPortal() { return (