1
1
import { useRegisterEvents , useSigma } from "@react-sigma/core" ;
2
+ import EventEmitter from "events" ;
2
3
import { mapValues , pick } from "lodash" ;
3
4
import { FC , useEffect , useRef } from "react" ;
4
5
import { Coordinates , MouseCoords } from "sigma/types" ;
@@ -26,7 +27,7 @@ export const EventsController: FC = () => {
26
27
const dragStateRef = useRef <
27
28
| { type : "idle" }
28
29
| {
29
- type : "dragging" ;
30
+ type : "dragging" | "downing" ;
30
31
initialMousePosition : Coordinates ;
31
32
initialNodesPosition : Record < string , Coordinates > ;
32
33
}
@@ -87,14 +88,15 @@ export const EventsController: FC = () => {
87
88
88
89
const initialNodesPosition : LayoutMapping = { } ;
89
90
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
90
92
graph . setNodeAttribute ( node , "fixed" , true ) ;
91
93
const { x, y } = graph . getNodeAttributes ( node ) ;
92
94
initialNodesPosition [ node ] = { x, y } ;
93
95
} ) ;
94
96
95
97
dragEventsCountRef . current = 0 ;
96
98
dragStateRef . current = {
97
- type : "dragging " ,
99
+ type : "downing " ,
98
100
initialNodesPosition,
99
101
initialMousePosition : { x, y } ,
100
102
} ;
@@ -107,24 +109,27 @@ export const EventsController: FC = () => {
107
109
} ,
108
110
mouseup : ( ) => {
109
111
const dragState = dragStateRef . current ;
110
- if ( dragState . type === "dragging" ) {
111
- // Save new positions in graph dataset:
112
+ if ( dragState . type === "downing" || dragState . type === "dragging" ) {
112
113
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
113
125
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
-
119
126
dragStateRef . current = { type : "idle" } ;
120
-
121
- resetHoveredNode ( ) ;
122
- resetHoveredEdge ( ) ;
123
127
}
124
128
} ,
125
129
mousemovebody : ( e ) => {
126
130
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" } ;
128
133
dragEventsCountRef . current ++ ;
129
134
const graph = sigma . getGraph ( ) ;
130
135
@@ -140,7 +145,7 @@ export const EventsController: FC = () => {
140
145
graph . setNodeAttribute ( node , "x" , initialPosition . x + delta . x ) ;
141
146
graph . setNodeAttribute ( node , "y" , initialPosition . y + delta . y ) ;
142
147
}
143
-
148
+ ( graph as EventEmitter ) . emit ( "nodesDragged" ) ;
144
149
// Prevent sigma to move camera:
145
150
e . preventSigmaDefault ( ) ;
146
151
e . original . preventDefault ( ) ;
0 commit comments