fixed internal examples, made code more understandable

This commit is contained in:
peterkogo
2025-01-07 11:57:33 +01:00
parent 4a0b5e6c21
commit 4fc8e08128
9 changed files with 32 additions and 30 deletions

View File

@@ -49,7 +49,7 @@ import NodeToolbar from '../examples/NodeToolbar';
import UseConnection from '../examples/UseConnection';
import UseNodesInitialized from '../examples/UseNodesInit';
import UseNodesData from '../examples/UseNodesData';
import UseHandleConnections from '../examples/UseHandleConnections';
import UseNodeConnections from '../examples/UseNodeConnections';
import AddNodeOnEdgeDrop from '../examples/AddNodeOnEdgeDrop';
import DevTools from '../examples/DevTools';
import Redux from '../examples/Redux';
@@ -313,9 +313,9 @@ const routes: IRoute[] = [
component: UseReactFlow,
},
{
name: 'useHandleConnections',
path: 'usehandleconnections',
component: UseHandleConnections,
name: 'useNodeConnections',
path: 'usenodeconnections',
component: UseNodeConnections,
},
{
name: 'useNodesData',

View File

@@ -1,5 +1,5 @@
import { memo, FC, useEffect, useCallback } from 'react';
import { Handle, Position, NodeProps, useHandleConnections, Connection, HandleProps } from '@xyflow/react';
import { Handle, Position, NodeProps, useNodeConnections, Connection, HandleProps } from '@xyflow/react';
function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) {
const onConnect = useCallback(
@@ -11,9 +11,9 @@ function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string
(connections: Connection[]) => console.log('onDisconnect handler, node id:', nodeId, connections),
[nodeId]
);
const connections = useHandleConnections({
const connections = useNodeConnections({
type: handleProps.type,
id: handleProps.id,
handleId: handleProps.id,
onConnect,
onDisconnect,
});

View File

@@ -1,5 +1,5 @@
import { memo, FC, useEffect, useCallback } from 'react';
import { Handle, Position, NodeProps, useHandleConnections, Connection, HandleProps } from '@xyflow/react';
import { Handle, Position, NodeProps, useNodeConnections, Connection, HandleProps } from '@xyflow/react';
function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) {
const onConnect = useCallback(
@@ -14,9 +14,9 @@ function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string
},
[nodeId]
);
const connections = useHandleConnections({
const connections = useNodeConnections({
type: handleProps.type,
id: handleProps.id,
handleId: handleProps.id,
onConnect,
onDisconnect,
});

View File

@@ -1,9 +1,9 @@
import { memo } from 'react';
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react';
import { Handle, Position, useNodeConnections, useNodesData } from '@xyflow/react';
import { isTextNode, type MyNode } from '.';
function ResultNode() {
const connections = useHandleConnections({
const connections = useNodeConnections({
type: 'target',
});
const nodesData = useNodesData<MyNode>(connections.map((connection) => connection.source));

View File

@@ -1,10 +1,10 @@
import { memo, useEffect } from 'react';
import { Position, NodeProps, useReactFlow, Handle, useHandleConnections, useNodesData } from '@xyflow/react';
import { Position, NodeProps, useReactFlow, Handle, useNodeConnections, useNodesData } from '@xyflow/react';
import { isTextNode, type TextNode, type MyNode } from '.';
function UppercaseNode({ id }: NodeProps) {
const { updateNodeData } = useReactFlow();
const connections = useHandleConnections({
const connections = useNodeConnections({
type: 'target',
});
const nodesData = useNodesData<MyNode>(connections[0]?.source);

View File

@@ -2,7 +2,7 @@
import {
Handle,
Position,
useHandleConnections,
useNodeConnections,
useNodesData,
type NodeProps
} from '@xyflow/svelte';
@@ -13,7 +13,7 @@
export let id: $$Props['id'];
$$restProps;
const connections = useHandleConnections({
const connections = useNodeConnections({
nodeId: id,
type: 'target'
});

View File

@@ -2,7 +2,7 @@
import {
Handle,
Position,
useHandleConnections,
useNodeConnections,
useNodesData,
useSvelteFlow,
type NodeProps
@@ -16,7 +16,7 @@
$$restProps;
const { updateNodeData } = useSvelteFlow();
const connections = useHandleConnections({
const connections = useNodeConnections({
nodeId: id,
type: 'target'
});

View File

@@ -457,18 +457,20 @@ function addConnectionToLookup(
nodeId: string,
handleId: string | null
) {
// create array of key fragments for easier iteration
const keyFragments = [nodeId, type, handleId];
let key = '';
for (const keyFragment of keyFragments) {
// values for each iteration: nodeId, nodeId-type, nodeId-type-handleId
key += keyFragment;
// create a new map if the key does not exist and add the connection
const prevMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, prevMap.set(connectionKey, connection));
// add - as seperator for next iteration
key += '-';
}
// We add the connection to the connectionLookup at the following keys
// 1. nodeId, 2. nodeId-type, 3. nodeId-type-handleId
// If the key already exists, we add the connection to the existing map
let key = nodeId;
const nodeMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, nodeMap.set(connectionKey, connection));
key = `${nodeId}-${type}`;
const typeMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, typeMap.set(connectionKey, connection));
key = `${nodeId}-${type}-${handleId}`;
const handleMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, handleMap.set(connectionKey, connection));
}
export function updateConnectionLookup(connectionLookup: ConnectionLookup, edgeLookup: EdgeLookup, edges: EdgeBase[]) {