add onConnect and onDisconnect to useNodeConnections
This commit is contained in:
@@ -25,7 +25,16 @@
|
|||||||
console.log('disconnect source', handleId, connection);
|
console.log('disconnect source', handleId, connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
const connections = useNodeConnections({ id: id, handleType: 'target' });
|
const connections = useNodeConnections({
|
||||||
|
id: id,
|
||||||
|
handleType: 'target',
|
||||||
|
onConnect: (connections) => {
|
||||||
|
console.log('onConnect', connections);
|
||||||
|
},
|
||||||
|
onDisconnect: (connections) => {
|
||||||
|
console.log('onDisconnect', connections);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$inspect(connections.current);
|
$inspect(connections.current);
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import { areConnectionMapsEqual, type NodeConnection, type HandleType } from '@xyflow/system';
|
import {
|
||||||
|
areConnectionMapsEqual,
|
||||||
|
type NodeConnection,
|
||||||
|
type HandleType,
|
||||||
|
type Connection,
|
||||||
|
handleConnectionChange
|
||||||
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
@@ -7,9 +13,8 @@ type UseNodeConnectionsParams = {
|
|||||||
id?: string;
|
id?: string;
|
||||||
handleType?: HandleType;
|
handleType?: HandleType;
|
||||||
handleId?: string;
|
handleId?: string;
|
||||||
// TODO: Svelte 5
|
onConnect?: (connections: Connection[]) => void;
|
||||||
// onConnect?: (connections: Connection[]) => void;
|
onDisconnect?: (connections: Connection[]) => void;
|
||||||
// onDisconnect?: (connections: Connection[]) => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialConnections: NodeConnection[] = [];
|
const initialConnections: NodeConnection[] = [];
|
||||||
@@ -21,28 +26,38 @@ const initialConnections: NodeConnection[] = [];
|
|||||||
* @param param.id - node id - optional if called inside a custom node
|
* @param param.id - node id - optional if called inside a custom node
|
||||||
* @param param.handleType - filter by handle type 'source' or 'target'
|
* @param param.handleType - filter by handle type 'source' or 'target'
|
||||||
* @param param.handleId - filter by handle id (this is only needed if the node has multiple handles of the same type)
|
* @param param.handleId - filter by handle id (this is only needed if the node has multiple handles of the same type)
|
||||||
* @todo @param param.onConnect - gets called when a connection is established
|
* @param param.onConnect - gets called when a connection is established
|
||||||
* @todo @param param.onDisconnect - gets called when a connection is removed
|
* @param param.onDisconnect - gets called when a connection is removed
|
||||||
* @returns an array with connections
|
* @returns an array with connections
|
||||||
*/
|
*/
|
||||||
export function useNodeConnections({ id, handleType, handleId }: UseNodeConnectionsParams = {}) {
|
export function useNodeConnections({
|
||||||
|
id,
|
||||||
|
handleType,
|
||||||
|
handleId,
|
||||||
|
onConnect,
|
||||||
|
onDisconnect
|
||||||
|
}: UseNodeConnectionsParams = {}) {
|
||||||
const { edges, connectionLookup } = $derived(useStore());
|
const { edges, connectionLookup } = $derived(useStore());
|
||||||
|
|
||||||
const contextNodeId = getContext<string>('svelteflow__node_id');
|
const contextNodeId = getContext<string>('svelteflow__node_id');
|
||||||
const nodeId = id ?? contextNodeId;
|
const nodeId = id ?? contextNodeId;
|
||||||
|
|
||||||
let prevConnections: Map<string, NodeConnection> | undefined = new Map();
|
let prevConnections: Map<string, NodeConnection> = new Map();
|
||||||
let connectionsArray: NodeConnection[] = initialConnections;
|
let connectionsArray: NodeConnection[] = initialConnections;
|
||||||
|
|
||||||
const connections = $derived.by(() => {
|
const connections = $derived.by(() => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
edges;
|
edges;
|
||||||
const nextConnections = connectionLookup.get(
|
const nextConnections =
|
||||||
`${nodeId}${handleType ? (handleId ? `-${handleType}-${handleId}` : `-${handleType}`) : ''}`
|
connectionLookup.get(
|
||||||
);
|
`${nodeId}${handleType ? (handleId ? `-${handleType}-${handleId}` : `-${handleType}`) : ''}`
|
||||||
|
) ?? new Map();
|
||||||
if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
|
if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
|
||||||
|
if (onConnect) handleConnectionChange(nextConnections, prevConnections, onConnect);
|
||||||
|
if (onDisconnect) handleConnectionChange(prevConnections, nextConnections, onDisconnect);
|
||||||
|
|
||||||
prevConnections = nextConnections;
|
prevConnections = nextConnections;
|
||||||
connectionsArray = Array.from(nextConnections?.values() || initialConnections);
|
connectionsArray = Array.from(nextConnections.values() || initialConnections);
|
||||||
}
|
}
|
||||||
return connectionsArray;
|
return connectionsArray;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user