Skip to content

Commit

Permalink
update absolute positions before initial fitView
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkogo committed Sep 19, 2024
1 parent ce51e02 commit f1118df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/svelte/src/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
type UpdateConnection,
type ConnectionState,
type NodeOrigin,
getFitViewNodes
getFitViewNodes,
updateAbsolutePositions
} from '@xyflow/system';

import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
Expand Down Expand Up @@ -96,6 +97,7 @@ export function createStore({

function updateNodeInternals(updates: Map<string, InternalNodeUpdate>) {
const nodeLookup = get(store.nodeLookup);
const parentLookup = get(store.parentLookup);
const { changes, updatedInternals } = updateNodeInternalsSystem(
updates,
nodeLookup,
Expand All @@ -108,6 +110,8 @@ export function createStore({
return;
}

updateAbsolutePositions(nodeLookup, parentLookup, { nodeOrigin, nodeExtent });

if (!get(store.fitViewOnInitDone) && get(store.fitViewOnInit)) {
const fitViewOptions = get(store.fitViewOptions);
const fitViewOnInitDone = fitViewSync({
Expand Down
11 changes: 7 additions & 4 deletions packages/system/src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ export function updateAbsolutePositions<NodeType extends NodeBase>(
) {
const _options = mergeObjects(defaultOptions, options);
for (const node of nodeLookup.values()) {
if (!node.parentId) {
continue;
if (node.parentId) {
updateChildNode(node, nodeLookup, parentLookup, _options);
} else {
const positionWithOrigin = getNodePositionWithOrigin(node, _options.nodeOrigin);
const extent = isCoordinateExtent(node.extent) ? node.extent : _options.nodeExtent;
const clampedPosition = clampPosition(positionWithOrigin, extent, getNodeDimensions(node));
node.internals.positionAbsolute = clampedPosition;
}

updateChildNode(node, nodeLookup, parentLookup, _options);
}
}

Expand Down

0 comments on commit f1118df

Please sign in to comment.