react-cosmos-diagram 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/components/A11yDescriptions/index.d.ts +3 -0
- package/dist/esm/components/A11yDescriptions/index.d.ts.map +1 -0
- package/dist/esm/components/ConnectionEdge/ConnectionPath.d.ts +13 -0
- package/dist/esm/components/ConnectionEdge/ConnectionPath.d.ts.map +1 -0
- package/dist/esm/components/ConnectionEdge/index.d.ts +8 -0
- package/dist/esm/components/ConnectionEdge/index.d.ts.map +1 -0
- package/dist/esm/components/Edges/Anchor.d.ts +16 -0
- package/dist/esm/components/Edges/Anchor.d.ts.map +1 -0
- package/dist/esm/components/Edges/BaseEdge.d.ts +7 -0
- package/dist/esm/components/Edges/BaseEdge.d.ts.map +1 -0
- package/dist/esm/components/Edges/EdgeLabel.d.ts +6 -0
- package/dist/esm/components/Edges/EdgeLabel.d.ts.map +1 -0
- package/dist/esm/components/Edges/StepEdge.d.ts +25 -0
- package/dist/esm/components/Edges/StepEdge.d.ts.map +1 -0
- package/dist/esm/components/Edges/type.d.ts +91 -0
- package/dist/esm/components/Edges/type.d.ts.map +1 -0
- package/dist/esm/components/Edges/utils.d.ts +7 -0
- package/dist/esm/components/Edges/utils.d.ts.map +1 -0
- package/dist/esm/components/Edges/wrapEdge.d.ts +11 -0
- package/dist/esm/components/Edges/wrapEdge.d.ts.map +1 -0
- package/dist/esm/components/Node/index.d.ts +4 -0
- package/dist/esm/components/Node/index.d.ts.map +1 -0
- package/dist/esm/components/Node/type.d.ts +84 -0
- package/dist/esm/components/Node/type.d.ts.map +1 -0
- package/dist/esm/components/Node/utils.d.ts +18 -0
- package/dist/esm/components/Node/utils.d.ts.map +1 -0
- package/dist/esm/components/Node/wrapNode.d.ts +10 -0
- package/dist/esm/components/Node/wrapNode.d.ts.map +1 -0
- package/dist/esm/components/Port/index.d.ts +9 -0
- package/dist/esm/components/Port/index.d.ts.map +1 -0
- package/dist/esm/components/Port/type.d.ts +12 -0
- package/dist/esm/components/Port/type.d.ts.map +1 -0
- package/dist/esm/components/Port/utils.d.ts +21 -0
- package/dist/esm/components/Port/utils.d.ts.map +1 -0
- package/dist/esm/components/ReactDiagramProvider/index.d.ts +4 -0
- package/dist/esm/components/ReactDiagramProvider/index.d.ts.map +1 -0
- package/dist/esm/components/ReactDiagramProvider/type.d.ts +64 -0
- package/dist/esm/components/ReactDiagramProvider/type.d.ts.map +1 -0
- package/dist/esm/components/StoreUpdater/index.d.ts +7 -0
- package/dist/esm/components/StoreUpdater/index.d.ts.map +1 -0
- package/dist/esm/container/DiagramRenderer/index.d.ts +13 -0
- package/dist/esm/container/DiagramRenderer/index.d.ts.map +1 -0
- package/dist/esm/container/EdgeRenderer/MarkerComponent.d.ts +11 -0
- package/dist/esm/container/EdgeRenderer/MarkerComponent.d.ts.map +1 -0
- package/dist/esm/container/EdgeRenderer/MarkerSymbols.d.ts +8 -0
- package/dist/esm/container/EdgeRenderer/MarkerSymbols.d.ts.map +1 -0
- package/dist/esm/container/EdgeRenderer/index.d.ts +17 -0
- package/dist/esm/container/EdgeRenderer/index.d.ts.map +1 -0
- package/dist/esm/container/EdgeRenderer/type.d.ts +9 -0
- package/dist/esm/container/EdgeRenderer/type.d.ts.map +1 -0
- package/dist/esm/container/EdgeRenderer/utils.d.ts +28 -0
- package/dist/esm/container/EdgeRenderer/utils.d.ts.map +1 -0
- package/dist/esm/container/NodeRenderer/index.d.ts +15 -0
- package/dist/esm/container/NodeRenderer/index.d.ts.map +1 -0
- package/dist/esm/container/NodeRenderer/type.d.ts +9 -0
- package/dist/esm/container/NodeRenderer/type.d.ts.map +1 -0
- package/dist/esm/container/NodeRenderer/utils.d.ts +3 -0
- package/dist/esm/container/NodeRenderer/utils.d.ts.map +1 -0
- package/dist/esm/container/ReactDiagram/DiagramView.d.ts +15 -0
- package/dist/esm/container/ReactDiagram/DiagramView.d.ts.map +1 -0
- package/dist/esm/container/ReactDiagram/Wrapper.d.ts +7 -0
- package/dist/esm/container/ReactDiagram/Wrapper.d.ts.map +1 -0
- package/dist/esm/container/ReactDiagram/index.d.ts +54 -0
- package/dist/esm/container/ReactDiagram/index.d.ts.map +1 -0
- package/dist/esm/container/Viewport/index.d.ts +7 -0
- package/dist/esm/container/Viewport/index.d.ts.map +1 -0
- package/dist/esm/container/ZoomPane/index.d.ts +8 -0
- package/dist/esm/container/ZoomPane/index.d.ts.map +1 -0
- package/dist/esm/contexts/NodeIdContext.d.ts +7 -0
- package/dist/esm/contexts/NodeIdContext.d.ts.map +1 -0
- package/dist/esm/contexts/RFStoreContext.d.ts +5 -0
- package/dist/esm/contexts/RFStoreContext.d.ts.map +1 -0
- package/dist/esm/fixtures/errorMessages.d.ts +11 -0
- package/dist/esm/fixtures/errorMessages.d.ts.map +1 -0
- package/dist/esm/hooks/useDrag/index.d.ts +10 -0
- package/dist/esm/hooks/useDrag/index.d.ts.map +1 -0
- package/dist/esm/hooks/useDrag/type.d.ts +3 -0
- package/dist/esm/hooks/useDrag/type.d.ts.map +1 -0
- package/dist/esm/hooks/useDrag/utils.d.ts +16 -0
- package/dist/esm/hooks/useDrag/utils.d.ts.map +1 -0
- package/dist/esm/hooks/useGetPointerPosition.d.ts +9 -0
- package/dist/esm/hooks/useGetPointerPosition.d.ts.map +1 -0
- package/dist/esm/hooks/useGlobalKeyHandler.d.ts +4 -0
- package/dist/esm/hooks/useGlobalKeyHandler.d.ts.map +1 -0
- package/dist/esm/hooks/useNodeOrEdgeTypes.d.ts +7 -0
- package/dist/esm/hooks/useNodeOrEdgeTypes.d.ts.map +1 -0
- package/dist/esm/hooks/useNodesEdgesState/index.d.ts +8 -0
- package/dist/esm/hooks/useNodesEdgesState/index.d.ts.map +1 -0
- package/dist/esm/hooks/useNodesEdgesState/type.d.ts +49 -0
- package/dist/esm/hooks/useNodesEdgesState/type.d.ts.map +1 -0
- package/dist/esm/hooks/useStore.d.ts +14 -0
- package/dist/esm/hooks/useStore.d.ts.map +1 -0
- package/dist/esm/hooks/useVisibleNodes.d.ts +3 -0
- package/dist/esm/hooks/useVisibleNodes.d.ts.map +1 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +2232 -0
- package/dist/esm/store/index.d.ts +4 -0
- package/dist/esm/store/index.d.ts.map +1 -0
- package/dist/esm/store/initialState.d.ts +6 -0
- package/dist/esm/store/initialState.d.ts.map +1 -0
- package/dist/esm/store/utils.d.ts +18 -0
- package/dist/esm/store/utils.d.ts.map +1 -0
- package/dist/esm/types/core.d.ts +55 -0
- package/dist/esm/types/core.d.ts.map +1 -0
- package/dist/esm/types/general.d.ts +23 -0
- package/dist/esm/types/general.d.ts.map +1 -0
- package/dist/esm/types/index.d.ts +12 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/dist/esm/types/utils.d.ts +26 -0
- package/dist/esm/types/utils.d.ts.map +1 -0
- package/dist/esm/utils/changes.d.ts +12 -0
- package/dist/esm/utils/changes.d.ts.map +1 -0
- package/dist/esm/utils/graph.d.ts +13 -0
- package/dist/esm/utils/graph.d.ts.map +1 -0
- package/dist/esm/utils/index.d.ts +22 -0
- package/dist/esm/utils/index.d.ts.map +1 -0
- package/dist/style.css +144 -0
- package/dist/umd/components/A11yDescriptions/index.d.ts +3 -0
- package/dist/umd/components/A11yDescriptions/index.d.ts.map +1 -0
- package/dist/umd/components/ConnectionEdge/ConnectionPath.d.ts +13 -0
- package/dist/umd/components/ConnectionEdge/ConnectionPath.d.ts.map +1 -0
- package/dist/umd/components/ConnectionEdge/index.d.ts +8 -0
- package/dist/umd/components/ConnectionEdge/index.d.ts.map +1 -0
- package/dist/umd/components/Edges/Anchor.d.ts +16 -0
- package/dist/umd/components/Edges/Anchor.d.ts.map +1 -0
- package/dist/umd/components/Edges/BaseEdge.d.ts +7 -0
- package/dist/umd/components/Edges/BaseEdge.d.ts.map +1 -0
- package/dist/umd/components/Edges/EdgeLabel.d.ts +6 -0
- package/dist/umd/components/Edges/EdgeLabel.d.ts.map +1 -0
- package/dist/umd/components/Edges/StepEdge.d.ts +25 -0
- package/dist/umd/components/Edges/StepEdge.d.ts.map +1 -0
- package/dist/umd/components/Edges/type.d.ts +91 -0
- package/dist/umd/components/Edges/type.d.ts.map +1 -0
- package/dist/umd/components/Edges/utils.d.ts +7 -0
- package/dist/umd/components/Edges/utils.d.ts.map +1 -0
- package/dist/umd/components/Edges/wrapEdge.d.ts +11 -0
- package/dist/umd/components/Edges/wrapEdge.d.ts.map +1 -0
- package/dist/umd/components/Node/index.d.ts +4 -0
- package/dist/umd/components/Node/index.d.ts.map +1 -0
- package/dist/umd/components/Node/type.d.ts +84 -0
- package/dist/umd/components/Node/type.d.ts.map +1 -0
- package/dist/umd/components/Node/utils.d.ts +18 -0
- package/dist/umd/components/Node/utils.d.ts.map +1 -0
- package/dist/umd/components/Node/wrapNode.d.ts +10 -0
- package/dist/umd/components/Node/wrapNode.d.ts.map +1 -0
- package/dist/umd/components/Port/index.d.ts +9 -0
- package/dist/umd/components/Port/index.d.ts.map +1 -0
- package/dist/umd/components/Port/type.d.ts +12 -0
- package/dist/umd/components/Port/type.d.ts.map +1 -0
- package/dist/umd/components/Port/utils.d.ts +21 -0
- package/dist/umd/components/Port/utils.d.ts.map +1 -0
- package/dist/umd/components/ReactDiagramProvider/index.d.ts +4 -0
- package/dist/umd/components/ReactDiagramProvider/index.d.ts.map +1 -0
- package/dist/umd/components/ReactDiagramProvider/type.d.ts +64 -0
- package/dist/umd/components/ReactDiagramProvider/type.d.ts.map +1 -0
- package/dist/umd/components/StoreUpdater/index.d.ts +7 -0
- package/dist/umd/components/StoreUpdater/index.d.ts.map +1 -0
- package/dist/umd/container/DiagramRenderer/index.d.ts +13 -0
- package/dist/umd/container/DiagramRenderer/index.d.ts.map +1 -0
- package/dist/umd/container/EdgeRenderer/MarkerComponent.d.ts +11 -0
- package/dist/umd/container/EdgeRenderer/MarkerComponent.d.ts.map +1 -0
- package/dist/umd/container/EdgeRenderer/MarkerSymbols.d.ts +8 -0
- package/dist/umd/container/EdgeRenderer/MarkerSymbols.d.ts.map +1 -0
- package/dist/umd/container/EdgeRenderer/index.d.ts +17 -0
- package/dist/umd/container/EdgeRenderer/index.d.ts.map +1 -0
- package/dist/umd/container/EdgeRenderer/type.d.ts +9 -0
- package/dist/umd/container/EdgeRenderer/type.d.ts.map +1 -0
- package/dist/umd/container/EdgeRenderer/utils.d.ts +28 -0
- package/dist/umd/container/EdgeRenderer/utils.d.ts.map +1 -0
- package/dist/umd/container/NodeRenderer/index.d.ts +15 -0
- package/dist/umd/container/NodeRenderer/index.d.ts.map +1 -0
- package/dist/umd/container/NodeRenderer/type.d.ts +9 -0
- package/dist/umd/container/NodeRenderer/type.d.ts.map +1 -0
- package/dist/umd/container/NodeRenderer/utils.d.ts +3 -0
- package/dist/umd/container/NodeRenderer/utils.d.ts.map +1 -0
- package/dist/umd/container/ReactDiagram/DiagramView.d.ts +15 -0
- package/dist/umd/container/ReactDiagram/DiagramView.d.ts.map +1 -0
- package/dist/umd/container/ReactDiagram/Wrapper.d.ts +7 -0
- package/dist/umd/container/ReactDiagram/Wrapper.d.ts.map +1 -0
- package/dist/umd/container/ReactDiagram/index.d.ts +54 -0
- package/dist/umd/container/ReactDiagram/index.d.ts.map +1 -0
- package/dist/umd/container/Viewport/index.d.ts +7 -0
- package/dist/umd/container/Viewport/index.d.ts.map +1 -0
- package/dist/umd/container/ZoomPane/index.d.ts +8 -0
- package/dist/umd/container/ZoomPane/index.d.ts.map +1 -0
- package/dist/umd/contexts/NodeIdContext.d.ts +7 -0
- package/dist/umd/contexts/NodeIdContext.d.ts.map +1 -0
- package/dist/umd/contexts/RFStoreContext.d.ts +5 -0
- package/dist/umd/contexts/RFStoreContext.d.ts.map +1 -0
- package/dist/umd/fixtures/errorMessages.d.ts +11 -0
- package/dist/umd/fixtures/errorMessages.d.ts.map +1 -0
- package/dist/umd/hooks/useDrag/index.d.ts +10 -0
- package/dist/umd/hooks/useDrag/index.d.ts.map +1 -0
- package/dist/umd/hooks/useDrag/type.d.ts +3 -0
- package/dist/umd/hooks/useDrag/type.d.ts.map +1 -0
- package/dist/umd/hooks/useDrag/utils.d.ts +16 -0
- package/dist/umd/hooks/useDrag/utils.d.ts.map +1 -0
- package/dist/umd/hooks/useGetPointerPosition.d.ts +9 -0
- package/dist/umd/hooks/useGetPointerPosition.d.ts.map +1 -0
- package/dist/umd/hooks/useGlobalKeyHandler.d.ts +4 -0
- package/dist/umd/hooks/useGlobalKeyHandler.d.ts.map +1 -0
- package/dist/umd/hooks/useNodeOrEdgeTypes.d.ts +7 -0
- package/dist/umd/hooks/useNodeOrEdgeTypes.d.ts.map +1 -0
- package/dist/umd/hooks/useNodesEdgesState/index.d.ts +8 -0
- package/dist/umd/hooks/useNodesEdgesState/index.d.ts.map +1 -0
- package/dist/umd/hooks/useNodesEdgesState/type.d.ts +49 -0
- package/dist/umd/hooks/useNodesEdgesState/type.d.ts.map +1 -0
- package/dist/umd/hooks/useStore.d.ts +14 -0
- package/dist/umd/hooks/useStore.d.ts.map +1 -0
- package/dist/umd/hooks/useVisibleNodes.d.ts +3 -0
- package/dist/umd/hooks/useVisibleNodes.d.ts.map +1 -0
- package/dist/umd/index.d.ts +14 -0
- package/dist/umd/index.d.ts.map +1 -0
- package/dist/umd/index.js +10 -0
- package/dist/umd/store/index.d.ts +4 -0
- package/dist/umd/store/index.d.ts.map +1 -0
- package/dist/umd/store/initialState.d.ts +6 -0
- package/dist/umd/store/initialState.d.ts.map +1 -0
- package/dist/umd/store/utils.d.ts +18 -0
- package/dist/umd/store/utils.d.ts.map +1 -0
- package/dist/umd/types/core.d.ts +55 -0
- package/dist/umd/types/core.d.ts.map +1 -0
- package/dist/umd/types/general.d.ts +23 -0
- package/dist/umd/types/general.d.ts.map +1 -0
- package/dist/umd/types/index.d.ts +12 -0
- package/dist/umd/types/index.d.ts.map +1 -0
- package/dist/umd/types/utils.d.ts +26 -0
- package/dist/umd/types/utils.d.ts.map +1 -0
- package/dist/umd/utils/changes.d.ts +12 -0
- package/dist/umd/utils/changes.d.ts.map +1 -0
- package/dist/umd/utils/graph.d.ts +13 -0
- package/dist/umd/utils/graph.d.ts.map +1 -0
- package/dist/umd/utils/index.d.ts +22 -0
- package/dist/umd/utils/index.d.ts.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1,2232 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useContext, useMemo, useState, useEffect, useRef, memo, useCallback, forwardRef } from 'react';
|
|
3
|
+
import { useStore as useStore$1, createStore } from 'zustand';
|
|
4
|
+
import { zoom, zoomIdentity } from 'd3-zoom';
|
|
5
|
+
import { select } from 'd3-selection';
|
|
6
|
+
import { shallow } from 'zustand/shallow';
|
|
7
|
+
import cc from 'classcat';
|
|
8
|
+
import { drag } from 'd3-drag';
|
|
9
|
+
|
|
10
|
+
const StoreContext = createContext(null);
|
|
11
|
+
const Provider$1 = StoreContext.Provider;
|
|
12
|
+
|
|
13
|
+
const errorMessages = {
|
|
14
|
+
'001': () => 'Seems like you have not used zustand provider as an ancestor',
|
|
15
|
+
'002': () => 'It looks like you`ve created a new nodeTypes or edgeTypes object. If this wasn`t on purpose please define the nodeTypes/edgeTypes outside of the component or memoize them.',
|
|
16
|
+
'010': (nodeType) => `Node type "${nodeType}" not found. Using fallback type "default".`,
|
|
17
|
+
'011': () => 'Only child nodes can use a parent extent',
|
|
18
|
+
'020': () => 'Can`t create edge. An edge needs a source and a target.',
|
|
19
|
+
'021': (id) => `The old edge with id=${id} does not exist.`,
|
|
20
|
+
'022': (type) => `Marker type "${type}" doesn't exist.`,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const zustandErrorMessage = errorMessages['001']();
|
|
24
|
+
function useStore(selector, equalityFn) {
|
|
25
|
+
const store = useContext(StoreContext);
|
|
26
|
+
if (store === null) {
|
|
27
|
+
throw new Error(zustandErrorMessage);
|
|
28
|
+
}
|
|
29
|
+
return useStore$1(store, selector, equalityFn);
|
|
30
|
+
}
|
|
31
|
+
const useStoreApi = () => {
|
|
32
|
+
const store = useContext(StoreContext);
|
|
33
|
+
if (store === null) {
|
|
34
|
+
throw new Error(zustandErrorMessage);
|
|
35
|
+
}
|
|
36
|
+
return useMemo(() => ({
|
|
37
|
+
getState: store.getState,
|
|
38
|
+
setState: store.setState,
|
|
39
|
+
subscribe: store.subscribe,
|
|
40
|
+
destroy: store.destroy,
|
|
41
|
+
}), [store]);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function useGlobalKeyHandler(multiSelectionKeyCode = 'Meta') {
|
|
45
|
+
const store = useStoreApi();
|
|
46
|
+
const [multiSelectionActivate, setMultiSelectionActivate] = useState(false);
|
|
47
|
+
const handleKeyDown = (e) => {
|
|
48
|
+
console.log(123123, e.key, multiSelectionKeyCode);
|
|
49
|
+
if (e.key === multiSelectionKeyCode) {
|
|
50
|
+
setMultiSelectionActivate(true);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const handleKeyUp = () => {
|
|
54
|
+
setMultiSelectionActivate(false);
|
|
55
|
+
};
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
58
|
+
document.addEventListener('keyup', handleKeyUp);
|
|
59
|
+
return () => {
|
|
60
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
61
|
+
document.removeEventListener('keyup', handleKeyUp);
|
|
62
|
+
};
|
|
63
|
+
}, []);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
store.setState({
|
|
66
|
+
multiSelectionActive: multiSelectionActivate,
|
|
67
|
+
});
|
|
68
|
+
}, [multiSelectionActivate]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const internalsSymbol = Symbol.for('internals');
|
|
72
|
+
const devWarn = (id, value = '') => {
|
|
73
|
+
if (process.env.NODE_ENV === 'development') {
|
|
74
|
+
console.warn(`[React Diagram]: ${id}-${errorMessages[id](value)}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const onErrorWrapper = (onError) => (id, value = '') => onError?.(id, errorMessages[id](value));
|
|
78
|
+
const isNumeric = (n) => !isNaN(n) && isFinite(n);
|
|
79
|
+
const getDimensions = (node) => ({
|
|
80
|
+
width: node.offsetWidth,
|
|
81
|
+
height: node.offsetHeight,
|
|
82
|
+
});
|
|
83
|
+
const clamp = (val, min = 0, max = 1) => Math.min(Math.max(val, min), max);
|
|
84
|
+
const clampPosition = (position = { x: 0, y: 0 }, extent) => ({
|
|
85
|
+
x: clamp(position.x, extent[0][0], extent[1][0]),
|
|
86
|
+
y: clamp(position.y, extent[0][1], extent[1][1]),
|
|
87
|
+
});
|
|
88
|
+
const rectToBox = ({ x, y, width, height }) => ({
|
|
89
|
+
x,
|
|
90
|
+
y,
|
|
91
|
+
x2: x + width,
|
|
92
|
+
y2: y + height,
|
|
93
|
+
});
|
|
94
|
+
const isMouseEvent = (event) => 'clientX' in event;
|
|
95
|
+
const getEventPosition = (event, bounds) => {
|
|
96
|
+
const isMouseTriggered = isMouseEvent(event);
|
|
97
|
+
const evtX = isMouseTriggered ? event.clientX : event.touches?.[0].clientX;
|
|
98
|
+
const evtY = isMouseTriggered ? event.clientY : event.touches?.[0].clientY;
|
|
99
|
+
return {
|
|
100
|
+
x: evtX - (bounds?.left ?? 0),
|
|
101
|
+
y: evtY - (bounds?.top ?? 0),
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
const calcAutoPanVelocity = (value, bound, radius, velocity) => {
|
|
105
|
+
const maxRadius = bound - radius;
|
|
106
|
+
if (value < radius) {
|
|
107
|
+
return (clamp(Math.abs(value - radius), 1, 50) / 50) * velocity;
|
|
108
|
+
}
|
|
109
|
+
else if (value > maxRadius) {
|
|
110
|
+
return (-clamp(Math.abs(value - maxRadius), 1, 50) / 50) * velocity;
|
|
111
|
+
}
|
|
112
|
+
return 0;
|
|
113
|
+
};
|
|
114
|
+
const calcAutoPanPosition = (pos, bounds) => {
|
|
115
|
+
const xMovement = calcAutoPanVelocity(pos.x, bounds.width, 30, 10);
|
|
116
|
+
const yMovement = calcAutoPanVelocity(pos.y, bounds.height, 30, 10);
|
|
117
|
+
return [xMovement, yMovement];
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const convertTransform = (transform) => {
|
|
121
|
+
const { x, y, k } = transform;
|
|
122
|
+
return {
|
|
123
|
+
x,
|
|
124
|
+
y,
|
|
125
|
+
zoom: k,
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
const isWrappedWithClass = (event, className) => event.target.closest(`.${className}`);
|
|
129
|
+
const isViewChanged = (prevViewport, eventViewport) => {
|
|
130
|
+
const { x: prevX, y: prevY, zoom: prevZoom } = prevViewport;
|
|
131
|
+
const { x, y, k } = eventViewport;
|
|
132
|
+
return prevX !== x || prevY !== y || prevZoom !== k;
|
|
133
|
+
};
|
|
134
|
+
const selector$6 = (s) => ({
|
|
135
|
+
d3Zoom: s.d3Zoom,
|
|
136
|
+
d3Selection: s.d3Selection,
|
|
137
|
+
});
|
|
138
|
+
function ZoomPane({ noPanClassName, panning, minZoom, maxZoom, defaultViewport, translateExtent, children, onMove, onMoveStart, onMoveEnd, }) {
|
|
139
|
+
const store = useStoreApi();
|
|
140
|
+
const isZoomingOrPanning = useRef(false);
|
|
141
|
+
const zoomPane = useRef(null);
|
|
142
|
+
const d3ZoomHandler = useRef();
|
|
143
|
+
const prevTransform = useRef({ x: 0, y: 0, zoom: 0 });
|
|
144
|
+
const timerId = useRef();
|
|
145
|
+
const { d3Zoom, d3Selection } = useStore(selector$6, shallow);
|
|
146
|
+
const onClick = (e) => {
|
|
147
|
+
if (e.target === zoomPane.current) {
|
|
148
|
+
store.getState().resetSelectedElements();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (zoomPane.current) {
|
|
153
|
+
const bbox = zoomPane.current.getBoundingClientRect();
|
|
154
|
+
const d3ZoomInstance = zoom()
|
|
155
|
+
.scaleExtent([minZoom, maxZoom])
|
|
156
|
+
.translateExtent(translateExtent);
|
|
157
|
+
const selection = select(zoomPane.current).call(d3ZoomInstance);
|
|
158
|
+
const updatedTransform = zoomIdentity
|
|
159
|
+
.translate(defaultViewport.x, defaultViewport.y)
|
|
160
|
+
.scale(clamp(defaultViewport.zoom, minZoom, maxZoom));
|
|
161
|
+
const extent = [
|
|
162
|
+
[0, 0],
|
|
163
|
+
[bbox.width, bbox.height],
|
|
164
|
+
];
|
|
165
|
+
const constrainedTransform = d3ZoomInstance.constrain()(updatedTransform, extent, translateExtent);
|
|
166
|
+
d3ZoomInstance.transform(selection, constrainedTransform);
|
|
167
|
+
d3ZoomHandler.current = selection.on('wheel.zoom');
|
|
168
|
+
store.setState({
|
|
169
|
+
d3Zoom: d3ZoomInstance,
|
|
170
|
+
d3Selection: selection,
|
|
171
|
+
// we need to pass transform because zoom handler is not registered when we set the initial transform
|
|
172
|
+
transform: [
|
|
173
|
+
constrainedTransform.x,
|
|
174
|
+
constrainedTransform.y,
|
|
175
|
+
constrainedTransform.k,
|
|
176
|
+
],
|
|
177
|
+
domNode: zoomPane.current.closest('.react-diagram'),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}, [translateExtent]);
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
if (d3Zoom && d3Selection) {
|
|
183
|
+
d3Selection.on('wheel.zoom', (event, d) => {
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
if (zoomPane.current && d3ZoomHandler.current) {
|
|
186
|
+
d3ZoomHandler.current.call(zoomPane.current, event, d);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}, [d3Zoom, d3Selection, d3ZoomHandler, panning]);
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
if (d3Zoom) {
|
|
193
|
+
d3Zoom.on('start', (event) => {
|
|
194
|
+
if (!event.sourceEvent) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
isZoomingOrPanning.current = true;
|
|
198
|
+
if (onMoveStart) {
|
|
199
|
+
const flowTransform = convertTransform(event.transform);
|
|
200
|
+
prevTransform.current = flowTransform;
|
|
201
|
+
onMoveStart?.(event.sourceEvent, flowTransform);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}, [d3Zoom, onMoveStart]);
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (d3Zoom) {
|
|
208
|
+
d3Zoom.on('zoom', (event) => {
|
|
209
|
+
store.setState({
|
|
210
|
+
transform: [
|
|
211
|
+
event.transform.x,
|
|
212
|
+
event.transform.y,
|
|
213
|
+
event.transform.k,
|
|
214
|
+
],
|
|
215
|
+
});
|
|
216
|
+
if (onMove) {
|
|
217
|
+
const flowTransform = convertTransform(event.transform);
|
|
218
|
+
onMove?.(event.sourceEvent, flowTransform);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}, [d3Zoom, onMove]);
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
if (d3Zoom) {
|
|
225
|
+
d3Zoom.on('end', (event) => {
|
|
226
|
+
isZoomingOrPanning.current = false;
|
|
227
|
+
if (onMoveEnd &&
|
|
228
|
+
isViewChanged(prevTransform.current, event.transform)) {
|
|
229
|
+
const flowTransform = convertTransform(event.transform);
|
|
230
|
+
prevTransform.current = flowTransform;
|
|
231
|
+
clearTimeout(timerId.current);
|
|
232
|
+
timerId.current = setTimeout(() => {
|
|
233
|
+
onMoveEnd?.(event.sourceEvent, flowTransform);
|
|
234
|
+
}, 0);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}, [d3Zoom]);
|
|
239
|
+
useEffect(() => {
|
|
240
|
+
if (d3Zoom) {
|
|
241
|
+
d3Zoom.filter((event) => {
|
|
242
|
+
if (isWrappedWithClass(event, noPanClassName) &&
|
|
243
|
+
event.type !== 'wheel') {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (!panning)
|
|
247
|
+
return false;
|
|
248
|
+
const buttonAllowed = !event.button || event.button <= 1;
|
|
249
|
+
if (!buttonAllowed)
|
|
250
|
+
return false;
|
|
251
|
+
return true;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}, [d3Zoom, panning]);
|
|
255
|
+
return (jsx("div", { className: "react-diagram__zoompane react-diagram__container", ref: zoomPane, onClick: onClick, children: children }));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const selector$5 = (s) => `translate(${s.transform[0]}px,${s.transform[1]}px) scale(${s.transform[2]})`;
|
|
259
|
+
function Viewport({ children }) {
|
|
260
|
+
const transform = useStore(selector$5);
|
|
261
|
+
return (jsx("div", { className: "react-diagram__viewport react-diagram__container", style: { transform }, children: children }));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const selector$4 = (s) => {
|
|
265
|
+
const { minZoom, maxZoom, translateExtent } = s;
|
|
266
|
+
return {
|
|
267
|
+
minZoom,
|
|
268
|
+
maxZoom,
|
|
269
|
+
translateExtent,
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
function DiagramRenderer({ children, multiSelectionKeyCode, noPanClassName, panning, defaultViewport, onMove, onMoveStart, onMoveEnd, }) {
|
|
273
|
+
const { minZoom, maxZoom, translateExtent } = useStore(selector$4);
|
|
274
|
+
useGlobalKeyHandler(multiSelectionKeyCode);
|
|
275
|
+
return (jsx(ZoomPane, { noPanClassName: noPanClassName, panning: panning, minZoom: minZoom, maxZoom: maxZoom, translateExtent: translateExtent, defaultViewport: defaultViewport, onMove: onMove, onMoveStart: onMoveStart, onMoveEnd: onMoveEnd, children: jsx(Viewport, { children: children }) }));
|
|
276
|
+
}
|
|
277
|
+
DiagramRenderer.displayName = 'DiagramRenderer';
|
|
278
|
+
var DiagramRenderer$1 = memo(DiagramRenderer);
|
|
279
|
+
|
|
280
|
+
function useVisibleNodes() {
|
|
281
|
+
const nodes = useStore(useCallback((s) => s.getNodes(), []));
|
|
282
|
+
return nodes;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
var Position;
|
|
286
|
+
(function (Position) {
|
|
287
|
+
Position["Left"] = "left";
|
|
288
|
+
Position["Top"] = "top";
|
|
289
|
+
Position["Right"] = "right";
|
|
290
|
+
Position["Bottom"] = "bottom";
|
|
291
|
+
})(Position || (Position = {}));
|
|
292
|
+
|
|
293
|
+
var MarkerType;
|
|
294
|
+
(function (MarkerType) {
|
|
295
|
+
MarkerType["Arrow"] = "arrow";
|
|
296
|
+
})(MarkerType || (MarkerType = {}));
|
|
297
|
+
|
|
298
|
+
const selector$3 = (s) => ({
|
|
299
|
+
nodesDraggable: s.nodesDraggable,
|
|
300
|
+
elementsSelectable: s.elementsSelectable,
|
|
301
|
+
updateNodeDimensions: s.updateNodeDimensions,
|
|
302
|
+
onError: s.onError,
|
|
303
|
+
});
|
|
304
|
+
function NodeRenderer({ nodeTypes, onNodeClick, onNodeMouseEnter, onNodeMouseMove, onNodeMouseLeave, onNodeContextMenu, onNodeDoubleClick, ...props }) {
|
|
305
|
+
const { nodesDraggable, elementsSelectable, updateNodeDimensions, onError } = useStore(selector$3, shallow);
|
|
306
|
+
const nodes = useVisibleNodes();
|
|
307
|
+
const resizeObserverRef = useRef();
|
|
308
|
+
const resizeObserver = useMemo(() => {
|
|
309
|
+
if (typeof ResizeObserver === 'undefined') {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
const observer = new ResizeObserver((entries) => {
|
|
313
|
+
const updates = entries.map((entry) => ({
|
|
314
|
+
id: entry.target.getAttribute('data-id'),
|
|
315
|
+
nodeElement: entry.target,
|
|
316
|
+
forceUpdate: true,
|
|
317
|
+
}));
|
|
318
|
+
updateNodeDimensions(updates);
|
|
319
|
+
});
|
|
320
|
+
resizeObserverRef.current = observer;
|
|
321
|
+
return observer;
|
|
322
|
+
}, []);
|
|
323
|
+
useEffect(() => () => {
|
|
324
|
+
resizeObserverRef?.current?.disconnect();
|
|
325
|
+
}, []);
|
|
326
|
+
return (jsx("div", { className: "react-diagram__nodes react-diagram__container", children: nodes.map((node) => {
|
|
327
|
+
const { data, type,
|
|
328
|
+
// elProps
|
|
329
|
+
id, className, style, ariaLabel, positionAbsolute, hidden, selected, selectable, draggable, } = node;
|
|
330
|
+
let nodeType = type || 'default';
|
|
331
|
+
if (!nodeTypes[nodeType]) {
|
|
332
|
+
onError?.('010', nodeType);
|
|
333
|
+
nodeType = 'default';
|
|
334
|
+
}
|
|
335
|
+
const NodeComponent = (nodeTypes[nodeType] ||
|
|
336
|
+
nodeTypes.default);
|
|
337
|
+
const isDraggable = !!(draggable ||
|
|
338
|
+
(nodesDraggable && typeof draggable === 'undefined'));
|
|
339
|
+
const isSelectable = !!(selectable ||
|
|
340
|
+
(elementsSelectable && typeof selectable === 'undefined'));
|
|
341
|
+
const elProps = {
|
|
342
|
+
id,
|
|
343
|
+
className,
|
|
344
|
+
style: {
|
|
345
|
+
...style,
|
|
346
|
+
width: node.width,
|
|
347
|
+
height: node.height,
|
|
348
|
+
},
|
|
349
|
+
ariaLabel,
|
|
350
|
+
};
|
|
351
|
+
const events = {
|
|
352
|
+
onClick: onNodeClick,
|
|
353
|
+
onMouseEnter: onNodeMouseEnter,
|
|
354
|
+
onMouseMove: onNodeMouseMove,
|
|
355
|
+
onMouseLeave: onNodeMouseLeave,
|
|
356
|
+
onContextMenu: onNodeContextMenu,
|
|
357
|
+
onDoubleClick: onNodeDoubleClick,
|
|
358
|
+
};
|
|
359
|
+
const position = {
|
|
360
|
+
positionX: positionAbsolute?.x || 0,
|
|
361
|
+
positionY: positionAbsolute?.y || 0,
|
|
362
|
+
sourcePosition: Position.Bottom,
|
|
363
|
+
targetPosition: Position.Top,
|
|
364
|
+
};
|
|
365
|
+
const booleanProps = {
|
|
366
|
+
selected: !!selected,
|
|
367
|
+
isSelectable,
|
|
368
|
+
isDraggable,
|
|
369
|
+
hidden,
|
|
370
|
+
isParent: !!node[internalsSymbol]?.isParent,
|
|
371
|
+
initialized: !!node.width && !!node.height,
|
|
372
|
+
};
|
|
373
|
+
return (jsx(NodeComponent, { ...props, ...elProps, ...position, ...events, ...booleanProps, zIndex: node[internalsSymbol]?.z ?? 0, type: nodeType, data: data, resizeObserver: resizeObserver }, id));
|
|
374
|
+
}) }));
|
|
375
|
+
}
|
|
376
|
+
NodeRenderer.displayName = 'NodeRenderer';
|
|
377
|
+
var NodeRenderer$1 = memo(NodeRenderer);
|
|
378
|
+
|
|
379
|
+
const ArrowSymbol = ({ color = 'none', strokeWidth = 1 }) => (jsx("polyline", { stroke: color, strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: strokeWidth, fill: color, points: "-5,-4 0,0 -5,4 -5,-4" }));
|
|
380
|
+
const MarkerSymbols = {
|
|
381
|
+
[MarkerType.Arrow]: ArrowSymbol,
|
|
382
|
+
};
|
|
383
|
+
function useMarkerSymbol(type) {
|
|
384
|
+
const store = useStoreApi();
|
|
385
|
+
const symbol = useMemo(() => {
|
|
386
|
+
const symbolExists = Object.prototype.hasOwnProperty.call(MarkerSymbols, type);
|
|
387
|
+
if (!symbolExists) {
|
|
388
|
+
store.getState().onError?.('022', type);
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
return MarkerSymbols[type];
|
|
392
|
+
}, [type]);
|
|
393
|
+
return symbol;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const getNodePositionWithOrigin = (node, nodeOrigin = [0, 0]) => {
|
|
397
|
+
if (!node) {
|
|
398
|
+
return {
|
|
399
|
+
x: 0,
|
|
400
|
+
y: 0,
|
|
401
|
+
positionAbsolute: {
|
|
402
|
+
x: 0,
|
|
403
|
+
y: 0,
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
const offsetX = (node.width ?? 0) * nodeOrigin[0];
|
|
408
|
+
const offsetY = (node.height ?? 0) * nodeOrigin[1];
|
|
409
|
+
const position = {
|
|
410
|
+
x: node.position.x - offsetX,
|
|
411
|
+
y: node.position.y - offsetY,
|
|
412
|
+
};
|
|
413
|
+
return {
|
|
414
|
+
...position,
|
|
415
|
+
positionAbsolute: node.positionAbsolute
|
|
416
|
+
? {
|
|
417
|
+
x: node.positionAbsolute.x - offsetX,
|
|
418
|
+
y: node.positionAbsolute.y - offsetY,
|
|
419
|
+
}
|
|
420
|
+
: position,
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
const getMarkerId = (marker, rfId) => {
|
|
424
|
+
if (typeof marker === 'undefined') {
|
|
425
|
+
return '';
|
|
426
|
+
}
|
|
427
|
+
if (typeof marker === 'string') {
|
|
428
|
+
return marker;
|
|
429
|
+
}
|
|
430
|
+
const idPrefix = rfId ? `${rfId}__` : '';
|
|
431
|
+
return `${idPrefix}${Object.keys(marker)
|
|
432
|
+
.sort()
|
|
433
|
+
.map((key) => `${key}=${marker[key]}`)
|
|
434
|
+
.join('&')}`;
|
|
435
|
+
};
|
|
436
|
+
const isEdge = (element) => 'source' in element && 'target' in element;
|
|
437
|
+
const getEdgeId = ({ source, target }) => `reactdiagram__edge-${source}-${target}`;
|
|
438
|
+
const isExistsConnection = (edge, edges) => edges.some((el) => el.source === edge.source && el.target === edge.target);
|
|
439
|
+
const addEdge = (edgeParams, edges) => {
|
|
440
|
+
if (!isEdge(edgeParams)) {
|
|
441
|
+
devWarn('020');
|
|
442
|
+
return edges;
|
|
443
|
+
}
|
|
444
|
+
if (isExistsConnection(edgeParams, edges)) {
|
|
445
|
+
return edges;
|
|
446
|
+
}
|
|
447
|
+
let edge;
|
|
448
|
+
if (edgeParams.id)
|
|
449
|
+
edge = { ...edgeParams };
|
|
450
|
+
else
|
|
451
|
+
edge = {
|
|
452
|
+
...edgeParams,
|
|
453
|
+
id: getEdgeId(edgeParams),
|
|
454
|
+
};
|
|
455
|
+
return edges.concat(edge);
|
|
456
|
+
};
|
|
457
|
+
const updateEdge = (oldEdge, newConnection, edges, options = { shouldReplaceId: true }) => {
|
|
458
|
+
const { id: oldEdgeId, ...rest } = oldEdge;
|
|
459
|
+
if (!newConnection.source || !newConnection.target)
|
|
460
|
+
devWarn('020');
|
|
461
|
+
const foundEdge = edges.find((e) => e.id === oldEdgeId);
|
|
462
|
+
if (!foundEdge)
|
|
463
|
+
devWarn('021', oldEdgeId);
|
|
464
|
+
const edge = {
|
|
465
|
+
...rest,
|
|
466
|
+
id: options.shouldReplaceId ? getEdgeId(newConnection) : oldEdgeId,
|
|
467
|
+
source: newConnection.source,
|
|
468
|
+
target: newConnection.target,
|
|
469
|
+
};
|
|
470
|
+
return edges.filter((e) => e.id !== oldEdgeId).concat(edge);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const Marker = ({ id, type, color, width = 12.5, height = 12.5, markerUnits = 'strokeWidth', strokeWidth, orient = 'auto-start-reverse', }) => {
|
|
474
|
+
const Symbol = useMarkerSymbol(type);
|
|
475
|
+
if (!Symbol) {
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
return (jsx("marker", { className: "react-diagram__arrowhead", id: id, markerWidth: `${width}`, markerHeight: `${height}`, viewBox: "-10 -10 20 20", markerUnits: markerUnits, orient: orient, refX: "0", refY: "0", children: jsx(Symbol, { color: color, strokeWidth: strokeWidth }) }));
|
|
479
|
+
};
|
|
480
|
+
const markerSelector = ({ defaultColor, rfId }) => (s) => {
|
|
481
|
+
const ids = [];
|
|
482
|
+
return s.edges
|
|
483
|
+
.reduce((markers, edge) => {
|
|
484
|
+
[edge.markerStart, edge.markerEnd].forEach((marker) => {
|
|
485
|
+
if (marker && typeof marker === 'object') {
|
|
486
|
+
const markerId = getMarkerId(marker, rfId);
|
|
487
|
+
if (!ids.includes(markerId)) {
|
|
488
|
+
markers.push({
|
|
489
|
+
id: markerId,
|
|
490
|
+
color: marker.color || defaultColor,
|
|
491
|
+
...marker,
|
|
492
|
+
});
|
|
493
|
+
ids.push(markerId);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
return markers;
|
|
498
|
+
}, [])
|
|
499
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
500
|
+
};
|
|
501
|
+
const MarkerComponent = ({ defaultColor, rfId }) => {
|
|
502
|
+
const markers = useStore(useCallback(markerSelector({ defaultColor, rfId }), [defaultColor, rfId]), (a, b) => !(a.length !== b.length ||
|
|
503
|
+
a.some((marker, i) => marker.id !== b[i].id)));
|
|
504
|
+
return (jsx("defs", { children: markers.map((marker) => (jsx(Marker, { id: marker.id, type: marker.type, color: marker.color, width: marker.width, height: marker.height, markerUnits: marker.markerUnits, strokeWidth: marker.strokeWidth, orient: marker.orient }, marker.id))) }));
|
|
505
|
+
};
|
|
506
|
+
MarkerComponent.displayName = 'MarkerComponent';
|
|
507
|
+
var MarkerComponent$1 = memo(MarkerComponent);
|
|
508
|
+
|
|
509
|
+
function EdgeLabel({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {}, labelBgPadding = [2, 4], labelBgBorderRadius = 2, children, className, ...rest }) {
|
|
510
|
+
const edgeRef = useRef(null);
|
|
511
|
+
const [edgeLabelBox, setEdgeLabelBox] = useState({
|
|
512
|
+
x: 0,
|
|
513
|
+
y: 0,
|
|
514
|
+
width: 0,
|
|
515
|
+
height: 0,
|
|
516
|
+
});
|
|
517
|
+
const edgeTextClasses = cc(['react-diagram__edge-text-wrapper', className]);
|
|
518
|
+
useEffect(() => {
|
|
519
|
+
if (edgeRef.current) {
|
|
520
|
+
const textBbox = edgeRef.current.getBBox();
|
|
521
|
+
setEdgeLabelBox({
|
|
522
|
+
x: textBbox.x,
|
|
523
|
+
y: textBbox.y,
|
|
524
|
+
width: textBbox.width,
|
|
525
|
+
height: textBbox.height,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}, [label]);
|
|
529
|
+
if (typeof label === 'undefined' || !label) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
return (jsxs("g", { transform: `translate(${x - edgeLabelBox.width / 2} ${y - edgeLabelBox.height / 2})`, className: edgeTextClasses, visibility: edgeLabelBox.width ? 'visible' : 'hidden', ...rest, children: [labelShowBg && (jsx("rect", { width: edgeLabelBox.width + 2 * labelBgPadding[0], x: -labelBgPadding[0], y: -labelBgPadding[1], height: edgeLabelBox.height + 2 * labelBgPadding[1], className: "react-diagram__edge-text-bg", style: labelBgStyle, rx: labelBgBorderRadius, ry: labelBgBorderRadius })), jsx("text", { className: "react-diagram__edge-text", y: edgeLabelBox.height / 2, dy: "0.3em", ref: edgeRef, style: labelStyle, children: label }), children] }));
|
|
533
|
+
}
|
|
534
|
+
var EdgeLabel$1 = memo(EdgeLabel);
|
|
535
|
+
|
|
536
|
+
function BaseEdge({ path, labelX, labelY, label, labelStyle, labelShowBg, labelBgStyle, labelBgPadding, labelBgBorderRadius, style, markerEnd, markerStart, }) {
|
|
537
|
+
return (jsxs(Fragment, { children: [jsx("path", { style: style, d: path, fill: "none", className: "react-diagram__edge-path", markerEnd: markerEnd, markerStart: markerStart }), label && isNumeric(labelX) && isNumeric(labelY) ? (jsx(EdgeLabel$1, { x: labelX, y: labelY, label: label, labelStyle: labelStyle, labelShowBg: labelShowBg, labelBgStyle: labelBgStyle, labelBgPadding: labelBgPadding, labelBgBorderRadius: labelBgBorderRadius })) : null] }));
|
|
538
|
+
}
|
|
539
|
+
BaseEdge.displayName = 'BaseEdge';
|
|
540
|
+
|
|
541
|
+
function getEdgeCenter({ sourceX, sourceY, targetX, targetY, }) {
|
|
542
|
+
const xOffset = Math.abs(targetX - sourceX) / 2;
|
|
543
|
+
const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset;
|
|
544
|
+
const yOffset = Math.abs(targetY - sourceY) / 2;
|
|
545
|
+
const centerY = targetY < sourceY ? targetY + yOffset : targetY - yOffset;
|
|
546
|
+
return [centerX, centerY, xOffset, yOffset];
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const HANDLE_DIRECTIONS = {
|
|
550
|
+
[Position.Left]: { x: -1, y: 0 },
|
|
551
|
+
[Position.Right]: { x: 1, y: 0 },
|
|
552
|
+
[Position.Top]: { x: 0, y: -1 },
|
|
553
|
+
[Position.Bottom]: { x: 0, y: 1 },
|
|
554
|
+
};
|
|
555
|
+
const getDirection = ({ source, sourcePosition = Position.Bottom, target, }) => {
|
|
556
|
+
if (sourcePosition === Position.Left || sourcePosition === Position.Right) {
|
|
557
|
+
// when source Node position is on the left side of a Port of target Node => x = 1
|
|
558
|
+
return source.x < target.x ? { x: 1, y: 0 } : { x: -1, y: 0 };
|
|
559
|
+
}
|
|
560
|
+
//when source Node position is above of a Port of target Node => y = 1
|
|
561
|
+
return source.y < target.y ? { x: 0, y: 1 } : { x: 0, y: -1 };
|
|
562
|
+
};
|
|
563
|
+
const distance = (a, b) => Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
|
|
564
|
+
function getPoints({ source, sourcePosition = Position.Bottom, target, targetPosition = Position.Top, center, offset, }) {
|
|
565
|
+
const sourceDir = HANDLE_DIRECTIONS[sourcePosition];
|
|
566
|
+
const targetDir = HANDLE_DIRECTIONS[targetPosition];
|
|
567
|
+
const sourceGapped = {
|
|
568
|
+
x: source.x + sourceDir.x * offset,
|
|
569
|
+
y: source.y + sourceDir.y * offset,
|
|
570
|
+
};
|
|
571
|
+
const targetGapped = {
|
|
572
|
+
x: target.x + targetDir.x * offset,
|
|
573
|
+
y: target.y + targetDir.y * offset,
|
|
574
|
+
};
|
|
575
|
+
const direction = getDirection({
|
|
576
|
+
source: sourceGapped,
|
|
577
|
+
sourcePosition,
|
|
578
|
+
target: targetGapped,
|
|
579
|
+
});
|
|
580
|
+
const dirAccessor = direction.x !== 0 ? 'x' : 'y';
|
|
581
|
+
const currentDirection = direction[dirAccessor];
|
|
582
|
+
let points = [];
|
|
583
|
+
let centerX = 0, centerY = 0;
|
|
584
|
+
const [defaultCenterX, defaultCenterY, defaultOffsetX, defaultOffsetY] = getEdgeCenter({
|
|
585
|
+
sourceX: source.x,
|
|
586
|
+
sourceY: source.y,
|
|
587
|
+
targetX: target.x,
|
|
588
|
+
targetY: target.y,
|
|
589
|
+
});
|
|
590
|
+
const isSourceAndTargetPositionsParallel = sourceDir[dirAccessor] * targetDir[dirAccessor] === -1;
|
|
591
|
+
if (isSourceAndTargetPositionsParallel) {
|
|
592
|
+
centerX = center.x || defaultCenterX;
|
|
593
|
+
centerY = center.y || defaultCenterY;
|
|
594
|
+
const verticalSplit = [
|
|
595
|
+
{ x: centerX, y: sourceGapped.y },
|
|
596
|
+
{ x: centerX, y: targetGapped.y },
|
|
597
|
+
];
|
|
598
|
+
const horizontalSplit = [
|
|
599
|
+
{ x: sourceGapped.x, y: centerY },
|
|
600
|
+
{ x: targetGapped.x, y: centerY },
|
|
601
|
+
];
|
|
602
|
+
const centerLineIsBent = sourceDir[dirAccessor] !== currentDirection;
|
|
603
|
+
if (centerLineIsBent) {
|
|
604
|
+
points = dirAccessor === 'x' ? horizontalSplit : verticalSplit;
|
|
605
|
+
}
|
|
606
|
+
else {
|
|
607
|
+
points = dirAccessor === 'x' ? verticalSplit : horizontalSplit;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const pathPoints = [source, sourceGapped, ...points, targetGapped, target];
|
|
611
|
+
return [pathPoints, centerX, centerY, defaultOffsetX, defaultOffsetY];
|
|
612
|
+
}
|
|
613
|
+
function getBend(a, b, c, size) {
|
|
614
|
+
const bendSize = Math.min(distance(a, b) / 2, distance(b, c) / 2, size);
|
|
615
|
+
const { x, y } = b;
|
|
616
|
+
// no bend
|
|
617
|
+
if ((a.x === x && x === c.x) || (a.y === y && y === c.y)) {
|
|
618
|
+
return `L${x} ${y}`;
|
|
619
|
+
}
|
|
620
|
+
// first segment is horizontal
|
|
621
|
+
if (a.y === y) {
|
|
622
|
+
const xDir = a.x < c.x ? -1 : 1;
|
|
623
|
+
const yDir = a.y < c.y ? 1 : -1;
|
|
624
|
+
return `L ${x + bendSize * xDir},${y}Q ${x},${y} ${x},${y + bendSize * yDir}`;
|
|
625
|
+
}
|
|
626
|
+
const xDir = a.x < c.x ? 1 : -1;
|
|
627
|
+
const yDir = a.y < c.y ? -1 : 1;
|
|
628
|
+
return `L ${x},${y + bendSize * yDir}Q ${x},${y} ${x + bendSize * xDir},${y}`;
|
|
629
|
+
}
|
|
630
|
+
function getStepPath({ sourceX, sourceY, sourcePosition = Position.Bottom, targetX, targetY, targetPosition = Position.Top, borderRadius = 5, centerX, centerY, offset = 20, }) {
|
|
631
|
+
const [points, labelX, labelY, offsetX, offsetY] = getPoints({
|
|
632
|
+
source: { x: sourceX, y: sourceY },
|
|
633
|
+
sourcePosition,
|
|
634
|
+
target: { x: targetX, y: targetY },
|
|
635
|
+
targetPosition,
|
|
636
|
+
center: { x: centerX, y: centerY },
|
|
637
|
+
offset,
|
|
638
|
+
});
|
|
639
|
+
const path = points.reduce((res, p, i) => {
|
|
640
|
+
let segment = '';
|
|
641
|
+
if (i > 0 && i < points.length - 1) {
|
|
642
|
+
segment = getBend(points[i - 1], p, points[i + 1], borderRadius);
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
segment = `${i === 0 ? 'M' : 'L'}${p.x} ${p.y}`;
|
|
646
|
+
}
|
|
647
|
+
res += segment;
|
|
648
|
+
return res;
|
|
649
|
+
}, '');
|
|
650
|
+
return [path, labelX, labelY, offsetX, offsetY];
|
|
651
|
+
}
|
|
652
|
+
const StepEdge = memo(({ sourceX, sourceY, targetX, targetY, label, labelStyle, labelShowBg, labelBgStyle, labelBgPadding, labelBgBorderRadius, style, sourcePosition = Position.Bottom, targetPosition = Position.Top, markerEnd, markerStart, pathOptions, }) => {
|
|
653
|
+
const [path, labelX, labelY] = getStepPath({
|
|
654
|
+
sourceX,
|
|
655
|
+
sourceY,
|
|
656
|
+
sourcePosition,
|
|
657
|
+
targetX,
|
|
658
|
+
targetY,
|
|
659
|
+
targetPosition,
|
|
660
|
+
borderRadius: pathOptions?.borderRadius,
|
|
661
|
+
offset: pathOptions?.offset,
|
|
662
|
+
});
|
|
663
|
+
return (jsx(BaseEdge, { path: path, labelX: labelX, labelY: labelY, label: label, labelStyle: labelStyle, labelShowBg: labelShowBg, labelBgStyle: labelBgStyle, labelBgPadding: labelBgPadding, labelBgBorderRadius: labelBgBorderRadius, style: style, markerEnd: markerEnd, markerStart: markerStart }));
|
|
664
|
+
});
|
|
665
|
+
StepEdge.displayName = 'StepEdge';
|
|
666
|
+
|
|
667
|
+
const ARIA_NODE_DESC_KEY = 'react-diagram__node-desc';
|
|
668
|
+
const ARIA_EDGE_DESC_KEY = 'react-diagram__edge-desc';
|
|
669
|
+
|
|
670
|
+
const getHostForElement = (element) => element.getRootNode?.() || window?.document;
|
|
671
|
+
const getPortType = (PortDomNode) => {
|
|
672
|
+
if (PortDomNode?.classList.contains('target')) {
|
|
673
|
+
return 'target';
|
|
674
|
+
}
|
|
675
|
+
else if (PortDomNode?.classList.contains('source')) {
|
|
676
|
+
return 'source';
|
|
677
|
+
}
|
|
678
|
+
return null;
|
|
679
|
+
};
|
|
680
|
+
const getConnection = (event, port, fromNodeId, fromType, doc) => {
|
|
681
|
+
const isTarget = fromType === 'target';
|
|
682
|
+
const result = {
|
|
683
|
+
isValid: false,
|
|
684
|
+
connection: {
|
|
685
|
+
source: null,
|
|
686
|
+
target: null,
|
|
687
|
+
},
|
|
688
|
+
};
|
|
689
|
+
const PortDomNode = doc.querySelector(`.react-diagram__port[data-id="${port?.nodeId}-${port?.type}"]`);
|
|
690
|
+
const { x, y } = getEventPosition(event);
|
|
691
|
+
const ElementFromPoint = doc.elementFromPoint(x, y);
|
|
692
|
+
const Port = ElementFromPoint?.classList.contains('react-diagram__port')
|
|
693
|
+
? ElementFromPoint
|
|
694
|
+
: PortDomNode;
|
|
695
|
+
if (Port) {
|
|
696
|
+
const portType = getPortType(Port);
|
|
697
|
+
const toNodeId = Port.getAttribute('data-nodeid');
|
|
698
|
+
const connection = {
|
|
699
|
+
source: isTarget ? toNodeId : fromNodeId,
|
|
700
|
+
target: isTarget ? fromNodeId : toNodeId,
|
|
701
|
+
};
|
|
702
|
+
result.connection = connection;
|
|
703
|
+
const isValid = (isTarget && portType === 'source') ||
|
|
704
|
+
(!isTarget && portType === 'target');
|
|
705
|
+
if (isValid) {
|
|
706
|
+
result.isValid = true;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return result;
|
|
710
|
+
};
|
|
711
|
+
const getPorts = (node, portBounds, type, currentPort) => (portBounds[type] || []).reduce((res, h) => {
|
|
712
|
+
if (`${node.id}-${type}` !== currentPort) {
|
|
713
|
+
res.push({
|
|
714
|
+
type,
|
|
715
|
+
nodeId: node.id,
|
|
716
|
+
x: (node.positionAbsolute?.x ?? 0) + h.x + h.width / 2,
|
|
717
|
+
y: (node.positionAbsolute?.y ?? 0) + h.y + h.height / 2,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
return res;
|
|
721
|
+
}, []);
|
|
722
|
+
const getAllPort = ({ nodes, nodeId, portType }) => nodes.reduce((res, node) => {
|
|
723
|
+
if (node[internalsSymbol]) {
|
|
724
|
+
const { portBounds } = node[internalsSymbol];
|
|
725
|
+
let sourcePorts = [];
|
|
726
|
+
let targetPorts = [];
|
|
727
|
+
if (portBounds) {
|
|
728
|
+
sourcePorts = getPorts(node, portBounds, 'source', `${nodeId}-${portType}`);
|
|
729
|
+
targetPorts = getPorts(node, portBounds, 'target', `${nodeId}-${portType}`);
|
|
730
|
+
}
|
|
731
|
+
res.push(...sourcePorts, ...targetPorts);
|
|
732
|
+
}
|
|
733
|
+
return res;
|
|
734
|
+
}, []);
|
|
735
|
+
const getClosestPort = (pos, connectionRadius, ports) => {
|
|
736
|
+
let closestPort = null;
|
|
737
|
+
let minDistance = Infinity;
|
|
738
|
+
ports.forEach((port) => {
|
|
739
|
+
const distance = Math.sqrt(Math.pow(port.x - pos.x, 2) + Math.pow(port.y - pos.y, 2));
|
|
740
|
+
if (distance <= connectionRadius && distance < minDistance) {
|
|
741
|
+
minDistance = distance;
|
|
742
|
+
closestPort = port;
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
return closestPort;
|
|
746
|
+
};
|
|
747
|
+
const handlePointerDown = ({ isAnchor = false, event, nodeId, portType, getState, setState, onConnect, onEdgeUpdateEnd, }) => {
|
|
748
|
+
const doc = getHostForElement(event.target);
|
|
749
|
+
const { domNode, autoPanOnConnect, getNodes, cancelConnection, onConnectStart, onConnectEnd, panBy, } = getState();
|
|
750
|
+
const containerBounds = domNode?.getBoundingClientRect();
|
|
751
|
+
const { x, y } = getEventPosition(event);
|
|
752
|
+
const clickedPort = doc?.elementFromPoint(x, y);
|
|
753
|
+
const clickedPortType = isAnchor ? portType : getPortType(clickedPort);
|
|
754
|
+
const allPort = getAllPort({
|
|
755
|
+
nodes: getNodes(),
|
|
756
|
+
nodeId,
|
|
757
|
+
portType,
|
|
758
|
+
});
|
|
759
|
+
let connectionPosition = getEventPosition(event, containerBounds);
|
|
760
|
+
let closestPort = null;
|
|
761
|
+
let isValid = false;
|
|
762
|
+
let connection = null;
|
|
763
|
+
let autoPanId = 0;
|
|
764
|
+
let autoPanStarted = false;
|
|
765
|
+
if (!containerBounds || !portType) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
const autoPan = () => {
|
|
769
|
+
if (!autoPanOnConnect) {
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
const [xMovement, yMovement] = calcAutoPanPosition(connectionPosition, containerBounds);
|
|
773
|
+
panBy({ x: xMovement, y: yMovement });
|
|
774
|
+
autoPanId = requestAnimationFrame(autoPan);
|
|
775
|
+
};
|
|
776
|
+
setState({
|
|
777
|
+
connectionPosition,
|
|
778
|
+
connectionNodeId: nodeId,
|
|
779
|
+
connectionPortType: clickedPortType,
|
|
780
|
+
});
|
|
781
|
+
onConnectStart?.(event, { nodeId, portType });
|
|
782
|
+
const onPointerMove = (event) => {
|
|
783
|
+
connectionPosition = getEventPosition(event, containerBounds);
|
|
784
|
+
closestPort = getClosestPort(connectionPosition, 20, allPort);
|
|
785
|
+
if (!autoPanStarted) {
|
|
786
|
+
autoPan();
|
|
787
|
+
autoPanStarted = true;
|
|
788
|
+
}
|
|
789
|
+
const result = getConnection(event, closestPort, nodeId, portType, doc);
|
|
790
|
+
isValid = result.isValid;
|
|
791
|
+
connection = result.connection;
|
|
792
|
+
if (closestPort && isValid) {
|
|
793
|
+
const { x, y } = closestPort;
|
|
794
|
+
setState({
|
|
795
|
+
connectionPosition: {
|
|
796
|
+
x,
|
|
797
|
+
y,
|
|
798
|
+
},
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
setState({
|
|
803
|
+
connectionPosition,
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
const onPointerUp = (event) => {
|
|
808
|
+
if (isValid && connection)
|
|
809
|
+
onConnect?.(connection);
|
|
810
|
+
onConnectEnd?.(event);
|
|
811
|
+
if (portType) {
|
|
812
|
+
onEdgeUpdateEnd?.(event);
|
|
813
|
+
}
|
|
814
|
+
cancelConnection();
|
|
815
|
+
cancelAnimationFrame(autoPanId);
|
|
816
|
+
isValid = false;
|
|
817
|
+
connection = null;
|
|
818
|
+
autoPanStarted = false;
|
|
819
|
+
doc.removeEventListener('mousemove', onPointerMove);
|
|
820
|
+
doc.removeEventListener('mouseup', onPointerUp);
|
|
821
|
+
doc.removeEventListener('touchmove', onPointerMove);
|
|
822
|
+
doc.removeEventListener('touchend', onPointerUp);
|
|
823
|
+
};
|
|
824
|
+
doc.addEventListener('mousemove', onPointerMove);
|
|
825
|
+
doc.addEventListener('mouseup', onPointerUp);
|
|
826
|
+
doc.addEventListener('touchmove', onPointerMove);
|
|
827
|
+
doc.addEventListener('touchend', onPointerUp);
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
const portPositionX = (x, shift, position) => {
|
|
831
|
+
if (position === Position.Left)
|
|
832
|
+
return x - shift;
|
|
833
|
+
if (position === Position.Right)
|
|
834
|
+
return x + shift;
|
|
835
|
+
return x;
|
|
836
|
+
};
|
|
837
|
+
const portPositionY = (y, shift, position) => {
|
|
838
|
+
if (position === Position.Top)
|
|
839
|
+
return y - shift;
|
|
840
|
+
if (position === Position.Bottom)
|
|
841
|
+
return y + shift;
|
|
842
|
+
return y;
|
|
843
|
+
};
|
|
844
|
+
const EdgeUpdaterClassName = 'react-diagram__edgeupdater';
|
|
845
|
+
function Anchor({ position, centerX, centerY, radius = 10, onMouseDown, onMouseEnter, onMouseOut, type, }) {
|
|
846
|
+
return (jsx("circle", { className: cc([
|
|
847
|
+
EdgeUpdaterClassName,
|
|
848
|
+
`${EdgeUpdaterClassName}-${type}`,
|
|
849
|
+
]), cx: portPositionX(centerX, radius, position), cy: portPositionY(centerY, radius, position), r: radius, stroke: "transparent", fill: "transparent", onMouseDown: onMouseDown, onMouseEnter: onMouseEnter, onMouseOut: onMouseOut }));
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function getMouseHandler$1(id, getState, handler) {
|
|
853
|
+
return handler === undefined
|
|
854
|
+
? handler
|
|
855
|
+
: (event) => {
|
|
856
|
+
const edge = getState().edges.find((e) => e.id === id);
|
|
857
|
+
if (edge) {
|
|
858
|
+
handler(event, { ...edge });
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
const wrapEdge = (EdgeComponent) => {
|
|
863
|
+
const EdgeWrapper = (props) => {
|
|
864
|
+
const { id, className, style, type, data, rfId, ariaLabel,
|
|
865
|
+
// sourceAndTargetIds
|
|
866
|
+
source, sourcePort, target, targetPort,
|
|
867
|
+
// marker
|
|
868
|
+
markerEnd, markerStart,
|
|
869
|
+
// labelProps
|
|
870
|
+
label, labelStyle, labelShowBg, labelBgStyle, labelBgPadding, labelBgBorderRadius,
|
|
871
|
+
// position
|
|
872
|
+
sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, selected, elementsSelectable, hidden, isFocusable, onClick, onDoubleClick, onContextMenu, onMouseEnter, onMouseMove, onMouseLeave, onEdgeUpdate, onEdgeUpdateStart, onEdgeUpdateEnd, } = props;
|
|
873
|
+
const edgeRef = useRef(null);
|
|
874
|
+
const [updating, setUpdating] = useState(false);
|
|
875
|
+
const store = useStoreApi();
|
|
876
|
+
const markerStartUrl = useMemo(() => `url(#${getMarkerId(markerStart, rfId)})`, [markerStart, rfId]);
|
|
877
|
+
const markerEndUrl = useMemo(() => `url(#${getMarkerId(markerEnd, rfId)})`, [markerEnd, rfId]);
|
|
878
|
+
if (hidden) {
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
const handleEdgeUpdater = (fromPortType) => (event) => {
|
|
882
|
+
if (event.button !== 0) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
const { edges } = store.getState();
|
|
886
|
+
const nodeId = props[fromPortType];
|
|
887
|
+
const edge = edges.find((e) => e.id === id);
|
|
888
|
+
setUpdating(true);
|
|
889
|
+
onEdgeUpdateStart?.(event, edge, fromPortType);
|
|
890
|
+
const onConnectEdge = (connection) => onEdgeUpdate?.(edge, connection);
|
|
891
|
+
const handleEdgeUpdateEnd = (evt) => {
|
|
892
|
+
setUpdating(false);
|
|
893
|
+
onEdgeUpdateEnd?.(evt, edge, fromPortType);
|
|
894
|
+
};
|
|
895
|
+
handlePointerDown({
|
|
896
|
+
isAnchor: true,
|
|
897
|
+
event,
|
|
898
|
+
nodeId,
|
|
899
|
+
portType: fromPortType,
|
|
900
|
+
getState: store.getState,
|
|
901
|
+
setState: store.setState,
|
|
902
|
+
onConnect: onConnectEdge,
|
|
903
|
+
onEdgeUpdateEnd: handleEdgeUpdateEnd,
|
|
904
|
+
});
|
|
905
|
+
};
|
|
906
|
+
const onEdgeClick = (event) => {
|
|
907
|
+
const { edges } = store.getState();
|
|
908
|
+
if (onClick) {
|
|
909
|
+
const edge = edges.find((e) => e.id === id);
|
|
910
|
+
onClick(event, edge);
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
const onEdgeDoubleClick = getMouseHandler$1(id, store.getState, onDoubleClick);
|
|
914
|
+
const onEdgeContextMenu = getMouseHandler$1(id, store.getState, onContextMenu);
|
|
915
|
+
const onEdgeMouseEnter = getMouseHandler$1(id, store.getState, onMouseEnter);
|
|
916
|
+
const onEdgeMouseMove = getMouseHandler$1(id, store.getState, onMouseMove);
|
|
917
|
+
const onEdgeMouseLeave = getMouseHandler$1(id, store.getState, onMouseLeave);
|
|
918
|
+
const inactive = !elementsSelectable;
|
|
919
|
+
const wrapperClassName = cc([
|
|
920
|
+
'react-diagram__edge',
|
|
921
|
+
`react-diagram__edge-${type}`,
|
|
922
|
+
className,
|
|
923
|
+
{ selected, inactive },
|
|
924
|
+
]);
|
|
925
|
+
const marker = { markerStart: markerStartUrl, markerEnd: markerEndUrl };
|
|
926
|
+
const sourceAndTargetIds = {
|
|
927
|
+
source,
|
|
928
|
+
sourcePort,
|
|
929
|
+
target,
|
|
930
|
+
targetPort,
|
|
931
|
+
};
|
|
932
|
+
const labelProps = {
|
|
933
|
+
label,
|
|
934
|
+
labelStyle,
|
|
935
|
+
labelShowBg,
|
|
936
|
+
labelBgStyle,
|
|
937
|
+
labelBgPadding,
|
|
938
|
+
labelBgBorderRadius,
|
|
939
|
+
};
|
|
940
|
+
const position = {
|
|
941
|
+
sourceX,
|
|
942
|
+
sourceY,
|
|
943
|
+
targetX,
|
|
944
|
+
targetY,
|
|
945
|
+
sourcePosition,
|
|
946
|
+
targetPosition,
|
|
947
|
+
};
|
|
948
|
+
const events = {
|
|
949
|
+
onClick: onEdgeClick,
|
|
950
|
+
onDoubleClick: onEdgeDoubleClick,
|
|
951
|
+
onContextMenu: onEdgeContextMenu,
|
|
952
|
+
onMouseEnter: onEdgeMouseEnter,
|
|
953
|
+
onMouseMove: onEdgeMouseMove,
|
|
954
|
+
onMouseLeave: onEdgeMouseLeave,
|
|
955
|
+
};
|
|
956
|
+
return (jsxs("g", { ...events, ref: edgeRef, className: wrapperClassName, tabIndex: isFocusable ? 0 : undefined, role: isFocusable ? 'button' : undefined, "aria-label": ariaLabel === null
|
|
957
|
+
? undefined
|
|
958
|
+
: ariaLabel
|
|
959
|
+
? ariaLabel
|
|
960
|
+
: `Edge from ${source} to ${target}`, "aria-describedby": isFocusable ? `${ARIA_EDGE_DESC_KEY}-${rfId}` : undefined, children: [!updating && (jsx(EdgeComponent, { ...sourceAndTargetIds, ...marker, ...labelProps, ...position, id: id, data: data, style: style, selected: selected })), jsx(Anchor, { position: sourcePosition, centerX: sourceX, centerY: sourceY, radius: 10, onMouseDown: handleEdgeUpdater('target'), onMouseEnter: console.log, onMouseOut: console.log, type: "source" }), jsx(Anchor, { position: targetPosition, centerX: targetX, centerY: targetY, radius: 10, onMouseDown: handleEdgeUpdater('source'), onMouseEnter: console.log, onMouseOut: console.log, type: "target" })] }));
|
|
961
|
+
};
|
|
962
|
+
EdgeWrapper.displayName = 'EdgeWrapper';
|
|
963
|
+
return memo(EdgeWrapper);
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
function createEdgeTypes(edgeTypes) {
|
|
967
|
+
const standardTypes = {
|
|
968
|
+
step: wrapEdge((edgeTypes.step || StepEdge)),
|
|
969
|
+
};
|
|
970
|
+
const wrappedTypes = {};
|
|
971
|
+
const specialTypes = Object.keys(edgeTypes)
|
|
972
|
+
.filter((k) => !['default', 'bezier'].includes(k))
|
|
973
|
+
.reduce((res, key) => {
|
|
974
|
+
res[key] = wrapEdge((edgeTypes[key] || StepEdge));
|
|
975
|
+
return res;
|
|
976
|
+
}, wrappedTypes);
|
|
977
|
+
return {
|
|
978
|
+
...standardTypes,
|
|
979
|
+
...specialTypes,
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
function getPortPosition(position, nodeRect, port = null) {
|
|
983
|
+
const x = (port?.x || 0) + nodeRect.x;
|
|
984
|
+
const y = (port?.y || 0) + nodeRect.y;
|
|
985
|
+
const width = port?.width || nodeRect.width;
|
|
986
|
+
const height = port?.height || nodeRect.height;
|
|
987
|
+
switch (position) {
|
|
988
|
+
case Position.Top:
|
|
989
|
+
return {
|
|
990
|
+
x: x + width / 2,
|
|
991
|
+
y,
|
|
992
|
+
};
|
|
993
|
+
case Position.Right:
|
|
994
|
+
return {
|
|
995
|
+
x: x + width,
|
|
996
|
+
y: y + height / 2,
|
|
997
|
+
};
|
|
998
|
+
case Position.Bottom:
|
|
999
|
+
return {
|
|
1000
|
+
x: x + width / 2,
|
|
1001
|
+
y: y + height,
|
|
1002
|
+
};
|
|
1003
|
+
case Position.Left:
|
|
1004
|
+
return {
|
|
1005
|
+
x,
|
|
1006
|
+
y: y + height / 2,
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
function getPort(bounds, portId) {
|
|
1011
|
+
if (!bounds) {
|
|
1012
|
+
return null;
|
|
1013
|
+
}
|
|
1014
|
+
if (bounds.length === 1 || !portId) {
|
|
1015
|
+
return bounds[0];
|
|
1016
|
+
}
|
|
1017
|
+
else if (portId) {
|
|
1018
|
+
return bounds.find((d) => d.id === portId) || null;
|
|
1019
|
+
}
|
|
1020
|
+
return null;
|
|
1021
|
+
}
|
|
1022
|
+
const getEdgePositions = (sourceNodeRect, sourcePort, sourcePosition, targetNodeRect, targetPort, targetPosition) => {
|
|
1023
|
+
const sourcePortPos = getPortPosition(sourcePosition, sourceNodeRect, sourcePort);
|
|
1024
|
+
const targetPortPos = getPortPosition(targetPosition, targetNodeRect, targetPort);
|
|
1025
|
+
return {
|
|
1026
|
+
sourceX: sourcePortPos.x,
|
|
1027
|
+
sourceY: sourcePortPos.y,
|
|
1028
|
+
targetX: targetPortPos.x,
|
|
1029
|
+
targetY: targetPortPos.y,
|
|
1030
|
+
};
|
|
1031
|
+
};
|
|
1032
|
+
function getNodeData(node) {
|
|
1033
|
+
const portBounds = node?.[internalsSymbol]?.portBounds || null;
|
|
1034
|
+
const isValid = portBounds &&
|
|
1035
|
+
node?.width &&
|
|
1036
|
+
node?.height &&
|
|
1037
|
+
typeof node?.positionAbsolute?.x !== 'undefined' &&
|
|
1038
|
+
typeof node?.positionAbsolute?.y !== 'undefined';
|
|
1039
|
+
return [
|
|
1040
|
+
{
|
|
1041
|
+
x: node?.positionAbsolute?.x || 0,
|
|
1042
|
+
y: node?.positionAbsolute?.y || 0,
|
|
1043
|
+
width: node?.width || 0,
|
|
1044
|
+
height: node?.height || 0,
|
|
1045
|
+
},
|
|
1046
|
+
portBounds,
|
|
1047
|
+
!!isValid,
|
|
1048
|
+
];
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
const selector$2 = (s) => ({
|
|
1052
|
+
edges: s.edges,
|
|
1053
|
+
width: s.width,
|
|
1054
|
+
height: s.height,
|
|
1055
|
+
nodeInternals: s.nodeInternals,
|
|
1056
|
+
onError: s.onError,
|
|
1057
|
+
});
|
|
1058
|
+
function EdgeRenderer({ rfId, edgeTypes, children, noPanClassName, onEdgeClick, onEdgeDoubleClick, onEdgeContextMenu, onEdgeMouseEnter, onEdgeMouseMove, onEdgeMouseLeave, onEdgeUpdate, onEdgeUpdateStart, onEdgeUpdateEnd, }) {
|
|
1059
|
+
const { edges, width, height, nodeInternals } = useStore(selector$2, shallow);
|
|
1060
|
+
return (jsxs(Fragment, { children: [jsxs("svg", { width: width || '100vw', height: height || '100vh', className: "react-diagram__edges react-diagram__container", children: [jsx(MarkerComponent$1, { defaultColor: "#000000", rfId: rfId }), jsx("g", { children: edges.map((edge) => {
|
|
1061
|
+
const { data, type,
|
|
1062
|
+
// elProps
|
|
1063
|
+
id, className, style, ariaLabel,
|
|
1064
|
+
// sourceAndTargetIds
|
|
1065
|
+
source, sourcePort, target, targetPort,
|
|
1066
|
+
// marker
|
|
1067
|
+
markerEnd, markerStart,
|
|
1068
|
+
// labelProps
|
|
1069
|
+
label, labelStyle, labelShowBg, labelBgStyle, labelBgPadding, labelBgBorderRadius, } = edge;
|
|
1070
|
+
const [sourceNodeRect, sourcePortBounds, sourceIsValid] = getNodeData(nodeInternals.get(source));
|
|
1071
|
+
const [targetNodeRect, targetPortBounds, targetIsValid] = getNodeData(nodeInternals.get(target));
|
|
1072
|
+
if (!sourceIsValid || !targetIsValid) {
|
|
1073
|
+
return null;
|
|
1074
|
+
}
|
|
1075
|
+
const edgeType = type || 'step';
|
|
1076
|
+
const EdgeComponent = edgeTypes[edgeType] || edgeTypes.default;
|
|
1077
|
+
const targetNodePorts = targetPortBounds.target;
|
|
1078
|
+
const sourcePortInfo = getPort(sourcePortBounds.source, sourcePort);
|
|
1079
|
+
const targetPortInfo = getPort(targetNodePorts, targetPort);
|
|
1080
|
+
const sourcePosition = sourcePortInfo?.position || Position.Bottom;
|
|
1081
|
+
const targetPosition = targetPortInfo?.position || Position.Top;
|
|
1082
|
+
const isFocusable = !!edge.focusable;
|
|
1083
|
+
if (!sourcePortInfo || !targetPortInfo) {
|
|
1084
|
+
return null;
|
|
1085
|
+
}
|
|
1086
|
+
const elProps = {
|
|
1087
|
+
id,
|
|
1088
|
+
className: cc([className, noPanClassName]),
|
|
1089
|
+
style,
|
|
1090
|
+
ariaLabel,
|
|
1091
|
+
};
|
|
1092
|
+
const sourceAndTargetIds = {
|
|
1093
|
+
source,
|
|
1094
|
+
sourcePort,
|
|
1095
|
+
target,
|
|
1096
|
+
targetPort,
|
|
1097
|
+
};
|
|
1098
|
+
const marker = {
|
|
1099
|
+
markerEnd,
|
|
1100
|
+
markerStart,
|
|
1101
|
+
};
|
|
1102
|
+
const labelProps = {
|
|
1103
|
+
label,
|
|
1104
|
+
labelStyle,
|
|
1105
|
+
labelShowBg,
|
|
1106
|
+
labelBgStyle,
|
|
1107
|
+
labelBgPadding,
|
|
1108
|
+
labelBgBorderRadius,
|
|
1109
|
+
};
|
|
1110
|
+
const edgePositions = getEdgePositions(sourceNodeRect, sourcePortInfo, sourcePosition, targetNodeRect, targetPortInfo, targetPosition);
|
|
1111
|
+
const position = {
|
|
1112
|
+
...edgePositions,
|
|
1113
|
+
sourcePosition,
|
|
1114
|
+
targetPosition,
|
|
1115
|
+
};
|
|
1116
|
+
const events = {
|
|
1117
|
+
onClick: onEdgeClick,
|
|
1118
|
+
onDoubleClick: onEdgeDoubleClick,
|
|
1119
|
+
onContextMenu: onEdgeContextMenu,
|
|
1120
|
+
onMouseEnter: onEdgeMouseEnter,
|
|
1121
|
+
onMouseMove: onEdgeMouseMove,
|
|
1122
|
+
onMouseLeave: onEdgeMouseLeave,
|
|
1123
|
+
onEdgeUpdate,
|
|
1124
|
+
onEdgeUpdateStart,
|
|
1125
|
+
onEdgeUpdateEnd,
|
|
1126
|
+
};
|
|
1127
|
+
return (jsx(EdgeComponent, { ...elProps, ...sourceAndTargetIds, ...marker, ...labelProps, ...position, ...events, rfId: rfId, type: edgeType, data: data, isFocusable: isFocusable }, id));
|
|
1128
|
+
}) })] }), children] }));
|
|
1129
|
+
}
|
|
1130
|
+
EdgeRenderer.displayName = 'EdgeRenderer';
|
|
1131
|
+
var EdgeRenderer$1 = memo(EdgeRenderer);
|
|
1132
|
+
|
|
1133
|
+
const oppositePosition = {
|
|
1134
|
+
[Position.Left]: Position.Right,
|
|
1135
|
+
[Position.Right]: Position.Left,
|
|
1136
|
+
[Position.Top]: Position.Bottom,
|
|
1137
|
+
[Position.Bottom]: Position.Top,
|
|
1138
|
+
};
|
|
1139
|
+
function ConnectionPath({ style, nodeId, portType }) {
|
|
1140
|
+
const { fromNode, toX, toY } = useStore(useCallback((s) => ({
|
|
1141
|
+
fromNode: s.nodeInternals.get(nodeId),
|
|
1142
|
+
toX: (s.connectionPosition.x - s.transform[0]) / s.transform[2],
|
|
1143
|
+
toY: (s.connectionPosition.y - s.transform[1]) / s.transform[2],
|
|
1144
|
+
}), [nodeId]), shallow);
|
|
1145
|
+
const fromPortBounds = fromNode?.[internalsSymbol]?.portBounds;
|
|
1146
|
+
const portBounds = fromPortBounds?.[portType];
|
|
1147
|
+
if (!fromNode || !portBounds) {
|
|
1148
|
+
return null;
|
|
1149
|
+
}
|
|
1150
|
+
const fromPort = portBounds[0];
|
|
1151
|
+
const fromPortX = fromPort
|
|
1152
|
+
? fromPort.x + fromPort.width / 2
|
|
1153
|
+
: (fromNode.width ?? 0) / 2;
|
|
1154
|
+
const fromPortY = fromPort
|
|
1155
|
+
? fromPort.y + fromPort.height / 2
|
|
1156
|
+
: fromNode.height ?? 0;
|
|
1157
|
+
const fromX = (fromNode.positionAbsolute?.x ?? 0) + fromPortX;
|
|
1158
|
+
const fromY = (fromNode.positionAbsolute?.y ?? 0) + fromPortY;
|
|
1159
|
+
const fromPosition = fromPort?.position;
|
|
1160
|
+
const toPosition = fromPosition ? oppositePosition[fromPosition] : null;
|
|
1161
|
+
if (!fromPosition || !toPosition) {
|
|
1162
|
+
return null;
|
|
1163
|
+
}
|
|
1164
|
+
// // straight edge
|
|
1165
|
+
// const dAttr = `M${fromX},${fromY} ${toX},${toY}`;
|
|
1166
|
+
let dAttr = '';
|
|
1167
|
+
const pathParams = {
|
|
1168
|
+
sourceX: fromX,
|
|
1169
|
+
sourceY: fromY,
|
|
1170
|
+
sourcePosition: fromPosition,
|
|
1171
|
+
targetX: toX,
|
|
1172
|
+
targetY: toY,
|
|
1173
|
+
targetPosition: toPosition,
|
|
1174
|
+
};
|
|
1175
|
+
[dAttr] = getStepPath({
|
|
1176
|
+
...pathParams,
|
|
1177
|
+
borderRadius: 0,
|
|
1178
|
+
});
|
|
1179
|
+
return (jsx("path", { d: dAttr, fill: "none", className: "react-diagram__connection-path", style: style }));
|
|
1180
|
+
}
|
|
1181
|
+
ConnectionPath.displayName = 'ConnectionPath';
|
|
1182
|
+
|
|
1183
|
+
const selector$1 = (s) => ({
|
|
1184
|
+
nodeId: s.connectionNodeId,
|
|
1185
|
+
portType: s.connectionPortType,
|
|
1186
|
+
});
|
|
1187
|
+
function ConnectionEdge({ containerStyle, style }) {
|
|
1188
|
+
const { nodeId, portType } = useStore(selector$1, shallow);
|
|
1189
|
+
const isValid = !!(nodeId && portType);
|
|
1190
|
+
if (!isValid) {
|
|
1191
|
+
return null;
|
|
1192
|
+
}
|
|
1193
|
+
return (jsx("svg", { style: containerStyle, className: "react-diagram__container react-diagram__edges react-diagram__connectionline", children: jsx("g", { className: "react-diagram__connection", children: jsx(ConnectionPath, { style: style, nodeId: nodeId, portType: portType }) }) }));
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
function DiagramView({ rfId,
|
|
1197
|
+
// DiagramRenderer props
|
|
1198
|
+
noPanClassName, panning, defaultViewport, multiSelectionKeyCode, onMove, onMoveStart, onMoveEnd,
|
|
1199
|
+
// NodeRenderer props
|
|
1200
|
+
onlyRenderVisibleElements, disableKeyboardA11y, noDragClassName, nodeOrigin, nodeTypes, onNodeClick, onNodeDoubleClick, onNodeContextMenu, onNodeMouseEnter, onNodeMouseMove, onNodeMouseLeave,
|
|
1201
|
+
// EdgeRenderer props
|
|
1202
|
+
edgeTypes, onEdgeClick, onEdgeDoubleClick, onEdgeContextMenu, onEdgeMouseEnter, onEdgeMouseMove, onEdgeMouseLeave, onEdgeUpdate, onEdgeUpdateStart, onEdgeUpdateEnd, }) {
|
|
1203
|
+
return (jsxs(DiagramRenderer$1, { multiSelectionKeyCode: multiSelectionKeyCode, noPanClassName: noPanClassName, panning: panning, defaultViewport: defaultViewport, onMove: onMove, onMoveStart: onMoveStart, onMoveEnd: onMoveEnd, children: [jsx(NodeRenderer$1, { rfId: rfId, nodeTypes: nodeTypes, onlyRenderVisibleElements: onlyRenderVisibleElements, disableKeyboardA11y: disableKeyboardA11y, nodeOrigin: nodeOrigin, noDragClassName: noDragClassName, noPanClassName: noPanClassName, onNodeClick: onNodeClick, onNodeDoubleClick: onNodeDoubleClick, onNodeContextMenu: onNodeContextMenu, onNodeMouseEnter: onNodeMouseEnter, onNodeMouseMove: onNodeMouseMove, onNodeMouseLeave: onNodeMouseLeave }), jsx(EdgeRenderer$1, { rfId: rfId, edgeTypes: edgeTypes, noPanClassName: noPanClassName, onEdgeClick: onEdgeClick, onEdgeDoubleClick: onEdgeDoubleClick, onEdgeContextMenu: onEdgeContextMenu, onEdgeMouseEnter: onEdgeMouseEnter, onEdgeMouseMove: onEdgeMouseMove, onEdgeMouseLeave: onEdgeMouseLeave, onEdgeUpdate: onEdgeUpdate, onEdgeUpdateStart: onEdgeUpdateStart, onEdgeUpdateEnd: onEdgeUpdateEnd, children: jsx(ConnectionEdge, {}) }), jsx("div", { className: "react-diagram__edgelabel-renderer" })] }));
|
|
1204
|
+
}
|
|
1205
|
+
DiagramView.displayName = 'DiagramView';
|
|
1206
|
+
var DiagramView$1 = memo(DiagramView);
|
|
1207
|
+
|
|
1208
|
+
const selector = (s) => {
|
|
1209
|
+
const { setNodes, setEdges, setNodeExtent, setTranslateExtent, setMinZoom, setMaxZoom, } = s;
|
|
1210
|
+
return {
|
|
1211
|
+
setNodes,
|
|
1212
|
+
setEdges,
|
|
1213
|
+
setNodeExtent,
|
|
1214
|
+
setTranslateExtent,
|
|
1215
|
+
setMinZoom,
|
|
1216
|
+
setMaxZoom,
|
|
1217
|
+
};
|
|
1218
|
+
};
|
|
1219
|
+
function useStoreUpdater(value, setStoreState) {
|
|
1220
|
+
useEffect(() => {
|
|
1221
|
+
if (typeof value !== 'undefined') {
|
|
1222
|
+
setStoreState(value);
|
|
1223
|
+
}
|
|
1224
|
+
}, [value]);
|
|
1225
|
+
}
|
|
1226
|
+
// updates with values in store that don't have a dedicated setter function
|
|
1227
|
+
function useDirectStoreUpdater(key, value, setState) {
|
|
1228
|
+
useEffect(() => {
|
|
1229
|
+
if (typeof value !== 'undefined') {
|
|
1230
|
+
setState({ [key]: value });
|
|
1231
|
+
}
|
|
1232
|
+
}, [value]);
|
|
1233
|
+
}
|
|
1234
|
+
const StoreUpdater = ({ nodes, onNodesChange, onNodeDrag, onNodeDragStart, onNodeDragEnd, edges, onEdgesChange, gridStep, elevateNodesOnSelect, nodesDraggable, autoPanOnNodeDrag, autoPanOnConnect, onConnect, onConnectStart, onConnectEnd, onError, nodeExtent, translateExtent, minZoom, maxZoom, }) => {
|
|
1235
|
+
const { setNodes, setEdges, setNodeExtent, setTranslateExtent, setMinZoom, setMaxZoom, } = useStore(selector, shallow);
|
|
1236
|
+
const store = useStoreApi();
|
|
1237
|
+
useDirectStoreUpdater('gridStep', gridStep, store.setState);
|
|
1238
|
+
useStoreUpdater(nodes, setNodes);
|
|
1239
|
+
useStoreUpdater(edges, setEdges);
|
|
1240
|
+
useStoreUpdater(nodeExtent, setNodeExtent);
|
|
1241
|
+
useStoreUpdater(translateExtent, setTranslateExtent);
|
|
1242
|
+
useStoreUpdater(minZoom, setMinZoom);
|
|
1243
|
+
useStoreUpdater(maxZoom, setMaxZoom);
|
|
1244
|
+
useDirectStoreUpdater('elevateNodesOnSelect', elevateNodesOnSelect, store.setState);
|
|
1245
|
+
useDirectStoreUpdater('nodesDraggable', nodesDraggable, store.setState);
|
|
1246
|
+
useDirectStoreUpdater('autoPanOnNodeDrag', autoPanOnNodeDrag, store.setState);
|
|
1247
|
+
useDirectStoreUpdater('autoPanOnConnect', autoPanOnConnect, store.setState);
|
|
1248
|
+
useDirectStoreUpdater('onNodesChange', onNodesChange, store.setState);
|
|
1249
|
+
useDirectStoreUpdater('onNodeDrag', onNodeDrag, store.setState);
|
|
1250
|
+
useDirectStoreUpdater('onNodeDragStart', onNodeDragStart, store.setState);
|
|
1251
|
+
useDirectStoreUpdater('onNodeDragEnd', onNodeDragEnd, store.setState);
|
|
1252
|
+
useDirectStoreUpdater('onEdgesChange', onEdgesChange, store.setState);
|
|
1253
|
+
useDirectStoreUpdater('onConnect', onConnect, store.setState);
|
|
1254
|
+
useDirectStoreUpdater('onConnectStart', onConnectStart, store.setState);
|
|
1255
|
+
useDirectStoreUpdater('onConnectEnd', onConnectEnd, store.setState);
|
|
1256
|
+
useDirectStoreUpdater('onError', onErrorWrapper(onError), store.setState);
|
|
1257
|
+
return null;
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
function useNodeOrEdgeTypes(nodeOrEdgeTypes, createTypes) {
|
|
1261
|
+
const typesKeysRef = useRef(null);
|
|
1262
|
+
const typesParsed = useMemo(() => {
|
|
1263
|
+
if (process.env.NODE_ENV === 'development') {
|
|
1264
|
+
const typeKeys = Object.keys(nodeOrEdgeTypes);
|
|
1265
|
+
if (shallow(typesKeysRef.current, typeKeys)) {
|
|
1266
|
+
devWarn('002');
|
|
1267
|
+
}
|
|
1268
|
+
typesKeysRef.current = typeKeys;
|
|
1269
|
+
}
|
|
1270
|
+
return createTypes(nodeOrEdgeTypes);
|
|
1271
|
+
}, [nodeOrEdgeTypes]);
|
|
1272
|
+
return typesParsed;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
const NodeIdContext = createContext(null);
|
|
1276
|
+
const Provider = NodeIdContext.Provider;
|
|
1277
|
+
NodeIdContext.Consumer;
|
|
1278
|
+
const useNodeId = () => {
|
|
1279
|
+
const nodeId = useContext(NodeIdContext);
|
|
1280
|
+
return nodeId;
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
function Port({ type, position }) {
|
|
1284
|
+
const store = useStoreApi();
|
|
1285
|
+
const nodeId = useNodeId();
|
|
1286
|
+
if (!nodeId)
|
|
1287
|
+
return null;
|
|
1288
|
+
const handleOnConnect = (connection) => {
|
|
1289
|
+
const { defaultEdgeOptions, onConnect } = store.getState();
|
|
1290
|
+
const edgeParams = {
|
|
1291
|
+
...defaultEdgeOptions,
|
|
1292
|
+
...connection,
|
|
1293
|
+
};
|
|
1294
|
+
onConnect?.(edgeParams);
|
|
1295
|
+
};
|
|
1296
|
+
const onPointerDown = (event) => {
|
|
1297
|
+
const isMouseTriggered = isMouseEvent(event);
|
|
1298
|
+
if ((isMouseTriggered && event.button === 0) || !isMouseTriggered) {
|
|
1299
|
+
handlePointerDown({
|
|
1300
|
+
event,
|
|
1301
|
+
nodeId,
|
|
1302
|
+
portType: type,
|
|
1303
|
+
getState: store.getState,
|
|
1304
|
+
setState: store.setState,
|
|
1305
|
+
onConnect: handleOnConnect,
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
return (jsx("div", { "data-nodeid": nodeId, "data-id": `${nodeId}-${type}`, "data-port-position": position, className: `react-diagram__port react-diagram__port-${position} ${type} nodrag`, onMouseDown: onPointerDown, onTouchStart: onPointerDown }));
|
|
1310
|
+
}
|
|
1311
|
+
Port.displayName = 'Port';
|
|
1312
|
+
var Port$1 = memo(Port);
|
|
1313
|
+
|
|
1314
|
+
function Nodes({ data }) {
|
|
1315
|
+
return (jsxs(Fragment, { children: [jsx(Port$1, { type: "target", position: Position.Top }), data.label, jsx(Port$1, { type: "source", position: Position.Bottom })] }));
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
function useGetPointerPosition() {
|
|
1319
|
+
const store = useStoreApi();
|
|
1320
|
+
// returns the pointer position projected to the RF coordinate system
|
|
1321
|
+
const getPointerPosition = useCallback(({ sourceEvent }) => {
|
|
1322
|
+
const { transform, gridStep } = store.getState();
|
|
1323
|
+
const x = sourceEvent.touches
|
|
1324
|
+
? sourceEvent.touches[0].clientX
|
|
1325
|
+
: sourceEvent.clientX;
|
|
1326
|
+
const y = sourceEvent.touches
|
|
1327
|
+
? sourceEvent.touches[0].clientY
|
|
1328
|
+
: sourceEvent.clientY;
|
|
1329
|
+
const pointerPos = {
|
|
1330
|
+
x: (x - transform[0]) / transform[2],
|
|
1331
|
+
y: (y - transform[1]) / transform[2],
|
|
1332
|
+
};
|
|
1333
|
+
// we need the snapped position in order to be able to skip unnecessary drag events
|
|
1334
|
+
return {
|
|
1335
|
+
xSnapped: gridStep
|
|
1336
|
+
? gridStep[0] * Math.round(pointerPos.x / gridStep[0])
|
|
1337
|
+
: pointerPos.x,
|
|
1338
|
+
ySnapped: gridStep
|
|
1339
|
+
? gridStep[1] * Math.round(pointerPos.y / gridStep[1])
|
|
1340
|
+
: pointerPos.y,
|
|
1341
|
+
...pointerPos,
|
|
1342
|
+
};
|
|
1343
|
+
}, []);
|
|
1344
|
+
return getPointerPosition;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
const getPortBounds = (selector, nodeElement, zoom, nodeOrigin) => {
|
|
1348
|
+
const ports = nodeElement.querySelectorAll(selector);
|
|
1349
|
+
if (!ports || !ports.length) {
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
const portsArray = Array.from(ports);
|
|
1353
|
+
const nodeBounds = nodeElement.getBoundingClientRect();
|
|
1354
|
+
const nodeOffset = {
|
|
1355
|
+
x: nodeBounds.width * nodeOrigin[0],
|
|
1356
|
+
y: nodeBounds.height * nodeOrigin[1],
|
|
1357
|
+
};
|
|
1358
|
+
return portsArray.map((port) => {
|
|
1359
|
+
const portBounds = port.getBoundingClientRect();
|
|
1360
|
+
return {
|
|
1361
|
+
id: port.getAttribute('data-portid'),
|
|
1362
|
+
position: port.dataset.portPosition,
|
|
1363
|
+
x: (portBounds.left - nodeBounds.left - nodeOffset.x) / zoom,
|
|
1364
|
+
y: (portBounds.top - nodeBounds.top - nodeOffset.y) / zoom,
|
|
1365
|
+
...getDimensions(port),
|
|
1366
|
+
};
|
|
1367
|
+
});
|
|
1368
|
+
};
|
|
1369
|
+
function getMouseHandler(id, getState, handler) {
|
|
1370
|
+
return handler === undefined
|
|
1371
|
+
? handler
|
|
1372
|
+
: (event) => {
|
|
1373
|
+
const node = getState().nodeInternals.get(id);
|
|
1374
|
+
handler(event, { ...node });
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
function handleNodeClick({ id, store, isSelectable, unselect = false, nodeRef, }) {
|
|
1378
|
+
if (!isSelectable)
|
|
1379
|
+
return;
|
|
1380
|
+
const { addSelectedNodes, unselectNodes, multiSelectionActive, nodeInternals, } = store.getState();
|
|
1381
|
+
const node = nodeInternals.get(id);
|
|
1382
|
+
store.setState({ nodesSelectionActive: false });
|
|
1383
|
+
if (!node.selected) {
|
|
1384
|
+
addSelectedNodes([id]);
|
|
1385
|
+
}
|
|
1386
|
+
else if (unselect || (node.selected && multiSelectionActive)) {
|
|
1387
|
+
unselectNodes({ nodes: [node] });
|
|
1388
|
+
requestAnimationFrame(() => nodeRef?.current?.blur());
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
function isParentSelected(node, nodeInternals) {
|
|
1393
|
+
if (!node.parentNode) {
|
|
1394
|
+
return false;
|
|
1395
|
+
}
|
|
1396
|
+
const parentNode = nodeInternals.get(node.parentNode);
|
|
1397
|
+
if (!parentNode) {
|
|
1398
|
+
return false;
|
|
1399
|
+
}
|
|
1400
|
+
if (parentNode.selected) {
|
|
1401
|
+
return true;
|
|
1402
|
+
}
|
|
1403
|
+
return isParentSelected(parentNode, nodeInternals);
|
|
1404
|
+
}
|
|
1405
|
+
function getDragItems(nodeInternals, nodesDraggable, mousePosition, nodeId) {
|
|
1406
|
+
return Array.from(nodeInternals.values())
|
|
1407
|
+
.filter((n) => (n.selected || n.id === nodeId) &&
|
|
1408
|
+
(!n.parentNode || !isParentSelected(n, nodeInternals)) &&
|
|
1409
|
+
(n.draggable ||
|
|
1410
|
+
(nodesDraggable && typeof n.draggable === 'undefined')))
|
|
1411
|
+
.map((n) => ({
|
|
1412
|
+
id: n.id,
|
|
1413
|
+
position: n.position || { x: 0, y: 0 },
|
|
1414
|
+
positionAbsolute: n.positionAbsolute || { x: 0, y: 0 },
|
|
1415
|
+
distance: {
|
|
1416
|
+
x: mousePosition.x - (n.positionAbsolute?.x ?? 0),
|
|
1417
|
+
y: mousePosition.y - (n.positionAbsolute?.y ?? 0),
|
|
1418
|
+
},
|
|
1419
|
+
delta: {
|
|
1420
|
+
x: 0,
|
|
1421
|
+
y: 0,
|
|
1422
|
+
},
|
|
1423
|
+
extent: n.extent,
|
|
1424
|
+
parentNode: n.parentNode,
|
|
1425
|
+
width: n.width,
|
|
1426
|
+
height: n.height,
|
|
1427
|
+
}));
|
|
1428
|
+
}
|
|
1429
|
+
function calcNextPosition(node, nextPosition, nodeInternals, nodeExtent, nodeOrigin = [0, 0], onError) {
|
|
1430
|
+
let currentExtent = node.extent || nodeExtent;
|
|
1431
|
+
if (node.extent === 'parent') {
|
|
1432
|
+
if (node.parentNode && node.width && node.height) {
|
|
1433
|
+
const parent = nodeInternals.get(node.parentNode);
|
|
1434
|
+
const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, nodeOrigin).positionAbsolute;
|
|
1435
|
+
currentExtent =
|
|
1436
|
+
parent &&
|
|
1437
|
+
isNumeric(parentX) &&
|
|
1438
|
+
isNumeric(parentY) &&
|
|
1439
|
+
isNumeric(parent.width) &&
|
|
1440
|
+
isNumeric(parent.height)
|
|
1441
|
+
? [
|
|
1442
|
+
[
|
|
1443
|
+
parentX + node.width * nodeOrigin[0],
|
|
1444
|
+
parentY + node.height * nodeOrigin[1],
|
|
1445
|
+
],
|
|
1446
|
+
[
|
|
1447
|
+
parentX +
|
|
1448
|
+
parent.width -
|
|
1449
|
+
node.width +
|
|
1450
|
+
node.width * nodeOrigin[0],
|
|
1451
|
+
parentY +
|
|
1452
|
+
parent.height -
|
|
1453
|
+
node.height +
|
|
1454
|
+
node.height * nodeOrigin[1],
|
|
1455
|
+
],
|
|
1456
|
+
]
|
|
1457
|
+
: currentExtent;
|
|
1458
|
+
}
|
|
1459
|
+
else {
|
|
1460
|
+
onError?.('011');
|
|
1461
|
+
currentExtent = nodeExtent;
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
else if (node.extent && node.parentNode) {
|
|
1465
|
+
const parent = nodeInternals.get(node.parentNode);
|
|
1466
|
+
const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, nodeOrigin).positionAbsolute;
|
|
1467
|
+
currentExtent = [
|
|
1468
|
+
[node.extent[0][0] + parentX, node.extent[0][1] + parentY],
|
|
1469
|
+
[node.extent[1][0] + parentX, node.extent[1][1] + parentY],
|
|
1470
|
+
];
|
|
1471
|
+
}
|
|
1472
|
+
let parentPosition = { x: 0, y: 0 };
|
|
1473
|
+
if (node.parentNode) {
|
|
1474
|
+
const parentNode = nodeInternals.get(node.parentNode);
|
|
1475
|
+
parentPosition = getNodePositionWithOrigin(parentNode, nodeOrigin).positionAbsolute;
|
|
1476
|
+
}
|
|
1477
|
+
const positionAbsolute = currentExtent
|
|
1478
|
+
? clampPosition(nextPosition, currentExtent)
|
|
1479
|
+
: nextPosition;
|
|
1480
|
+
return {
|
|
1481
|
+
position: {
|
|
1482
|
+
x: positionAbsolute.x - parentPosition.x,
|
|
1483
|
+
y: positionAbsolute.y - parentPosition.y,
|
|
1484
|
+
},
|
|
1485
|
+
positionAbsolute,
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
function getEventHandlerParams({ nodeId, dragItems, nodeInternals, }) {
|
|
1489
|
+
const extentedDragItems = dragItems.map((n) => {
|
|
1490
|
+
const node = nodeInternals.get(n.id);
|
|
1491
|
+
return {
|
|
1492
|
+
...node,
|
|
1493
|
+
position: n.position,
|
|
1494
|
+
positionAbsolute: n.positionAbsolute,
|
|
1495
|
+
};
|
|
1496
|
+
});
|
|
1497
|
+
return [
|
|
1498
|
+
nodeId
|
|
1499
|
+
? extentedDragItems.find((n) => n.id === nodeId)
|
|
1500
|
+
: extentedDragItems[0],
|
|
1501
|
+
extentedDragItems,
|
|
1502
|
+
];
|
|
1503
|
+
}
|
|
1504
|
+
function hasSelector(target, selector, nodeRef) {
|
|
1505
|
+
let current = target;
|
|
1506
|
+
do {
|
|
1507
|
+
if (current?.matches(selector))
|
|
1508
|
+
return true;
|
|
1509
|
+
if (current === nodeRef.current)
|
|
1510
|
+
return false;
|
|
1511
|
+
current = current.parentElement;
|
|
1512
|
+
} while (current);
|
|
1513
|
+
return false;
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
function useDrag({ nodeRef, nodeId, isSelectable, noDragClassName, }) {
|
|
1517
|
+
const store = useStoreApi();
|
|
1518
|
+
const dragItems = useRef([]);
|
|
1519
|
+
const containerBounds = useRef(null);
|
|
1520
|
+
const mousePosition = useRef({ x: 0, y: 0 });
|
|
1521
|
+
const lastPosition = useRef({
|
|
1522
|
+
x: null,
|
|
1523
|
+
y: null,
|
|
1524
|
+
});
|
|
1525
|
+
const dragEvent = useRef(null);
|
|
1526
|
+
const autoPanStarted = useRef(false);
|
|
1527
|
+
const autoPanId = useRef(0);
|
|
1528
|
+
const [dragging, setDragging] = useState(false);
|
|
1529
|
+
const getPointerPosition = useGetPointerPosition();
|
|
1530
|
+
useEffect(() => {
|
|
1531
|
+
if (nodeRef?.current) {
|
|
1532
|
+
const selection = select(nodeRef.current);
|
|
1533
|
+
const updateNodes = ({ x, y }) => {
|
|
1534
|
+
const { nodeInternals, onNodeDrag, updateNodePositions, nodeExtent, nodeOrigin, gridStep, onError, } = store.getState();
|
|
1535
|
+
lastPosition.current = { x, y };
|
|
1536
|
+
let hasChange = false;
|
|
1537
|
+
dragItems.current = dragItems.current.map((n) => {
|
|
1538
|
+
const nextPosition = {
|
|
1539
|
+
x: x - n.distance.x,
|
|
1540
|
+
y: y - n.distance.y,
|
|
1541
|
+
};
|
|
1542
|
+
if (gridStep) {
|
|
1543
|
+
nextPosition.x =
|
|
1544
|
+
gridStep[0] * Math.round(nextPosition.x / gridStep[0]);
|
|
1545
|
+
nextPosition.y =
|
|
1546
|
+
gridStep[1] * Math.round(nextPosition.y / gridStep[1]);
|
|
1547
|
+
}
|
|
1548
|
+
const updatedPos = calcNextPosition(n, nextPosition, nodeInternals, nodeExtent, nodeOrigin, onError);
|
|
1549
|
+
// we want to make sure that we only fire a change event when there is a changes
|
|
1550
|
+
hasChange =
|
|
1551
|
+
hasChange ||
|
|
1552
|
+
n.position.x !== updatedPos.position.x ||
|
|
1553
|
+
n.position.y !== updatedPos.position.y;
|
|
1554
|
+
n.position = updatedPos.position;
|
|
1555
|
+
n.positionAbsolute = updatedPos.positionAbsolute;
|
|
1556
|
+
return n;
|
|
1557
|
+
});
|
|
1558
|
+
if (!hasChange) {
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
updateNodePositions(dragItems.current, true, true);
|
|
1562
|
+
setDragging(true);
|
|
1563
|
+
if (onNodeDrag && dragEvent.current) {
|
|
1564
|
+
const [currentNode, nodes] = getEventHandlerParams({
|
|
1565
|
+
nodeId,
|
|
1566
|
+
dragItems: dragItems.current,
|
|
1567
|
+
nodeInternals,
|
|
1568
|
+
});
|
|
1569
|
+
onNodeDrag(dragEvent.current, currentNode, nodes);
|
|
1570
|
+
}
|
|
1571
|
+
};
|
|
1572
|
+
const autoPan = () => {
|
|
1573
|
+
if (!containerBounds.current) {
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
const [xMovement, yMovement] = calcAutoPanPosition(mousePosition.current, containerBounds.current);
|
|
1577
|
+
if (xMovement !== 0 || yMovement !== 0) {
|
|
1578
|
+
const { transform, panBy } = store.getState();
|
|
1579
|
+
lastPosition.current.x =
|
|
1580
|
+
(lastPosition.current.x ?? 0) - xMovement / transform[2];
|
|
1581
|
+
lastPosition.current.y =
|
|
1582
|
+
(lastPosition.current.y ?? 0) - yMovement / transform[2];
|
|
1583
|
+
updateNodes(lastPosition.current);
|
|
1584
|
+
panBy({ x: xMovement, y: yMovement });
|
|
1585
|
+
}
|
|
1586
|
+
autoPanId.current = requestAnimationFrame(autoPan);
|
|
1587
|
+
};
|
|
1588
|
+
const dragHandle = drag()
|
|
1589
|
+
.on('start', (e) => {
|
|
1590
|
+
// only allow left click
|
|
1591
|
+
if (e.sourceEvent.which !== 1)
|
|
1592
|
+
return;
|
|
1593
|
+
const { nodeInternals, nodesDraggable, domNode, onNodeDragStart, } = store.getState();
|
|
1594
|
+
if (nodeId) {
|
|
1595
|
+
handleNodeClick({
|
|
1596
|
+
id: nodeId,
|
|
1597
|
+
store,
|
|
1598
|
+
nodeRef: nodeRef,
|
|
1599
|
+
isSelectable,
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
const pointerPosition = getPointerPosition(e);
|
|
1603
|
+
dragItems.current = getDragItems(nodeInternals, nodesDraggable, pointerPosition, nodeId);
|
|
1604
|
+
if (onNodeDragStart && dragItems.current) {
|
|
1605
|
+
const [currentNode, nodes] = getEventHandlerParams({
|
|
1606
|
+
nodeId,
|
|
1607
|
+
dragItems: dragItems.current,
|
|
1608
|
+
nodeInternals,
|
|
1609
|
+
});
|
|
1610
|
+
onNodeDragStart(e.sourceEvent, currentNode, nodes);
|
|
1611
|
+
}
|
|
1612
|
+
containerBounds.current =
|
|
1613
|
+
domNode?.getBoundingClientRect() || null;
|
|
1614
|
+
mousePosition.current = getEventPosition(e.sourceEvent, containerBounds.current);
|
|
1615
|
+
})
|
|
1616
|
+
.on('drag', (e) => {
|
|
1617
|
+
const pointerPosition = getPointerPosition(e);
|
|
1618
|
+
const { autoPanOnNodeDrag } = store.getState();
|
|
1619
|
+
if (!autoPanStarted.current && autoPanOnNodeDrag) {
|
|
1620
|
+
autoPanStarted.current = true;
|
|
1621
|
+
autoPan();
|
|
1622
|
+
}
|
|
1623
|
+
if ((lastPosition.current.x !== pointerPosition.xSnapped ||
|
|
1624
|
+
lastPosition.current.y !== pointerPosition.ySnapped) &&
|
|
1625
|
+
dragItems.current) {
|
|
1626
|
+
dragEvent.current = e.sourceEvent;
|
|
1627
|
+
mousePosition.current = getEventPosition(e.sourceEvent, containerBounds.current);
|
|
1628
|
+
updateNodes(pointerPosition);
|
|
1629
|
+
}
|
|
1630
|
+
})
|
|
1631
|
+
.on('end', (event) => {
|
|
1632
|
+
setDragging(false);
|
|
1633
|
+
autoPanStarted.current = false;
|
|
1634
|
+
cancelAnimationFrame(autoPanId.current);
|
|
1635
|
+
if (dragItems.current) {
|
|
1636
|
+
const { nodeInternals, updateNodePositions, onNodeDragEnd } = store.getState();
|
|
1637
|
+
updateNodePositions(dragItems.current, false, false);
|
|
1638
|
+
if (onNodeDragEnd) {
|
|
1639
|
+
const [currentNode, nodes] = getEventHandlerParams({
|
|
1640
|
+
nodeId,
|
|
1641
|
+
dragItems: dragItems.current,
|
|
1642
|
+
nodeInternals,
|
|
1643
|
+
});
|
|
1644
|
+
onNodeDragEnd(event.sourceEvent, currentNode, nodes);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
})
|
|
1648
|
+
.filter((event) => {
|
|
1649
|
+
const target = event.target;
|
|
1650
|
+
const isDraggable = (!event.button && !noDragClassName) ||
|
|
1651
|
+
!hasSelector(target, `.${noDragClassName}`, nodeRef);
|
|
1652
|
+
return isDraggable;
|
|
1653
|
+
});
|
|
1654
|
+
selection.call(dragHandle);
|
|
1655
|
+
return () => {
|
|
1656
|
+
selection.on('.drag', null);
|
|
1657
|
+
};
|
|
1658
|
+
}
|
|
1659
|
+
}, [store, nodeRef, nodeId, isSelectable, noDragClassName]);
|
|
1660
|
+
return dragging;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
const wrapNode = (NodeComponent) => {
|
|
1664
|
+
function NodeWrapper({ id, type, data, positionX, positionY, sourcePosition, targetPosition, onClick, onMouseEnter, onMouseMove, onMouseLeave, onContextMenu, onDoubleClick, style, className, selected, isSelectable, isDraggable, hidden, resizeObserver, dragHandle, zIndex, isParent, initialized, disableKeyboardA11y, ariaLabel, rfId, noDragClassName, noPanClassName, }) {
|
|
1665
|
+
const store = useStoreApi();
|
|
1666
|
+
const nodeRef = useRef(null);
|
|
1667
|
+
const prevSourcePosition = useRef(sourcePosition);
|
|
1668
|
+
const prevTargetPosition = useRef(targetPosition);
|
|
1669
|
+
const prevType = useRef(type);
|
|
1670
|
+
const hasPointerEvents = isDraggable || onClick || onMouseEnter || onMouseMove || onMouseLeave;
|
|
1671
|
+
const onSelectNodeHandler = (event) => {
|
|
1672
|
+
if (!isDraggable) {
|
|
1673
|
+
handleNodeClick({
|
|
1674
|
+
id,
|
|
1675
|
+
store,
|
|
1676
|
+
nodeRef,
|
|
1677
|
+
});
|
|
1678
|
+
}
|
|
1679
|
+
if (onClick) {
|
|
1680
|
+
const node = store.getState().nodeInternals.get(id);
|
|
1681
|
+
onClick(event, { ...node });
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
useEffect(() => {
|
|
1685
|
+
if (nodeRef.current && !hidden) {
|
|
1686
|
+
const currNode = nodeRef.current;
|
|
1687
|
+
resizeObserver?.observe(currNode);
|
|
1688
|
+
return () => resizeObserver?.unobserve(currNode);
|
|
1689
|
+
}
|
|
1690
|
+
}, [hidden]);
|
|
1691
|
+
useEffect(() => {
|
|
1692
|
+
// when the user programmatically changes the source or handle position, we re-initialize the node
|
|
1693
|
+
const typeChanged = prevType.current !== type;
|
|
1694
|
+
const sourcePosChanged = prevSourcePosition.current !== sourcePosition;
|
|
1695
|
+
const targetPosChanged = prevTargetPosition.current !== targetPosition;
|
|
1696
|
+
if (nodeRef.current &&
|
|
1697
|
+
(typeChanged || sourcePosChanged || targetPosChanged)) {
|
|
1698
|
+
if (typeChanged) {
|
|
1699
|
+
prevType.current = type;
|
|
1700
|
+
}
|
|
1701
|
+
if (sourcePosChanged) {
|
|
1702
|
+
prevSourcePosition.current = sourcePosition;
|
|
1703
|
+
}
|
|
1704
|
+
if (targetPosChanged) {
|
|
1705
|
+
prevTargetPosition.current = targetPosition;
|
|
1706
|
+
}
|
|
1707
|
+
store
|
|
1708
|
+
.getState()
|
|
1709
|
+
.updateNodeDimensions([
|
|
1710
|
+
{ id, nodeElement: nodeRef.current, forceUpdate: true },
|
|
1711
|
+
]);
|
|
1712
|
+
}
|
|
1713
|
+
}, [id, type, sourcePosition, targetPosition]);
|
|
1714
|
+
const dragging = useDrag({
|
|
1715
|
+
nodeRef,
|
|
1716
|
+
nodeId: id,
|
|
1717
|
+
isSelectable,
|
|
1718
|
+
noDragClassName,
|
|
1719
|
+
});
|
|
1720
|
+
if (hidden) {
|
|
1721
|
+
return null;
|
|
1722
|
+
}
|
|
1723
|
+
const wrapperClassName = cc([
|
|
1724
|
+
'react-diagram__node',
|
|
1725
|
+
`react-diagram__node-${type}`,
|
|
1726
|
+
{
|
|
1727
|
+
[noPanClassName]: isDraggable,
|
|
1728
|
+
},
|
|
1729
|
+
className,
|
|
1730
|
+
{
|
|
1731
|
+
selected,
|
|
1732
|
+
parent: isParent,
|
|
1733
|
+
dragging,
|
|
1734
|
+
},
|
|
1735
|
+
]);
|
|
1736
|
+
const wrapperStyle = {
|
|
1737
|
+
zIndex,
|
|
1738
|
+
transform: `translate(${positionX}px,${positionY}px)`,
|
|
1739
|
+
pointerEvents: hasPointerEvents ? 'all' : 'none',
|
|
1740
|
+
visibility: initialized ? 'visible' : 'hidden',
|
|
1741
|
+
...style,
|
|
1742
|
+
};
|
|
1743
|
+
const events = {
|
|
1744
|
+
onClick: onSelectNodeHandler,
|
|
1745
|
+
onDoubleClick: getMouseHandler(id, store.getState, onDoubleClick),
|
|
1746
|
+
onContextMenu: getMouseHandler(id, store.getState, onContextMenu),
|
|
1747
|
+
onMouseEnter: getMouseHandler(id, store.getState, onMouseEnter),
|
|
1748
|
+
onMouseMove: getMouseHandler(id, store.getState, onMouseMove),
|
|
1749
|
+
onMouseLeave: getMouseHandler(id, store.getState, onMouseLeave),
|
|
1750
|
+
};
|
|
1751
|
+
const position = {
|
|
1752
|
+
positionX,
|
|
1753
|
+
positionY,
|
|
1754
|
+
sourcePosition,
|
|
1755
|
+
targetPosition,
|
|
1756
|
+
};
|
|
1757
|
+
return (jsx("div", { ...events, ref: nodeRef, className: wrapperClassName, style: wrapperStyle, "data-id": id, tabIndex: 0, role: "button", "aria-describedby": disableKeyboardA11y ? undefined : `${ARIA_NODE_DESC_KEY}-${rfId}`, "aria-label": ariaLabel, children: jsx(Provider, { value: id, children: jsx(NodeComponent, { ...position, id: id, zIndex: zIndex, type: type, data: data, dragHandle: dragHandle }) }) }));
|
|
1758
|
+
}
|
|
1759
|
+
NodeWrapper.displayName = 'NodeWrapper';
|
|
1760
|
+
return memo(NodeWrapper);
|
|
1761
|
+
};
|
|
1762
|
+
|
|
1763
|
+
function createNodeTypes(nodeTypes) {
|
|
1764
|
+
const defaultType = {
|
|
1765
|
+
default: wrapNode((nodeTypes.default || Nodes)),
|
|
1766
|
+
};
|
|
1767
|
+
const wrappedTypes = {};
|
|
1768
|
+
const customTypes = Object.keys(nodeTypes)
|
|
1769
|
+
.filter((key) => key !== 'default')
|
|
1770
|
+
.reduce((res, key) => {
|
|
1771
|
+
res[key] = wrapNode((nodeTypes[key] || Nodes));
|
|
1772
|
+
return res;
|
|
1773
|
+
}, wrappedTypes);
|
|
1774
|
+
return {
|
|
1775
|
+
...defaultType,
|
|
1776
|
+
...customTypes,
|
|
1777
|
+
};
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
function calculateXYZPosition(node, nodeInternals, result, nodeOrigin) {
|
|
1781
|
+
if (!node.parentNode) {
|
|
1782
|
+
return result;
|
|
1783
|
+
}
|
|
1784
|
+
const parentNode = nodeInternals.get(node.parentNode);
|
|
1785
|
+
const parentNodePosition = getNodePositionWithOrigin(parentNode, nodeOrigin);
|
|
1786
|
+
return calculateXYZPosition(parentNode, nodeInternals, {
|
|
1787
|
+
x: (result.x ?? 0) + parentNodePosition.x,
|
|
1788
|
+
y: (result.y ?? 0) + parentNodePosition.y,
|
|
1789
|
+
z: (parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0)
|
|
1790
|
+
? parentNode[internalsSymbol]?.z ?? 0
|
|
1791
|
+
: result.z ?? 0,
|
|
1792
|
+
}, nodeOrigin);
|
|
1793
|
+
}
|
|
1794
|
+
function updateAbsoluteNodePositions(nodeInternals, nodeOrigin, parentNodes) {
|
|
1795
|
+
nodeInternals.forEach((node) => {
|
|
1796
|
+
if (node.parentNode && !nodeInternals.has(node.parentNode)) {
|
|
1797
|
+
throw new Error(`Parent node ${node.parentNode} not found`);
|
|
1798
|
+
}
|
|
1799
|
+
if (node.parentNode || parentNodes?.[node.id]) {
|
|
1800
|
+
const { x, y, z } = calculateXYZPosition(node, nodeInternals, {
|
|
1801
|
+
...node.position,
|
|
1802
|
+
z: node[internalsSymbol]?.z ?? 0,
|
|
1803
|
+
}, nodeOrigin);
|
|
1804
|
+
node.positionAbsolute = {
|
|
1805
|
+
x,
|
|
1806
|
+
y,
|
|
1807
|
+
};
|
|
1808
|
+
node[internalsSymbol].z = z;
|
|
1809
|
+
if (parentNodes?.[node.id]) {
|
|
1810
|
+
node[internalsSymbol].isParent = true;
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
});
|
|
1814
|
+
}
|
|
1815
|
+
function createNodeInternals(nodes, nodeInternals, nodeOrigin, elevateNodesOnSelect) {
|
|
1816
|
+
const nextNodeInternals = new Map();
|
|
1817
|
+
const parentNodes = {};
|
|
1818
|
+
const selectedNodeZ = elevateNodesOnSelect ? 1000 : 0;
|
|
1819
|
+
nodes.forEach((node) => {
|
|
1820
|
+
const z = (isNumeric(node.zIndex) ? node.zIndex : 0) +
|
|
1821
|
+
(node.selected ? selectedNodeZ : 0);
|
|
1822
|
+
const currInternals = nodeInternals.get(node.id);
|
|
1823
|
+
const internals = {
|
|
1824
|
+
width: currInternals?.width,
|
|
1825
|
+
height: currInternals?.height,
|
|
1826
|
+
...node,
|
|
1827
|
+
positionAbsolute: {
|
|
1828
|
+
x: node.position.x,
|
|
1829
|
+
y: node.position.y,
|
|
1830
|
+
},
|
|
1831
|
+
};
|
|
1832
|
+
if (node.parentNode) {
|
|
1833
|
+
internals.parentNode = node.parentNode;
|
|
1834
|
+
parentNodes[node.parentNode] = true;
|
|
1835
|
+
}
|
|
1836
|
+
Object.defineProperty(internals, internalsSymbol, {
|
|
1837
|
+
enumerable: false,
|
|
1838
|
+
value: {
|
|
1839
|
+
portBounds: currInternals?.[internalsSymbol]?.portBounds,
|
|
1840
|
+
z,
|
|
1841
|
+
},
|
|
1842
|
+
});
|
|
1843
|
+
nextNodeInternals.set(node.id, internals);
|
|
1844
|
+
});
|
|
1845
|
+
updateAbsoluteNodePositions(nextNodeInternals, nodeOrigin, parentNodes);
|
|
1846
|
+
return nextNodeInternals;
|
|
1847
|
+
}
|
|
1848
|
+
function updateNodesSelections({ changedNodes, get, }) {
|
|
1849
|
+
const { onNodesChange } = get();
|
|
1850
|
+
if (changedNodes?.length) {
|
|
1851
|
+
onNodesChange?.(changedNodes);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
const handleParentExpand = (res, updateItem) => {
|
|
1856
|
+
const parent = res.find((e) => e.id === updateItem.parentNode);
|
|
1857
|
+
if (parent) {
|
|
1858
|
+
const extendWidth = updateItem.position.x + updateItem.width - parent.width;
|
|
1859
|
+
const extendHeight = updateItem.position.y + updateItem.height - parent.height;
|
|
1860
|
+
if (extendWidth > 0 ||
|
|
1861
|
+
extendHeight > 0 ||
|
|
1862
|
+
updateItem.position.x < 0 ||
|
|
1863
|
+
updateItem.position.y < 0) {
|
|
1864
|
+
parent.style = { ...parent.style } || {};
|
|
1865
|
+
parent.style.width = parent.style.width ?? parent.width;
|
|
1866
|
+
parent.style.height = parent.style.height ?? parent.height;
|
|
1867
|
+
if (extendWidth > 0) {
|
|
1868
|
+
parent.style.width += extendWidth;
|
|
1869
|
+
}
|
|
1870
|
+
if (extendHeight > 0) {
|
|
1871
|
+
parent.style.height += extendHeight;
|
|
1872
|
+
}
|
|
1873
|
+
if (updateItem.position.x < 0) {
|
|
1874
|
+
const xDiff = Math.abs(updateItem.position.x);
|
|
1875
|
+
parent.position.x = parent.position.x - xDiff;
|
|
1876
|
+
parent.style.width += xDiff;
|
|
1877
|
+
updateItem.position.x = 0;
|
|
1878
|
+
}
|
|
1879
|
+
if (updateItem.position.y < 0) {
|
|
1880
|
+
const yDiff = Math.abs(updateItem.position.y);
|
|
1881
|
+
parent.position.y = parent.position.y - yDiff;
|
|
1882
|
+
parent.style.height += yDiff;
|
|
1883
|
+
updateItem.position.y = 0;
|
|
1884
|
+
}
|
|
1885
|
+
parent.width = parent.style.width;
|
|
1886
|
+
parent.height = parent.style.height;
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
};
|
|
1890
|
+
const applyChanges = (changes, elements) => {
|
|
1891
|
+
// we need this hack to handle the setNodes and setEdges function of the useReactDiagram hook for controlled flows
|
|
1892
|
+
if (changes.some((c) => c.type === 'reset')) {
|
|
1893
|
+
return changes.filter((c) => c.type === 'reset').map((c) => c.item);
|
|
1894
|
+
}
|
|
1895
|
+
const initElements = changes
|
|
1896
|
+
.filter((c) => c.type === 'add')
|
|
1897
|
+
.map((c) => c.item);
|
|
1898
|
+
return elements.reduce((res, item) => {
|
|
1899
|
+
const currentChanges = changes.filter((c) => c.id === item.id);
|
|
1900
|
+
if (currentChanges.length === 0) {
|
|
1901
|
+
res.push(item);
|
|
1902
|
+
return res;
|
|
1903
|
+
}
|
|
1904
|
+
const updateItem = { ...item };
|
|
1905
|
+
for (const currentChange of currentChanges) {
|
|
1906
|
+
if (currentChange) {
|
|
1907
|
+
switch (currentChange.type) {
|
|
1908
|
+
case 'select': {
|
|
1909
|
+
updateItem.selected = currentChange.selected;
|
|
1910
|
+
break;
|
|
1911
|
+
}
|
|
1912
|
+
case 'position': {
|
|
1913
|
+
if (typeof currentChange.position !== 'undefined') {
|
|
1914
|
+
updateItem.position = currentChange.position;
|
|
1915
|
+
}
|
|
1916
|
+
if (typeof currentChange.positionAbsolute !== 'undefined') {
|
|
1917
|
+
updateItem.positionAbsolute =
|
|
1918
|
+
currentChange.positionAbsolute;
|
|
1919
|
+
}
|
|
1920
|
+
if (typeof currentChange.dragging !== 'undefined') {
|
|
1921
|
+
updateItem.dragging = currentChange.dragging;
|
|
1922
|
+
}
|
|
1923
|
+
if (updateItem.expandParent) {
|
|
1924
|
+
handleParentExpand(res, updateItem);
|
|
1925
|
+
}
|
|
1926
|
+
break;
|
|
1927
|
+
}
|
|
1928
|
+
case 'dimensions': {
|
|
1929
|
+
if (typeof currentChange.dimensions !== 'undefined') {
|
|
1930
|
+
updateItem.width = currentChange.dimensions.width;
|
|
1931
|
+
updateItem.height = currentChange.dimensions.height;
|
|
1932
|
+
}
|
|
1933
|
+
if (typeof currentChange.updateStyle !== 'undefined') {
|
|
1934
|
+
updateItem.style = {
|
|
1935
|
+
...(updateItem.style || {}),
|
|
1936
|
+
...currentChange.dimensions,
|
|
1937
|
+
};
|
|
1938
|
+
}
|
|
1939
|
+
if (typeof currentChange.resizing === 'boolean') {
|
|
1940
|
+
updateItem.resizing = currentChange.resizing;
|
|
1941
|
+
}
|
|
1942
|
+
if (updateItem.expandParent) {
|
|
1943
|
+
handleParentExpand(res, updateItem);
|
|
1944
|
+
}
|
|
1945
|
+
break;
|
|
1946
|
+
}
|
|
1947
|
+
case 'remove': {
|
|
1948
|
+
return res;
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
res.push(updateItem);
|
|
1954
|
+
return res;
|
|
1955
|
+
}, initElements);
|
|
1956
|
+
};
|
|
1957
|
+
function applyNodeChanges(changes, nodes) {
|
|
1958
|
+
return applyChanges(changes, nodes);
|
|
1959
|
+
}
|
|
1960
|
+
function applyEdgeChanges(changes, edges) {
|
|
1961
|
+
return applyChanges(changes, edges);
|
|
1962
|
+
}
|
|
1963
|
+
const createSelectionChange = (id, selected) => ({
|
|
1964
|
+
id,
|
|
1965
|
+
type: 'select',
|
|
1966
|
+
selected,
|
|
1967
|
+
});
|
|
1968
|
+
function getSelectionChanges(items, selectedIds) {
|
|
1969
|
+
return items.reduce((res, item) => {
|
|
1970
|
+
const willBeSelected = selectedIds.includes(item.id);
|
|
1971
|
+
if (!item.selected && willBeSelected) {
|
|
1972
|
+
item.selected = true;
|
|
1973
|
+
res.push(createSelectionChange(item.id, true));
|
|
1974
|
+
}
|
|
1975
|
+
else if (item.selected && !willBeSelected) {
|
|
1976
|
+
item.selected = false;
|
|
1977
|
+
res.push(createSelectionChange(item.id, false));
|
|
1978
|
+
}
|
|
1979
|
+
return res;
|
|
1980
|
+
}, []);
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
const infiniteExtent = [
|
|
1984
|
+
[Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY],
|
|
1985
|
+
[Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY],
|
|
1986
|
+
];
|
|
1987
|
+
const initialState = {
|
|
1988
|
+
rfId: '1',
|
|
1989
|
+
width: 0,
|
|
1990
|
+
height: 0,
|
|
1991
|
+
transform: [0, 0, 1],
|
|
1992
|
+
nodeInternals: new Map(),
|
|
1993
|
+
edges: [],
|
|
1994
|
+
elementsSelectable: true,
|
|
1995
|
+
onNodesChange: null,
|
|
1996
|
+
domNode: null,
|
|
1997
|
+
nodeOrigin: [0, 0],
|
|
1998
|
+
gridStep: undefined,
|
|
1999
|
+
nodesSelectionActive: false,
|
|
2000
|
+
elevateNodesOnSelect: true,
|
|
2001
|
+
nodesDraggable: true,
|
|
2002
|
+
multiSelectionActive: false,
|
|
2003
|
+
d3Zoom: null,
|
|
2004
|
+
d3Selection: null,
|
|
2005
|
+
minZoom: 0.5,
|
|
2006
|
+
maxZoom: 2,
|
|
2007
|
+
connectionPosition: { x: 0, y: 0 },
|
|
2008
|
+
connectionNodeId: null,
|
|
2009
|
+
connectionPortType: 'source',
|
|
2010
|
+
autoPanOnNodeDrag: true,
|
|
2011
|
+
autoPanOnConnect: true,
|
|
2012
|
+
nodeExtent: infiniteExtent,
|
|
2013
|
+
translateExtent: infiniteExtent,
|
|
2014
|
+
onError: devWarn,
|
|
2015
|
+
};
|
|
2016
|
+
|
|
2017
|
+
const createRFStore = () => createStore((set, get) => ({
|
|
2018
|
+
...initialState,
|
|
2019
|
+
setNodes: (nodes) => {
|
|
2020
|
+
const { nodeInternals, nodeOrigin, elevateNodesOnSelect } = get();
|
|
2021
|
+
set({
|
|
2022
|
+
nodeInternals: createNodeInternals(nodes, nodeInternals, nodeOrigin, elevateNodesOnSelect),
|
|
2023
|
+
});
|
|
2024
|
+
},
|
|
2025
|
+
getNodes: () => Array.from(get().nodeInternals.values()),
|
|
2026
|
+
setEdges: (edges) => {
|
|
2027
|
+
const { defaultEdgeOptions = {} } = get();
|
|
2028
|
+
set({ edges: edges.map((e) => ({ ...defaultEdgeOptions, ...e })) });
|
|
2029
|
+
},
|
|
2030
|
+
updateNodeDimensions: (updates) => {
|
|
2031
|
+
const { onNodesChange, nodeInternals, domNode, nodeOrigin } = get();
|
|
2032
|
+
const viewportNode = domNode?.querySelector('.react-diagram__viewport');
|
|
2033
|
+
if (!viewportNode) {
|
|
2034
|
+
return;
|
|
2035
|
+
}
|
|
2036
|
+
const style = window.getComputedStyle(viewportNode);
|
|
2037
|
+
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform);
|
|
2038
|
+
const changes = updates.reduce((res, update) => {
|
|
2039
|
+
const node = nodeInternals.get(update.id);
|
|
2040
|
+
if (node) {
|
|
2041
|
+
const dimensions = getDimensions(update.nodeElement);
|
|
2042
|
+
const doUpdate = !!(dimensions.width &&
|
|
2043
|
+
dimensions.height &&
|
|
2044
|
+
(node.width !== dimensions.width ||
|
|
2045
|
+
node.height !== dimensions.height ||
|
|
2046
|
+
update.forceUpdate));
|
|
2047
|
+
if (doUpdate) {
|
|
2048
|
+
nodeInternals.set(node.id, {
|
|
2049
|
+
...node,
|
|
2050
|
+
[internalsSymbol]: {
|
|
2051
|
+
...node[internalsSymbol],
|
|
2052
|
+
portBounds: {
|
|
2053
|
+
source: getPortBounds('.source', update.nodeElement, zoom, nodeOrigin),
|
|
2054
|
+
target: getPortBounds('.target', update.nodeElement, zoom, nodeOrigin),
|
|
2055
|
+
},
|
|
2056
|
+
},
|
|
2057
|
+
...dimensions,
|
|
2058
|
+
});
|
|
2059
|
+
res.push({
|
|
2060
|
+
id: node.id,
|
|
2061
|
+
type: 'dimensions',
|
|
2062
|
+
dimensions,
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
return res;
|
|
2067
|
+
}, []);
|
|
2068
|
+
updateAbsoluteNodePositions(nodeInternals, nodeOrigin);
|
|
2069
|
+
set({
|
|
2070
|
+
nodeInternals: new Map(nodeInternals),
|
|
2071
|
+
});
|
|
2072
|
+
if (changes?.length > 0) {
|
|
2073
|
+
onNodesChange?.(changes);
|
|
2074
|
+
}
|
|
2075
|
+
},
|
|
2076
|
+
updateNodePositions: (nodeDragItems, positionChanged = true, dragging = false) => {
|
|
2077
|
+
const { triggerNodeChanges } = get();
|
|
2078
|
+
const changes = nodeDragItems.map((node) => {
|
|
2079
|
+
const change = {
|
|
2080
|
+
id: node.id,
|
|
2081
|
+
type: 'position',
|
|
2082
|
+
dragging,
|
|
2083
|
+
};
|
|
2084
|
+
if (positionChanged) {
|
|
2085
|
+
change.positionAbsolute = node.positionAbsolute;
|
|
2086
|
+
change.position = node.position;
|
|
2087
|
+
}
|
|
2088
|
+
return change;
|
|
2089
|
+
});
|
|
2090
|
+
triggerNodeChanges(changes);
|
|
2091
|
+
},
|
|
2092
|
+
triggerNodeChanges: (changes) => {
|
|
2093
|
+
const { onNodesChange } = get();
|
|
2094
|
+
if (changes?.length) {
|
|
2095
|
+
onNodesChange?.(changes);
|
|
2096
|
+
}
|
|
2097
|
+
},
|
|
2098
|
+
addSelectedNodes: (selectedNodeIds) => {
|
|
2099
|
+
const { multiSelectionActive, getNodes } = get();
|
|
2100
|
+
let changedNodes;
|
|
2101
|
+
if (multiSelectionActive) {
|
|
2102
|
+
changedNodes = selectedNodeIds.map((nodeId) => createSelectionChange(nodeId, true));
|
|
2103
|
+
}
|
|
2104
|
+
else {
|
|
2105
|
+
changedNodes = getSelectionChanges(getNodes(), selectedNodeIds);
|
|
2106
|
+
}
|
|
2107
|
+
updateNodesSelections({
|
|
2108
|
+
changedNodes,
|
|
2109
|
+
get,
|
|
2110
|
+
set,
|
|
2111
|
+
});
|
|
2112
|
+
},
|
|
2113
|
+
unselectNodes: ({ nodes } = {}) => {
|
|
2114
|
+
const { getNodes } = get();
|
|
2115
|
+
const nodesToUnselect = nodes ? nodes : getNodes();
|
|
2116
|
+
const changedNodes = nodesToUnselect.map((n) => {
|
|
2117
|
+
n.selected = false;
|
|
2118
|
+
return createSelectionChange(n.id, false);
|
|
2119
|
+
});
|
|
2120
|
+
updateNodesSelections({
|
|
2121
|
+
changedNodes,
|
|
2122
|
+
get,
|
|
2123
|
+
set,
|
|
2124
|
+
});
|
|
2125
|
+
},
|
|
2126
|
+
resetSelectedElements: () => {
|
|
2127
|
+
const { getNodes } = get();
|
|
2128
|
+
const nodes = getNodes();
|
|
2129
|
+
const nodesToUnselect = nodes
|
|
2130
|
+
.filter((e) => e.selected)
|
|
2131
|
+
.map((n) => createSelectionChange(n.id, false));
|
|
2132
|
+
updateNodesSelections({
|
|
2133
|
+
changedNodes: nodesToUnselect,
|
|
2134
|
+
get,
|
|
2135
|
+
set,
|
|
2136
|
+
});
|
|
2137
|
+
},
|
|
2138
|
+
cancelConnection: () => set({
|
|
2139
|
+
connectionNodeId: initialState.connectionNodeId,
|
|
2140
|
+
connectionPortType: initialState.connectionPortType,
|
|
2141
|
+
}),
|
|
2142
|
+
panBy: (delta) => {
|
|
2143
|
+
const { transform, width, height, d3Zoom, d3Selection } = get();
|
|
2144
|
+
if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
|
|
2145
|
+
return;
|
|
2146
|
+
}
|
|
2147
|
+
const nextTransform = zoomIdentity
|
|
2148
|
+
.translate(transform[0] + delta.x, transform[1] + delta.y)
|
|
2149
|
+
.scale(transform[2]);
|
|
2150
|
+
const extent = [
|
|
2151
|
+
[0, 0],
|
|
2152
|
+
[width, height],
|
|
2153
|
+
];
|
|
2154
|
+
const constrainedTransform = d3Zoom?.constrain()(nextTransform, extent, infiniteExtent);
|
|
2155
|
+
d3Zoom.transform(d3Selection, constrainedTransform);
|
|
2156
|
+
},
|
|
2157
|
+
setNodeExtent: (nodeExtent) => {
|
|
2158
|
+
const { nodeInternals } = get();
|
|
2159
|
+
nodeInternals.forEach((node) => {
|
|
2160
|
+
node.positionAbsolute = clampPosition(node.position, nodeExtent);
|
|
2161
|
+
});
|
|
2162
|
+
set({
|
|
2163
|
+
nodeExtent,
|
|
2164
|
+
nodeInternals: new Map(nodeInternals),
|
|
2165
|
+
});
|
|
2166
|
+
},
|
|
2167
|
+
setTranslateExtent: (translateExtent) => {
|
|
2168
|
+
get().d3Zoom?.translateExtent(translateExtent);
|
|
2169
|
+
set({ translateExtent });
|
|
2170
|
+
},
|
|
2171
|
+
setMinZoom: (minZoom) => {
|
|
2172
|
+
const { d3Zoom, maxZoom } = get();
|
|
2173
|
+
d3Zoom?.scaleExtent([minZoom, maxZoom]);
|
|
2174
|
+
set({ minZoom });
|
|
2175
|
+
},
|
|
2176
|
+
setMaxZoom: (maxZoom) => {
|
|
2177
|
+
const { d3Zoom, minZoom } = get();
|
|
2178
|
+
d3Zoom?.scaleExtent([minZoom, maxZoom]);
|
|
2179
|
+
set({ maxZoom });
|
|
2180
|
+
},
|
|
2181
|
+
}));
|
|
2182
|
+
|
|
2183
|
+
const ReactDiagramProvider = ({ children }) => {
|
|
2184
|
+
const storeRef = useRef(null);
|
|
2185
|
+
if (!storeRef.current) {
|
|
2186
|
+
storeRef.current = createRFStore();
|
|
2187
|
+
}
|
|
2188
|
+
return jsx(Provider$1, { value: storeRef.current, children: children });
|
|
2189
|
+
};
|
|
2190
|
+
ReactDiagramProvider.displayName = 'ReactDiagramProvider';
|
|
2191
|
+
|
|
2192
|
+
function Wrapper({ children }) {
|
|
2193
|
+
const isWrapped = useContext(StoreContext);
|
|
2194
|
+
if (isWrapped) {
|
|
2195
|
+
return jsx(Fragment, { children: children });
|
|
2196
|
+
}
|
|
2197
|
+
return jsx(ReactDiagramProvider, { children: children });
|
|
2198
|
+
}
|
|
2199
|
+
Wrapper.displayName = 'ReactDiagramWrapper';
|
|
2200
|
+
|
|
2201
|
+
const initViewport = { x: 0, y: 0, zoom: 1 };
|
|
2202
|
+
const initNodeOrigin = [0, 0];
|
|
2203
|
+
const defaultNodeTypes = {
|
|
2204
|
+
default: Nodes,
|
|
2205
|
+
};
|
|
2206
|
+
const defaultEdgeTypes = {
|
|
2207
|
+
step: StepEdge,
|
|
2208
|
+
};
|
|
2209
|
+
const ReactDiagram = forwardRef(({ id,
|
|
2210
|
+
// DiagramView props
|
|
2211
|
+
panning = true, minZoom, maxZoom, translateExtent, nodeExtent, defaultViewport = initViewport, multiSelectionKeyCode, onlyRenderVisibleElements = false, disableKeyboardA11y = false, noDragClassName = 'nodrag', noPanClassName = 'nopan', nodeOrigin = initNodeOrigin, nodeTypes = defaultNodeTypes, onNodeClick, onNodeDoubleClick, onNodeContextMenu, onNodeMouseEnter, onNodeMouseMove, onNodeMouseLeave, edgeTypes = defaultEdgeTypes, onEdgeClick, onEdgeDoubleClick, onEdgeContextMenu, onEdgeMouseEnter, onEdgeMouseMove, onEdgeMouseLeave, onEdgeUpdate, onEdgeUpdateStart, onEdgeUpdateEnd, onMove, onMoveStart, onMoveEnd,
|
|
2212
|
+
// StoreUpdater props
|
|
2213
|
+
nodes, edges, nodesDraggable, elevateNodesOnSelect, autoPanOnNodeDrag, autoPanOnConnect, onNodesChange, onNodeDrag, onNodeDragStart, onNodeDragEnd, onEdgesChange, onConnect, onConnectStart, onConnectEnd, onError, }, ref) => {
|
|
2214
|
+
const rfId = id || '1';
|
|
2215
|
+
const nodeTypesWrapped = useNodeOrEdgeTypes(nodeTypes, createNodeTypes);
|
|
2216
|
+
const edgeTypesWrapped = useNodeOrEdgeTypes(edgeTypes, createEdgeTypes);
|
|
2217
|
+
console.log(nodeTypes, nodeTypesWrapped);
|
|
2218
|
+
return (jsx("div", { ref: ref, className: "react-diagram", children: jsxs(Wrapper, { children: [jsx(DiagramView$1, { rfId: rfId, panning: panning, defaultViewport: defaultViewport, multiSelectionKeyCode: multiSelectionKeyCode, onlyRenderVisibleElements: onlyRenderVisibleElements, disableKeyboardA11y: disableKeyboardA11y, noDragClassName: noDragClassName, noPanClassName: noPanClassName, nodeOrigin: nodeOrigin, nodeTypes: nodeTypesWrapped, edgeTypes: edgeTypesWrapped, onNodeClick: onNodeClick, onNodeDoubleClick: onNodeDoubleClick, onNodeContextMenu: onNodeContextMenu, onNodeMouseEnter: onNodeMouseEnter, onNodeMouseMove: onNodeMouseMove, onNodeMouseLeave: onNodeMouseLeave, onEdgeClick: onEdgeClick, onEdgeDoubleClick: onEdgeDoubleClick, onEdgeContextMenu: onEdgeContextMenu, onEdgeMouseEnter: onEdgeMouseEnter, onEdgeMouseMove: onEdgeMouseMove, onEdgeMouseLeave: onEdgeMouseLeave, onEdgeUpdate: onEdgeUpdate, onEdgeUpdateStart: onEdgeUpdateStart, onEdgeUpdateEnd: onEdgeUpdateEnd, onMove: onMove, onMoveStart: onMoveStart, onMoveEnd: onMoveEnd }), jsx(StoreUpdater, { rfId: rfId, nodes: nodes, edges: edges, nodesDraggable: nodesDraggable, elevateNodesOnSelect: elevateNodesOnSelect, autoPanOnNodeDrag: autoPanOnNodeDrag, autoPanOnConnect: autoPanOnConnect, nodeExtent: nodeExtent, translateExtent: translateExtent, minZoom: minZoom, maxZoom: maxZoom, onNodesChange: onNodesChange, onNodeDrag: onNodeDrag, onNodeDragStart: onNodeDragStart, onNodeDragEnd: onNodeDragEnd, onEdgesChange: onEdgesChange, onConnect: onConnect, onConnectStart: onConnectStart, onConnectEnd: onConnectEnd, onError: onError })] }) }));
|
|
2219
|
+
});
|
|
2220
|
+
ReactDiagram.displayName = 'ReactDiagram';
|
|
2221
|
+
|
|
2222
|
+
function createUseItemsState(applyChanges) {
|
|
2223
|
+
return (initialItems) => {
|
|
2224
|
+
const [items, setItems] = useState(initialItems);
|
|
2225
|
+
const onItemsChange = useCallback((changes) => setItems((items) => applyChanges(changes, items)), []);
|
|
2226
|
+
return [items, setItems, onItemsChange];
|
|
2227
|
+
};
|
|
2228
|
+
}
|
|
2229
|
+
const useNodesState = createUseItemsState(applyNodeChanges);
|
|
2230
|
+
const useEdgesState = createUseItemsState(applyEdgeChanges);
|
|
2231
|
+
|
|
2232
|
+
export { BaseEdge, MarkerType, Port$1 as Port, Position, ReactDiagram, ReactDiagramProvider, StepEdge, addEdge, applyEdgeChanges, applyNodeChanges, clamp, getNodePositionWithOrigin, internalsSymbol, rectToBox, updateEdge, useEdgesState, useGetPointerPosition, useNodeId, useNodesState, useStore, useStoreApi };
|