feat(svelte) add fitViewOptions, closes #3440

This commit is contained in:
Peter
2023-09-25 12:58:04 +02:00
parent 339df4c651
commit ada5b94673
9 changed files with 71 additions and 44 deletions
+29 -25
View File
@@ -90,13 +90,39 @@ export function createStore(): SvelteFlowStore {
return;
}
const fitViewOnInitDone =
get(store.fitViewOnInitDone) || (get(store.fitViewOnInit) && fitView({ nodes: nextNodes }));
if (!get(store.fitViewOnInitDone) && get(store.fitViewOnInit)) {
const fitViewOptions = get(store.fitViewOptions);
const fitViewOnInitDone = fitView(nextNodes, {
...fitViewOptions,
nodes: fitViewOptions?.nodes || nextNodes
});
store.fitViewOnInitDone.set(fitViewOnInitDone);
}
store.fitViewOnInitDone.set(fitViewOnInitDone);
store.nodes.set(nextNodes);
}
function fitView(nodes: Node[], options?: FitViewOptions) {
const panZoom = get(store.panZoom);
if (!panZoom) {
return false;
}
return fitViewUtil(
{
nodes,
width: get(store.width),
height: get(store.height),
minZoom: get(store.minZoom),
maxZoom: get(store.maxZoom),
panZoom,
nodeOrigin: get(store.nodeOrigin)
},
options
);
}
function zoomBy(factor: number, options?: ViewportHelperFunctionOptions) {
const panZoom = get(store.panZoom);
@@ -140,28 +166,6 @@ export function createStore(): SvelteFlowStore {
}
}
function fitView(options?: FitViewOptions) {
const panZoom = get(store.panZoom);
const fitViewNodes = options?.nodes || get(store.nodes);
if (!panZoom) {
return false;
}
return fitViewUtil(
{
nodes: fitViewNodes as Node[],
width: get(store.width),
height: get(store.height),
minZoom: get(store.minZoom),
maxZoom: get(store.maxZoom),
panZoom,
nodeOrigin: get(store.nodeOrigin)
},
{}
);
}
function resetSelectedItem<T extends Node | Edge>(item: T) {
if (item.selected) {
return {
@@ -24,7 +24,14 @@ import BezierEdge from '$lib/components/edges/BezierEdge.svelte';
import StraightEdge from '$lib/components/edges/StraightEdge.svelte';
import SmoothStepEdge from '$lib/components/edges/SmoothStepEdge.svelte';
import StepEdge from '$lib/components/edges/StepEdge.svelte';
import type { ConnectionData, NodeTypes, EdgeTypes, EdgeLayouted, Node } from '$lib/types';
import type {
ConnectionData,
NodeTypes,
EdgeTypes,
EdgeLayouted,
Node,
FitViewOptions
} from '$lib/types';
import { createNodesStore, createEdgesStore } from './utils';
export const initConnectionData = {
@@ -64,6 +71,7 @@ export const getInitialStore = () => ({
autoPanOnConnect: writable<boolean>(true),
fitViewOnInit: writable<boolean>(false),
fitViewOnInitDone: writable<boolean>(false),
fitViewOptions: writable<FitViewOptions>(undefined),
panZoom: writable<PanZoomInstance | null>(null),
snapGrid: writable<SnapGrid | null>(null),
dragging: writable<boolean>(false),