2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
memo,
|
|
|
|
useCallback,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
2024-05-27 21:57:08 +08:00
|
|
|
import cn from 'classnames'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { intersection } from 'lodash-es'
|
|
|
|
import type { EdgeProps } from 'reactflow'
|
|
|
|
import {
|
|
|
|
BaseEdge,
|
|
|
|
EdgeLabelRenderer,
|
|
|
|
Position,
|
2024-04-15 15:49:40 +08:00
|
|
|
getBezierPath,
|
2024-04-08 18:51:46 +08:00
|
|
|
} from 'reactflow'
|
|
|
|
import {
|
2024-05-27 21:57:08 +08:00
|
|
|
useAvailableBlocks,
|
2024-04-08 18:51:46 +08:00
|
|
|
useNodesInteractions,
|
|
|
|
} from './hooks'
|
|
|
|
import BlockSelector from './block-selector'
|
|
|
|
import type {
|
|
|
|
Edge,
|
|
|
|
OnSelectBlock,
|
|
|
|
} from './types'
|
2024-05-27 21:57:08 +08:00
|
|
|
import { ITERATION_CHILDREN_Z_INDEX } from './constants'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
const CustomEdge = ({
|
|
|
|
id,
|
|
|
|
data,
|
|
|
|
source,
|
|
|
|
sourceHandleId,
|
|
|
|
target,
|
|
|
|
targetHandleId,
|
|
|
|
sourceX,
|
|
|
|
sourceY,
|
|
|
|
targetX,
|
|
|
|
targetY,
|
|
|
|
selected,
|
|
|
|
}: EdgeProps) => {
|
|
|
|
const [
|
|
|
|
edgePath,
|
|
|
|
labelX,
|
|
|
|
labelY,
|
2024-04-15 15:49:40 +08:00
|
|
|
] = getBezierPath({
|
2024-04-08 18:51:46 +08:00
|
|
|
sourceX: sourceX - 8,
|
|
|
|
sourceY,
|
|
|
|
sourcePosition: Position.Right,
|
|
|
|
targetX: targetX + 8,
|
|
|
|
targetY,
|
|
|
|
targetPosition: Position.Left,
|
2024-04-15 15:49:40 +08:00
|
|
|
curvature: 0.16,
|
2024-04-08 18:51:46 +08:00
|
|
|
})
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const { handleNodeAdd } = useNodesInteractions()
|
2024-05-27 21:57:08 +08:00
|
|
|
const { availablePrevBlocks } = useAvailableBlocks((data as Edge['data'])!.targetType, (data as Edge['data'])?.isInIteration)
|
|
|
|
const { availableNextBlocks } = useAvailableBlocks((data as Edge['data'])!.sourceType, (data as Edge['data'])?.isInIteration)
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
const handleOpenChange = useCallback((v: boolean) => {
|
|
|
|
setOpen(v)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleInsert = useCallback<OnSelectBlock>((nodeType, toolDefaultValue) => {
|
|
|
|
handleNodeAdd(
|
|
|
|
{
|
|
|
|
nodeType,
|
|
|
|
toolDefaultValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prevNodeId: source,
|
|
|
|
prevNodeSourceHandle: sourceHandleId || 'source',
|
|
|
|
nextNodeId: target,
|
|
|
|
nextNodeTargetHandle: targetHandleId || 'target',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}, [handleNodeAdd, source, sourceHandleId, target, targetHandleId])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<BaseEdge
|
|
|
|
id={id}
|
|
|
|
path={edgePath}
|
|
|
|
style={{
|
|
|
|
stroke: (selected || data?._connectedNodeIsHovering || data?._runned) ? '#2970FF' : '#D0D5DD',
|
|
|
|
strokeWidth: 2,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<EdgeLabelRenderer>
|
|
|
|
<div
|
2024-05-27 21:57:08 +08:00
|
|
|
className={cn(
|
|
|
|
'nopan nodrag hover:scale-125',
|
|
|
|
data?._hovering ? 'block' : 'hidden',
|
|
|
|
open && '!block',
|
|
|
|
data.isInIteration && `z-[${ITERATION_CHILDREN_Z_INDEX}]`,
|
|
|
|
)}
|
2024-04-08 18:51:46 +08:00
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
transform: `translate(-50%, -50%) translate(${labelX}px, ${labelY}px)`,
|
|
|
|
pointerEvents: 'all',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<BlockSelector
|
|
|
|
open={open}
|
|
|
|
onOpenChange={handleOpenChange}
|
|
|
|
asChild
|
|
|
|
onSelect={handleInsert}
|
2024-05-27 21:57:08 +08:00
|
|
|
availableBlocksTypes={intersection(availablePrevBlocks, availableNextBlocks)}
|
2024-04-08 18:51:46 +08:00
|
|
|
triggerClassName={() => 'hover:scale-150 transition-all'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</EdgeLabelRenderer>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default memo(CustomEdge)
|