Merge pull request #4569 from xyflow/fix-intersections

Fix intersections in Svelte Flow
This commit is contained in:
Moritz Klack
2024-08-19 15:22:34 +02:00
committed by GitHub
4 changed files with 39 additions and 14 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@xyflow/svelte': patch
---
Fix getIntersectingNodes for subflows in Svelte Flow
@@ -13,12 +13,14 @@
const { getIntersectingNodes } = useSvelteFlow(); const { getIntersectingNodes } = useSvelteFlow();
function onNodeDrag({ detail: { targetNode } }) { function onNodeDrag({ detail: { targetNode } }) {
const intersections = getIntersectingNodes(targetNode).map((n) => n.id); if (targetNode) {
const intersections = getIntersectingNodes(targetNode).map((n) => n.id);
$nodes.forEach((n) => { $nodes.forEach((n) => {
n.class = intersections.includes(n.id) ? 'highlight' : ''; n.class = intersections.includes(n.id) ? 'highlight' : '';
}); });
$nodes = $nodes; $nodes = $nodes;
}
} }
</script> </script>
@@ -10,7 +10,8 @@ export const initialNodes: Node[] = [
{ {
id: '2', id: '2',
data: { label: 'Node 2' }, data: { label: 'Node 2' },
position: { x: 0, y: 150 } position: { x: 0, y: 150 },
parentId: '1'
}, },
{ {
id: '3', id: '3',
+25 -8
View File
@@ -14,7 +14,7 @@ import {
getViewportForBounds, getViewportForBounds,
getElementsToRemove, getElementsToRemove,
rendererPointToPoint, rendererPointToPoint,
nodeHasDimensions evaluateAbsolutePosition
} from '@xyflow/system'; } from '@xyflow/system';
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
@@ -247,15 +247,32 @@ export function useSvelteFlow(): {
edges, edges,
domNode, domNode,
nodeLookup, nodeLookup,
edgeLookup edgeLookup,
nodeOrigin
} = useStore(); } = useStore();
const getNodeRect = (nodeOrRect: Node | { id: Node['id'] }): Rect | null => { const getNodeRect = (node: Node | { id: Node['id'] }): Rect | null => {
const node = const $nodeLookup = get(nodeLookup);
isNode(nodeOrRect) && nodeHasDimensions(nodeOrRect) const nodeToUse = isNode(node) ? node : $nodeLookup.get(node.id)!;
? nodeOrRect const position = nodeToUse.parentId
: get(nodeLookup).get(nodeOrRect.id); ? evaluateAbsolutePosition(
return node ? nodeToRect(node) : null; nodeToUse.position,
nodeToUse.measured,
nodeToUse.parentId,
$nodeLookup,
get(nodeOrigin)
)
: nodeToUse.position;
const nodeWithPosition = {
id: nodeToUse.id,
position,
width: nodeToUse.measured?.width ?? nodeToUse.width,
height: nodeToUse.measured?.height ?? nodeToUse.height,
data: nodeToUse.data
};
return nodeToRect(nodeWithPosition);
}; };
const updateNode = ( const updateNode = (