|
| 1 | +import type { ComputedRef, MaybeRefOrGetter } from 'vue' |
| 2 | +import { computed, ref, toRef, toValue, watch } from 'vue' |
| 3 | +import type { HandleConnection, HandleElement, HandleType } from '../types' |
| 4 | +import { useNodeId } from './useNodeId' |
| 5 | +import { useVueFlow } from './useVueFlow' |
| 6 | + |
| 7 | +export interface UseNodeConnectionsParams { |
| 8 | + type: MaybeRefOrGetter<HandleType> |
| 9 | + nodeId?: MaybeRefOrGetter<string | null> |
| 10 | + onConnect?: (connections: HandleConnection[]) => void |
| 11 | + onDisconnect?: (connections: HandleConnection[]) => void |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Composable that returns existing connections of a node by handle type. |
| 16 | + * This is useful when you want to get all connections of a node by a specific handle type. |
| 17 | + * |
| 18 | + * @public |
| 19 | + * @param params |
| 20 | + * @param params.type - handle type `source` or `target` |
| 21 | + * @param params.nodeId - node id - if not provided, the node id from the `useNodeId` (meaning, the context-based injection) is used |
| 22 | + * @param params.onConnect - gets called when a connection is created |
| 23 | + * @param params.onDisconnect - gets called when a connection is removed |
| 24 | + * |
| 25 | + * @returns An array of connections |
| 26 | + */ |
| 27 | +export function useNodeConnections(params: UseNodeConnectionsParams): ComputedRef<HandleConnection[]> { |
| 28 | + const { type, nodeId, onConnect, onDisconnect } = params |
| 29 | + |
| 30 | + const { connectionLookup, findNode } = useVueFlow() |
| 31 | + |
| 32 | + const _nodeId = useNodeId() |
| 33 | + |
| 34 | + const currentNodeId = toRef(() => toValue(nodeId) ?? _nodeId) |
| 35 | + |
| 36 | + const handleType = toRef(() => toValue(type)) |
| 37 | + |
| 38 | + const node = computed(() => findNode(currentNodeId.value)) |
| 39 | + |
| 40 | + const handleIds = computed(() => { |
| 41 | + if (!node.value) { |
| 42 | + return [] |
| 43 | + } |
| 44 | + |
| 45 | + const handles: HandleElement['id'][] = [] |
| 46 | + for (const handle of node.value?.handleBounds?.[handleType.value] ?? []) { |
| 47 | + handles.push(handle.id) |
| 48 | + } |
| 49 | + |
| 50 | + return handles |
| 51 | + }) |
| 52 | + |
| 53 | + const prevConnections = ref<Map<string, HandleConnection> | null>(null) |
| 54 | + |
| 55 | + const connectionsFromLookup = computed(() => { |
| 56 | + const nodeConnections = [] as Map<string, HandleConnection>[] |
| 57 | + |
| 58 | + for (const handleId of handleIds.value) { |
| 59 | + const connectionMap = connectionLookup.value.get(`${currentNodeId.value}-${handleType.value}-${handleId}`) |
| 60 | + if (connectionMap) { |
| 61 | + nodeConnections.push(connectionMap) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return nodeConnections |
| 66 | + }) |
| 67 | + |
| 68 | + watch( |
| 69 | + [connectionsFromLookup, () => typeof onConnect !== 'undefined', () => typeof onDisconnect !== 'undefined'], |
| 70 | + ([currentConnections]) => { |
| 71 | + if (!currentConnections) { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + const newConnections = new Map<string, HandleConnection>() |
| 76 | + |
| 77 | + for (const connectionMap of currentConnections) { |
| 78 | + for (const [key, connection] of connectionMap) { |
| 79 | + newConnections.set(key, connection) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (!prevConnections.value) { |
| 84 | + prevConnections.value = new Map(newConnections) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + const prevConnectionsValue = prevConnections.value |
| 89 | + |
| 90 | + const addedConnections = Array.from(newConnections.keys()).filter((key) => !prevConnectionsValue.has(key)) |
| 91 | + |
| 92 | + const removedConnections = Array.from(prevConnectionsValue.keys()).filter((key) => !newConnections.has(key)) |
| 93 | + |
| 94 | + if (addedConnections.length && onConnect) { |
| 95 | + const added = addedConnections.map((key) => newConnections.get(key)!) |
| 96 | + onConnect(added) |
| 97 | + } |
| 98 | + |
| 99 | + if (removedConnections.length && onDisconnect) { |
| 100 | + const removed = removedConnections.map((key) => prevConnectionsValue.get(key)!) |
| 101 | + onDisconnect(removed) |
| 102 | + } |
| 103 | + |
| 104 | + prevConnections.value = new Map(newConnections) |
| 105 | + }, |
| 106 | + { immediate: true }, |
| 107 | + ) |
| 108 | + |
| 109 | + return computed(() => { |
| 110 | + const connections = [] as HandleConnection[] |
| 111 | + |
| 112 | + for (const connectionMap of connectionsFromLookup.value) { |
| 113 | + for (const connection of connectionMap.values()) { |
| 114 | + connections.push(connection) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return connections |
| 119 | + }) |
| 120 | +} |
0 commit comments