@octanejs/xyflow 0.1.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/LICENSE +21 -0
- package/README.md +26 -0
- package/package.json +69 -0
- package/src/additional-components/Background/Background.tsrx +144 -0
- package/src/additional-components/Background/Patterns.tsrx +32 -0
- package/src/additional-components/Background/index.tsrx +2 -0
- package/src/additional-components/Background/types.ts +60 -0
- package/src/additional-components/Controls/ControlButton.tsrx +34 -0
- package/src/additional-components/Controls/Controls.tsrx +148 -0
- package/src/additional-components/Controls/Icons/FitView.tsrx +7 -0
- package/src/additional-components/Controls/Icons/Lock.tsrx +7 -0
- package/src/additional-components/Controls/Icons/Minus.tsrx +5 -0
- package/src/additional-components/Controls/Icons/Plus.tsrx +5 -0
- package/src/additional-components/Controls/Icons/Unlock.tsrx +7 -0
- package/src/additional-components/Controls/index.tsrx +3 -0
- package/src/additional-components/Controls/types.ts +68 -0
- package/src/additional-components/EdgeToolbar/EdgeToolbar.tsrx +79 -0
- package/src/additional-components/EdgeToolbar/index.tsrx +2 -0
- package/src/additional-components/EdgeToolbar/types.ts +14 -0
- package/src/additional-components/MiniMap/MiniMap.tsrx +249 -0
- package/src/additional-components/MiniMap/MiniMapNode.tsrx +43 -0
- package/src/additional-components/MiniMap/MiniMapNodes.tsrx +136 -0
- package/src/additional-components/MiniMap/index.ts +3 -0
- package/src/additional-components/MiniMap/types.ts +139 -0
- package/src/additional-components/NodeResizer/NodeResizeControl.tsrx +250 -0
- package/src/additional-components/NodeResizer/NodeResizer.tsrx +102 -0
- package/src/additional-components/NodeResizer/index.ts +4 -0
- package/src/additional-components/NodeResizer/types.ts +122 -0
- package/src/additional-components/NodeToolbar/NodeToolbar.tsrx +141 -0
- package/src/additional-components/NodeToolbar/NodeToolbarPortal.tsrx +17 -0
- package/src/additional-components/NodeToolbar/index.tsrx +2 -0
- package/src/additional-components/NodeToolbar/types.ts +32 -0
- package/src/additional-components/index.ts +6 -0
- package/src/components/A11yDescriptions/index.tsrx +58 -0
- package/src/components/Attribution/index.tsrx +31 -0
- package/src/components/BatchProvider/index.tsrx +122 -0
- package/src/components/BatchProvider/types.ts +7 -0
- package/src/components/BatchProvider/useQueue.ts +62 -0
- package/src/components/ConnectionLine/index.tsrx +140 -0
- package/src/components/EdgeLabelRenderer/index.tsrx +62 -0
- package/src/components/EdgeWrapper/EdgeUpdateAnchors.tsrx +151 -0
- package/src/components/EdgeWrapper/index.tsrx +274 -0
- package/src/components/EdgeWrapper/utils.ts +26 -0
- package/src/components/Edges/BaseEdge.tsrx +76 -0
- package/src/components/Edges/BezierEdge.tsrx +94 -0
- package/src/components/Edges/EdgeAnchor.tsrx +54 -0
- package/src/components/Edges/EdgeText.tsrx +97 -0
- package/src/components/Edges/SimpleBezierEdge.tsrx +150 -0
- package/src/components/Edges/SmoothStepEdge.tsrx +96 -0
- package/src/components/Edges/StepEdge.tsrx +55 -0
- package/src/components/Edges/StraightEdge.tsrx +81 -0
- package/src/components/Edges/index.ts +11 -0
- package/src/components/Handle/index.tsrx +314 -0
- package/src/components/NodeWrapper/index.tsrx +259 -0
- package/src/components/NodeWrapper/useNodeObserver.ts +79 -0
- package/src/components/NodeWrapper/utils.tsrx +40 -0
- package/src/components/Nodes/DefaultNode.tsrx +17 -0
- package/src/components/Nodes/GroupNode.tsrx +3 -0
- package/src/components/Nodes/InputNode.tsrx +13 -0
- package/src/components/Nodes/OutputNode.tsrx +13 -0
- package/src/components/Nodes/utils.ts +45 -0
- package/src/components/NodesSelection/index.tsrx +103 -0
- package/src/components/Panel/index.tsrx +34 -0
- package/src/components/ReactFlowProvider/index.tsrx +128 -0
- package/src/components/SelectionListener/index.tsrx +73 -0
- package/src/components/StoreUpdater/index.tsrx +173 -0
- package/src/components/UserSelection/index.tsrx +27 -0
- package/src/components/ViewportPortal/index.tsrx +42 -0
- package/src/container/EdgeRenderer/MarkerDefinitions.tsrx +87 -0
- package/src/container/EdgeRenderer/MarkerSymbols.tsrx +60 -0
- package/src/container/EdgeRenderer/index.tsrx +98 -0
- package/src/container/FlowRenderer/index.tsrx +146 -0
- package/src/container/GraphView/index.tsrx +212 -0
- package/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +39 -0
- package/src/container/GraphView/useStylesLoadedWarning.ts +26 -0
- package/src/container/NodeRenderer/index.tsrx +103 -0
- package/src/container/NodeRenderer/useResizeObserver.ts +38 -0
- package/src/container/Pane/index.tsrx +397 -0
- package/src/container/ReactFlow/Wrapper.tsrx +66 -0
- package/src/container/ReactFlow/index.tsrx +352 -0
- package/src/container/ReactFlow/init-values.ts +4 -0
- package/src/container/Viewport/index.tsrx +52 -0
- package/src/container/ZoomPane/index.tsrx +159 -0
- package/src/contexts/HandleConfigContext.tsrx +38 -0
- package/src/contexts/NodeIdContext.ts +39 -0
- package/src/contexts/StoreContext.ts +8 -0
- package/src/custom.d.ts +13 -0
- package/src/hooks/slot.ts +22 -0
- package/src/hooks/useColorModeClass.ts +50 -0
- package/src/hooks/useConnection.ts +69 -0
- package/src/hooks/useDrag.ts +95 -0
- package/src/hooks/useEdges.ts +32 -0
- package/src/hooks/useGlobalKeyHandler.ts +55 -0
- package/src/hooks/useHandleConnections.ts +77 -0
- package/src/hooks/useInternalNode.ts +47 -0
- package/src/hooks/useIsomorphicLayoutEffect.ts +5 -0
- package/src/hooks/useKeyPress.ts +217 -0
- package/src/hooks/useMoveSelectedNodes.ts +82 -0
- package/src/hooks/useNodeConnections.ts +86 -0
- package/src/hooks/useNodes.ts +33 -0
- package/src/hooks/useNodesData.ts +66 -0
- package/src/hooks/useNodesEdgesState.ts +138 -0
- package/src/hooks/useNodesInitialized.ts +74 -0
- package/src/hooks/useOnEdgesChangeMiddleware.ts +46 -0
- package/src/hooks/useOnInitHandler.ts +27 -0
- package/src/hooks/useOnNodesChangeMiddleware.ts +46 -0
- package/src/hooks/useOnSelectionChange.ts +78 -0
- package/src/hooks/useOnViewportChange.ts +68 -0
- package/src/hooks/useReactFlow.ts +347 -0
- package/src/hooks/useResizeHandler.ts +54 -0
- package/src/hooks/useStore.ts +58 -0
- package/src/hooks/useUpdateNodeInternals.ts +73 -0
- package/src/hooks/useViewport.ts +48 -0
- package/src/hooks/useViewportHelper.ts +103 -0
- package/src/hooks/useViewportSync.ts +33 -0
- package/src/hooks/useVisibleEdgeIds.ts +52 -0
- package/src/hooks/useVisibleNodeIds.ts +36 -0
- package/src/index.ts +151 -0
- package/src/react-shim.ts +93 -0
- package/src/store/index.ts +516 -0
- package/src/store/initialState.ts +162 -0
- package/src/styles/base.css +516 -0
- package/src/styles/style.css +648 -0
- package/src/styles/utils.ts +9 -0
- package/src/types/component-props.ts +716 -0
- package/src/types/edges.ts +312 -0
- package/src/types/general.ts +239 -0
- package/src/types/index.ts +6 -0
- package/src/types/instance.ts +265 -0
- package/src/types/nodes.ts +144 -0
- package/src/types/store.ts +198 -0
- package/src/utils/changes.ts +329 -0
- package/src/utils/edges.ts +34 -0
- package/src/utils/general.ts +72 -0
- package/src/utils/index.ts +3 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useEffect, useState } from 'octane';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import type { ColorMode, ColorModeClass } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
function getMediaQuery() {
|
|
6
|
+
if (typeof window === 'undefined' || !window.matchMedia) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return window.matchMedia('(prefers-color-scheme: dark)');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Hook for receiving the current color mode class 'dark' or 'light'.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
* @param colorMode - The color mode to use ('dark', 'light' or 'system')
|
|
18
|
+
*/
|
|
19
|
+
export function useColorModeClass(colorMode: ColorMode, ...rest: [slot?: symbol]): ColorModeClass {
|
|
20
|
+
const slot = resolveHookSlot(rest);
|
|
21
|
+
const [colorModeClass, setColorModeClass] = useState<ColorModeClass | null>(
|
|
22
|
+
colorMode === 'system' ? null : colorMode,
|
|
23
|
+
slot,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
useEffect(
|
|
27
|
+
function syncColorModeClass() {
|
|
28
|
+
if (colorMode !== 'system') {
|
|
29
|
+
setColorModeClass(colorMode);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const mediaQuery = getMediaQuery();
|
|
34
|
+
const updateColorModeClass = function updateColorModeClass() {
|
|
35
|
+
setColorModeClass(mediaQuery?.matches ? 'dark' : 'light');
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
updateColorModeClass();
|
|
39
|
+
mediaQuery?.addEventListener('change', updateColorModeClass);
|
|
40
|
+
|
|
41
|
+
return function cleanup() {
|
|
42
|
+
mediaQuery?.removeEventListener('change', updateColorModeClass);
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
[colorMode],
|
|
46
|
+
slot,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return colorModeClass !== null ? colorModeClass : getMediaQuery()?.matches ? 'dark' : 'light';
|
|
50
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import { ConnectionState, pointToRendererPoint } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import { useStore } from './useStore';
|
|
6
|
+
import type { InternalNode, Node, ReactFlowStore } from '../types';
|
|
7
|
+
|
|
8
|
+
function storeSelector(s: ReactFlowStore) {
|
|
9
|
+
return s.connection.inProgress
|
|
10
|
+
? { ...s.connection, to: pointToRendererPoint(s.connection.to, s.transform) }
|
|
11
|
+
: { ...s.connection };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getSelector<
|
|
15
|
+
NodeType extends Node = Node,
|
|
16
|
+
SelectorReturn = ConnectionState<InternalNode<NodeType>>,
|
|
17
|
+
>(
|
|
18
|
+
connectionSelector?: (connection: ConnectionState<InternalNode<NodeType>>) => SelectorReturn,
|
|
19
|
+
): (s: ReactFlowStore) => SelectorReturn | ConnectionState<InternalNode> {
|
|
20
|
+
if (connectionSelector) {
|
|
21
|
+
const combinedSelector = (s: ReactFlowStore) => {
|
|
22
|
+
const connection = storeSelector(s) as ConnectionState<InternalNode<NodeType>>;
|
|
23
|
+
return connectionSelector(connection);
|
|
24
|
+
};
|
|
25
|
+
return combinedSelector;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return storeSelector;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The `useConnection` hook returns the current connection when there is an active
|
|
32
|
+
* connection interaction. If no connection interaction is active, it returns null
|
|
33
|
+
* for every property. A typical use case for this hook is to colorize handles
|
|
34
|
+
* based on a certain condition (e.g. if the connection is valid or not).
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
* @param connectionSelector - An optional selector function used to extract a slice of the
|
|
38
|
+
* `ConnectionState` data. Using a selector can prevent component re-renders where data you don't
|
|
39
|
+
* otherwise care about might change. If a selector is not provided, the entire `ConnectionState`
|
|
40
|
+
* object is returned unchanged.
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* ```tsx
|
|
44
|
+
*import { useConnection } from '@xyflow/react';
|
|
45
|
+
*
|
|
46
|
+
*function App() {
|
|
47
|
+
* const connection = useConnection();
|
|
48
|
+
*
|
|
49
|
+
* return (
|
|
50
|
+
* <div> {connection ? `Someone is trying to make a connection from ${connection.fromNode} to this one.` : 'There are currently no incoming connections!'}
|
|
51
|
+
*
|
|
52
|
+
* </div>
|
|
53
|
+
* );
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @returns ConnectionState
|
|
58
|
+
*/
|
|
59
|
+
export function useConnection<
|
|
60
|
+
NodeType extends Node = Node,
|
|
61
|
+
SelectorReturn = ConnectionState<InternalNode<NodeType>>,
|
|
62
|
+
>(
|
|
63
|
+
connectionSelector?: (connection: ConnectionState<InternalNode<NodeType>>) => SelectorReturn,
|
|
64
|
+
...rest: [slot?: symbol]
|
|
65
|
+
): SelectorReturn {
|
|
66
|
+
const slot = resolveHookSlot(rest);
|
|
67
|
+
const combinedSelector = getSelector<NodeType, SelectorReturn>(connectionSelector);
|
|
68
|
+
return useStore(combinedSelector, shallow, slot) as SelectorReturn;
|
|
69
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'octane';
|
|
2
|
+
import type { RefObject } from '../react-shim.js';
|
|
3
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
4
|
+
import { XYDrag, type XYDragInstance } from '@xyflow/system';
|
|
5
|
+
|
|
6
|
+
import { handleNodeClick } from '../components/Nodes/utils';
|
|
7
|
+
import { useStoreApi } from './useStore';
|
|
8
|
+
|
|
9
|
+
type UseDragParams = {
|
|
10
|
+
nodeRef: RefObject<HTMLDivElement>;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
noDragClassName?: string;
|
|
13
|
+
handleSelector?: string;
|
|
14
|
+
nodeId?: string;
|
|
15
|
+
isSelectable?: boolean;
|
|
16
|
+
nodeClickDistance?: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Hook for calling XYDrag helper from @xyflow/system.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export function useDrag(
|
|
25
|
+
{
|
|
26
|
+
nodeRef,
|
|
27
|
+
disabled = false,
|
|
28
|
+
noDragClassName,
|
|
29
|
+
handleSelector,
|
|
30
|
+
nodeId,
|
|
31
|
+
isSelectable,
|
|
32
|
+
nodeClickDistance,
|
|
33
|
+
}: UseDragParams,
|
|
34
|
+
...rest: [slot?: symbol]
|
|
35
|
+
) {
|
|
36
|
+
const slot = resolveHookSlot(rest);
|
|
37
|
+
const store = useStoreApi(slot);
|
|
38
|
+
const [dragging, setDragging] = useState<boolean>(false, subSlot(slot, 'dragging'));
|
|
39
|
+
const xyDrag = useRef<XYDragInstance | undefined>(undefined, subSlot(slot, 'xyDrag'));
|
|
40
|
+
|
|
41
|
+
useEffect(
|
|
42
|
+
function setupDrag() {
|
|
43
|
+
if (disabled) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
xyDrag.current = XYDrag({
|
|
48
|
+
getStoreItems: function getStoreItems() {
|
|
49
|
+
return store.getState();
|
|
50
|
+
},
|
|
51
|
+
onNodeMouseDown: function onNodeMouseDown(id: string) {
|
|
52
|
+
handleNodeClick({
|
|
53
|
+
id,
|
|
54
|
+
store,
|
|
55
|
+
nodeRef,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
onDragStart: function onDragStart() {
|
|
59
|
+
setDragging(true);
|
|
60
|
+
},
|
|
61
|
+
onDragStop: function onDragStop() {
|
|
62
|
+
setDragging(false);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return function destroyDrag() {
|
|
67
|
+
xyDrag.current?.destroy();
|
|
68
|
+
xyDrag.current = undefined;
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
[disabled, store, nodeRef],
|
|
72
|
+
subSlot(slot, 'setup'),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
useEffect(
|
|
76
|
+
function updateDrag() {
|
|
77
|
+
if (disabled || !nodeRef.current || !xyDrag.current) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
xyDrag.current.update({
|
|
82
|
+
noDragClassName,
|
|
83
|
+
handleSelector,
|
|
84
|
+
domNode: nodeRef.current,
|
|
85
|
+
isSelectable,
|
|
86
|
+
nodeId,
|
|
87
|
+
nodeClickDistance,
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
[noDragClassName, handleSelector, disabled, isSelectable, nodeRef, nodeId, nodeClickDistance],
|
|
91
|
+
subSlot(slot, 'update'),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return dragging;
|
|
95
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
|
|
4
|
+
import { useStore } from '../hooks/useStore';
|
|
5
|
+
import type { Edge, ReactFlowState } from '../types';
|
|
6
|
+
|
|
7
|
+
const edgesSelector = (state: ReactFlowState) => state.edges;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This hook returns an array of the current edges. Components that use this hook
|
|
11
|
+
* will re-render **whenever any edge changes**.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
* @returns An array of all edges currently in the flow.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
*import { useEdges } from '@xyflow/react';
|
|
19
|
+
*
|
|
20
|
+
*export default function () {
|
|
21
|
+
* const edges = useEdges();
|
|
22
|
+
*
|
|
23
|
+
* return <div>There are currently {edges.length} edges!</div>;
|
|
24
|
+
*}
|
|
25
|
+
*```
|
|
26
|
+
*/
|
|
27
|
+
export function useEdges<EdgeType extends Edge = Edge>(...rest: [slot?: symbol]): EdgeType[] {
|
|
28
|
+
const slot = resolveHookSlot(rest);
|
|
29
|
+
const edges = useStore(edgesSelector, shallow, slot) as EdgeType[];
|
|
30
|
+
|
|
31
|
+
return edges;
|
|
32
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect } from 'octane';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import type { KeyCode } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import { useStoreApi } from '../hooks/useStore';
|
|
6
|
+
import { useKeyPress } from './useKeyPress';
|
|
7
|
+
import { useReactFlow } from './useReactFlow';
|
|
8
|
+
import { Edge, Node } from '../types';
|
|
9
|
+
|
|
10
|
+
const selected = (item: Node | Edge) => item.selected;
|
|
11
|
+
|
|
12
|
+
const win = typeof window !== 'undefined' ? window : undefined;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hook for handling global key events.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function useGlobalKeyHandler(
|
|
20
|
+
{
|
|
21
|
+
deleteKeyCode,
|
|
22
|
+
multiSelectionKeyCode,
|
|
23
|
+
}: {
|
|
24
|
+
deleteKeyCode: KeyCode | null;
|
|
25
|
+
multiSelectionKeyCode: KeyCode | null;
|
|
26
|
+
},
|
|
27
|
+
...rest: [slot?: symbol]
|
|
28
|
+
): void {
|
|
29
|
+
const slot = resolveHookSlot(rest);
|
|
30
|
+
const store = useStoreApi(slot);
|
|
31
|
+
const { deleteElements } = useReactFlow(slot);
|
|
32
|
+
|
|
33
|
+
const deleteKeyPressed = useKeyPress(deleteKeyCode, { actInsideInputWithModifier: false }, slot);
|
|
34
|
+
const multiSelectionKeyPressed = useKeyPress(multiSelectionKeyCode, { target: win }, slot);
|
|
35
|
+
|
|
36
|
+
useEffect(
|
|
37
|
+
function handleDeleteKey() {
|
|
38
|
+
if (deleteKeyPressed) {
|
|
39
|
+
const { edges, nodes } = store.getState();
|
|
40
|
+
deleteElements({ nodes: nodes.filter(selected), edges: edges.filter(selected) });
|
|
41
|
+
store.setState({ nodesSelectionActive: false });
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
[deleteKeyPressed],
|
|
45
|
+
slot,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
useEffect(
|
|
49
|
+
function handleMultiSelectionKey() {
|
|
50
|
+
store.setState({ multiSelectionActive: multiSelectionKeyPressed });
|
|
51
|
+
},
|
|
52
|
+
[multiSelectionKeyPressed],
|
|
53
|
+
slot,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'octane';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
|
+
import {
|
|
4
|
+
Connection,
|
|
5
|
+
HandleConnection,
|
|
6
|
+
HandleType,
|
|
7
|
+
areConnectionMapsEqual,
|
|
8
|
+
handleConnectionChange,
|
|
9
|
+
} from '@xyflow/system';
|
|
10
|
+
|
|
11
|
+
import { useStore } from './useStore';
|
|
12
|
+
import { useNodeId } from '../contexts/NodeIdContext';
|
|
13
|
+
|
|
14
|
+
type UseHandleConnectionsParams = {
|
|
15
|
+
/** What type of handle connections do you want to observe? */
|
|
16
|
+
type: HandleType;
|
|
17
|
+
/** The handle id (this is only needed if the node has multiple handles of the same type). */
|
|
18
|
+
id?: string | null;
|
|
19
|
+
/** If node id is not provided, the node id from the `NodeIdContext` is used. */
|
|
20
|
+
nodeId?: string;
|
|
21
|
+
/** Gets called when a connection is established. */
|
|
22
|
+
onConnect?: (connections: Connection[]) => void;
|
|
23
|
+
/** Gets called when a connection is removed. */
|
|
24
|
+
onDisconnect?: (connections: Connection[]) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
|
|
29
|
+
*
|
|
30
|
+
* @public
|
|
31
|
+
* @deprecated Use `useNodeConnections` instead.
|
|
32
|
+
* @returns An array with handle connections.
|
|
33
|
+
*/
|
|
34
|
+
export function useHandleConnections(
|
|
35
|
+
{ type, id, nodeId, onConnect, onDisconnect }: UseHandleConnectionsParams,
|
|
36
|
+
...rest: [slot?: symbol]
|
|
37
|
+
): HandleConnection[] {
|
|
38
|
+
const slot = resolveHookSlot(rest);
|
|
39
|
+
console.warn(
|
|
40
|
+
'[DEPRECATED] `useHandleConnections` is deprecated. Instead use `useNodeConnections` https://reactflow.dev/api-reference/hooks/useNodeConnections',
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const _nodeId = useNodeId();
|
|
44
|
+
const currentNodeId = nodeId ?? _nodeId;
|
|
45
|
+
|
|
46
|
+
const prevConnections = useRef<Map<string, HandleConnection> | null>(
|
|
47
|
+
null,
|
|
48
|
+
subSlot(slot, 'previous-connections'),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const connections = useStore(
|
|
52
|
+
(state) => state.connectionLookup.get(`${currentNodeId}-${type}${id ? `-${id}` : ''}`),
|
|
53
|
+
areConnectionMapsEqual,
|
|
54
|
+
subSlot(slot, 'connections'),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
useEffect(
|
|
58
|
+
() => {
|
|
59
|
+
// @todo discuss if onConnect/onDisconnect should be called when the component mounts/unmounts
|
|
60
|
+
if (prevConnections.current && prevConnections.current !== connections) {
|
|
61
|
+
const _connections = connections ?? new Map();
|
|
62
|
+
handleConnectionChange(prevConnections.current, _connections, onDisconnect);
|
|
63
|
+
handleConnectionChange(_connections, prevConnections.current, onConnect);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
prevConnections.current = connections ?? new Map();
|
|
67
|
+
},
|
|
68
|
+
[connections, onConnect, onDisconnect],
|
|
69
|
+
subSlot(slot, 'connection-change'),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return useMemo(
|
|
73
|
+
() => Array.from(connections?.values() ?? []),
|
|
74
|
+
[connections],
|
|
75
|
+
subSlot(slot, 'connection-list'),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useCallback } from 'octane';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
4
|
+
|
|
5
|
+
import { useStore } from './useStore';
|
|
6
|
+
import type { InternalNode, Node } from '../types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This hook returns the internal representation of a specific node.
|
|
10
|
+
* Components that use this hook will re-render **whenever the node changes**,
|
|
11
|
+
* including when a node is selected or moved.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
* @param id - The ID of a node you want to observe.
|
|
15
|
+
* @returns The `InternalNode` object for the node with the given ID.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
*import { useInternalNode } from '@xyflow/react';
|
|
20
|
+
*
|
|
21
|
+
*export default function () {
|
|
22
|
+
* const internalNode = useInternalNode('node-1');
|
|
23
|
+
* const absolutePosition = internalNode.internals.positionAbsolute;
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <div>
|
|
27
|
+
* The absolute position of the node is at:
|
|
28
|
+
* <p>x: {absolutePosition.x}</p>
|
|
29
|
+
* <p>y: {absolutePosition.y}</p>
|
|
30
|
+
* </div>
|
|
31
|
+
* );
|
|
32
|
+
*}
|
|
33
|
+
*```
|
|
34
|
+
*/
|
|
35
|
+
export function useInternalNode<NodeType extends Node = Node>(
|
|
36
|
+
id: string,
|
|
37
|
+
...rest: [slot?: symbol]
|
|
38
|
+
): InternalNode<NodeType> | undefined {
|
|
39
|
+
const slot = resolveHookSlot(rest);
|
|
40
|
+
const node = useStore(
|
|
41
|
+
useCallback((s) => s.nodeLookup.get(id) as InternalNode<NodeType> | undefined, [id]),
|
|
42
|
+
shallow,
|
|
43
|
+
slot,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return node;
|
|
47
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { useState, useEffect, useRef, useMemo } from 'octane';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
|
+
import { isInputDOMNode, type KeyCode } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
type Keys = Array<string>;
|
|
6
|
+
type PressedKeys = Set<string>;
|
|
7
|
+
type KeyOrCode = 'key' | 'code';
|
|
8
|
+
|
|
9
|
+
export type UseKeyPressOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* Listen to key presses on a specific element.
|
|
12
|
+
* @default document
|
|
13
|
+
*/
|
|
14
|
+
target?: Window | Document | HTMLElement | ShadowRoot | null;
|
|
15
|
+
/**
|
|
16
|
+
* You can use this flag to prevent triggering the key press hook when an input field is focused.
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
actInsideInputWithModifier?: boolean;
|
|
20
|
+
preventDefault?: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const defaultDoc = typeof document !== 'undefined' ? document : null;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* This hook lets you listen for specific key codes and tells you whether they are
|
|
27
|
+
* currently pressed or not.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
* @param options - Options
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
*import { useKeyPress } from '@xyflow/react';
|
|
35
|
+
*
|
|
36
|
+
*export default function () {
|
|
37
|
+
* const spacePressed = useKeyPress('Space');
|
|
38
|
+
* const cmdAndSPressed = useKeyPress(['Meta+s', 'Strg+s']);
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <div>
|
|
42
|
+
* {spacePressed && <p>Space pressed!</p>}
|
|
43
|
+
* {cmdAndSPressed && <p>Cmd + S pressed!</p>}
|
|
44
|
+
* </div>
|
|
45
|
+
* );
|
|
46
|
+
*}
|
|
47
|
+
*```
|
|
48
|
+
*/
|
|
49
|
+
export function useKeyPress(
|
|
50
|
+
/**
|
|
51
|
+
* The key code (string or array of strings) specifies which key(s) should trigger
|
|
52
|
+
* an action.
|
|
53
|
+
*
|
|
54
|
+
* A **string** can represent:
|
|
55
|
+
* - A **single key**, e.g. `'a'`
|
|
56
|
+
* - A **key combination**, using `'+'` to separate keys, e.g. `'a+d'`
|
|
57
|
+
*
|
|
58
|
+
* An **array of strings** represents **multiple possible key inputs**. For example, `['a', 'd+s']`
|
|
59
|
+
* means the user can press either the single key `'a'` or the combination of `'d'` and `'s'`.
|
|
60
|
+
* @default null
|
|
61
|
+
*/
|
|
62
|
+
keyCode: KeyCode | null = null,
|
|
63
|
+
options: UseKeyPressOptions = { target: defaultDoc, actInsideInputWithModifier: true },
|
|
64
|
+
...rest: [slot?: symbol]
|
|
65
|
+
): boolean {
|
|
66
|
+
const slot = resolveHookSlot(rest);
|
|
67
|
+
const [keyPressed, setKeyPressed] = useState(false, subSlot(slot, 'pressed'));
|
|
68
|
+
|
|
69
|
+
// we need to remember if a modifier key is pressed in order to track it
|
|
70
|
+
const modifierPressed = useRef(false, subSlot(slot, 'modifier'));
|
|
71
|
+
|
|
72
|
+
// we need to remember the pressed keys in order to support combinations
|
|
73
|
+
const pressedKeys = useRef<PressedKeys>(new Set([]), subSlot(slot, 'keys'));
|
|
74
|
+
|
|
75
|
+
/*
|
|
76
|
+
* keyCodes = array with single keys [['a']] or key combinations [['a', 's']]
|
|
77
|
+
* keysToWatch = array with all keys flattened ['a', 'd', 'ShiftLeft']
|
|
78
|
+
* used to check if we store event.code or event.key. When the code is in the list of keysToWatch
|
|
79
|
+
* we use the code otherwise the key. Explainer: When you press the left "command" key, the code is "MetaLeft"
|
|
80
|
+
* and the key is "Meta". We want users to be able to pass keys and codes so we assume that the key is meant when
|
|
81
|
+
* we can't find it in the list of keysToWatch.
|
|
82
|
+
*/
|
|
83
|
+
const [keyCodes, keysToWatch] = useMemo<[Array<Keys>, Keys]>(
|
|
84
|
+
function parseKeyCodes() {
|
|
85
|
+
if (keyCode !== null) {
|
|
86
|
+
const keyCodeArr = Array.isArray(keyCode) ? keyCode : [keyCode];
|
|
87
|
+
const keys = keyCodeArr
|
|
88
|
+
.filter(function isStringKeyCode(kc) {
|
|
89
|
+
return typeof kc === 'string';
|
|
90
|
+
})
|
|
91
|
+
/*
|
|
92
|
+
* we first replace all '+' with '\n' which we will use to split the keys on
|
|
93
|
+
* then we replace '\n\n' with '\n+', this way we can also support the combination 'key++'
|
|
94
|
+
* in the end we simply split on '\n' to get the key array
|
|
95
|
+
*/
|
|
96
|
+
.map(function splitKeyCombination(kc) {
|
|
97
|
+
return kc.replace('+', '\n').replace('\n\n', '\n+').split('\n');
|
|
98
|
+
});
|
|
99
|
+
const keysFlat = keys.reduce(function flattenKeys(res: Keys, item) {
|
|
100
|
+
return res.concat(...item);
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
return [keys, keysFlat];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return [[], []];
|
|
107
|
+
},
|
|
108
|
+
[keyCode],
|
|
109
|
+
subSlot(slot, 'memo'),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
useEffect(
|
|
113
|
+
function listenForKeyPress() {
|
|
114
|
+
const target = options?.target ?? defaultDoc;
|
|
115
|
+
const actInsideInputWithModifier = options?.actInsideInputWithModifier ?? true;
|
|
116
|
+
|
|
117
|
+
if (keyCode !== null) {
|
|
118
|
+
const downHandler = function downHandler(event: KeyboardEvent) {
|
|
119
|
+
modifierPressed.current =
|
|
120
|
+
event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
|
|
121
|
+
const preventAction =
|
|
122
|
+
(!modifierPressed.current ||
|
|
123
|
+
(modifierPressed.current && !actInsideInputWithModifier)) &&
|
|
124
|
+
isInputDOMNode(event);
|
|
125
|
+
|
|
126
|
+
if (preventAction) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const keyOrCode = useKeyOrCode(event.code, keysToWatch);
|
|
130
|
+
pressedKeys.current.add(event[keyOrCode]);
|
|
131
|
+
|
|
132
|
+
if (isMatchingKey(keyCodes, pressedKeys.current, false)) {
|
|
133
|
+
const target = (event.composedPath?.()?.[0] || event.target) as Element | null;
|
|
134
|
+
const isInteractiveElement = target?.nodeName === 'BUTTON' || target?.nodeName === 'A';
|
|
135
|
+
|
|
136
|
+
if (
|
|
137
|
+
options.preventDefault !== false &&
|
|
138
|
+
(modifierPressed.current || !isInteractiveElement)
|
|
139
|
+
) {
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
setKeyPressed(true);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const upHandler = function upHandler(event: KeyboardEvent) {
|
|
148
|
+
const keyOrCode = useKeyOrCode(event.code, keysToWatch);
|
|
149
|
+
|
|
150
|
+
if (isMatchingKey(keyCodes, pressedKeys.current, true)) {
|
|
151
|
+
setKeyPressed(false);
|
|
152
|
+
pressedKeys.current.clear();
|
|
153
|
+
} else {
|
|
154
|
+
pressedKeys.current.delete(event[keyOrCode]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// fix for Mac: when cmd key is pressed, keyup is not triggered for any other key, see: https://stackoverflow.com/questions/27380018/when-cmd-key-is-kept-pressed-keyup-is-not-triggered-for-any-other-key
|
|
158
|
+
if (event.key === 'Meta') {
|
|
159
|
+
pressedKeys.current.clear();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
modifierPressed.current = false;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const resetHandler = function resetHandler() {
|
|
166
|
+
pressedKeys.current.clear();
|
|
167
|
+
setKeyPressed(false);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
target?.addEventListener('keydown', downHandler as EventListenerOrEventListenerObject);
|
|
171
|
+
target?.addEventListener('keyup', upHandler as EventListenerOrEventListenerObject);
|
|
172
|
+
window.addEventListener('blur', resetHandler);
|
|
173
|
+
window.addEventListener('contextmenu', resetHandler);
|
|
174
|
+
|
|
175
|
+
return function cleanup() {
|
|
176
|
+
target?.removeEventListener('keydown', downHandler as EventListenerOrEventListenerObject);
|
|
177
|
+
target?.removeEventListener('keyup', upHandler as EventListenerOrEventListenerObject);
|
|
178
|
+
window.removeEventListener('blur', resetHandler);
|
|
179
|
+
window.removeEventListener('contextmenu', resetHandler);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
[keyCode, setKeyPressed],
|
|
184
|
+
subSlot(slot, 'effect'),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return keyPressed;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// utils
|
|
191
|
+
|
|
192
|
+
function isMatchingKey(keyCodes: Array<Keys>, pressedKeys: PressedKeys, isUp: boolean): boolean {
|
|
193
|
+
return (
|
|
194
|
+
keyCodes
|
|
195
|
+
/*
|
|
196
|
+
* we only want to compare same sizes of keyCode definitions
|
|
197
|
+
* and pressed keys. When the user specified 'Meta' as a key somewhere
|
|
198
|
+
* this would also be truthy without this filter when user presses 'Meta' + 'r'
|
|
199
|
+
*/
|
|
200
|
+
.filter(function filterBySize(keys) {
|
|
201
|
+
return isUp || keys.length === pressedKeys.size;
|
|
202
|
+
})
|
|
203
|
+
/*
|
|
204
|
+
* since we want to support multiple possibilities only one of the
|
|
205
|
+
* combinations need to be part of the pressed keys
|
|
206
|
+
*/
|
|
207
|
+
.some(function someMatchingKey(keys) {
|
|
208
|
+
return keys.every(function hasKey(k) {
|
|
209
|
+
return pressedKeys.has(k);
|
|
210
|
+
});
|
|
211
|
+
})
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function useKeyOrCode(eventCode: string, keysToWatch: KeyCode): KeyOrCode {
|
|
216
|
+
return keysToWatch.includes(eventCode) ? 'code' : 'key';
|
|
217
|
+
}
|