@octanejs/xyflow 0.1.9 → 0.1.10
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/package.json +3 -2
- package/src/components/BatchProvider/useQueue.ts +19 -10
- package/src/components/NodeWrapper/useNodeObserver.ts +74 -57
- package/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +29 -14
- package/src/container/GraphView/useStylesLoadedWarning.ts +19 -13
- package/src/container/NodeRenderer/useResizeObserver.ts +32 -23
- package/src/hooks/useConnection.ts +13 -2
- package/src/hooks/useGlobalKeyHandler.ts +15 -7
- package/src/hooks/useInternalNode.ts +7 -3
- package/src/hooks/useKeyPress.ts +24 -4
- package/src/hooks/useMoveSelectedNodes.ts +3 -3
- package/src/hooks/useNodeConnections.ts +1 -1
- package/src/hooks/useNodesData.ts +3 -2
- package/src/hooks/useOnInitHandler.ts +4 -4
- package/src/hooks/useOnSelectionChange.ts +3 -3
- package/src/hooks/useResizeHandler.ts +3 -3
- package/src/hooks/useUpdateNodeInternals.ts +20 -16
- package/src/hooks/useViewportSync.ts +7 -6
- package/src/hooks/useVisibleEdgeIds.ts +3 -2
- package/src/hooks/useVisibleNodeIds.ts +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/xyflow",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@tsrx/react": "^0.3.0",
|
|
55
|
+
"@types/jsdom": "30.0.0",
|
|
55
56
|
"@types/react": "^19.2.17",
|
|
56
57
|
"@types/react-dom": "^19.2.3",
|
|
57
58
|
"@xyflow/react": "12.11.2",
|
|
@@ -60,7 +61,7 @@
|
|
|
60
61
|
"react-dom": "19.2.7",
|
|
61
62
|
"vitest": "^4.1.10",
|
|
62
63
|
"@octanejs/zustand": "0.1.55",
|
|
63
|
-
"octane": "0.4.
|
|
64
|
+
"octane": "0.4.3"
|
|
64
65
|
},
|
|
65
66
|
"scripts": {
|
|
66
67
|
"test": "vitest run --root ../.. --project xyflow --project xyflow-differential",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useState } from 'octane';
|
|
2
2
|
|
|
3
3
|
import { useIsomorphicLayoutEffect } from '../../hooks/useIsomorphicLayoutEffect';
|
|
4
|
+
import { resolveHookSlot, subSlot } from '../../hooks/slot';
|
|
4
5
|
import { Queue, QueueItem } from './types';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -11,7 +12,8 @@ import { Queue, QueueItem } from './types';
|
|
|
11
12
|
*
|
|
12
13
|
* @returns a Queue object
|
|
13
14
|
*/
|
|
14
|
-
export function useQueue<T>(runQueue: (items: QueueItem<T>[]) => void) {
|
|
15
|
+
export function useQueue<T>(runQueue: (items: QueueItem<T>[]) => void, ...rest: [slot?: symbol]) {
|
|
16
|
+
const slot = resolveHookSlot(rest);
|
|
15
17
|
/*
|
|
16
18
|
* Because we're using a ref above, we need some way to let React know when to
|
|
17
19
|
* actually process the queue. We increment this number any time we mutate the
|
|
@@ -19,29 +21,36 @@ export function useQueue<T>(runQueue: (items: QueueItem<T>[]) => void) {
|
|
|
19
21
|
* Using a boolean dirty flag here instead would lead to issues related to
|
|
20
22
|
* automatic batching. (https://github.com/xyflow/xyflow/issues/4779)
|
|
21
23
|
*/
|
|
22
|
-
const [serial, setSerial] = useState(BigInt(0));
|
|
24
|
+
const [serial, setSerial] = useState(BigInt(0), subSlot(slot, 'serial'));
|
|
23
25
|
|
|
24
26
|
/*
|
|
25
27
|
* A reference of all the batched updates to process before the next render. We
|
|
26
28
|
* want a reference here so multiple synchronous calls to `setNodes` etc can be
|
|
27
29
|
* batched together.
|
|
28
30
|
*/
|
|
29
|
-
const [queue] = useState(
|
|
31
|
+
const [queue] = useState(
|
|
32
|
+
() => createQueue<T>(() => setSerial((n) => n + BigInt(1))),
|
|
33
|
+
subSlot(slot, 'queue'),
|
|
34
|
+
);
|
|
30
35
|
|
|
31
36
|
/*
|
|
32
37
|
* Layout effects are guaranteed to run before the next render which means we
|
|
33
38
|
* shouldn't run into any issues with stale state or weird issues that come from
|
|
34
39
|
* rendering things one frame later than expected (we used to use `setTimeout`).
|
|
35
40
|
*/
|
|
36
|
-
useIsomorphicLayoutEffect(
|
|
37
|
-
|
|
41
|
+
useIsomorphicLayoutEffect(
|
|
42
|
+
() => {
|
|
43
|
+
const queueItems = queue.get();
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
if (queueItems.length) {
|
|
46
|
+
runQueue(queueItems);
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
queue.reset();
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
[serial],
|
|
52
|
+
subSlot(slot, 'flush'),
|
|
53
|
+
);
|
|
45
54
|
|
|
46
55
|
return queue;
|
|
47
56
|
}
|
|
@@ -2,6 +2,7 @@ import { useEffect, useRef } from 'octane';
|
|
|
2
2
|
|
|
3
3
|
import type { InternalNode } from '../../types';
|
|
4
4
|
import { useStoreApi } from '../../hooks/useStore';
|
|
5
|
+
import { resolveHookSlot, subSlot } from '../../hooks/slot';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Hook to handle the resize observation + internal updates for the passed node.
|
|
@@ -9,71 +10,87 @@ import { useStoreApi } from '../../hooks/useStore';
|
|
|
9
10
|
* @internal
|
|
10
11
|
* @returns nodeRef - reference to the node element
|
|
11
12
|
*/
|
|
12
|
-
export function useNodeObserver(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
13
|
+
export function useNodeObserver(
|
|
14
|
+
{
|
|
15
|
+
node,
|
|
16
|
+
nodeType,
|
|
17
|
+
hasDimensions,
|
|
18
|
+
resizeObserver,
|
|
19
|
+
}: {
|
|
20
|
+
node: InternalNode;
|
|
21
|
+
nodeType: string;
|
|
22
|
+
hasDimensions: boolean;
|
|
23
|
+
resizeObserver: ResizeObserver | null;
|
|
24
|
+
},
|
|
25
|
+
...rest: [slot?: symbol]
|
|
26
|
+
) {
|
|
27
|
+
const slot = resolveHookSlot(rest);
|
|
28
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
29
|
+
const nodeRef = useRef<HTMLDivElement | null>(null, subSlot(slot, 'node'));
|
|
30
|
+
const observedNode = useRef<HTMLDivElement | null>(null, subSlot(slot, 'observed-node'));
|
|
31
|
+
const prevSourcePosition = useRef(node.sourcePosition, subSlot(slot, 'prev-source-position'));
|
|
32
|
+
const prevTargetPosition = useRef(node.targetPosition, subSlot(slot, 'prev-target-position'));
|
|
33
|
+
const prevType = useRef(nodeType, subSlot(slot, 'prev-type'));
|
|
29
34
|
const isInitialized = hasDimensions && !!node.internals.handleBounds;
|
|
30
35
|
|
|
31
|
-
useEffect(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
useEffect(
|
|
37
|
+
() => {
|
|
38
|
+
if (
|
|
39
|
+
nodeRef.current &&
|
|
40
|
+
!node.hidden &&
|
|
41
|
+
(!isInitialized || observedNode.current !== nodeRef.current)
|
|
42
|
+
) {
|
|
43
|
+
if (observedNode.current) {
|
|
44
|
+
resizeObserver?.unobserve(observedNode.current);
|
|
45
|
+
}
|
|
46
|
+
resizeObserver?.observe(nodeRef.current);
|
|
47
|
+
observedNode.current = nodeRef.current;
|
|
39
48
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
},
|
|
50
|
+
[isInitialized, node.hidden],
|
|
51
|
+
subSlot(slot, 'observe'),
|
|
52
|
+
);
|
|
44
53
|
|
|
45
|
-
useEffect(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
useEffect(
|
|
55
|
+
() => {
|
|
56
|
+
return () => {
|
|
57
|
+
if (observedNode.current) {
|
|
58
|
+
resizeObserver?.unobserve(observedNode.current);
|
|
59
|
+
observedNode.current = null;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
[],
|
|
64
|
+
subSlot(slot, 'unobserve'),
|
|
65
|
+
);
|
|
53
66
|
|
|
54
|
-
useEffect(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
useEffect(
|
|
68
|
+
() => {
|
|
69
|
+
if (nodeRef.current) {
|
|
70
|
+
/*
|
|
71
|
+
* when the user programmatically changes the source or handle position, we need to update the internals
|
|
72
|
+
* to make sure the edges are updated correctly
|
|
73
|
+
*/
|
|
74
|
+
const typeChanged = prevType.current !== nodeType;
|
|
75
|
+
const sourcePosChanged = prevSourcePosition.current !== node.sourcePosition;
|
|
76
|
+
const targetPosChanged = prevTargetPosition.current !== node.targetPosition;
|
|
63
77
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
78
|
+
if (typeChanged || sourcePosChanged || targetPosChanged) {
|
|
79
|
+
prevType.current = nodeType;
|
|
80
|
+
prevSourcePosition.current = node.sourcePosition;
|
|
81
|
+
prevTargetPosition.current = node.targetPosition;
|
|
68
82
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
83
|
+
store
|
|
84
|
+
.getState()
|
|
85
|
+
.updateNodeInternals(
|
|
86
|
+
new Map([[node.id, { id: node.id, nodeElement: nodeRef.current, force: true }]]),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
74
89
|
}
|
|
75
|
-
}
|
|
76
|
-
|
|
90
|
+
},
|
|
91
|
+
[node.id, nodeType, node.sourcePosition, node.targetPosition],
|
|
92
|
+
subSlot(slot, 'update-internals'),
|
|
93
|
+
);
|
|
77
94
|
|
|
78
95
|
return nodeRef;
|
|
79
96
|
}
|
|
@@ -2,6 +2,7 @@ import { useEffect, useRef } from 'octane';
|
|
|
2
2
|
import { errorMessages } from '@xyflow/system';
|
|
3
3
|
|
|
4
4
|
import { useStoreApi } from '../../hooks/useStore';
|
|
5
|
+
import { resolveHookSlot, subSlot, withoutSlot } from '../../hooks/slot';
|
|
5
6
|
import type { EdgeTypes, NodeTypes } from '../../types';
|
|
6
7
|
|
|
7
8
|
// Kept module-local so browser consumers do not need `@types/node`.
|
|
@@ -18,22 +19,36 @@ const emptyTypes = {};
|
|
|
18
19
|
export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: NodeTypes): void;
|
|
19
20
|
export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: EdgeTypes): void;
|
|
20
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
-
export function useNodeOrEdgeTypesWarning(
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
export function useNodeOrEdgeTypesWarning(
|
|
23
|
+
nodeOrEdgeTypesArg: any = emptyTypes,
|
|
24
|
+
...rest: [slot?: symbol]
|
|
25
|
+
): any {
|
|
26
|
+
const slot =
|
|
27
|
+
resolveHookSlot(rest) ??
|
|
28
|
+
(typeof nodeOrEdgeTypesArg === 'symbol' ? nodeOrEdgeTypesArg : undefined);
|
|
29
|
+
const nodeOrEdgeTypes = withoutSlot(nodeOrEdgeTypesArg) ?? emptyTypes;
|
|
30
|
+
const typesRef = useRef(nodeOrEdgeTypes, subSlot(slot, 'types'));
|
|
31
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
24
32
|
|
|
25
|
-
useEffect(
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
useEffect(
|
|
34
|
+
() => {
|
|
35
|
+
if (process.env.NODE_ENV === 'development') {
|
|
36
|
+
const usedKeys = new Set([
|
|
37
|
+
...Object.keys(typesRef.current),
|
|
38
|
+
...Object.keys(nodeOrEdgeTypes),
|
|
39
|
+
]);
|
|
28
40
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
for (const key of usedKeys) {
|
|
42
|
+
if (typesRef.current[key] !== nodeOrEdgeTypes[key]) {
|
|
43
|
+
store.getState().onError?.('002', errorMessages['error002']());
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
33
46
|
}
|
|
34
|
-
}
|
|
35
47
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
typesRef.current = nodeOrEdgeTypes;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
[nodeOrEdgeTypes],
|
|
52
|
+
subSlot(slot, 'warn'),
|
|
53
|
+
);
|
|
39
54
|
}
|
|
@@ -2,25 +2,31 @@ import { useEffect, useRef } from 'octane';
|
|
|
2
2
|
import { errorMessages } from '@xyflow/system';
|
|
3
3
|
|
|
4
4
|
import { useStoreApi } from '../../hooks/useStore';
|
|
5
|
+
import { resolveHookSlot, subSlot } from '../../hooks/slot';
|
|
5
6
|
|
|
6
7
|
// Kept module-local so browser consumers do not need `@types/node`.
|
|
7
8
|
declare const process: { env: { NODE_ENV?: string } };
|
|
8
9
|
|
|
9
|
-
export function useStylesLoadedWarning() {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
export function useStylesLoadedWarning(...rest: [slot?: symbol]) {
|
|
11
|
+
const slot = resolveHookSlot(rest);
|
|
12
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
13
|
+
const checked = useRef(false, subSlot(slot, 'checked'));
|
|
12
14
|
|
|
13
|
-
useEffect(
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
15
|
+
useEffect(
|
|
16
|
+
() => {
|
|
17
|
+
if (process.env.NODE_ENV === 'development') {
|
|
18
|
+
if (!checked.current) {
|
|
19
|
+
const pane = document.querySelector('.react-flow__pane');
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
if (pane && !(window.getComputedStyle(pane).zIndex === '1')) {
|
|
22
|
+
store.getState().onError?.('013', errorMessages['error013']('react'));
|
|
23
|
+
}
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
checked.current = true;
|
|
26
|
+
}
|
|
23
27
|
}
|
|
24
|
-
}
|
|
25
|
-
|
|
28
|
+
},
|
|
29
|
+
[],
|
|
30
|
+
subSlot(slot, 'warn'),
|
|
31
|
+
);
|
|
26
32
|
}
|
|
@@ -2,37 +2,46 @@ import { useEffect, useState } from 'octane';
|
|
|
2
2
|
|
|
3
3
|
import { ReactFlowState } from '../../types';
|
|
4
4
|
import { useStore } from '../../hooks/useStore';
|
|
5
|
+
import { resolveHookSlot, subSlot } from '../../hooks/slot';
|
|
5
6
|
import { InternalNodeUpdate } from '@xyflow/system';
|
|
6
7
|
|
|
7
8
|
const selector = (s: ReactFlowState) => s.updateNodeInternals;
|
|
8
9
|
|
|
9
|
-
export function useResizeObserver() {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
export function useResizeObserver(...rest: [slot?: symbol]) {
|
|
11
|
+
const slot = resolveHookSlot(rest);
|
|
12
|
+
const updateNodeInternals = useStore(selector, undefined, subSlot(slot, 'update-internals'));
|
|
13
|
+
const [resizeObserver] = useState(
|
|
14
|
+
() => {
|
|
15
|
+
if (typeof ResizeObserver === 'undefined') {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
return new ResizeObserver((entries: ResizeObserverEntry[]) => {
|
|
20
|
+
const updates = new Map<string, InternalNodeUpdate>();
|
|
21
|
+
entries.forEach((entry: ResizeObserverEntry) => {
|
|
22
|
+
const id = entry.target.getAttribute('data-id') as string;
|
|
23
|
+
updates.set(id, {
|
|
24
|
+
id,
|
|
25
|
+
nodeElement: entry.target as HTMLDivElement,
|
|
26
|
+
force: true,
|
|
27
|
+
});
|
|
24
28
|
});
|
|
25
|
-
});
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
updateNodeInternals(updates);
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
subSlot(slot, 'observer'),
|
|
34
|
+
);
|
|
30
35
|
|
|
31
|
-
useEffect(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
useEffect(
|
|
37
|
+
() => {
|
|
38
|
+
return () => {
|
|
39
|
+
resizeObserver?.disconnect();
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
[resizeObserver],
|
|
43
|
+
subSlot(slot, 'disconnect'),
|
|
44
|
+
);
|
|
36
45
|
|
|
37
46
|
return resizeObserver;
|
|
38
47
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { shallow } from '@octanejs/zustand/shallow';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, withoutSlot } from './slot';
|
|
3
3
|
import { ConnectionState, pointToRendererPoint } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStore } from './useStore';
|
|
@@ -62,8 +62,19 @@ export function useConnection<
|
|
|
62
62
|
>(
|
|
63
63
|
connectionSelector?: (connection: ConnectionState<InternalNode<NodeType>>) => SelectorReturn,
|
|
64
64
|
...rest: [slot?: symbol]
|
|
65
|
+
): SelectorReturn;
|
|
66
|
+
export function useConnection<
|
|
67
|
+
NodeType extends Node = Node,
|
|
68
|
+
SelectorReturn = ConnectionState<InternalNode<NodeType>>,
|
|
69
|
+
>(
|
|
70
|
+
connectionSelectorArg?:
|
|
71
|
+
((connection: ConnectionState<InternalNode<NodeType>>) => SelectorReturn) | symbol,
|
|
72
|
+
...rest: [slot?: symbol]
|
|
65
73
|
): SelectorReturn {
|
|
66
|
-
const slot =
|
|
74
|
+
const slot =
|
|
75
|
+
resolveHookSlot(rest) ??
|
|
76
|
+
(typeof connectionSelectorArg === 'symbol' ? connectionSelectorArg : undefined);
|
|
77
|
+
const connectionSelector = withoutSlot(connectionSelectorArg);
|
|
67
78
|
const combinedSelector = getSelector<NodeType, SelectorReturn>(connectionSelector);
|
|
68
79
|
return useStore(combinedSelector, shallow, slot) as SelectorReturn;
|
|
69
80
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import type { KeyCode } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStoreApi } from '../hooks/useStore';
|
|
@@ -27,11 +27,19 @@ export function useGlobalKeyHandler(
|
|
|
27
27
|
...rest: [slot?: symbol]
|
|
28
28
|
): void {
|
|
29
29
|
const slot = resolveHookSlot(rest);
|
|
30
|
-
const store = useStoreApi(slot);
|
|
31
|
-
const { deleteElements } = useReactFlow(slot);
|
|
30
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
31
|
+
const { deleteElements } = useReactFlow(subSlot(slot, 'flow'));
|
|
32
32
|
|
|
33
|
-
const deleteKeyPressed = useKeyPress(
|
|
34
|
-
|
|
33
|
+
const deleteKeyPressed = useKeyPress(
|
|
34
|
+
deleteKeyCode,
|
|
35
|
+
{ actInsideInputWithModifier: false },
|
|
36
|
+
subSlot(slot, 'delete-key'),
|
|
37
|
+
);
|
|
38
|
+
const multiSelectionKeyPressed = useKeyPress(
|
|
39
|
+
multiSelectionKeyCode,
|
|
40
|
+
{ target: win },
|
|
41
|
+
subSlot(slot, 'multi-selection-key'),
|
|
42
|
+
);
|
|
35
43
|
|
|
36
44
|
useEffect(
|
|
37
45
|
function handleDeleteKey() {
|
|
@@ -42,7 +50,7 @@ export function useGlobalKeyHandler(
|
|
|
42
50
|
}
|
|
43
51
|
},
|
|
44
52
|
[deleteKeyPressed],
|
|
45
|
-
slot,
|
|
53
|
+
subSlot(slot, 'delete-effect'),
|
|
46
54
|
);
|
|
47
55
|
|
|
48
56
|
useEffect(
|
|
@@ -50,6 +58,6 @@ export function useGlobalKeyHandler(
|
|
|
50
58
|
store.setState({ multiSelectionActive: multiSelectionKeyPressed });
|
|
51
59
|
},
|
|
52
60
|
[multiSelectionKeyPressed],
|
|
53
|
-
slot,
|
|
61
|
+
subSlot(slot, 'multi-selection-effect'),
|
|
54
62
|
);
|
|
55
63
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import { shallow } from '@octanejs/zustand/shallow';
|
|
4
4
|
|
|
5
5
|
import { useStore } from './useStore';
|
|
@@ -38,9 +38,13 @@ export function useInternalNode<NodeType extends Node = Node>(
|
|
|
38
38
|
): InternalNode<NodeType> | undefined {
|
|
39
39
|
const slot = resolveHookSlot(rest);
|
|
40
40
|
const node = useStore(
|
|
41
|
-
useCallback(
|
|
41
|
+
useCallback(
|
|
42
|
+
(s) => s.nodeLookup.get(id) as InternalNode<NodeType> | undefined,
|
|
43
|
+
[id],
|
|
44
|
+
subSlot(slot, 'selector'),
|
|
45
|
+
),
|
|
42
46
|
shallow,
|
|
43
|
-
slot,
|
|
47
|
+
subSlot(slot, 'node'),
|
|
44
48
|
);
|
|
45
49
|
|
|
46
50
|
return node;
|
package/src/hooks/useKeyPress.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState, useEffect, useRef, useMemo } from 'octane';
|
|
2
|
-
import { resolveHookSlot, subSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot, withoutSlot } from './slot';
|
|
3
3
|
import { isInputDOMNode, type KeyCode } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
type Keys = Array<string>;
|
|
@@ -46,6 +46,11 @@ const defaultDoc = typeof document !== 'undefined' ? document : null;
|
|
|
46
46
|
*}
|
|
47
47
|
*```
|
|
48
48
|
*/
|
|
49
|
+
export function useKeyPress(
|
|
50
|
+
keyCode?: KeyCode | null,
|
|
51
|
+
options?: UseKeyPressOptions,
|
|
52
|
+
...rest: [slot?: symbol]
|
|
53
|
+
): boolean;
|
|
49
54
|
export function useKeyPress(
|
|
50
55
|
/**
|
|
51
56
|
* The key code (string or array of strings) specifies which key(s) should trigger
|
|
@@ -59,11 +64,26 @@ export function useKeyPress(
|
|
|
59
64
|
* means the user can press either the single key `'a'` or the combination of `'d'` and `'s'`.
|
|
60
65
|
* @default null
|
|
61
66
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
keyCodeArg: KeyCode | null | symbol = null,
|
|
68
|
+
optionsArg: UseKeyPressOptions | symbol = {
|
|
69
|
+
target: defaultDoc,
|
|
70
|
+
actInsideInputWithModifier: true,
|
|
71
|
+
},
|
|
64
72
|
...rest: [slot?: symbol]
|
|
65
73
|
): boolean {
|
|
66
|
-
|
|
74
|
+
// The trailing slot can occupy an omitted key or options argument.
|
|
75
|
+
const slot =
|
|
76
|
+
resolveHookSlot(rest) ??
|
|
77
|
+
(typeof optionsArg === 'symbol'
|
|
78
|
+
? optionsArg
|
|
79
|
+
: typeof keyCodeArg === 'symbol'
|
|
80
|
+
? keyCodeArg
|
|
81
|
+
: undefined);
|
|
82
|
+
const keyCode = withoutSlot(keyCodeArg) ?? null;
|
|
83
|
+
const options = withoutSlot(optionsArg) ?? {
|
|
84
|
+
target: defaultDoc,
|
|
85
|
+
actInsideInputWithModifier: true,
|
|
86
|
+
};
|
|
67
87
|
const [keyPressed, setKeyPressed] = useState(false, subSlot(slot, 'pressed'));
|
|
68
88
|
|
|
69
89
|
// we need to remember if a modifier key is pressed in order to track it
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import { calculateNodePosition, snapPosition, type XYPosition } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { type Node } from '../types';
|
|
@@ -16,7 +16,7 @@ const selectedAndDraggable = (nodesDraggable: boolean) => (n: Node) =>
|
|
|
16
16
|
*/
|
|
17
17
|
export function useMoveSelectedNodes(...rest: [slot?: symbol]) {
|
|
18
18
|
const slot = resolveHookSlot(rest);
|
|
19
|
-
const store = useStoreApi(slot);
|
|
19
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
20
20
|
|
|
21
21
|
const moveSelectedNodes = useCallback(
|
|
22
22
|
function moveSelectedNodes(params: { direction: XYPosition; factor: number }) {
|
|
@@ -75,7 +75,7 @@ export function useMoveSelectedNodes(...rest: [slot?: symbol]) {
|
|
|
75
75
|
updateNodePositions(nodeUpdates);
|
|
76
76
|
},
|
|
77
77
|
[],
|
|
78
|
-
slot,
|
|
78
|
+
subSlot(slot, 'move'),
|
|
79
79
|
);
|
|
80
80
|
|
|
81
81
|
return moveSelectedNodes;
|
|
@@ -39,7 +39,7 @@ export function useNodeConnections(
|
|
|
39
39
|
params: UseNodeConnectionsParams | symbol = {},
|
|
40
40
|
...rest: [slot?: symbol]
|
|
41
41
|
): NodeConnection[] {
|
|
42
|
-
|
|
42
|
+
const slot = resolveHookSlot(rest) ?? (typeof params === 'symbol' ? params : undefined);
|
|
43
43
|
const resolved = withoutSlot(params) ?? {};
|
|
44
44
|
const { id, handleType, handleId, onConnect, onDisconnect } = resolved;
|
|
45
45
|
const nodeId = useNodeId();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import { DistributivePick, shallowNodeData } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStore } from '../hooks/useStore';
|
|
@@ -57,9 +57,10 @@ export function useNodesData(nodeIds: any, ...rest: [slot?: symbol]): any {
|
|
|
57
57
|
return isArrayOfIds ? data : (data[0] ?? null);
|
|
58
58
|
},
|
|
59
59
|
[nodeIds],
|
|
60
|
+
subSlot(slot, 'selector'),
|
|
60
61
|
),
|
|
61
62
|
shallowNodeData,
|
|
62
|
-
slot,
|
|
63
|
+
subSlot(slot, 'data'),
|
|
63
64
|
);
|
|
64
65
|
|
|
65
66
|
return nodesData;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useEffect, useRef } from 'octane';
|
|
2
2
|
|
|
3
3
|
import { useReactFlow } from './useReactFlow';
|
|
4
|
-
import { resolveHookSlot } from './slot';
|
|
4
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
5
5
|
import type { OnInit, Node, Edge } from '../types';
|
|
6
6
|
|
|
7
7
|
export function useOnInitHandler<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
|
@@ -9,8 +9,8 @@ export function useOnInitHandler<NodeType extends Node = Node, EdgeType extends
|
|
|
9
9
|
...rest: [slot?: symbol]
|
|
10
10
|
) {
|
|
11
11
|
const slot = resolveHookSlot(rest);
|
|
12
|
-
const rfInstance = useReactFlow<NodeType, EdgeType>(slot);
|
|
13
|
-
const isInitialized = useRef<boolean>(false, slot);
|
|
12
|
+
const rfInstance = useReactFlow<NodeType, EdgeType>(subSlot(slot, 'flow'));
|
|
13
|
+
const isInitialized = useRef<boolean>(false, subSlot(slot, 'initialized'));
|
|
14
14
|
|
|
15
15
|
useEffect(
|
|
16
16
|
function onInitEffect() {
|
|
@@ -22,6 +22,6 @@ export function useOnInitHandler<NodeType extends Node = Node, EdgeType extends
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
[onInit, rfInstance.viewportInitialized],
|
|
25
|
-
slot,
|
|
25
|
+
subSlot(slot, 'init'),
|
|
26
26
|
);
|
|
27
27
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
|
|
4
4
|
import { useStoreApi } from './useStore';
|
|
5
5
|
import type { OnSelectionChangeFunc, Node, Edge } from '../types';
|
|
@@ -53,7 +53,7 @@ export function useOnSelectionChange<NodeType extends Node = Node, EdgeType exte
|
|
|
53
53
|
...rest: [slot?: symbol]
|
|
54
54
|
) {
|
|
55
55
|
const slot = resolveHookSlot(rest);
|
|
56
|
-
const store = useStoreApi<NodeType, EdgeType>(slot);
|
|
56
|
+
const store = useStoreApi<NodeType, EdgeType>(subSlot(slot, 'store'));
|
|
57
57
|
|
|
58
58
|
useEffect(
|
|
59
59
|
function registerSelectionChange() {
|
|
@@ -73,6 +73,6 @@ export function useOnSelectionChange<NodeType extends Node = Node, EdgeType exte
|
|
|
73
73
|
};
|
|
74
74
|
},
|
|
75
75
|
[onChange],
|
|
76
|
-
slot,
|
|
76
|
+
subSlot(slot, 'register'),
|
|
77
77
|
);
|
|
78
78
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'octane';
|
|
2
2
|
import type { RefObject } from '../react-shim.js';
|
|
3
|
-
import { resolveHookSlot } from './slot';
|
|
3
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
4
4
|
import { errorMessages, getDimensions } from '@xyflow/system';
|
|
5
5
|
|
|
6
6
|
import { useStoreApi } from '../hooks/useStore';
|
|
@@ -15,7 +15,7 @@ export function useResizeHandler(
|
|
|
15
15
|
...rest: [slot?: symbol]
|
|
16
16
|
): void {
|
|
17
17
|
const slot = resolveHookSlot(rest);
|
|
18
|
-
const store = useStoreApi(slot);
|
|
18
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
19
19
|
|
|
20
20
|
useEffect(
|
|
21
21
|
function observeResize() {
|
|
@@ -49,6 +49,6 @@ export function useResizeHandler(
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
[],
|
|
52
|
-
slot,
|
|
52
|
+
subSlot(slot, 'observe'),
|
|
53
53
|
);
|
|
54
54
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import type { UpdateNodeInternals, InternalNodeUpdate } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStoreApi } from '../hooks/useStore';
|
|
@@ -51,23 +51,27 @@ import { useStoreApi } from '../hooks/useStore';
|
|
|
51
51
|
*/
|
|
52
52
|
export function useUpdateNodeInternals(...rest: [slot?: symbol]): UpdateNodeInternals {
|
|
53
53
|
const slot = resolveHookSlot(rest);
|
|
54
|
-
const store = useStoreApi(slot);
|
|
54
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
55
55
|
|
|
56
|
-
return useCallback<UpdateNodeInternals>(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
return useCallback<UpdateNodeInternals>(
|
|
57
|
+
(id: string | string[]) => {
|
|
58
|
+
const { domNode, updateNodeInternals } = store.getState();
|
|
59
|
+
const updateIds = Array.isArray(id) ? id : [id];
|
|
60
|
+
const updates = new Map<string, InternalNodeUpdate>();
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
updateIds.forEach((updateId) => {
|
|
63
|
+
const nodeElement = domNode?.querySelector(
|
|
64
|
+
`.react-flow__node[data-id="${updateId}"]`,
|
|
65
|
+
) as HTMLDivElement;
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
if (nodeElement) {
|
|
68
|
+
updates.set(updateId, { id: updateId, nodeElement, force: true });
|
|
69
|
+
}
|
|
70
|
+
});
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
requestAnimationFrame(() => updateNodeInternals(updates, { triggerFitView: false }));
|
|
73
|
+
},
|
|
74
|
+
[],
|
|
75
|
+
subSlot(slot, 'update'),
|
|
76
|
+
);
|
|
73
77
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot, withoutSlot } from './slot';
|
|
3
3
|
import type { Viewport } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStore, useStoreApi } from './useStore';
|
|
@@ -13,10 +13,11 @@ const selector = (state: ReactFlowState) => state.panZoom?.syncViewport;
|
|
|
13
13
|
* @internal
|
|
14
14
|
* @param viewport
|
|
15
15
|
*/
|
|
16
|
-
export function useViewportSync(
|
|
17
|
-
const slot = resolveHookSlot(rest);
|
|
18
|
-
const
|
|
19
|
-
const
|
|
16
|
+
export function useViewportSync(viewportArg?: Viewport | symbol, ...rest: [slot?: symbol]) {
|
|
17
|
+
const slot = resolveHookSlot(rest) ?? (typeof viewportArg === 'symbol' ? viewportArg : undefined);
|
|
18
|
+
const viewport = withoutSlot(viewportArg);
|
|
19
|
+
const syncViewport = useStore(selector, undefined, subSlot(slot, 'sync'));
|
|
20
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
20
21
|
|
|
21
22
|
useEffect(
|
|
22
23
|
function syncViewportEffect() {
|
|
@@ -26,7 +27,7 @@ export function useViewportSync(viewport?: Viewport, ...rest: [slot?: symbol]) {
|
|
|
26
27
|
}
|
|
27
28
|
},
|
|
28
29
|
[viewport, syncViewport],
|
|
29
|
-
slot,
|
|
30
|
+
subSlot(slot, 'effect'),
|
|
30
31
|
);
|
|
31
32
|
|
|
32
33
|
return null;
|
|
@@ -3,7 +3,7 @@ import { shallow } from '@octanejs/zustand/shallow';
|
|
|
3
3
|
import { isEdgeVisible } from '@xyflow/system';
|
|
4
4
|
|
|
5
5
|
import { useStore } from './useStore';
|
|
6
|
-
import { resolveHookSlot } from './slot';
|
|
6
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
7
7
|
import { type ReactFlowState } from '../types';
|
|
8
8
|
|
|
9
9
|
export function useVisibleEdgeIds(onlyRenderVisible: boolean, ...rest: [slot?: symbol]): string[] {
|
|
@@ -43,9 +43,10 @@ export function useVisibleEdgeIds(onlyRenderVisible: boolean, ...rest: [slot?: s
|
|
|
43
43
|
return visibleEdgeIds;
|
|
44
44
|
},
|
|
45
45
|
[onlyRenderVisible],
|
|
46
|
+
subSlot(slot, 'selector'),
|
|
46
47
|
),
|
|
47
48
|
shallow,
|
|
48
|
-
slot,
|
|
49
|
+
subSlot(slot, 'ids'),
|
|
49
50
|
);
|
|
50
51
|
|
|
51
52
|
return edgeIds;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback } from 'octane';
|
|
2
|
-
import { resolveHookSlot } from './slot';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
3
|
import { shallow } from '@octanejs/zustand/shallow';
|
|
4
4
|
import { getNodesInside } from '@xyflow/system';
|
|
5
5
|
|
|
@@ -27,9 +27,9 @@ const selector = (onlyRenderVisible: boolean) => (s: ReactFlowState) => {
|
|
|
27
27
|
export function useVisibleNodeIds(onlyRenderVisible: boolean, ...rest: [slot?: symbol]) {
|
|
28
28
|
const slot = resolveHookSlot(rest);
|
|
29
29
|
const nodeIds = useStore(
|
|
30
|
-
useCallback(selector(onlyRenderVisible), [onlyRenderVisible]),
|
|
30
|
+
useCallback(selector(onlyRenderVisible), [onlyRenderVisible], subSlot(slot, 'selector')),
|
|
31
31
|
shallow,
|
|
32
|
-
slot,
|
|
32
|
+
subSlot(slot, 'ids'),
|
|
33
33
|
);
|
|
34
34
|
|
|
35
35
|
return nodeIds;
|