fix some hooks, fix dragndrop example

This commit is contained in:
peterkogo
2024-12-12 16:43:42 +01:00
parent d2fd846c67
commit efa163203c
6 changed files with 17 additions and 17 deletions
@@ -56,11 +56,11 @@
}; };
nodes.push(newNode); nodes.push(newNode);
edges.push({ // edges.push({
source: connectingNodeId, // source: connectingNodeId,
target: id, // target: id,
id: `${connectingNodeId}--${id}` // id: `${connectingNodeId}--${id}`
}); // });
} }
}; };
</script> </script>
@@ -13,7 +13,7 @@
import '@xyflow/svelte/dist/style.css'; import '@xyflow/svelte/dist/style.css';
const nodes = writable<Node[]>([ const initialNodes = [
{ {
id: '1', id: '1',
type: 'input', type: 'input',
@@ -32,9 +32,11 @@
data: { label: 'Output Node' }, data: { label: 'Output Node' },
position: { x: 300, y: 150 } position: { x: 300, y: 150 }
} }
]); ];
const edges = writable([ let nodes = $state.raw<Node[]>(initialNodes);
let edges = $state.raw([
{ {
id: '1-2', id: '1-2',
type: 'default', type: 'default',
@@ -50,7 +52,7 @@
} }
]); ]);
const svelteFlow = useSvelteFlow(); const { screenToFlowPosition } = $derived(useSvelteFlow());
const onDragOver = (event: DragEvent) => { const onDragOver = (event: DragEvent) => {
event.preventDefault(); event.preventDefault();
@@ -68,7 +70,7 @@
} }
const type = event.dataTransfer.getData('application/svelteflow') || 'default'; const type = event.dataTransfer.getData('application/svelteflow') || 'default';
const position = svelteFlow.screenToFlowPosition({ const position = screenToFlowPosition({
x: event.clientX, x: event.clientX,
y: event.clientY y: event.clientY
}); });
@@ -79,15 +81,14 @@
data: { label: `${type} node` } data: { label: `${type} node` }
}; };
$nodes.push(newNode); nodes = [...nodes, newNode];
$nodes = $nodes;
}; };
$inspect($nodes); $inspect(nodes);
</script> </script>
<main> <main>
<SvelteFlow {nodes} {edges} fitView on:dragover={onDragOver} on:drop={onDrop}> <SvelteFlow bind:nodes bind:edges fitView ondragover={onDragOver} ondrop={onDrop}>
<Controls /> <Controls />
<Background variant={BackgroundVariant.Dots} /> <Background variant={BackgroundVariant.Dots} />
<MiniMap /> <MiniMap />
@@ -10,7 +10,7 @@
event.dataTransfer.effectAllowed = 'move'; event.dataTransfer.effectAllowed = 'move';
}; };
const { zoomIn, zoomOut, fitView, viewport, nodes } = useSvelteFlow(); // const { zoomIn, zoomOut, fitView, viewport, nodes } = $derived(useSvelteFlow());
</script> </script>
<aside> <aside>
@@ -1,4 +1,3 @@
import { get } from 'svelte/store';
import { errorMessages } from '@xyflow/system'; import { errorMessages } from '@xyflow/system';
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
+1 -1
View File
@@ -33,7 +33,7 @@ export * from '$lib/utils';
//hooks //hooks
export * from '$lib/hooks/useSvelteFlow'; export * from '$lib/hooks/useSvelteFlow';
export * from '$lib/hooks/useUpdateNodeInternals'; export * from '$lib/hooks/useUpdateNodeInternals';
export * from '$lib/hooks/useConnection.svelte'; export * from '$lib/hooks/useConnection';
export * from '$lib/hooks/useNodesEdges'; export * from '$lib/hooks/useNodesEdges';
export * from '$lib/hooks/useHandleConnections'; export * from '$lib/hooks/useHandleConnections';
export * from '$lib/hooks/useNodesData'; export * from '$lib/hooks/useNodesData';