Skip to content

Commit

Permalink
update cluster position after node dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiTsybulskyi committed Nov 1, 2024
1 parent 520655c commit a75b83a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/GraphScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
Fragment,
ReactNode,
Ref,
useCallback,
useImperativeHandle,
useMemo
} from 'react';
Expand Down Expand Up @@ -345,7 +346,7 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
const camera = useThree(state => state.camera);

// Mount and build the graph
useGraph(rest);
const { updateLayout } = useGraph(rest);

if (
clusterAttribute &&
Expand Down Expand Up @@ -382,6 +383,18 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
[centerNodesById, fitNodesInViewById, graph, gl, scene, camera]
);

const onNodeDraggedHandler = useCallback(
(node: InternalGraphNode) => {
onNodeDragged?.(node);

// Update layout to recalculate the cluster positions when a node is dragged
if (clusterAttribute) {
updateLayout();
}
},
[clusterAttribute, onNodeDragged, updateLayout]
);

const nodeComponents = useMemo(
() =>
nodes.map(n => (
Expand All @@ -400,7 +413,7 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
onContextMenu={onNodeContextMenu}
onPointerOver={onNodePointerOver}
onPointerOut={onNodePointerOut}
onDragged={onNodeDragged}
onDragged={onNodeDraggedHandler}
/>
)),
[
Expand All @@ -414,7 +427,7 @@ export const GraphScene: FC<GraphSceneProps & { ref?: Ref<GraphSceneRef> }> =
onNodeClick,
onNodeContextMenu,
onNodeDoubleClick,
onNodeDragged,
onNodeDraggedHandler,
onNodePointerOut,
onNodePointerOver,
renderNode
Expand Down
4 changes: 4 additions & 0 deletions src/useGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,8 @@ export const useGraph = ({
updateLayout(layout.current);
}
}, [sizingType, sizingAttribute, labelType, updateLayout]);

return {
updateLayout
};
};

0 comments on commit a75b83a

Please sign in to comment.