-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReactFlow.tsx
More file actions
212 lines (189 loc) · 8.23 KB
/
Copy pathReactFlow.tsx
File metadata and controls
212 lines (189 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, { ReactElement, Ref } from "react";
import { KeyCode as KeyCodeV9 } from "react-flow-renderer";
import { KeyCode as KeyCodeV12 } from "@xyflow/react";
import { CLASSPREFIX as eccgui } from "../../../configuration/constants";
import { ReactFlowMarkers } from "../../../extensions/react-flow/markers/ReactFlowMarkers";
import { ReactFlowVersions } from "../../../extensions/react-flow/versionsupport";
import { ReactFlowHotkeyContext } from "../extensions/ReactFlowHotkeyContext";
import { useReactFlowScrollOnDragV9 } from "../extensions/scrollOnDragHook";
import * as graphConfig from "./../configuration/graph";
import * as linkingConfig from "./../configuration/linking";
import * as unspecifiedConfig from "./../configuration/unspecified";
import * as workflowConfig from "./../configuration/workflow";
import { ReactFlowV9Container, ReactFlowV9ContainerProps } from "./ReactFlowV9";
import { ReactFlowV12Container, ReactFlowV12ContainerProps } from "./ReactFlowV12";
export interface ReactFlowExtendedExtraProps {
/**
* Load `ReactFlow` component with pre-configured values for `nodeTypes` and `edgeTypes`
*/
configuration?: "unspecified" | "graph" | "workflow" | "linking";
/**
* Types data transfers that can be dragged in and dropped on the canvas.
*/
dropzoneFor?: string[];
}
export interface ReactFlowExtendedScrollProps {
/** If defined the canvas scrolls on all drag operations (node, selection, edge connect)
* when the mouse pointer comes near the canvas borders or goes beyond them.
* The `id` property of the ReactFlow component must be set in order for this to work.
*
* NOTE: If scrollOnDrag is defined, a ReactFlowProvider must be wrapped around this component (or a parent). */
scrollOnDrag?: {
/** Time in milliseconds to wait before the canvas scrolls the next step. */
scrollInterval: number;
/**
* The size of each scroll step.
* This should be a number between 0.0 - 1.0.
* E.g. a value of 0.25 will lead to a scroll step size of a quarter of the visible canvas. */
scrollStepSize: number;
};
}
interface ReactFlowExtendedVersion9SupportProps {
/**
* Set version of `ReactFlow` that is used internally.
* If not set then v9 is used.
*/
flowVersion?: ReactFlowVersions.V9;
}
interface ReactFlowExtendedVersion12SupportProps {
/**
* Set version of `ReactFlow` that is used internally.
*/
flowVersion: ReactFlowVersions.V12;
/**
* Extra scroll on drag (pan on drag) support from our side should not be necessary anymore with v12.
*/
scrollOnDrag?: never;
}
export type ReactFlowExtendedPropsV9 = ReactFlowExtendedVersion9SupportProps &
ReactFlowV9ContainerProps &
ReactFlowExtendedExtraProps &
ReactFlowExtendedScrollProps;
export type ReactFlowExtendedPropsV12 = ReactFlowExtendedVersion12SupportProps &
ReactFlowV12ContainerProps &
ReactFlowExtendedExtraProps;
export type ReactFlowExtendedProps = ReactFlowExtendedPropsV9 | ReactFlowExtendedPropsV12;
/**
* `ReactFlow` container extension that includes pre-configured nodes and edges for
* Corporate Memory tools.
*
* @param T The concrete type of the corresponding version, i.e. either one of ReactFlowExtendedPropsV9 or ReactFlowExtendedPropsV12
*/
const ReactFlowExtendedPlain = <T extends ReactFlowExtendedProps>(
{
configuration = "unspecified",
flowVersion = ReactFlowVersions.V9,
dropzoneFor,
scrollOnDrag,
children,
className,
selectionKeyCode,
multiSelectionKeyCode,
deleteKeyCode,
zoomActivationKeyCode,
...originalProps
}: T,
outerRef: Ref<HTMLDivElement>,
) => {
const innerRef = React.useRef<HTMLDivElement>(null);
React.useImperativeHandle(outerRef, () => innerRef.current!, []);
React.useEffect(() => {
const reactflowContainer = innerRef?.current;
if (reactflowContainer && dropzoneFor) {
const addDragover = (event: DragEvent) => {
reactflowContainer.classList.add(`${eccgui}-graphviz__canvas--draghover`);
event.preventDefault();
};
const removeDragover = (event: DragEvent) => {
if (reactflowContainer === event.target) {
reactflowContainer.classList.remove(`${eccgui}-graphviz__canvas--draghover`);
}
};
reactflowContainer.addEventListener("dragover", addDragover);
reactflowContainer.addEventListener("dragleave", removeDragover);
reactflowContainer.addEventListener("drop", removeDragover);
return () => {
reactflowContainer.removeEventListener("dragover", addDragover);
reactflowContainer.removeEventListener("dragleave", removeDragover);
reactflowContainer.removeEventListener("drop", removeDragover);
};
}
return;
}, [innerRef, dropzoneFor]);
/** If the hot keys should be disabled. By default, they are always disabled. */
const { hotKeysDisabled } = React.useContext(ReactFlowHotkeyContext);
const configReactFlow = {
unspecified: unspecifiedConfig,
graph: graphConfig,
workflow: workflowConfig,
linking: linkingConfig,
};
const sharedProperties = {
className:
`${eccgui}-graphviz__canvas` +
` ${eccgui}-configuration--colors__react-flow-${configuration}` +
(className ? ` ${className}` : ""),
nodeTypes: configReactFlow[configuration].nodeTypes,
edgeTypes: configReactFlow[configuration].edgeTypes,
"data-dropzone-for": dropzoneFor ? dropzoneFor.join(" ") : undefined,
};
let keyCodeConfig = {};
switch (flowVersion) {
case "v9":
keyCodeConfig = {
selectionKeyCode: hotKeysDisabled ? undefined : (selectionKeyCode as KeyCodeV9),
deleteKeyCode: hotKeysDisabled ? undefined : (deleteKeyCode as KeyCodeV9),
multiSelectionKeyCode: hotKeysDisabled ? undefined : (multiSelectionKeyCode as KeyCodeV9),
zoomActivationKeyCode: hotKeysDisabled ? undefined : (zoomActivationKeyCode as KeyCodeV9),
};
break;
case "v12":
keyCodeConfig = {
selectionKeyCode: hotKeysDisabled ? null : (selectionKeyCode as KeyCodeV12),
deleteKeyCode: hotKeysDisabled ? null : (deleteKeyCode as KeyCodeV12),
multiSelectionKeyCode: hotKeysDisabled ? null : (multiSelectionKeyCode as KeyCodeV12),
zoomActivationKeyCode: hotKeysDisabled ? null : (zoomActivationKeyCode as KeyCodeV12),
};
break;
}
let scrollOnDragFunctions = {};
switch (flowVersion) {
case "v9":
scrollOnDragFunctions = useReactFlowScrollOnDragV9({
reactFlowProps: originalProps as unknown as ReactFlowV9ContainerProps,
scrollOnDrag,
});
break;
// should not be necessary for v12
}
const containerConfig = {
...sharedProperties,
...keyCodeConfig,
...originalProps,
...scrollOnDragFunctions,
};
switch (flowVersion) {
case "v9":
return (
<ReactFlowV9Container ref={innerRef} {...(containerConfig as unknown as ReactFlowV9ContainerProps)}>
{children}
<ReactFlowMarkers />
</ReactFlowV9Container>
);
case "v12":
return (
<ReactFlowV12Container ref={innerRef} {...(containerConfig as ReactFlowV12ContainerProps)}>
{children}
<ReactFlowMarkers />
</ReactFlowV12Container>
);
default:
return <div></div>;
}
};
/** Hack to make the Type Parameter work with the forward ref. */
export const ReactFlowExtended = React.forwardRef(ReactFlowExtendedPlain) as <T extends ReactFlowExtendedProps>(
p: T & { ref?: Ref<HTMLDivElement> },
) => ReactElement;
/** Classes that when set for an element, prevent that they trigger react-flow dragging, wheel and panning actions. */
export const preventReactFlowActionsClasses = "nodrag nopan nowheel";