chore(svelte/nodewrapper): cleanup

This commit is contained in:
moklick
2025-06-05 16:40:16 +02:00
parent d434f0cde7
commit c44f0c6156
4 changed files with 48 additions and 40 deletions
+23 -1
View File
@@ -14,7 +14,8 @@ import {
type ConnectionState,
updateAbsolutePositions,
snapPosition,
calculateNodePosition
calculateNodePosition,
type SetCenterOptions
} from '@xyflow/system';
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
@@ -126,6 +127,26 @@ export function createStore<NodeType extends Node = Node, EdgeType extends Edge
return fitViewResolver.promise;
}
async function setCenter(x: number, y: number, options?: SetCenterOptions) {
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : store.maxZoom;
const currentPanZoom = store.panZoom;
if (!currentPanZoom) {
return Promise.resolve(false);
}
await currentPanZoom.setViewport(
{
x: store.width / 2 - x * nextZoom,
y: store.height / 2 - y * nextZoom,
zoom: nextZoom
},
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
);
return Promise.resolve(true);
}
function zoomBy(factor: number, options?: ViewportHelperFunctionOptions) {
const panZoom = store.panZoom;
if (!panZoom) {
@@ -372,6 +393,7 @@ export function createStore<NodeType extends Node = Node, EdgeType extends Edge
zoomIn,
zoomOut,
fitView,
setCenter,
setMinZoom,
setMaxZoom,
setTranslateExtent,
+3 -1
View File
@@ -6,7 +6,8 @@ import type {
UpdateNodePositions,
CoordinateExtent,
UpdateConnection,
Viewport
Viewport,
SetCenter
} from '@xyflow/system';
import type { getInitialStore } from './initial-store.svelte';
@@ -24,6 +25,7 @@ export type SvelteFlowStoreActions<NodeType extends Node = Node, EdgeType extend
setTranslateExtent: (extent: CoordinateExtent) => void;
setPaneClickDistance: (distance: number) => void;
fitView: (options?: FitViewOptions) => Promise<boolean>;
setCenter: SetCenter;
updateNodePositions: UpdateNodePositions;
updateNodeInternals: (updates: Map<string, InternalNodeUpdate>) => void;
unselectNodesAndEdges: (params?: { nodes?: NodeType[]; edges?: EdgeType[] }) => void;