mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 11:42:29 +08:00
45deaee762
Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Patryk Garstecki <patryk20120@yahoo.pl> Co-authored-by: Sebastian.W <thiner@gmail.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: takatost <takatost@users.noreply.github.com> Co-authored-by: rechardwang <wh_goodjob@163.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Chenhe Gu <guchenhe@gmail.com> Co-authored-by: Joshua <138381132+joshua20231026@users.noreply.github.com> Co-authored-by: Weaxs <459312872@qq.com> Co-authored-by: Ikko Eltociear Ashimine <eltociear@gmail.com> Co-authored-by: leejoo0 <81673835+leejoo0@users.noreply.github.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Vikey Chen <vikeytk@gmail.com> Co-authored-by: wanghl <Wang-HL@users.noreply.github.com> Co-authored-by: Haolin Wang-汪皓临 <haolin.wang@atlaslovestravel.com> Co-authored-by: Zixuan Cheng <61724187+Theysua@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Bowen Liang <bowenliang@apache.org> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: fanghongtai <42790567+fanghongtai@users.noreply.github.com> Co-authored-by: wxfanghongtai <wxfanghongtai@gf.com.cn> Co-authored-by: Matri <qjp@bithuman.io> Co-authored-by: Benjamin <benjaminx@gmail.com>
115 lines
2.8 KiB
TypeScript
115 lines
2.8 KiB
TypeScript
import {
|
|
memo,
|
|
useCallback,
|
|
useState,
|
|
} from 'react'
|
|
import cn from 'classnames'
|
|
import { intersection } from 'lodash-es'
|
|
import type { EdgeProps } from 'reactflow'
|
|
import {
|
|
BaseEdge,
|
|
EdgeLabelRenderer,
|
|
Position,
|
|
getBezierPath,
|
|
} from 'reactflow'
|
|
import {
|
|
useAvailableBlocks,
|
|
useNodesInteractions,
|
|
} from './hooks'
|
|
import BlockSelector from './block-selector'
|
|
import type {
|
|
Edge,
|
|
OnSelectBlock,
|
|
} from './types'
|
|
import { ITERATION_CHILDREN_Z_INDEX } from './constants'
|
|
|
|
const CustomEdge = ({
|
|
id,
|
|
data,
|
|
source,
|
|
sourceHandleId,
|
|
target,
|
|
targetHandleId,
|
|
sourceX,
|
|
sourceY,
|
|
targetX,
|
|
targetY,
|
|
selected,
|
|
}: EdgeProps) => {
|
|
const [
|
|
edgePath,
|
|
labelX,
|
|
labelY,
|
|
] = getBezierPath({
|
|
sourceX: sourceX - 8,
|
|
sourceY,
|
|
sourcePosition: Position.Right,
|
|
targetX: targetX + 8,
|
|
targetY,
|
|
targetPosition: Position.Left,
|
|
curvature: 0.16,
|
|
})
|
|
const [open, setOpen] = useState(false)
|
|
const { handleNodeAdd } = useNodesInteractions()
|
|
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)
|
|
|
|
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
|
|
className={cn(
|
|
'nopan nodrag hover:scale-125',
|
|
data?._hovering ? 'block' : 'hidden',
|
|
open && '!block',
|
|
data.isInIteration && `z-[${ITERATION_CHILDREN_Z_INDEX}]`,
|
|
)}
|
|
style={{
|
|
position: 'absolute',
|
|
transform: `translate(-50%, -50%) translate(${labelX}px, ${labelY}px)`,
|
|
pointerEvents: 'all',
|
|
}}
|
|
>
|
|
<BlockSelector
|
|
open={open}
|
|
onOpenChange={handleOpenChange}
|
|
asChild
|
|
onSelect={handleInsert}
|
|
availableBlocksTypes={intersection(availablePrevBlocks, availableNextBlocks)}
|
|
triggerClassName={() => 'hover:scale-150 transition-all'}
|
|
/>
|
|
</div>
|
|
</EdgeLabelRenderer>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default memo(CustomEdge)
|