feat(core): add edge id to handle connections

This commit is contained in:
braks
2024-06-06 10:45:38 +02:00
committed by Braks
parent fa7087f7ff
commit 1bb7a19e1b
4 changed files with 31 additions and 21 deletions
@@ -1,6 +1,6 @@
import type { ComputedRef, MaybeRefOrGetter } from 'vue' import type { ComputedRef, MaybeRefOrGetter } from 'vue'
import { computed, ref, toRef, toValue, watch } from 'vue' import { computed, ref, toRef, toValue, watch } from 'vue'
import type { Connection, HandleType } from '../types' import type { HandleConnection, HandleType } from '../types'
import { areConnectionMapsEqual, handleConnectionChange } from '../utils' import { areConnectionMapsEqual, handleConnectionChange } from '../utils'
import { useNodeId } from './useNodeId' import { useNodeId } from './useNodeId'
import { useVueFlow } from './useVueFlow' import { useVueFlow } from './useVueFlow'
@@ -9,14 +9,12 @@ export interface UseHandleConnectionsParams {
type: MaybeRefOrGetter<HandleType> type: MaybeRefOrGetter<HandleType>
id?: MaybeRefOrGetter<string | null> id?: MaybeRefOrGetter<string | null>
nodeId?: MaybeRefOrGetter<string | null> nodeId?: MaybeRefOrGetter<string | null>
onConnect?: (connections: Connection[]) => void onConnect?: (connections: HandleConnection[]) => void
onDisconnect?: (connections: Connection[]) => void onDisconnect?: (connections: HandleConnection[]) => void
} }
// todo: add edge id to connection params
/** /**
* Composable that returns existing connections of a handle * Composable that returns existing connections of a `<Handle />`.
* *
* @public * @public
* @param params * @param params
@@ -28,7 +26,7 @@ export interface UseHandleConnectionsParams {
* *
* @returns An array of connections * @returns An array of connections
*/ */
export function useHandleConnections(params: UseHandleConnectionsParams): ComputedRef<Connection[]> { export function useHandleConnections(params: UseHandleConnectionsParams): ComputedRef<HandleConnection[]> {
const { type, id, nodeId, onConnect, onDisconnect } = params const { type, id, nodeId, onConnect, onDisconnect } = params
const { connectionLookup } = useVueFlow() const { connectionLookup } = useVueFlow()
@@ -41,9 +39,9 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
const handleId = toRef(() => toValue(id) ?? null) const handleId = toRef(() => toValue(id) ?? null)
const prevConnections = ref<Map<string, Connection> | null>(null) const prevConnections = ref<Map<string, HandleConnection> | null>(null)
const connections = ref<Map<string, Connection>>() const connections = ref<Map<string, HandleConnection>>()
watch( watch(
() => connectionLookup.value.get(`${currentNodeId.value}-${handleType.value}-${handleId.value}`), () => connectionLookup.value.get(`${currentNodeId.value}-${handleType.value}-${handleId.value}`),
@@ -59,17 +57,22 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
watch( watch(
[connections, () => typeof onConnect !== 'undefined', () => typeof onDisconnect !== 'undefined'], [connections, () => typeof onConnect !== 'undefined', () => typeof onDisconnect !== 'undefined'],
([currentConnections]) => { ([currentConnections = new Map<string, HandleConnection>()]) => {
if (prevConnections.value && prevConnections.value !== currentConnections) { if (prevConnections.value && prevConnections.value !== currentConnections) {
const _connections = currentConnections ?? new Map() handleConnectionChange(prevConnections.value, currentConnections, onDisconnect)
handleConnectionChange(prevConnections.value, _connections, onDisconnect) handleConnectionChange(currentConnections, prevConnections.value, onConnect)
handleConnectionChange(_connections, prevConnections.value, onConnect)
} }
prevConnections.value = currentConnections ?? new Map() prevConnections.value = currentConnections
}, },
{ immediate: true }, { immediate: true },
) )
return computed(() => Array.from(connections.value?.values() ?? [])) return computed(() => {
if (!connections.value) {
return []
}
return Array.from(connections.value.values())
})
} }
+7
View File
@@ -33,6 +33,11 @@ export interface Connection {
targetHandle?: string | null targetHandle?: string | null
} }
/** Connection with edge id */
export interface HandleConnection extends Connection {
edgeId: string
}
export type Connector = ( export type Connector = (
params: Connection, params: Connection,
) => Promise<(Connection & Partial<Edge>) | false> | ((Connection & Partial<Edge>) | false) ) => Promise<(Connection & Partial<Edge>) | false> | ((Connection & Partial<Edge>) | false)
@@ -83,3 +88,5 @@ export interface ConnectionLineProps {
/** status of the connection (valid, invalid) */ /** status of the connection (valid, invalid) */
connectionStatus: ConnectionStatus | null connectionStatus: ConnectionStatus | null
} }
export type ConnectionLookup = Map<string, Map<string, HandleConnection>>
+1 -2
View File
@@ -19,6 +19,7 @@ import type {
Connection, Connection,
ConnectionLineOptions, ConnectionLineOptions,
ConnectionLineType, ConnectionLineType,
ConnectionLookup,
ConnectionMode, ConnectionMode,
ConnectionStatus, ConnectionStatus,
Connector, Connector,
@@ -30,8 +31,6 @@ import type { CustomEvent, FlowHooks, FlowHooksEmit, FlowHooksOn } from './hooks
import type { EdgeChange, NodeChange, NodeDragItem } from './changes' import type { EdgeChange, NodeChange, NodeDragItem } from './changes'
import type { ConnectingHandle, ValidConnectionFunc } from './handle' import type { ConnectingHandle, ValidConnectionFunc } from './handle'
export type ConnectionLookup = Map<string, Map<string, Connection>>
export interface UpdateNodeDimensionsParams { export interface UpdateNodeDimensionsParams {
id: string id: string
nodeElement: HTMLDivElement nodeElement: HTMLDivElement
+5 -4
View File
@@ -7,6 +7,7 @@ import type {
Edge, Edge,
GraphEdge, GraphEdge,
GraphNode, GraphNode,
HandleConnection,
Node, Node,
State, State,
ValidConnectionFunc, ValidConnectionFunc,
@@ -162,15 +163,15 @@ export function updateConnectionLookup(connectionLookup: ConnectionLookup, edges
* @internal * @internal
*/ */
export function handleConnectionChange( export function handleConnectionChange(
a: Map<string, Connection>, a: Map<string, HandleConnection>,
b: Map<string, Connection>, b: Map<string, HandleConnection>,
cb?: (diff: Connection[]) => void, cb?: (diff: HandleConnection[]) => void,
) { ) {
if (!cb) { if (!cb) {
return return
} }
const diff: Connection[] = [] const diff: HandleConnection[] = []
for (const key of a.keys()) { for (const key of a.keys()) {
if (!b.has(key)) { if (!b.has(key)) {