fix lost handle bounds

This commit is contained in:
peterkogo
2025-01-15 13:19:53 +01:00
parent ce6fcc7a82
commit a989fa9a8b
3 changed files with 50 additions and 48 deletions
@@ -25,8 +25,6 @@
onnodecontextmenu onnodecontextmenu
}: NodeWrapperProps & NodeEvents = $props(); }: NodeWrapperProps & NodeEvents = $props();
// $inspect(node);
let { let {
data = {}, data = {},
selected = false, selected = false,
+1 -1
View File
@@ -51,7 +51,7 @@ export type {
EdgeTypes, EdgeTypes,
DefaultEdgeOptions DefaultEdgeOptions
} from '$lib/types/edges'; } from '$lib/types/edges';
export type { HandleProps, FitViewOptions } from '$lib/types/general'; export type { HandleProps, FitViewOptions, OnBeforeDelete } from '$lib/types/general';
export type { Node, NodeTypes, BuiltInNode, NodeProps, InternalNode } from '$lib/types/nodes'; export type { Node, NodeTypes, BuiltInNode, NodeProps, InternalNode } from '$lib/types/nodes';
export type { SvelteFlowStore } from '$lib/store/types'; export type { SvelteFlowStore } from '$lib/store/types';
export * from '$lib/types/events'; export * from '$lib/types/events';
+49 -45
View File
@@ -87,6 +87,7 @@ export function adoptUserNodes<NodeType extends NodeBase>(
options?: UpdateNodesOptions<NodeType> options?: UpdateNodesOptions<NodeType>
) { ) {
const _options = mergeObjects(adoptUserNodesDefaultOptions, options); const _options = mergeObjects(adoptUserNodesDefaultOptions, options);
const tmpLookup = new Map(nodeLookup); const tmpLookup = new Map(nodeLookup);
const selectedNodeZ: number = _options?.elevateNodesOnSelect ? 1000 : 0; const selectedNodeZ: number = _options?.elevateNodesOnSelect ? 1000 : 0;
@@ -345,59 +346,62 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
}, },
}); });
updatedInternals = true; updatedInternals = true;
} else { continue;
const dimensions = getDimensions(update.nodeElement); }
const dimensionChanged = node.measured.width !== dimensions.width || node.measured.height !== dimensions.height;
const doUpdate = !!(
dimensions.width &&
dimensions.height &&
(dimensionChanged || !node.internals.handleBounds || update.force)
);
if (doUpdate) { const dimensions = getDimensions(update.nodeElement);
const nodeBounds = update.nodeElement.getBoundingClientRect(); const dimensionChanged = node.measured.width !== dimensions.width || node.measured.height !== dimensions.height;
const extent = isCoordinateExtent(node.extent) ? node.extent : nodeExtent; const doUpdate = !!(
let { positionAbsolute } = node.internals; dimensions.width &&
dimensions.height &&
(dimensionChanged || !node.internals.handleBounds || update.force)
);
if (node.parentId && node.extent === 'parent') { if (doUpdate) {
positionAbsolute = clampPositionToParent(positionAbsolute, dimensions, nodeLookup.get(node.parentId)!); const nodeBounds = update.nodeElement.getBoundingClientRect();
} else if (extent) { const extent = isCoordinateExtent(node.extent) ? node.extent : nodeExtent;
positionAbsolute = clampPosition(positionAbsolute, extent, dimensions); let { positionAbsolute } = node.internals;
}
nodeLookup.set(node.id, { if (node.parentId && node.extent === 'parent') {
...node, positionAbsolute = clampPositionToParent(positionAbsolute, dimensions, nodeLookup.get(node.parentId)!);
measured: dimensions, } else if (extent) {
internals: { positionAbsolute = clampPosition(positionAbsolute, extent, dimensions);
...node.internals, }
positionAbsolute,
handleBounds: { const newNode = {
source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id), ...node,
target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id), measured: dimensions,
}, internals: {
...node.internals,
positionAbsolute,
handleBounds: {
source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id),
target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id),
}, },
},
};
nodeLookup.set(node.id, newNode);
if (node.parentId) {
updateChildNode(newNode, nodeLookup, parentLookup, { nodeOrigin });
}
updatedInternals = true;
if (dimensionChanged) {
changes.push({
id: node.id,
type: 'dimensions',
dimensions,
}); });
if (node.parentId) { if (node.expandParent && node.parentId) {
updateChildNode(node, nodeLookup, parentLookup, { nodeOrigin }); parentExpandChildren.push({
}
updatedInternals = true;
if (dimensionChanged) {
changes.push({
id: node.id, id: node.id,
type: 'dimensions', parentId: node.parentId,
dimensions, rect: nodeToRect(newNode, nodeOrigin),
}); });
if (node.expandParent && node.parentId) {
parentExpandChildren.push({
id: node.id,
parentId: node.parentId,
rect: nodeToRect(node, nodeOrigin),
});
}
} }
} }
} }