1+ import { NODE_DIMENSIONS } from "src/constants/graph" ;
12import useConfig from "src/store/useConfig" ;
23
34type Text = string | [ string , string ] [ ] ;
@@ -24,24 +25,23 @@ const calculateWidthAndHeight = (str: string, single = false) => {
2425 if ( ! str ) return { width : 45 , height : 45 } ;
2526
2627 const dummyElement = document . createElement ( "div" ) ;
27-
2828 dummyElement . style . whiteSpace = single ? "nowrap" : "pre-wrap" ;
2929 dummyElement . innerHTML = str ;
3030 dummyElement . style . fontSize = "12px" ;
3131 dummyElement . style . width = "fit-content" ;
32- dummyElement . style . height = "fit-content" ;
33- dummyElement . style . padding = "10px" ;
32+ dummyElement . style . padding = "0 10px" ;
3433 dummyElement . style . fontWeight = "500" ;
35- dummyElement . style . overflowWrap = "break-word" ;
3634 dummyElement . style . fontFamily = "monospace" ;
3735 document . body . appendChild ( dummyElement ) ;
3836
3937 const clientRect = dummyElement . getBoundingClientRect ( ) ;
38+ const lines = str . split ( "\n" ) . length ;
39+
4040 const width = clientRect . width + 4 ;
41- const height = clientRect . height ;
41+ // Use parent height for single line nodes that are parents
42+ const height = single ? NODE_DIMENSIONS . PARENT_HEIGHT : lines * NODE_DIMENSIONS . ROW_HEIGHT ;
4243
4344 document . body . removeChild ( dummyElement ) ;
44-
4545 return { width, height } ;
4646} ;
4747
@@ -59,7 +59,6 @@ export const calculateNodeSize = (text: Text, isParent = false) => {
5959 // check cache if data already exists
6060 if ( sizeCache . has ( cacheKey ) ) {
6161 const size = sizeCache . get ( cacheKey ) ;
62-
6362 if ( size ) return size ;
6463 }
6564
@@ -71,7 +70,7 @@ export const calculateNodeSize = (text: Text, isParent = false) => {
7170 sizes . height = 80 ;
7271 }
7372
74- if ( isParent ) sizes . width += 100 ;
73+ if ( isParent ) sizes . width += 80 ;
7574 if ( sizes . width > 700 ) sizes . width = 700 ;
7675
7776 sizeCache . set ( cacheKey , sizes ) ;
0 commit comments