ruv commited on
Commit
d438333
·
verified ·
1 Parent(s): f1b285f

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +79 -44
src/App.js CHANGED
@@ -1,62 +1,97 @@
1
- import React, { useCallback } from 'react';
2
  import ReactFlow, {
 
3
  addEdge,
4
- MiniMap,
5
- Controls,
6
- Background,
7
  useNodesState,
8
  useEdgesState,
 
9
  } from 'reactflow';
10
-
11
- import { nodes as initialNodes, edges as initialEdges } from './initial-elements';
12
- import CustomNode from './CustomNode';
13
-
14
  import 'reactflow/dist/style.css';
15
- import './overview.css';
16
 
17
- const nodeTypes = {
18
- custom: CustomNode,
19
- };
20
 
21
- const minimapStyle = {
22
- height: 120,
23
- };
 
 
 
 
 
 
 
24
 
25
- const onInit = (reactFlowInstance) => console.log('flow loaded:', reactFlowInstance);
 
26
 
27
- const OverviewFlow = () => {
 
28
  const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
29
- const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
30
- const onConnect = useCallback((params) => setEdges((eds) => addEdge(params, eds)), []);
31
 
32
- // we are using a bit of a shortcut here to adjust the edge type
33
- // this could also be done with a custom edge for example
34
- const edgesWithUpdatedTypes = edges.map((edge) => {
35
- if (edge.sourceHandle) {
36
- const edgeType = nodes.find((node) => node.type === 'custom').data.selects[edge.sourceHandle];
37
- edge.type = edgeType;
38
- }
 
 
 
 
 
 
 
 
39
 
40
- return edge;
41
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  return (
44
- <ReactFlow
45
- nodes={nodes}
46
- edges={edgesWithUpdatedTypes}
47
- onNodesChange={onNodesChange}
48
- onEdgesChange={onEdgesChange}
49
- onConnect={onConnect}
50
- onInit={onInit}
51
- fitView
52
- attributionPosition="top-right"
53
- nodeTypes={nodeTypes}
54
- >
55
- <MiniMap style={minimapStyle} zoomable pannable />
56
- <Controls />
57
- <Background color="#aaa" gap={16} />
58
- </ReactFlow>
 
 
 
 
 
59
  );
60
  };
61
 
62
- export default OverviewFlow;
 
1
+ import React, { useState, useRef, useCallback } from 'react';
2
  import ReactFlow, {
3
+ ReactFlowProvider,
4
  addEdge,
 
 
 
5
  useNodesState,
6
  useEdgesState,
7
+ Controls,
8
  } from 'reactflow';
 
 
 
 
9
  import 'reactflow/dist/style.css';
 
10
 
11
+ import Sidebar from './Sidebar';
 
 
12
 
13
+ import './index.css';
14
+
15
+ const initialNodes = [
16
+ {
17
+ id: '1',
18
+ type: 'input',
19
+ data: { label: 'input node' },
20
+ position: { x: 250, y: 5 },
21
+ },
22
+ ];
23
 
24
+ let id = 0;
25
+ const getId = () => `dndnode_${id++}`;
26
 
27
+ const DnDFlow = () => {
28
+ const reactFlowWrapper = useRef(null);
29
  const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
30
+ const [edges, setEdges, onEdgesChange] = useEdgesState([]);
31
+ const [reactFlowInstance, setReactFlowInstance] = useState(null);
32
 
33
+ const onConnect = useCallback(
34
+ (params) => setEdges((eds) => addEdge(params, eds)),
35
+ [],
36
+ );
37
+
38
+ const onDragOver = useCallback((event) => {
39
+ event.preventDefault();
40
+ event.dataTransfer.dropEffect = 'move';
41
+ }, []);
42
+
43
+ const onDrop = useCallback(
44
+ (event) => {
45
+ event.preventDefault();
46
+
47
+ const type = event.dataTransfer.getData('application/reactflow');
48
 
49
+ // check if the dropped element is valid
50
+ if (typeof type === 'undefined' || !type) {
51
+ return;
52
+ }
53
+
54
+ // reactFlowInstance.project was renamed to reactFlowInstance.screenToFlowPosition
55
+ // and you don't need to subtract the reactFlowBounds.left/top anymore
56
+ // details: https://reactflow.dev/whats-new/2023-11-10
57
+ const position = reactFlowInstance.screenToFlowPosition({
58
+ x: event.clientX,
59
+ y: event.clientY,
60
+ });
61
+ const newNode = {
62
+ id: getId(),
63
+ type,
64
+ position,
65
+ data: { label: `${type} node` },
66
+ };
67
+
68
+ setNodes((nds) => nds.concat(newNode));
69
+ },
70
+ [reactFlowInstance],
71
+ );
72
 
73
  return (
74
+ <div className="dndflow">
75
+ <ReactFlowProvider>
76
+ <div className="reactflow-wrapper" ref={reactFlowWrapper}>
77
+ <ReactFlow
78
+ nodes={nodes}
79
+ edges={edges}
80
+ onNodesChange={onNodesChange}
81
+ onEdgesChange={onEdgesChange}
82
+ onConnect={onConnect}
83
+ onInit={setReactFlowInstance}
84
+ onDrop={onDrop}
85
+ onDragOver={onDragOver}
86
+ fitView
87
+ >
88
+ <Controls />
89
+ </ReactFlow>
90
+ </div>
91
+ <Sidebar />
92
+ </ReactFlowProvider>
93
+ </div>
94
  );
95
  };
96
 
97
+ export default DnDFlow;