import type { SvelteFlowStore } from '$lib/store/types'; import type { EdgeTypes, NodeTypes } from '$lib/types'; import type { CoordinateExtent } from '@xyflow/system'; import type { Writable } from 'svelte/store'; // 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 }: { nodeTypes?: NodeTypes; edgeTypes?: EdgeTypes; minZoom?: number; maxZoom?: number; translateExtent?: CoordinateExtent; } ) { 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); } } 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; nodeDragThreshold?: UnwrapWritable; }; export function updateStoreByKeys(store: SvelteFlowStore, keys: UpdatableStoreProps) { getKeys(keys).forEach((prop) => { const update = keys[prop]; if (update !== undefined) { // @todo: how to fix this TS error? // @ts-ignore store[prop].set(update); } }); }