@@ -12,26 +12,39 @@ import ReactFlow, {
12
12
applyEdgeChanges , applyNodeChanges , NodeChange , EdgeChange , Edge , Node
13
13
// useStore
14
14
} from "reactflow" ;
15
- import "./styles.scss" ;
15
+ import ".. /styles.scss" ;
16
16
import styled , { ThemeProvider } from 'styled-components' ;
17
- import { darkTheme , lightTheme } from "./theme" ;
17
+ import { darkTheme , lightTheme } from ".. /theme" ;
18
18
import React from "react" ;
19
- import { resetHandlePathHighlight } from "./interactions/EntityRelationHighlight" ;
19
+ import { resetHandlePathHighlight } from ".. /interactions/EntityRelationHighlight" ;
20
20
import "reactflow/dist/style.css" ;
21
- import { getLayoutedElements } from "./core/layouts/dagre" ;
22
- import { CanvasEdge , CanvasNode , FlowCanvasProps } from "./core/types" ;
23
- import { defaultCanvasSettings , defaultCanvasStyle } from "./settings" ;
24
- import { CanvasNodeTemplates } from "./nodeTemplates" ;
25
- import { CanvasEdgeTemplates } from "./edgeTemplates" ;
26
- import CanvasInteractions from "./interactions/interactions" ;
27
- import ContextMenu from "./components/ContextMenu" ;
28
-
29
- const canvasInteractions = new CanvasInteractions ( )
30
- const FlowCanvas = ( { children, initialNodes, initialEdges = [ ] ,
21
+ // import { getLayoutedElements } from "../layouts/dagre";
22
+ import { CanvasEdge , CanvasNode , FlowCanvasProps } from "../core/types" ;
23
+ import { defaultCanvasSettings , defaultCanvasStyle } from "../settings" ;
24
+ import { CanvasNodeTemplates } from "../nodeTemplates" ;
25
+ import { CanvasEdgeTemplates } from "../edgeTemplates" ;
26
+ import CanvasInteractions from "../interactions/interactions" ;
27
+ import GenericNodeContextMenu from "../components/ContextMenu/GenericNodeContextMenu" ;
28
+ import GenericEdgeContextMenu from "../components/ContextMenu/GenericEdgeContextMenu" ;
29
+ import { defaultLayoutChange } from "../layouts/noLayout" ;
30
+
31
+
32
+
33
+
34
+ const BaseFlowCanvas = ( {
35
+ children,
36
+ initialNodes,
37
+ initialEdges = [ ] ,
38
+ onLayoutChange,
39
+ NodeContextMenu,
40
+ EdgeContextMenu,
31
41
style = defaultCanvasStyle ,
32
42
canvasSettings = defaultCanvasSettings ,
33
43
canvasNodeTemplates = CanvasNodeTemplates ,
34
- canvasEdgeTemplates = CanvasEdgeTemplates
44
+ canvasEdgeTemplates = CanvasEdgeTemplates ,
45
+ canvasInteractions = new CanvasInteractions ( ) ,
46
+
47
+
35
48
} : FlowCanvasProps ) => {
36
49
37
50
@@ -45,12 +58,16 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
45
58
// };
46
59
// });
47
60
// }
61
+
48
62
const [ flowInstance , setFlowInstance ] = useState < ReactFlowInstance | null | undefined > ( null ) ;
49
63
50
- const { layoutedNodes, layoutedEdges } = getLayoutedElements (
64
+ console . log ( "====onLayoutChange" , onLayoutChange ) ;
65
+
66
+ const { layoutedNodes, layoutedEdges } = onLayoutChange (
51
67
initialNodes ,
52
68
initialEdges ,
53
- flowInstance
69
+ flowInstance ,
70
+ "LR"
54
71
) ;
55
72
56
73
const [ mode , setMode ] = useState ( 'dark' ) ;
@@ -66,7 +83,7 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
66
83
const [ nodes , setNodes ] = useNodesState ( layoutedNodes ) ;
67
84
const [ edges , setEdges ] = useEdgesState ( layoutedEdges ) ;
68
85
69
- const [ menu , setMenu ] = useState ( null ) ;
86
+ const [ contextMenuItem , setContextMenuItem ] = useState ( null ) ;
70
87
71
88
const [ direction , setDirection ] = useState ( "LR" ) ;
72
89
@@ -82,51 +99,53 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
82
99
83
100
const onNodeContextMenu = useCallback (
84
101
( event : React . MouseEvent , node : Node ) => {
85
- // Prevent native context menu from showing
102
+ // Prevent native context contextMenuItem from showing
86
103
event . preventDefault ( ) ;
87
- console . log ( "====onNodeContextMenu" , node , event , event . clientX , event . clientY )
88
- const pane = ref . current . getBoundingClientRect ( ) ;
89
- setMenu ( {
90
- id : node . id ,
91
- top : event . clientY < pane . height - 200 && event . clientY ,
92
- left : event . clientX < pane . width - 200 && event . clientX ,
93
- right : event . clientX >= pane . width - 200 && pane . width - event . clientX ,
94
- bottom : event . clientY >= pane . height - 200 && pane . height - event . clientY ,
95
- } ) ;
96
-
104
+ const pane = ref . current . getBoundingClientRect ( ) ;
105
+ setContextMenuItem ( {
106
+ id : node . id ,
107
+ type : " node" ,
108
+ top : event . clientY < pane . height - 200 && event . clientY ,
109
+ left : event . clientX < pane . width - 200 && event . clientX ,
110
+ right : event . clientX >= pane . width - 200 && pane . width - event . clientX ,
111
+ bottom : event . clientY >= pane . height - 200 && pane . height - event . clientY ,
112
+ } ) ;
113
+
97
114
} ,
98
- [ setMenu ]
115
+ [ setContextMenuItem ]
99
116
) ;
100
117
101
118
const onEdgeContextMenu = useCallback (
102
119
( event : React . MouseEvent , edge : Edge ) => {
103
- // Prevent native context menu from showing
120
+ // Prevent native context contextMenuItem from showing
104
121
event . preventDefault ( ) ;
105
- console . log ( "====onEdgeContextMenu" , edge , event , event . clientX , event . clientY )
106
- const pane = ref . current . getBoundingClientRect ( ) ;
107
- setMenu ( {
108
- id : edge . id ,
109
- top : event . clientY < pane . height - 200 && event . clientY ,
110
- left : event . clientX < pane . width - 200 && event . clientX ,
111
- right : event . clientX >= pane . width - 200 && pane . width - event . clientX ,
112
- bottom : event . clientY >= pane . height - 200 && pane . height - event . clientY ,
113
- } ) ;
114
-
122
+ console . log ( "====onEdgeContextMenu" , edge , event , event . clientX , event . clientY )
123
+ const pane = ref . current . getBoundingClientRect ( ) ;
124
+ setContextMenuItem ( {
125
+ id : edge . id ,
126
+ type : "edge" ,
127
+ top : event . clientY < pane . height - 200 && event . clientY ,
128
+ left : event . clientX < pane . width - 200 && event . clientX ,
129
+ right : event . clientX >= pane . width - 200 && pane . width - event . clientX ,
130
+ bottom : event . clientY >= pane . height - 200 && pane . height - event . clientY ,
131
+ } ) ;
132
+
115
133
} ,
116
- [ setMenu ]
134
+ [ setContextMenuItem ]
117
135
) ;
118
136
119
137
120
138
121
- const onPaneClick = useCallback ( ( ) => setMenu ( null ) , [ setMenu ] ) ;
139
+ const onPaneClick = useCallback ( ( ) => setContextMenuItem ( null ) , [ setContextMenuItem ] ) ;
122
140
123
141
124
142
const onInit = ( reactFlowInstance : ReactFlowInstance ) => {
125
143
console . log ( "flow loaded:" , reactFlowInstance ) ;
126
144
setFlowInstance ( reactFlowInstance ) ;
127
- reactFlowInstance . zoomTo ( 1 ) ;
128
- reactFlowInstance . fitView ( ) ;
129
- // onLayout(direction)
145
+ reactFlowInstance ?. zoomTo ( 1 ) ;
146
+ reactFlowInstance ?. fitView ( ) ;
147
+
148
+ onLayout ( direction )
130
149
}
131
150
132
151
// const onNodeClick = (event: React.MouseEvent, object: CanvasNode) => {
@@ -161,7 +180,7 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
161
180
const {
162
181
layoutedNodes,
163
182
layoutedEdges
164
- } = getLayoutedElements ( nodes , edges , flowInstance , direction ) ;
183
+ } = onLayoutChange ( nodes , edges , flowInstance , direction ) ;
165
184
setNodes ( [ ...layoutedNodes ] ) ;
166
185
setEdges ( [ ...layoutedEdges ] ) ;
167
186
} ,
@@ -258,7 +277,9 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
258
277
// }
259
278
>
260
279
261
- { menu && < ContextMenu onClick = { onPaneClick } { ...menu } /> }
280
+ { contextMenuItem && contextMenuItem ?. type === "edge" && < NodeContextMenu onClick = { onPaneClick } { ...contextMenuItem } /> }
281
+ { contextMenuItem && contextMenuItem ?. type === "node" && < EdgeContextMenu onClick = { onPaneClick } { ...contextMenuItem } /> }
282
+
262
283
< MiniMapStyled
263
284
nodeColor = { ( node ) => {
264
285
switch ( node . type ) {
@@ -292,4 +313,13 @@ const FlowCanvas = ({ children, initialNodes, initialEdges = [],
292
313
) ;
293
314
} ;
294
315
295
- export default FlowCanvas ;
316
+
317
+ BaseFlowCanvas . defaultProps = {
318
+ onLayoutChange : defaultLayoutChange ,
319
+ canvasInteractions : new CanvasInteractions ( ) ,
320
+ NodeContextMenu : GenericNodeContextMenu ,
321
+ EdgeContextMenu : GenericEdgeContextMenu
322
+
323
+ } ;
324
+
325
+ export default BaseFlowCanvas ;
0 commit comments