Skip to content

Commit

Permalink
✨ feat: add drag nod
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Apr 4, 2023
1 parent 8ed3fee commit 28e1e2f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
38 changes: 35 additions & 3 deletions src/components/Containers/FlowContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import { NODE_IDENTIFIER, NodeContainer } from '@/components'
import { useAppStore } from '@/store'
import { Connection } from '@reactflow/core/dist/esm/types'
import { Edge } from '@reactflow/core/dist/esm/types/edges'
import React, { useCallback, useRef } from 'react'
import React, { useCallback, useRef, useState } from 'react'
import ReactFlow, { Background, BackgroundVariant, Controls } from 'reactflow'
import 'reactflow/dist/style.css'
import { shallow } from 'zustand/shallow'

const nodeTypes = { [NODE_IDENTIFIER]: NodeContainer }

const FlowContainer: React.FC = () => {
const reactFlowWrapper = useRef(null)
const edgeUpdateSuccessful = useRef(true)
const [reactFlowInstance, setReactFlowInstance] = useState(null)

const { nodes, edges, onNodesChange, onEdgesChange, onConnect, onInit } = useAppStore(
const { nodes, edges, onNodesChange, onEdgesChange, onConnect, onInit, onAddNode } = useAppStore(
(st) => ({
nodes: st.nodes,
edges: st.edges,
onNodesChange: st.onNodesChange,
onEdgesChange: st.onEdgesChange,
onConnect: st.onConnect,
onInit: st.onInit,
onAddNode: st.onAddNode,
}),
shallow
)
Expand Down Expand Up @@ -51,8 +54,34 @@ const FlowContainer: React.FC = () => {
edgeUpdateSuccessful.current = true
}, [])

const onDragOver = useCallback((event: any) => {
event.preventDefault()
event.dataTransfer.dropEffect = 'move'
}, [])

const onDrop = useCallback(
(event: any) => {
event.preventDefault()
// @ts-ignore
const reactFlowBounds = reactFlowWrapper.current.getBoundingClientRect()
const widget = JSON.parse(event.dataTransfer.getData('application/reactflow'))
if (!widget) return
// @ts-ignore
const position = reactFlowInstance.project({
x: event.clientX - reactFlowBounds.left,
y: event.clientY - reactFlowBounds.top,
})
onAddNode({
widget,
position,
})
},
[reactFlowInstance]
)

return (
<ReactFlow
ref={reactFlowWrapper}
nodes={nodes}
edges={edges}
fitView
Expand All @@ -66,7 +95,10 @@ const FlowContainer: React.FC = () => {
onEdgeUpdateStart={onEdgeUpdateStart}
onEdgeUpdateEnd={onEdgeUpdateEnd}
onConnect={onConnect}
onInit={() => {
onDrop={onDrop}
onDragOver={onDragOver}
onInit={(e: any) => {
setReactFlowInstance(e)
void onInit()
}}
>
Expand Down
12 changes: 10 additions & 2 deletions src/components/ControlPanelComponent/NodePickerGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ const NodePickerGroup: React.FC<NodePickerGroupProps> = ({ cat, data, onAddNode,
<CollapseTitle title={startCase(cat)} key={cat} expand={expand} onExpandChange={setExpand}>
<Space wrap>
{data.map((i) => (
<Button key={i.name} onClick={() => onAddNode({ widget: i })}>
<span className="align-middle px-0.5">{startCase(i.name)}</span>
<Button
key={i.name}
onClick={() => onAddNode({ widget: i })}
draggable
onDragStart={(event) => {
event.dataTransfer.setData('application/reactflow', JSON.stringify(i))
event.dataTransfer.effectAllowed = 'move'
}}
>
{startCase(i.name)}
</Button>
))}
</Space>
Expand Down

0 comments on commit 28e1e2f

Please sign in to comment.