Update src/App.js
Browse files- 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 |
-
|
| 18 |
-
custom: CustomNode,
|
| 19 |
-
};
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
const
|
|
|
|
| 28 |
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
|
| 29 |
-
const [edges, setEdges, onEdgesChange] = useEdgesState(
|
| 30 |
-
const
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
return (
|
| 44 |
-
<
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
);
|
| 60 |
};
|
| 61 |
|
| 62 |
-
export default
|
|
|
|
| 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;
|