From e47adb446f480cadef06dd6b24fcd5ee5269bf3f Mon Sep 17 00:00:00 2001 From: peterkogo Date: Thu, 24 Apr 2025 16:32:42 +0200 Subject: [PATCH] make updatenodeinternal node id optional --- .../useupdatenodeinternals/CustomNode.svelte | 2 +- .../src/lib/hooks/useUpdateNodeInternals.svelte.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/svelte/src/routes/examples/useupdatenodeinternals/CustomNode.svelte b/examples/svelte/src/routes/examples/useupdatenodeinternals/CustomNode.svelte index fcbd852f..1b302a8b 100644 --- a/examples/svelte/src/routes/examples/useupdatenodeinternals/CustomNode.svelte +++ b/examples/svelte/src/routes/examples/useupdatenodeinternals/CustomNode.svelte @@ -9,7 +9,7 @@ const onClick = () => { handleCount += 1; - updateNodeInternals(id); + updateNodeInternals(); }; diff --git a/packages/svelte/src/lib/hooks/useUpdateNodeInternals.svelte.ts b/packages/svelte/src/lib/hooks/useUpdateNodeInternals.svelte.ts index 4b5ea5f5..2c515f22 100644 --- a/packages/svelte/src/lib/hooks/useUpdateNodeInternals.svelte.ts +++ b/packages/svelte/src/lib/hooks/useUpdateNodeInternals.svelte.ts @@ -1,6 +1,5 @@ -import type { UpdateNodeInternals } from '@xyflow/system'; - import { useStore } from '$lib/store'; +import { getContext } from 'svelte'; /** * Hook for updating node internals. @@ -8,12 +7,16 @@ import { useStore } from '$lib/store'; * @public * @returns function for updating node internals */ -export function useUpdateNodeInternals(): UpdateNodeInternals { +export function useUpdateNodeInternals(): (nodeId?: string | string[]) => void { const { domNode, updateNodeInternals } = $derived(useStore()); + const nodeId = getContext('svelteflow__node_id') as string | undefined; // @todo: do we want to add this to system? - const updateInternals = (id: string | string[]) => { - const updateIds = Array.isArray(id) ? id : [id]; + const updateInternals = (id?: string | string[]) => { + if (!id && !nodeId) { + throw new Error('When using outside of a node, you must provide an id.'); + } + const updateIds = id ? (Array.isArray(id) ? id : [id]) : [nodeId]; const updates = new Map(); updateIds.forEach((updateId) => {