import type { Writable } from 'svelte/store'; import type { CoordinateExtent } from '@xyflow/system'; import type { SvelteFlowStore } from '$lib/store/types'; import type { EdgeTypes, NodeTypes } from '$lib/types'; // this is helper function for updating the store // for props where we need to call a specific store action export function updateStore( store: SvelteFlowStore, { nodeTypes, edgeTypes, minZoom, maxZoom, translateExtent, paneClickDistance }: { nodeTypes?: NodeTypes; edgeTypes?: EdgeTypes; minZoom?: number; maxZoom?: number; translateExtent?: CoordinateExtent; paneClickDistance?: number; } ) { if (nodeTypes !== undefined) { store.setNodeTypes(nodeTypes); } if (edgeTypes !== undefined) { store.setEdgeTypes(edgeTypes); } if (minZoom !== undefined) { store.setMinZoom(minZoom); } if (maxZoom !== undefined) { store.setMaxZoom(maxZoom); } if (translateExtent !== undefined) { store.setTranslateExtent(translateExtent); } if (paneClickDistance !== undefined) { store.setPaneClickDistance(paneClickDistance); } } const getKeys = (obj: T) => Object.keys(obj) as Array; type UnwrapWritable = T extends Writable ? U : T; // @todo there must be a better way to define the types here.. export type UpdatableStoreProps = { flowId?: UnwrapWritable; connectionLineType?: UnwrapWritable; connectionRadius?: UnwrapWritable; selectionMode?: UnwrapWritable; snapGrid?: UnwrapWritable; defaultMarkerColor?: UnwrapWritable; nodesDraggable?: UnwrapWritable; nodesConnectable?: UnwrapWritable; elementsSelectable?: UnwrapWritable; onlyRenderVisibleElements?: UnwrapWritable; isValidConnection?: UnwrapWritable; autoPanOnConnect?: UnwrapWritable; autoPanOnNodeDrag?: UnwrapWritable; connectionMode?: UnwrapWritable; onerror?: UnwrapWritable; ondelete?: UnwrapWritable; onedgecreate?: UnwrapWritable; nodeDragThreshold?: UnwrapWritable; onconnect?: UnwrapWritable; onconnectstart?: UnwrapWritable; onconnectend?: UnwrapWritable; onbeforedelete?: UnwrapWritable; nodeOrigin?: UnwrapWritable; }; export function updateStoreByKeys(store: SvelteFlowStore, keys: UpdatableStoreProps) { getKeys(keys).forEach((prop) => { const update = keys[prop]; if (update !== undefined) { // @ts-expect-error @todo: how to fix this TS error? store[prop].set(update); } }); }