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

View File

@@ -56,11 +56,11 @@
};
nodes.push(newNode);
edges.push({
source: connectingNodeId,
target: id,
id: `${connectingNodeId}--${id}`
});
// edges.push({
// source: connectingNodeId,
// target: id,
// id: `${connectingNodeId}--${id}`
// });
}
};
</script>

View File

@@ -13,7 +13,7 @@
import '@xyflow/svelte/dist/style.css';
const nodes = writable<Node[]>([
const initialNodes = [
{
id: '1',
type: 'input',
@@ -32,9 +32,11 @@
data: { label: 'Output Node' },
position: { x: 300, y: 150 }
}
]);
];
const edges = writable([
let nodes = $state.raw<Node[]>(initialNodes);
let edges = $state.raw([
{
id: '1-2',
type: 'default',
@@ -50,7 +52,7 @@
}
]);
const svelteFlow = useSvelteFlow();
const { screenToFlowPosition } = $derived(useSvelteFlow());
const onDragOver = (event: DragEvent) => {
event.preventDefault();
@@ -68,7 +70,7 @@
}
const type = event.dataTransfer.getData('application/svelteflow') || 'default';
const position = svelteFlow.screenToFlowPosition({
const position = screenToFlowPosition({
x: event.clientX,
y: event.clientY
});
@@ -79,15 +81,14 @@
data: { label: `${type} node` }
};
$nodes.push(newNode);
$nodes = $nodes;
nodes = [...nodes, newNode];
};
$inspect($nodes);
$inspect(nodes);
</script>
<main>
<SvelteFlow {nodes} {edges} fitView on:dragover={onDragOver} on:drop={onDrop}>
<SvelteFlow bind:nodes bind:edges fitView ondragover={onDragOver} ondrop={onDrop}>
<Controls />
<Background variant={BackgroundVariant.Dots} />
<MiniMap />

View File

@@ -10,7 +10,7 @@
event.dataTransfer.effectAllowed = 'move';
};
const { zoomIn, zoomOut, fitView, viewport, nodes } = useSvelteFlow();
// const { zoomIn, zoomOut, fitView, viewport, nodes } = $derived(useSvelteFlow());
</script>
<aside>

View File

@@ -1,4 +1,3 @@
import { get } from 'svelte/store';
import { errorMessages } from '@xyflow/system';
import { useStore } from '$lib/store';

View File

@@ -33,7 +33,7 @@ export * from '$lib/utils';
//hooks
export * from '$lib/hooks/useSvelteFlow';
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/useHandleConnections';
export * from '$lib/hooks/useNodesData';