chore(react/svelte): cleanup edge creation

This commit is contained in:
moklick
2023-11-29 16:33:57 +01:00
parent 1d7803aff7
commit 95acea54d1
3 changed files with 29 additions and 10 deletions
@@ -6,7 +6,8 @@
Background, Background,
BackgroundVariant, BackgroundVariant,
MiniMap, MiniMap,
MarkerType MarkerType,
type Connection
} from '@xyflow/svelte'; } from '@xyflow/svelte';
import '@xyflow/svelte/dist/style.css'; import '@xyflow/svelte/dist/style.css';
@@ -143,6 +144,12 @@
} }
} }
]); ]);
$: console.log('edges', $edges);
function getEdgeId(connection: Connection) {
return `edge-${connection.source}-${connection.target}}`;
}
</script> </script>
<SvelteFlow <SvelteFlow
@@ -150,9 +157,13 @@
{edges} {edges}
fitView fitView
nodeDragThreshold={2} nodeDragThreshold={2}
onedgecreate={(e) => { onedgecreate={(connection) => {
console.log('on edge create', e); console.log('on edge create', connection);
return e;
return {
...connection,
id: getEdgeId(connection)
};
}} }}
> >
<Controls /> <Controls />
@@ -80,17 +80,17 @@
updateConnection, updateConnection,
cancelConnection, cancelConnection,
panBy, panBy,
onConnect: (_connection) => { onConnect: (connection) => {
let connection = $onedgecreate ? $onedgecreate(_connection) : _connection; const edge = $onedgecreate ? $onedgecreate(connection) : connection;
if (!connection) { if (!edge) {
return; return;
} }
addEdge(connection); addEdge(edge);
// @todo: should we change/ improve the stuff we are passing here? // @todo: should we change/ improve the stuff we are passing here?
// instead of source/target we could pass fromNodeId, fromHandleId, etc // instead of source/target we could pass fromNodeId, fromHandleId, etc
dispatch('connect', { connection: _connection }); dispatch('connect', { connection });
}, },
onConnectStart: (event, startParams) => { onConnectStart: (event, startParams) => {
dispatch('connectstart', { dispatch('connectstart', {
+9 -1
View File
@@ -112,7 +112,7 @@ export function isEdgeVisible({ sourceNode, targetNode, width, height, transform
} }
const getEdgeId = ({ source, sourceHandle, target, targetHandle }: Connection | EdgeBase): string => const getEdgeId = ({ source, sourceHandle, target, targetHandle }: Connection | EdgeBase): string =>
`xyflow__edge-${source}${sourceHandle || ''}-${target}${targetHandle || ''}`; `xy-edge__${source}${sourceHandle || ''}-${target}${targetHandle || ''}`;
const connectionExists = (edge: EdgeBase, edges: EdgeBase[]) => { const connectionExists = (edge: EdgeBase, edges: EdgeBase[]) => {
return edges.some( return edges.some(
@@ -148,6 +148,14 @@ export const addEdgeBase = <EdgeType extends EdgeBase>(
return edges; return edges;
} }
if (edge.sourceHandle === null) {
delete edge.sourceHandle;
}
if (edge.targetHandle === null) {
delete edge.targetHandle;
}
return edges.concat(edge); return edges.concat(edge);
}; };