1
1
import axios from 'axios' ;
2
- import { useCallback , useEffect , useMemo , useState } from 'react' ;
3
- import { DeployedPackageInfo } from '../types/project-types' ;
2
+ import { useCallback , useEffect , useMemo , useState , useRef } from 'react' ;
3
+ import { DeployedPackageInfo , Project } from '../types/project-types' ;
4
4
// import './DeployCanvas.css'
5
5
import { DeployedPackage , DeployedObject , ObjectNode , PackageNode } from './DeployedObjects'
6
6
import { ScaleLoader } from 'react-spinners' ;
7
7
import LoadingOverlay from 'react-loading-overlay-ts' ;
8
8
import { useWallet } from '@suiet/wallet-kit' ;
9
9
10
- import ReactFlow , { Background , Controls , MiniMap , applyEdgeChanges , applyNodeChanges } from 'reactflow' ;
10
+ import ReactFlow , { Background , Controls , MiniMap , applyEdgeChanges , applyNodeChanges , useNodesState , useStore } from 'reactflow' ;
11
11
import 'reactflow/dist/style.css' ;
12
+ import DeployCanvasContextMenu from './DeployCanvasContextMenu' ;
12
13
13
14
const BACKEND_URL = process . env . REACT_APP_BACKEND_URL || 'http://localhost:80/' ;
14
15
@@ -43,10 +44,18 @@ const BACKEND_URL = process.env.REACT_APP_BACKEND_URL || 'http://localhost:80/';
43
44
// ];
44
45
45
46
// const initialEdges = [{ id: '1-2', source: '1', target: '2', label: 'to the', type: 'step' }];
46
-
47
+ let id = 0 ;
48
+ const getId = ( ) => `dndnode_${ id ++ } ` ;
47
49
48
50
function DeployCanvas (
49
51
props : {
52
+ projectList : string [ ] ,
53
+ currentProject : Project | null ,
54
+ changeProject : ( project : string ) => void ,
55
+ publishPackage : ( ) => void ,
56
+ addExistingObject : ( objectId : string ) => void ,
57
+ addFromTransactions : ( transactionId : string ) => void ,
58
+ compileError : string ,
50
59
// theme: string,
51
60
deployedObjects : DeployedPackageInfo [ ] ,
52
61
toasts : JSX . Element | undefined ,
@@ -57,7 +66,8 @@ function DeployCanvas (
57
66
setFailTxn : ( digest : string ) => void ,
58
67
removeDeployedObject : ( id : string ) => void ,
59
68
rearrangeDeployedObjects : ( draggedId : string , draggedOverId : string ) => void ,
60
- useSuiVision : boolean
69
+ useSuiVision : boolean ,
70
+ dropped : ( ) => void
61
71
}
62
72
) {
63
73
@@ -66,16 +76,19 @@ function DeployCanvas (
66
76
const [ draggedId , setDraggedId ] = useState < string | undefined > ( undefined )
67
77
const [ draggedOverId , setDraggedOverId ] = useState < string | undefined > ( undefined )
68
78
79
+ const reactFlowWrapper = useRef ( null ) ;
80
+ const [ reactFlowInstance , setReactFlowInstance ] = useState ( null ) ;
81
+
69
82
const [ loading , setLoading ] = useState < boolean > ( false ) ;
70
83
71
- const nodeTypes = useMemo ( ( ) => ( { package : PackageNode , Object : ObjectNode } ) , [ ] ) ;
72
- const [ nodes , setNodes ] = useState < [ ] > ( [ ] ) ;
84
+ const nodeTypes = useMemo ( ( ) => ( { package : PackageNode , object : ObjectNode } ) , [ ] ) ;
85
+ const [ nodes , setNodes , onNodesChange ] = useNodesState ( [ ] ) ;
73
86
// const [edges, setEdges] = useState(initialEdges);
74
87
75
- const onNodesChange = useCallback (
76
- ( changes : any ) => setNodes ( ( nds ) => applyNodeChanges ( changes , nds ) as any ) ,
77
- [ ]
78
- ) ;
88
+ // const onNodesChange = useCallback(
89
+ // (changes: any) => setNodes((nds) => applyNodeChanges(changes, nds) as any),
90
+ // []
91
+ // );
79
92
// const onEdgesChange = useCallback(
80
93
// (changes: any) => setEdges((eds) => applyEdgeChanges(changes, eds) as any),
81
94
// []
@@ -98,6 +111,55 @@ function DeployCanvas (
98
111
setLoading ( false ) ;
99
112
} , [ nodes ] )
100
113
114
+ const onDragOver = useCallback ( ( event : any ) => {
115
+ event . preventDefault ( ) ;
116
+ event . dataTransfer . dropEffect = 'move' ;
117
+ } , [ ] ) ;
118
+
119
+ // useEffect(() => {
120
+ // document.addEventListener('contextmenu',(event) => {
121
+ // event.preventDefault();
122
+ // console.log(event.type);
123
+ // });
124
+ // }, []);
125
+
126
+ const onDrop = ( ) => {
127
+ props . dropped ( )
128
+ }
129
+
130
+
131
+ // const onDrop = useCallback(
132
+ // (event: any) => {
133
+ // event.preventDefault();
134
+
135
+ // if (reactFlowInstance === null || reactFlowWrapper.current === null) {
136
+ // return;
137
+ // }
138
+
139
+ // const reactFlowBounds = (reactFlowWrapper.current as any).getBoundingClientRect();
140
+ // const type = event.dataTransfer.getData('application/reactflow');
141
+
142
+ // // check if the dropped element is valid
143
+ // if (typeof type === 'undefined' || !type) {
144
+ // return;
145
+ // }
146
+
147
+ // const position = (reactFlowInstance as any).project({
148
+ // x: event.clientX - reactFlowBounds.left,
149
+ // y: event.clientY - reactFlowBounds.top,
150
+ // });
151
+ // const newNode = {
152
+ // id: getId(),
153
+ // type,
154
+ // position,
155
+ // data: { label: `${type} node` },
156
+ // };
157
+
158
+ // setNodes((nds) => nds.concat(newNode));
159
+ // },
160
+ // [reactFlowInstance]
161
+ // );
162
+
101
163
const updateDeployedObjects = async ( ) => {
102
164
props . setIsOverlayActive ( true ) ;
103
165
setLoading ( true ) ;
@@ -391,14 +453,26 @@ function DeployCanvas (
391
453
// // }
392
454
// }}
393
455
// >
394
- < div style = { { height : '100%' } } >
395
-
456
+ < div id = 'deplyAndInteractMenu' style = { { height : '100%' } } >
457
+ < DeployCanvasContextMenu
458
+ targetId = 'deplyAndInteractMenu'
459
+ projectList = { props . projectList }
460
+ changeProject = { props . changeProject }
461
+ addExistingObject = { props . addExistingObject }
462
+ addFromTransactions = { props . addFromTransactions }
463
+ publishPackage = { props . publishPackage }
464
+ currentProject = { props . currentProject }
465
+ compileError = { props . compileError }
466
+ />
396
467
< ReactFlow
397
468
nodes = { nodes }
398
469
onNodesChange = { onNodesChange }
399
470
nodeTypes = { nodeTypes }
400
471
// edges={edges}
401
472
// onEdgesChange={onEdgesChange}
473
+ onInit = { setReactFlowInstance as any }
474
+ onDrop = { onDrop }
475
+ onDragOver = { onDragOver }
402
476
>
403
477
{ /* <LoadingOverlay
404
478
active={props.isOverlayActive}
@@ -419,6 +493,7 @@ function DeployCanvas (
419
493
}}
420
494
> */ }
421
495
< Background />
496
+
422
497
< Controls />
423
498
{ /* <MiniMap /> */ }
424
499
{ /* </LoadingOverlay> */ }
0 commit comments