NodeChangeset
A NodeChangeset is what Svelte Flow passes to your onnodeschange
handler whenever nodes change. It holds the NodeChange objects that are
about to be applied. Svelte Flow calls your handler before it applies the changes and then applies whatever
is left in the changeset to your nodes.
<script lang="ts">
import { SvelteFlow, type Node, type Edge, type NodeChangeset } from '@xyflow/svelte';
let nodes = $state.raw<Node[]>([]);
let edges = $state.raw<Edge[]>([]);
function onnodeschange(changes: NodeChangeset<Node>) {
console.log(changes.toArray());
}
</script>
<SvelteFlow bind:nodes bind:edges {onnodeschange} />Reading changes
A changeset is iterable, and it can hand you changes by type or by node — so inspecting an update doesn’t mean scanning an array yourself.
// every change, in order
for (const change of changes) {
console.log(change.type, change.id);
}
// just the removals
if (changes.getByType('remove').length > 0) {
saveUndoSnapshot();
}
// everything queued for one node
const changesForNode = changes.getForNode('node-1');Editing changes
Because the changeset is a real object, you can drop or add changes inside your handler before Svelte Flow applies them — handy for vetoing an update without rebuilding the array.
function onnodeschange(changes: NodeChangeset<Node>) {
// ignore anything that would move or delete the locked node
changes.removeForNode('locked-node');
}Applying changes yourself
If you want to apply the changes yourself, use applyTo() and call clear() afterwards, so that Svelte Flow
doesn’t apply them a second time.
function onnodeschange(changes: NodeChangeset<Node>) {
nodes = changes.applyTo(nodes);
changes.clear();
}Notes
- Construct one yourself with
new NodeChangeset(changes?)— for example to replay recorded changes. - Edges have the mirrored
EdgeChangeset.