-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathuseNodesData.ts
73 lines (63 loc) · 1.87 KB
/
useNodesData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { ComputedRef, MaybeRefOrGetter } from 'vue'
import { computed, toValue } from 'vue'
import type { GraphNode, Node } from '../types'
import { warn } from '../utils'
import { useVueFlow } from './useVueFlow'
interface NodeData<NodeType extends Node = GraphNode> {
id: string
type: NodeType['type']
data: NonNullable<NodeType['data']>
}
/**
* Composable for receiving data of one or multiple nodes
*
* @public
* @param nodeId - The id (or ids) of the node to get the data from
* @param guard - Optional guard function to narrow down the node type
* @returns An array of data objects
*/
export function useNodesData<NodeType extends Node = GraphNode>(
nodeId: MaybeRefOrGetter<string>,
): ComputedRef<NodeData<NodeType> | null>
export function useNodesData<NodeType extends Node = GraphNode>(
nodeIds: MaybeRefOrGetter<string[]>,
): ComputedRef<NodeData<NodeType>[]>
export function useNodesData<NodeType extends Node = GraphNode>(
nodeIds: MaybeRefOrGetter<string[]>,
guard: (node: Node) => node is NodeType,
): ComputedRef<NodeData<NodeType>[]>
export function useNodesData(_nodeIds: any): any {
const { findNode } = useVueFlow()
return computed({
get() {
const nodeIds = toValue(_nodeIds)
if (!Array.isArray(nodeIds)) {
const node = findNode(nodeIds)
if (node) {
return {
id: node.id,
type: node.type,
data: node.data,
}
}
return null
}
const data: NodeData<Node>[] = []
for (const nodeId of nodeIds) {
const node = findNode(nodeId)
if (node) {
data.push({
id: node.id,
type: node.type,
data: node.data,
})
}
}
return data
},
set() {
// noop
warn('You are trying to set node data via useNodesData. This is not supported.')
},
})
}