feat(svelte): add updateNodeInternals

This commit is contained in:
moklick
2023-09-14 19:31:15 +02:00
parent 89610ccd29
commit f919cb152d
8 changed files with 172 additions and 37 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@xyflow/svelte",
"version": "0.0.18",
"version": "0.0.19",
"description": "Svelte Flow - A highly customizable Svelte library for building node-based editors, workflow systems, diagrams and more.",
"keywords": [
"svelte",
@@ -27,7 +27,6 @@ export function useSvelteFlow(): {
nodes: SvelteFlowStore['nodes'];
edges: SvelteFlowStore['edges'];
} {
// how to get the new context here? fit view doesn't work, because the store is not updated (uses old nodes store)
const {
zoomIn,
zoomOut,
@@ -0,0 +1,28 @@
import { get } from 'svelte/store';
import type { UpdateNodeInternals, NodeDimensionUpdate } from '@xyflow/system';
import { useStore } from '$lib/store';
export function useUpdateNodeInternals(): UpdateNodeInternals {
const { domNode, updateNodeDimensions } = useStore();
// @todo: do we want to add this to system?
const updateInternals = (id: string | string[]) => {
const updateIds = Array.isArray(id) ? id : [id];
const updates = updateIds.reduce<NodeDimensionUpdate[]>((res, updateId) => {
const nodeElement = get(domNode)?.querySelector(
`.svelte-flow__node[data-id="${updateId}"]`
) as HTMLDivElement;
if (nodeElement) {
res.push({ id: updateId, nodeElement, forceUpdate: true });
}
return res;
}, []);
requestAnimationFrame(() => updateNodeDimensions(updates));
};
return updateInternals;
}
+1
View File
@@ -20,6 +20,7 @@ export { useStore } from '$lib/store';
// utils
export * from '$lib/utils';
export * from '$lib/hooks/useSvelteFlow';
export * from '$lib/hooks/useUpdateNodeInternals';
// types
export type { Edge, EdgeProps, EdgeTypes, DefaultEdgeOptions } from '$lib/types/edges';