1- import { Position , Node , XYPosition } from "@xyflow/react" ;
1+ import { Position , Node } from "@xyflow/react" ;
22
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- ) => {
3+ export const getHandlePosition = ( node : Node , handleId : Position ) => {
254 const nodeElement = document . querySelector ( `[data-id="${ node . id } "]` ) ;
265 const nodeRect = nodeElement ! . getBoundingClientRect ( ) ;
276 const nodeWidth = nodeRect . width ;
287 const nodeHeight = nodeRect . height ;
298
30- const absolutePosition = getAbsolutePosition ( node , nodes ) ;
31-
329 const positions = {
3310 [ Position . Left ] : {
34- x : absolutePosition . x ,
35- y : absolutePosition . y + nodeHeight / 2 ,
11+ x : node . position . x ,
12+ y : node . position . y + nodeHeight / 2 ,
3613 } ,
3714 [ Position . Right ] : {
38- x : absolutePosition . x + nodeWidth ,
39- y : absolutePosition . y + nodeHeight / 2 ,
15+ x : node . position . x + nodeWidth ,
16+ y : node . position . y + nodeHeight / 2 ,
4017 } ,
4118 [ Position . Top ] : {
42- x : absolutePosition . x + nodeWidth / 2 ,
43- y : absolutePosition . y ,
19+ x : node . position . x + nodeWidth / 2 ,
20+ y : node . position . y ,
4421 } ,
4522 [ Position . Bottom ] : {
46- x : absolutePosition . x + nodeWidth / 2 ,
47- y : absolutePosition . y + nodeHeight ,
23+ x : node . position . x + nodeWidth / 2 ,
24+ y : node . position . y + nodeHeight ,
4825 } ,
4926 } ;
5027
5128 return positions [ handleId ] ;
5229} ;
5330
54- export const calculateBestHandles = (
55- sourceNode : Node ,
56- targetNode : Node ,
57- nodes : Node [ ] ,
58- ) => {
31+ export const calculateBestHandles = ( sourceNode : Node , targetNode : Node ) => {
5932 const handlePositions = [
6033 Position . Left ,
6134 Position . Right ,
@@ -69,9 +42,9 @@ export const calculateBestHandles = (
6942 } ;
7043
7144 handlePositions . forEach ( ( sourceHandle ) => {
72- const sourcePosition = getHandlePosition ( sourceNode , sourceHandle , nodes ) ;
45+ const sourcePosition = getHandlePosition ( sourceNode , sourceHandle ) ;
7346 handlePositions . forEach ( ( targetHandle ) => {
74- const targetPosition = getHandlePosition ( targetNode , targetHandle , nodes ) ;
47+ const targetPosition = getHandlePosition ( targetNode , targetHandle ) ;
7548 const distance = Math . hypot (
7649 sourcePosition . x - targetPosition . x ,
7750 sourcePosition . y - targetPosition . y ,
0 commit comments