add labels
This commit is contained in:
@@ -1,13 +1,20 @@
|
|||||||
import React from 'react'
|
import React, { useContext, useMemo } from 'react'
|
||||||
import {
|
import {
|
||||||
BaseEdge,
|
BaseEdge,
|
||||||
getBezierPath,
|
getBezierPath,
|
||||||
type EdgeProps,
|
type EdgeProps,
|
||||||
} from '@xyflow/react'
|
} from '@xyflow/react'
|
||||||
|
import FlowContext from '../../lib/flowContext'
|
||||||
|
|
||||||
const EDGE_STROKE_WIDTH = 2
|
const EDGE_STROKE_WIDTH = 2
|
||||||
const DOT_MARKER_R = 1.5
|
const DOT_MARKER_R = 1.5
|
||||||
|
|
||||||
|
function getEdgeLabelByTargetType(targetType: string | undefined): string | undefined {
|
||||||
|
if (targetType === 'render') return 'render'
|
||||||
|
if (targetType === 'config') return 'add input'
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
export function AnimatedEdge({
|
export function AnimatedEdge({
|
||||||
id,
|
id,
|
||||||
sourceX,
|
sourceX,
|
||||||
@@ -17,17 +24,20 @@ export function AnimatedEdge({
|
|||||||
sourcePosition,
|
sourcePosition,
|
||||||
targetPosition,
|
targetPosition,
|
||||||
style,
|
style,
|
||||||
label,
|
label: labelProp,
|
||||||
labelStyle,
|
|
||||||
labelShowBg,
|
|
||||||
labelBgStyle,
|
|
||||||
labelBgPadding,
|
|
||||||
labelBgBorderRadius,
|
|
||||||
labelX,
|
|
||||||
labelY,
|
|
||||||
interactionWidth,
|
interactionWidth,
|
||||||
|
target,
|
||||||
}: EdgeProps) {
|
}: EdgeProps) {
|
||||||
const [edgePath] = getBezierPath({
|
const ctx = useContext(FlowContext)
|
||||||
|
const nodes = ctx?.nodes ?? []
|
||||||
|
const targetNode = useMemo(() => nodes.find((n: any) => n.id === target), [nodes, target])
|
||||||
|
const derivedLabel = useMemo(
|
||||||
|
() => getEdgeLabelByTargetType(targetNode?.type),
|
||||||
|
[targetNode?.type]
|
||||||
|
)
|
||||||
|
const label = labelProp ?? derivedLabel
|
||||||
|
|
||||||
|
const [edgePath, edgeLabelX, edgeLabelY] = getBezierPath({
|
||||||
sourceX,
|
sourceX,
|
||||||
sourceY,
|
sourceY,
|
||||||
targetX,
|
targetX,
|
||||||
@@ -84,16 +94,29 @@ export function AnimatedEdge({
|
|||||||
...style,
|
...style,
|
||||||
}}
|
}}
|
||||||
className="animated-edge-path"
|
className="animated-edge-path"
|
||||||
label={label}
|
|
||||||
labelStyle={labelStyle}
|
|
||||||
labelShowBg={labelShowBg}
|
|
||||||
labelBgStyle={labelBgStyle}
|
|
||||||
labelBgPadding={labelBgPadding}
|
|
||||||
labelBgBorderRadius={labelBgBorderRadius}
|
|
||||||
labelX={labelX}
|
|
||||||
labelY={labelY}
|
|
||||||
interactionWidth={interactionWidth}
|
interactionWidth={interactionWidth}
|
||||||
/>
|
/>
|
||||||
|
{label != null && (
|
||||||
|
<g transform={`translate(${edgeLabelX}, ${edgeLabelY})`} className="nodrag nopan">
|
||||||
|
<rect
|
||||||
|
x={-32}
|
||||||
|
y={-9}
|
||||||
|
width={64}
|
||||||
|
height={18}
|
||||||
|
rx={4}
|
||||||
|
ry={4}
|
||||||
|
className="fill-background stroke-border"
|
||||||
|
strokeWidth={1}
|
||||||
|
/>
|
||||||
|
<text
|
||||||
|
textAnchor="middle"
|
||||||
|
dominantBaseline="middle"
|
||||||
|
className="fill-foreground text-[10px] font-medium"
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user