Skip to content

Commit 0420b5a

Browse files
committed
[layout] don't save nodes position while layout is running (#138)
Nodes position should not be saved while a layout is running, they will be saved when the layout will be stopped.i Otherwise it triggers a reset of the sigma's graph, which leads to restart of the layout with the inital data.
1 parent f4b9da8 commit 0420b5a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/gephi-lite/src/core/layouts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const stopLayout = asyncAction(async (isForRestart = false) => {
4444
layoutState.supervisor.stop();
4545
layoutState.supervisor.kill();
4646

47-
// DOn't save position if it's for a restart
47+
// Don't save position if it's for a restart
4848
if (!isForRestart) {
4949
// Save data
5050
const positions: LayoutMapping = {};

packages/gephi-lite/src/views/graphPage/controllers/EventsController.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Coordinates, MouseCoords } from "sigma/types";
66

77
import {
88
useGraphDatasetActions,
9+
useLayoutState,
910
useSelection,
1011
useSelectionActions,
1112
useSigmaActions,
@@ -26,6 +27,7 @@ export const EventsController: FC = () => {
2627
const { setNodePositions } = useGraphDatasetActions();
2728
const { select, toggle, emptySelection } = useSelectionActions();
2829
const { setHoveredNode, resetHoveredNode, setHoveredEdge, resetHoveredEdge } = useSigmaActions();
30+
const { type: layoutStatus } = useLayoutState();
2931

3032
const dragStateRef = useRef<
3133
| { type: "idle" }
@@ -94,7 +96,7 @@ export const EventsController: FC = () => {
9496

9597
const initialNodesPosition: LayoutMapping = {};
9698
nodes.forEach((node) => {
97-
// I think the fixed attribute is a failed tryout to solve the drag during layout issue https://github.com/gephi/gephi-lite/issues/138
99+
// Fixing the node position, it's needed while a layout is running
98100
graph.setNodeAttribute(node, "fixed", true);
99101
const { x, y } = graph.getNodeAttributes(node);
100102
initialNodesPosition[node] = { x, y };
@@ -147,16 +149,19 @@ export const EventsController: FC = () => {
147149
if (dragState.type === "downing" || dragState.type === "dragging") {
148150
const graph = sigma.getGraph();
149151
if (dragState.type === "dragging") {
150-
// Save new positions in graph dataset:
151-
const positions = mapValues(dragState.initialNodesPosition, (_initialPosition, id) =>
152-
pick(graph.getNodeAttributes(id), ["x", "y"]),
153-
);
154-
setNodePositions(positions);
152+
// Save new positions in graph dataset if layout is not running
153+
// Positions will be saved when the algo will be stopped and saving positions here
154+
// will retrigger the layout with the initial positions (#138)
155+
if (layoutStatus !== "running") {
156+
const positions = mapValues(dragState.initialNodesPosition, (_initialPosition, id) =>
157+
pick(graph.getNodeAttributes(id), ["x", "y"]),
158+
);
159+
setNodePositions(positions);
160+
}
155161

156162
resetHoveredNode();
157163
resetHoveredEdge();
158164
}
159-
// I think the fixed attribute is a failed tryout to solve the drag during layout issue https://github.com/gephi/gephi-lite/issues/138
160165
graph.forEachNode((node) => graph.setNodeAttribute(node, "fixed", false));
161166
dragStateRef.current = { type: "idle" };
162167
}
@@ -179,6 +184,7 @@ export const EventsController: FC = () => {
179184
toggle,
180185
setNodePositions,
181186
globalEmitter,
187+
layoutStatus,
182188
]);
183189

184190
// DOM events not handled by sigma:

0 commit comments

Comments
 (0)