merge useNodeConnections

This commit is contained in:
peterkogo
2025-01-08 12:59:17 +01:00
27 changed files with 273 additions and 107 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

@@ -147,6 +147,12 @@
function hideUnhide() {
updateNode('hideunhide', (node) => ({ hidden: !node.hidden }));
}
// $inspect(edges);
$effect(() => {
// console.log(edges.map((edge) => ({ id: edge.id, selected: edge.selected })));
edges.forEach((edge) => console.log({ id: edge.id, selected: edge.selected }));
});
</script>
<SvelteFlow

View File

@@ -18,7 +18,7 @@
default: DebugNode
};
let nodes = $state<Node[]>([
let nodes = $state.raw<Node[]>([
{
id: '1',
type: 'input',
@@ -95,7 +95,7 @@
}
]);
let edges = $state<Edge[]>([
let edges = $state.raw<Edge[]>([
{
id: 'e1-2',
source: '1',

View File

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

View File

@@ -2,7 +2,7 @@
import {
Handle,
Position,
useHandleConnections,
useNodeConnections,
useNodesData,
useSvelteFlow,
type Node,
@@ -14,7 +14,7 @@
let { id, data }: NodeProps<Node<{ text: string }>> = $props();
const { updateNodeData } = useSvelteFlow();
const connections = useHandleConnections({
const connections = useNodeConnections({
nodeId: id,
type: 'target'
});