import { readable, writable } from 'svelte/store'; import { infiniteExtent, SelectionMode, ConnectionMode, ConnectionLineType, devWarn, adoptUserNodes, getNodesBounds, getViewportForBounds, updateConnectionLookup, type SelectionRect, type SnapGrid, type MarkerProps, type PanZoomInstance, type CoordinateExtent, type NodeOrigin, type OnError, type Viewport, type ConnectionLookup, type OnConnect, type OnConnectStart, type OnConnectEnd, type NodeLookup, type EdgeLookup } from '@xyflow/system'; import DefaultNode from '$lib/components/nodes/DefaultNode.svelte'; import InputNode from '$lib/components/nodes/InputNode.svelte'; import OutputNode from '$lib/components/nodes/OutputNode.svelte'; import GroupNode from '$lib/components/nodes/GroupNode.svelte'; import { BezierEdgeInternal, SmoothStepEdgeInternal, StraightEdgeInternal, StepEdgeInternal } from '$lib/components/edges'; import type { NodeTypes, EdgeTypes, EdgeLayouted, Node, Edge, FitViewOptions, OnDelete, OnEdgeCreate, OnBeforeDelete, IsValidConnection, InternalNode } from '$lib/types'; import { createNodesStore, createEdgesStore } from './utils'; import { initConnectionProps, type ConnectionProps } from './derived-connection-props'; export const initialNodeTypes = { input: InputNode, output: OutputNode, default: DefaultNode, group: GroupNode }; export const initialEdgeTypes = { straight: StraightEdgeInternal, smoothstep: SmoothStepEdgeInternal, default: BezierEdgeInternal, step: StepEdgeInternal }; export const getInitialStore = ({ nodes = [], edges = [], width, height, fitView }: { nodes?: Node[]; edges?: Edge[]; width?: number; height?: number; fitView?: boolean; }) => { const nodeLookup: NodeLookup = new Map(); const parentLookup = new Map(); adoptUserNodes(nodes, nodeLookup, parentLookup, { nodeOrigin: [0, 0], elevateNodesOnSelect: false, checkEquality: false }); const connectionLookup = new Map(); const edgeLookup = new Map(); updateConnectionLookup(connectionLookup, edgeLookup, edges); let viewport: Viewport = { x: 0, y: 0, zoom: 1 }; if (fitView && width && height) { const nodesWithDimensions = nodes.filter( (node) => (node.width && node.height) || (node.initialWidth && node.initialHeight) ); // @todo users nodeOrigin should be used here const bounds = getNodesBounds(nodesWithDimensions, { nodeOrigin: [0, 0] }); viewport = getViewportForBounds(bounds, width, height, 0.5, 2, 0.1); } return { flowId: writable(null), nodes: createNodesStore(nodes, nodeLookup, parentLookup), nodeLookup: readable>(nodeLookup), parentLookup: readable>(parentLookup), edgeLookup: readable>(edgeLookup), visibleNodes: readable([]), edges: createEdgesStore(edges, connectionLookup, edgeLookup), visibleEdges: readable([]), connectionLookup: readable(connectionLookup), height: writable(500), width: writable(500), minZoom: writable(0.5), maxZoom: writable(2), nodeOrigin: writable([0, 0]), nodeDragThreshold: writable(1), nodeExtent: writable(infiniteExtent), translateExtent: writable(infiniteExtent), autoPanOnNodeDrag: writable(true), autoPanOnConnect: writable(true), fitViewOnInit: writable(false), fitViewOnInitDone: writable(false), fitViewOptions: writable(undefined), panZoom: writable(null), snapGrid: writable(null), dragging: writable(false), selectionRect: writable(null), selectionKeyPressed: writable(false), multiselectionKeyPressed: writable(false), deleteKeyPressed: writable(false), panActivationKeyPressed: writable(false), zoomActivationKeyPressed: writable(false), selectionRectMode: writable(null), selectionMode: writable(SelectionMode.Partial), nodeTypes: writable(initialNodeTypes), edgeTypes: writable(initialEdgeTypes), viewport: writable(viewport), connectionMode: writable(ConnectionMode.Strict), domNode: writable(null), connection: readable(initConnectionProps), connectionLineType: writable(ConnectionLineType.Bezier), connectionRadius: writable(20), isValidConnection: writable(() => true), nodesDraggable: writable(true), nodesConnectable: writable(true), elementsSelectable: writable(true), selectNodesOnDrag: writable(true), markers: readable([]), defaultMarkerColor: writable('#b1b1b7'), lib: readable('svelte'), onlyRenderVisibleElements: writable(false), onerror: writable(devWarn), ondelete: writable(undefined), onedgecreate: writable(undefined), onconnect: writable(undefined), onconnectstart: writable(undefined), onconnectend: writable(undefined), onbeforedelete: writable(undefined), nodesInitialized: writable(false), edgesInitialized: writable(false), viewportInitialized: writable(false), initialized: readable(false) }; };