Skip to content

Commit f043a50

Browse files
committed
include version splitter for react flow edges
1 parent a230b0c commit f043a50

2 files changed

Lines changed: 63 additions & 58 deletions

File tree

src/extensions/react-flow/edges/EdgeDefault.tsx

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { EdgeProps as ReactFlowEdgeProps } from "react-flow-renderer/dist/types"
44

55
import { intentClassName, IntentTypes } from "../../../common/Intent";
66
import { CLASSPREFIX as eccgui } from "../../../configuration/constants";
7+
import { ReactFlowVersions, useReactFlowVersion } from "../versionsupport";
78

89
import { nodeContentUtils } from "./../nodes/NodeContent";
910
import { NodeHighlightColor } from "./../nodes/sharedTypes";
11+
import { EdgeDefaultV12, EdgeDefaultV12Props } from "./EdgeDefaultV12";
1012
import { drawEdgeStep, drawEdgeStraight } from "./utils";
1113

1214
export interface EdgeDefaultDataProps {
@@ -30,11 +32,7 @@ export interface EdgeDefaultDataProps {
3032
* Direction of the SVG path is inversed.
3133
* This is important for the placement of the markers and the animation movement.
3234
*/
33-
inversePath?: boolean;
34-
/**
35-
* Reference link to the SVG marker used for the start of the edge
36-
*/
37-
markerStart?: string;
35+
inversePath?: boolean; // FIXME: diection of animation is not inverted
3836
/**
3937
* Callback handler that returns a React element used as edge title.
4038
*/
@@ -46,19 +44,35 @@ export interface EdgeDefaultDataProps {
4644
edgeSvgProps?: React.SVGProps<SVGGElement>;
4745
}
4846

49-
export interface EdgeDefaultProps extends ReactFlowEdgeProps {
47+
/**
48+
* @deprecated (v26) v9 support is removed after v25
49+
*/
50+
interface EdgeDefaultV9DataProps extends EdgeDefaultDataProps {
51+
/**
52+
* Reference link to the SVG marker used for the start of the edge
53+
* @deprecated (v26) only necessary for react flow v9
54+
*/
55+
markerStart?: string;
56+
}
57+
58+
/**
59+
* @deprecated (v26) v9 support is removed after v25
60+
*/
61+
interface EdgeDefaultV9Props extends ReactFlowEdgeProps {
5062
/**
5163
* Defining content and markers for the edge.
5264
*/
53-
data?: EdgeDefaultDataProps;
65+
data?: EdgeDefaultV9DataProps;
5466
/**
5567
* Callback handler that returns a SVG path as string to define how the edge is rendered.
5668
*/
5769
drawSvgPath?: (edge: ReactFlowEdgeProps) => string;
5870
}
5971

60-
export const EdgeDefault = memo(
61-
({ data = {}, drawSvgPath = drawEdgeStraight, ...edgeOriginalProperties }: EdgeDefaultProps) => {
72+
export type EdgeDefaultProps = EdgeDefaultV9Props | EdgeDefaultV12Props;
73+
74+
const EdgeDefaultV9 = memo(
75+
({ data = {}, drawSvgPath = drawEdgeStraight, ...edgeOriginalProperties }: EdgeDefaultV9Props) => {
6276
const { pathGlowWidth = 10, markerStart, strokeType, intent, highlightColor, edgeSvgProps } = data;
6377

6478
const pathDisplay = drawSvgPath({ ...edgeOriginalProperties, data });
@@ -94,7 +108,11 @@ export const EdgeDefault = memo(
94108
return (
95109
<g
96110
{...edgeSvgProps}
97-
className={createEdgeDefaultClassName({ intent }, edgeSvgProps?.className ?? "")}
111+
className={createEdgeDefaultClassName(
112+
{ intent },
113+
`${edgeSvgProps?.className ?? ""}`,
114+
ReactFlowVersions.V9
115+
)}
98116
style={{
99117
...edgeSvgProps?.style,
100118
...edgeStyle,
@@ -130,13 +148,31 @@ export const EdgeDefault = memo(
130148
}
131149
);
132150

151+
/**
152+
* This element cannot be used directly, it must be connected via a `edgeTypes` definition.
153+
* @see https://reactflow.dev/docs/api/nodes/
154+
*/
155+
export const EdgeDefault = memo((props: EdgeDefaultProps) => {
156+
const flowVersionCheck = useReactFlowVersion();
157+
switch (flowVersionCheck) {
158+
case "v9":
159+
return <EdgeDefaultV9 {...(props as EdgeDefaultV9Props)} />;
160+
case "v12":
161+
return <EdgeDefaultV12 {...(props as EdgeDefaultV12Props)} />;
162+
default:
163+
return <></>;
164+
}
165+
});
166+
133167
const createEdgeDefaultClassName = (
134168
{ strokeType, intent, highlightColor }: EdgeDefaultDataProps,
135-
baseClass = "react-flow__edge-path"
169+
baseClass = "react-flow__edge-path",
170+
flowVersion?: ReactFlowVersions
136171
) => {
137172
const { highlightClassNameSuffix } = nodeContentUtils.evaluateHighlightColors("--edge-highlight", highlightColor);
138173
return (
139174
baseClass +
175+
(flowVersion ? ` react-flow__edge--${flowVersion}` : "") +
140176
(strokeType ? ` ${baseClass}--stroke-${strokeType}` : "") +
141177
(intent ? ` ${intentClassName(intent)}` : "") +
142178
(highlightClassNameSuffix.length > 0

src/extensions/react-flow/edges/EdgeDefaultV12.tsx

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,20 @@
11
import { memo } from "react";
22
import React from "react";
3-
import { BaseEdge, Edge, EdgeProps, EdgeText, getBezierPath, getEdgeCenter } from "@xyflow/react";
3+
import { BaseEdge, Edge, EdgeProps, EdgeText, getBezierPath } from "@xyflow/react";
44

5-
import { IntentTypes } from "../../../common/Intent";
65
import { nodeContentUtils } from "../nodes/NodeContent";
7-
import { NodeHighlightColor } from "../nodes/sharedTypes";
6+
import { ReactFlowVersions } from "../versionsupport";
87

9-
import { edgeDefaultUtils } from "./EdgeDefault";
8+
import { EdgeDefaultDataProps, edgeDefaultUtils } from "./EdgeDefault";
109

11-
export type EdgeDefaultV12DataProps = Record<string, unknown> & {
12-
/**
13-
* Overwrites the default style how the edge stroke is displayed.
14-
*/
15-
strokeType?: "solid" | "dashed" | "dotted" | "double" | "doubledashed";
16-
/**
17-
* Feedback state of the node.
18-
*/
19-
intent?: IntentTypes;
20-
/**
21-
* Set the color of used highlights to mark the edge.
22-
*/
23-
highlightColor?: NodeHighlightColor | [NodeHighlightColor, NodeHighlightColor];
24-
/**
25-
* Size of the "glow" effect when the edge is hovered.
26-
*/
27-
pathGlowWidth?: number;
28-
/*
29-
* Direction of the SVG path is inversed.
30-
* This is important for the placement of the markers and the animation movement.
31-
*/
32-
inversePath?: boolean;
33-
/**
34-
* Callback handler that returns a React element used as edge title.
35-
*/
36-
renderLabel?: (edgeCenter: [number, number, number, number]) => React.ReactNode;
37-
/**
38-
* Properties are forwarded to the internally used SVG `g` element.
39-
* Data attributes for test ids coud be included here.
40-
*/
41-
edgeSvgProps?: React.SVGProps<SVGGElement>;
42-
};
10+
/**
11+
* @deprecated (v26) use EdgeDefaultDataProps
12+
*/
13+
type EdgeDefaultV12DataProps = Record<string, unknown> & EdgeDefaultDataProps;
14+
/**
15+
* @deprecated (v26) use EdgeDefaultProps
16+
*/
17+
export type EdgeDefaultV12Props = EdgeProps<Edge<EdgeDefaultV12DataProps>>;
4318

4419
/**
4520
* This element cannot be used directly, it must be connected via a `edgeTypes` definition.
@@ -63,7 +38,7 @@ export const EdgeDefaultV12 = memo(
6338
labelBgBorderRadius = 3,
6439
data = {},
6540
...edgeOriginalProperties
66-
}: EdgeProps<Edge<EdgeDefaultV12DataProps>>) => {
41+
}: EdgeDefaultV12Props) => {
6742
const { pathGlowWidth = 10, highlightColor, renderLabel, edgeSvgProps, intent, inversePath, strokeType } = data;
6843

6944
const [edgePath, labelX, labelY] = getBezierPath({
@@ -81,19 +56,12 @@ export const EdgeDefaultV12 = memo(
8156
highlightColor
8257
);
8358

84-
const edgeCenter = getEdgeCenter({
85-
sourceX,
86-
sourceY,
87-
targetX,
88-
targetY,
89-
});
90-
9159
const renderedLabel =
9260
renderLabel?.([labelX, labelY, sourceX, targetX]) ??
9361
(label ? (
9462
<EdgeText
95-
x={edgeCenter[0]}
96-
y={edgeCenter[1]}
63+
x={labelX}
64+
y={labelY}
9765
label={label}
9866
labelStyle={labelStyle}
9967
labelShowBg={labelShowBg}
@@ -109,7 +77,8 @@ export const EdgeDefaultV12 = memo(
10977
"react-flow__edge " +
11078
edgeDefaultUtils.createEdgeDefaultClassName(
11179
{ intent },
112-
`${edgeOriginalProperties.selected ? "selected" : ""}`
80+
`${edgeOriginalProperties.selected ? "selected" : ""}`,
81+
ReactFlowVersions.V12
11382
)
11483
}
11584
tabIndex={0}

0 commit comments

Comments
 (0)