1- import { Position , Node } from "@xyflow/react" ;
1+ import { Position , Node , XYPosition } from "@xyflow/react" ;
22
3- export const getHandlePosition = ( node : Node , handleId : Position ) => {
3+ const getAbsolutePosition = ( node : Node , nodes : Node [ ] ) : XYPosition => {
4+ if ( ! node . parentId ) {
5+ return node . position ;
6+ }
7+
8+ const parentNode = nodes . find ( ( n ) => n . id === node . parentId ) ;
9+ if ( ! parentNode ) {
10+ return node . position ;
11+ }
12+
13+ const parentPosition : XYPosition = getAbsolutePosition ( parentNode , nodes ) ;
14+ return {
15+ x : parentPosition . x + node . position . x ,
16+ y : parentPosition . y + node . position . y ,
17+ } ;
18+ } ;
19+
20+ export const getHandlePosition = (
21+ node : Node ,
22+ handleId : Position ,
23+ nodes : Node [ ] ,
24+ ) => {
425 const nodeElement = document . querySelector ( `[data-id="${ node . id } "]` ) ;
526 const nodeRect = nodeElement ! . getBoundingClientRect ( ) ;
627 const nodeWidth = nodeRect . width ;
728 const nodeHeight = nodeRect . height ;
829
30+ const absolutePosition = getAbsolutePosition ( node , nodes ) ;
31+
932 const positions = {
1033 [ Position . Left ] : {
11- x : node . position . x ,
12- y : node . position . y + nodeHeight / 2 ,
34+ x : absolutePosition . x ,
35+ y : absolutePosition . y + nodeHeight / 2 ,
1336 } ,
1437 [ Position . Right ] : {
15- x : node . position . x + nodeWidth ,
16- y : node . position . y + nodeHeight / 2 ,
38+ x : absolutePosition . x + nodeWidth ,
39+ y : absolutePosition . y + nodeHeight / 2 ,
1740 } ,
1841 [ Position . Top ] : {
19- x : node . position . x + nodeWidth / 2 ,
20- y : node . position . y ,
42+ x : absolutePosition . x + nodeWidth / 2 ,
43+ y : absolutePosition . y ,
2144 } ,
2245 [ Position . Bottom ] : {
23- x : node . position . x + nodeWidth / 2 ,
24- y : node . position . y + nodeHeight ,
46+ x : absolutePosition . x + nodeWidth / 2 ,
47+ y : absolutePosition . y + nodeHeight ,
2548 } ,
2649 } ;
2750
2851 return positions [ handleId ] ;
2952} ;
3053
31- export const calculateBestHandles = ( sourceNode : Node , targetNode : Node ) => {
54+ export const calculateBestHandles = (
55+ sourceNode : Node ,
56+ targetNode : Node ,
57+ nodes : Node [ ] ,
58+ ) => {
3259 const handlePositions = [
3360 Position . Left ,
3461 Position . Right ,
@@ -42,9 +69,9 @@ export const calculateBestHandles = (sourceNode: Node, targetNode: Node) => {
4269 } ;
4370
4471 handlePositions . forEach ( ( sourceHandle ) => {
45- const sourcePosition = getHandlePosition ( sourceNode , sourceHandle ) ;
72+ const sourcePosition = getHandlePosition ( sourceNode , sourceHandle , nodes ) ;
4673 handlePositions . forEach ( ( targetHandle ) => {
47- const targetPosition = getHandlePosition ( targetNode , targetHandle ) ;
74+ const targetPosition = getHandlePosition ( targetNode , targetHandle , nodes ) ;
4875 const distance = Math . hypot (
4976 sourcePosition . x - targetPosition . x ,
5077 sourcePosition . y - targetPosition . y ,
0 commit comments