Skip to content

Commit f73ced0

Browse files
committed
[dragNdrop] add a downing state
to avoid an instant dragging event which update positions when clicking on a node
1 parent a298323 commit f73ced0

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/views/graphPage/controllers/EventsController.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useRegisterEvents, useSigma } from "@react-sigma/core";
2+
import EventEmitter from "events";
23
import { mapValues, pick } from "lodash";
34
import { FC, useEffect, useRef } from "react";
45
import { Coordinates, MouseCoords } from "sigma/types";
@@ -26,7 +27,7 @@ export const EventsController: FC = () => {
2627
const dragStateRef = useRef<
2728
| { type: "idle" }
2829
| {
29-
type: "dragging";
30+
type: "dragging" | "downing";
3031
initialMousePosition: Coordinates;
3132
initialNodesPosition: Record<string, Coordinates>;
3233
}
@@ -87,14 +88,15 @@ export const EventsController: FC = () => {
8788

8889
const initialNodesPosition: LayoutMapping = {};
8990
nodes.forEach((node) => {
91+
// I think the fixed attribute is a failed tryout to solve the drag during layout issue https://github.com/gephi/gephi-lite/issues/138
9092
graph.setNodeAttribute(node, "fixed", true);
9193
const { x, y } = graph.getNodeAttributes(node);
9294
initialNodesPosition[node] = { x, y };
9395
});
9496

9597
dragEventsCountRef.current = 0;
9698
dragStateRef.current = {
97-
type: "dragging",
99+
type: "downing",
98100
initialNodesPosition,
99101
initialMousePosition: { x, y },
100102
};
@@ -107,24 +109,27 @@ export const EventsController: FC = () => {
107109
},
108110
mouseup: () => {
109111
const dragState = dragStateRef.current;
110-
if (dragState.type === "dragging") {
111-
// Save new positions in graph dataset:
112+
if (dragState.type === "downing" || dragState.type === "dragging") {
112113
const graph = sigma.getGraph();
114+
if (dragState.type === "dragging") {
115+
// Save new positions in graph dataset:
116+
const positions = mapValues(dragState.initialNodesPosition, (_initialPosition, id) =>
117+
pick(graph.getNodeAttributes(id), ["x", "y"]),
118+
);
119+
setNodePositions(positions);
120+
121+
resetHoveredNode();
122+
resetHoveredEdge();
123+
}
124+
// I think the fixed attribute is a failed tryout to solve the drag during layout issue https://github.com/gephi/gephi-lite/issues/138
113125
graph.forEachNode((node) => graph.setNodeAttribute(node, "fixed", false));
114-
const positions = mapValues(dragState.initialNodesPosition, (_initialPosition, id) =>
115-
pick(graph.getNodeAttributes(id), ["x", "y"]),
116-
);
117-
setNodePositions(positions);
118-
119126
dragStateRef.current = { type: "idle" };
120-
121-
resetHoveredNode();
122-
resetHoveredEdge();
123127
}
124128
},
125129
mousemovebody: (e) => {
126130
const dragState = dragStateRef.current;
127-
if (dragState.type === "dragging") {
131+
if (dragState.type === "downing" || dragState.type === "dragging") {
132+
if (dragState.type === "downing") dragStateRef.current = { ...dragState, type: "dragging" };
128133
dragEventsCountRef.current++;
129134
const graph = sigma.getGraph();
130135

@@ -140,7 +145,7 @@ export const EventsController: FC = () => {
140145
graph.setNodeAttribute(node, "x", initialPosition.x + delta.x);
141146
graph.setNodeAttribute(node, "y", initialPosition.y + delta.y);
142147
}
143-
148+
(graph as EventEmitter).emit("nodesDragged");
144149
// Prevent sigma to move camera:
145150
e.preventSigmaDefault();
146151
e.original.preventDefault();

0 commit comments

Comments
 (0)