Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cluster position after node dragging #292

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};
};
Loading