fixed node extent flashing
This commit is contained in:
@@ -41,7 +41,7 @@ export function NodeWrapper<NodeType extends Node>({
|
|||||||
nodeClickDistance,
|
nodeClickDistance,
|
||||||
onError,
|
onError,
|
||||||
}: NodeWrapperProps<NodeType>) {
|
}: NodeWrapperProps<NodeType>) {
|
||||||
const { node, internals, isParent, origin } = useStore((s) => {
|
const { node, internals, isParent } = useStore((s) => {
|
||||||
const node = s.nodeLookup.get(id)! as InternalNode<NodeType>;
|
const node = s.nodeLookup.get(id)! as InternalNode<NodeType>;
|
||||||
const isParent = s.parentLookup.has(id);
|
const isParent = s.parentLookup.has(id);
|
||||||
|
|
||||||
@@ -49,7 +49,6 @@ export function NodeWrapper<NodeType extends Node>({
|
|||||||
node,
|
node,
|
||||||
internals: node.internals,
|
internals: node.internals,
|
||||||
isParent,
|
isParent,
|
||||||
origin: node.origin ?? s.nodeOrigin,
|
|
||||||
};
|
};
|
||||||
}, shallow);
|
}, shallow);
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ const createStore = ({
|
|||||||
fitViewOnInitOptions,
|
fitViewOnInitOptions,
|
||||||
domNode,
|
domNode,
|
||||||
nodeOrigin,
|
nodeOrigin,
|
||||||
|
nodeExtent,
|
||||||
debug,
|
debug,
|
||||||
fitViewSync,
|
fitViewSync,
|
||||||
} = get();
|
} = get();
|
||||||
@@ -104,7 +105,8 @@ const createStore = ({
|
|||||||
nodeLookup,
|
nodeLookup,
|
||||||
parentLookup,
|
parentLookup,
|
||||||
domNode,
|
domNode,
|
||||||
nodeOrigin
|
nodeOrigin,
|
||||||
|
nodeExtent
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!updatedInternals) {
|
if (!updatedInternals) {
|
||||||
@@ -286,23 +288,10 @@ const createStore = ({
|
|||||||
triggerEdgeChanges(edgeChanges);
|
triggerEdgeChanges(edgeChanges);
|
||||||
},
|
},
|
||||||
setNodeExtent: (nodeExtent) => {
|
setNodeExtent: (nodeExtent) => {
|
||||||
const { nodeLookup } = get();
|
|
||||||
|
|
||||||
for (const [, node] of nodeLookup) {
|
|
||||||
const positionAbsolute = clampPosition(node.internals.positionAbsolute, nodeExtent, node.measured);
|
|
||||||
|
|
||||||
nodeLookup.set(node.id, {
|
|
||||||
...node,
|
|
||||||
internals: {
|
|
||||||
...node.internals,
|
|
||||||
positionAbsolute,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
set({
|
set({
|
||||||
nodeExtent,
|
nodeExtent,
|
||||||
});
|
});
|
||||||
|
get().setNodes(get().nodes);
|
||||||
},
|
},
|
||||||
panBy: (delta): Promise<boolean> => {
|
panBy: (delta): Promise<boolean> => {
|
||||||
const { transform, width, height, panZoom, translateExtent } = get();
|
const { transform, width, height, panZoom, translateExtent } = get();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { infiniteExtent } from '..';
|
import { Dimensions, infiniteExtent } from '..';
|
||||||
import {
|
import {
|
||||||
NodeBase,
|
NodeBase,
|
||||||
CoordinateExtent,
|
CoordinateExtent,
|
||||||
@@ -153,6 +153,24 @@ function updateChildPosition<NodeType extends NodeBase>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clampPositionToParent<NodeType extends NodeBase>(
|
||||||
|
childPosition: XYPosition,
|
||||||
|
childDimensions: Dimensions,
|
||||||
|
parent: InternalNodeBase<NodeType>
|
||||||
|
) {
|
||||||
|
const parentDimensions = getNodeDimensions(parent);
|
||||||
|
const parentPosition = parent.internals.positionAbsolute;
|
||||||
|
|
||||||
|
return clampPosition(
|
||||||
|
childPosition,
|
||||||
|
[
|
||||||
|
[parentPosition.x, parentPosition.y],
|
||||||
|
[parentPosition.x + parentDimensions.width, parentPosition.y + parentDimensions.height],
|
||||||
|
],
|
||||||
|
childDimensions
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function calculateZ(node: NodeBase, selectedNodeZ: number) {
|
function calculateZ(node: NodeBase, selectedNodeZ: number) {
|
||||||
return (isNumeric(node.zIndex) ? node.zIndex : 0) + (node.selected ? selectedNodeZ : 0);
|
return (isNumeric(node.zIndex) ? node.zIndex : 0) + (node.selected ? selectedNodeZ : 0);
|
||||||
}
|
}
|
||||||
@@ -175,15 +193,7 @@ function calculateChildXYZ<NodeType extends NodeBase>(
|
|||||||
let absolutePosition = { x: parentPosition.x + position.x, y: parentPosition.y + position.y };
|
let absolutePosition = { x: parentPosition.x + position.x, y: parentPosition.y + position.y };
|
||||||
|
|
||||||
if (childNode.extent === 'parent') {
|
if (childNode.extent === 'parent') {
|
||||||
const parentDimensions = getNodeDimensions(parentNode);
|
absolutePosition = clampPositionToParent(absolutePosition, childDimensions, parentNode);
|
||||||
absolutePosition = clampPosition(
|
|
||||||
absolutePosition,
|
|
||||||
[
|
|
||||||
[parentPosition.x, parentPosition.y],
|
|
||||||
[parentPosition.x + parentDimensions.width, parentPosition.y + parentDimensions.height],
|
|
||||||
],
|
|
||||||
childDimensions
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const childZ = calculateZ(childNode, selectedNodeZ);
|
const childZ = calculateZ(childNode, selectedNodeZ);
|
||||||
@@ -287,7 +297,8 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
|
|||||||
nodeLookup: NodeLookup<NodeType>,
|
nodeLookup: NodeLookup<NodeType>,
|
||||||
parentLookup: ParentLookup<NodeType>,
|
parentLookup: ParentLookup<NodeType>,
|
||||||
domNode: HTMLElement | null,
|
domNode: HTMLElement | null,
|
||||||
nodeOrigin?: NodeOrigin
|
nodeOrigin?: NodeOrigin,
|
||||||
|
nodeExtent?: CoordinateExtent
|
||||||
): { changes: (NodeDimensionChange | NodePositionChange)[]; updatedInternals: boolean } {
|
): { changes: (NodeDimensionChange | NodePositionChange)[]; updatedInternals: boolean } {
|
||||||
const viewportNode = domNode?.querySelector('.xyflow__viewport');
|
const viewportNode = domNode?.querySelector('.xyflow__viewport');
|
||||||
let updatedInternals = false;
|
let updatedInternals = false;
|
||||||
@@ -325,11 +336,22 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
|
|||||||
|
|
||||||
if (doUpdate) {
|
if (doUpdate) {
|
||||||
const nodeBounds = update.nodeElement.getBoundingClientRect();
|
const nodeBounds = update.nodeElement.getBoundingClientRect();
|
||||||
|
let extent = isCoordinateExtent(node.extent) ? node.extent : nodeExtent;
|
||||||
|
let positionAbsolute = node.internals.positionAbsolute;
|
||||||
|
if (node.parentId && node.extent === 'parent') {
|
||||||
|
positionAbsolute = clampPositionToParent(
|
||||||
|
positionAbsolute,
|
||||||
|
getNodeDimensions(node),
|
||||||
|
nodeLookup.get(node.parentId)!
|
||||||
|
);
|
||||||
|
} else if (extent) {
|
||||||
|
positionAbsolute = clampPosition(positionAbsolute, extent, dimensions);
|
||||||
|
}
|
||||||
|
|
||||||
node.measured = dimensions;
|
node.measured = dimensions;
|
||||||
node.internals = {
|
node.internals = {
|
||||||
...node.internals,
|
...node.internals,
|
||||||
positionAbsolute: getNodePositionWithOrigin(node, nodeOrigin),
|
positionAbsolute,
|
||||||
handleBounds: {
|
handleBounds: {
|
||||||
source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id),
|
source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id),
|
||||||
target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id),
|
target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id),
|
||||||
|
|||||||
Reference in New Issue
Block a user