@@ -214,10 +214,10 @@ export function useLayout<
214214 graph ,
215215 { name, properties : layoutOptions } as LayoutAlgorithmConfiguration ,
216216 layoutData ,
217- context ?. reactFlowRef ?. current ?? undefined
217+ context ?. reactFlowRef
218218 )
219219 . then ( ( ) => {
220- const { arrangedNodes, arrangedEdges } = transferLayout ( graph )
220+ const { arrangedNodes, arrangedEdges } = transferLayout ( graph , context ?. reactFlowRef )
221221 setNodes ( arrangedNodes )
222222 setEdges ( arrangedEdges )
223223 setTimeout ( ( ) => fitView ( ) )
@@ -248,11 +248,6 @@ export interface LayoutSupport {
248248 * Transfers the calculated layout from the graph to lists of nodes and edges.
249249 */
250250 transferLayout : ( graph : IGraph ) => { arrangedNodes : Node [ ] ; arrangedEdges : Edge [ ] }
251- /**
252- * An optional reference to the React Flow element. This reference is particularly useful in scenarios where there
253- * are multiple React Flow elements on the same page or when the flow is encapsulated within a closed shadow DOM.
254- */
255- reactFlowRef ?: RefObject < HTMLElement >
256251}
257252
258253/**
@@ -334,11 +329,19 @@ export interface LayoutSupport {
334329 * )
335330 * }
336331 * ```
337- *
332+ * @param {RefObject<HTMLElement> }[reactFlowRef] An optional reference to the React Flow element. This reference is
333+ * particularly useful in scenarios where there are multiple React Flow elements on the same page or when the flow is
334+ * encapsulated within a closed shadow DOM.
338335 * @returns functions to transfer the data into a graph and get the layout information from the graph.
339336 */
340- export function useLayoutSupport ( ) : LayoutSupport {
341- return { buildGraph, transferLayout }
337+ export function useLayoutSupport ( reactFlowRef ?: RefObject < HTMLElement > ) : LayoutSupport {
338+ return {
339+ buildGraph : useCallback (
340+ ( nodes , edges , zoom ) => buildGraph ( nodes , edges , zoom , reactFlowRef ) ,
341+ [ reactFlowRef ]
342+ ) ,
343+ transferLayout : useCallback ( graph => transferLayout ( graph , reactFlowRef ) , [ reactFlowRef ] )
344+ }
342345}
343346
344347/**
@@ -411,7 +414,7 @@ function buildGraph(
411414 nodes : Node [ ] ,
412415 edges : Edge [ ] ,
413416 zoom : number ,
414- reactFlowRef ?: RefObject < HTMLDivElement >
417+ reactFlowRef ?: RefObject < HTMLElement >
415418) : IGraph {
416419 if ( ! checkLicense ( ) ) {
417420 return new Graph ( )
@@ -455,13 +458,11 @@ function buildGraph(
455458
456459 const graph = builder . buildGraph ( )
457460
461+ const rootElement = getRootNode ( reactFlowRef )
462+
458463 graph . nodes . forEach ( node => {
459464 node . labels . forEach ( ( label , index ) => {
460- const preferredSize = measureLabel (
461- `node-label-${ node . tag . id } -${ index } ` ,
462- zoom ,
463- reactFlowRef ?. current ?? undefined
464- )
465+ const preferredSize = measureLabel ( `node-label-${ node . tag . id } -${ index } ` , zoom , rootElement )
465466 if ( preferredSize . width > 0 && preferredSize . height > 0 ) {
466467 graph . setLabelPreferredSize ( label , preferredSize )
467468 }
@@ -494,11 +495,7 @@ function buildGraph(
494495
495496 graph . edges . forEach ( edge => {
496497 edge . labels . forEach ( ( label , index ) => {
497- const preferredSize = measureLabel (
498- `edge-label-${ edge . tag . id } -${ index } ` ,
499- zoom ,
500- reactFlowRef ?. current ?? undefined
501- )
498+ const preferredSize = measureLabel ( `edge-label-${ edge . tag . id } -${ index } ` , zoom , rootElement )
502499 if ( preferredSize . width > 0 && preferredSize . height > 0 ) {
503500 graph . setLabelPreferredSize ( label , preferredSize )
504501 }
@@ -550,7 +547,10 @@ function buildGraph(
550547 return graph
551548}
552549
553- function transferLayout ( graph : IGraph ) : { arrangedNodes : Node [ ] ; arrangedEdges : Edge [ ] } {
550+ function transferLayout (
551+ graph : IGraph ,
552+ reactFlowRef ?: RefObject < HTMLElement >
553+ ) : { arrangedNodes : Node [ ] ; arrangedEdges : Edge [ ] } {
554554 if ( ! checkLicense ( ) ) {
555555 return { arrangedNodes : [ ] , arrangedEdges : [ ] }
556556 }
@@ -569,6 +569,8 @@ function transferLayout(graph: IGraph): { arrangedNodes: Node[]; arrangedEdges:
569569 }
570570 } )
571571
572+ const rootElement = getRootNode ( reactFlowRef )
573+
572574 const arrangedNodes = graph . nodes
573575 . map ( node => {
574576 const offset = nodeOffsets . get ( node ) ?? { dx : 0 , dy : 0 }
@@ -605,7 +607,7 @@ function transferLayout(graph: IGraph): { arrangedNodes: Node[]; arrangedEdges:
605607 . map ( ( label , index ) => {
606608 const rect = label . layout as OrientedRectangle
607609 const labelId = `node-label-${ node . tag . id } -${ index } `
608- const padding = calculatePadding ( labelId )
610+ const padding = calculatePadding ( labelId , rootElement )
609611 const angle = rect . angle
610612 return {
611613 id : labelId ,
@@ -646,7 +648,7 @@ function transferLayout(graph: IGraph): { arrangedNodes: Node[]; arrangedEdges:
646648 . map ( ( label , index ) => {
647649 const rect = label . layout as OrientedRectangle
648650 const labelId = `edge-label-${ edge . tag . id } -${ index } `
649- const padding = calculatePadding ( labelId )
651+ const padding = calculatePadding ( labelId , rootElement )
650652 return {
651653 id : labelId ,
652654 x : rect . anchorX ,
@@ -745,17 +747,20 @@ function translateSide(side: 'left' | 'right' | 'top' | 'bottom' | 'center' | 'o
745747 }
746748}
747749
748- function measureLabel ( labelId : string , zoom : number , reactFlowElement ?: HTMLDivElement ) : Size {
749- const root = getRootNode ( reactFlowElement )
750- const labelElement = root . querySelector ( `[data-id="${ labelId } "]` ) as HTMLDivElement
750+ function measureLabel (
751+ labelId : string ,
752+ zoom : number ,
753+ rootElement : Element | Document | DocumentFragment
754+ ) : Size {
755+ const labelElement = rootElement . querySelector ( `[data-id="${ labelId } "]` ) as HTMLDivElement
751756 if ( labelElement ) {
752757 const oldTransform = labelElement . style . transform
753758 labelElement . style . transform = ''
754759 const oldBoxSizing = labelElement . style . boxSizing
755760 labelElement . style . boxSizing = 'content-box'
756761 const computedStyle = window . getComputedStyle ( labelElement )
757762 // label padding should also be included in the width/size during the layout
758- const labelPadding = calculatePadding ( labelId )
763+ const labelPadding = calculatePadding ( labelId , rootElement )
759764 let width = parseFloat ( computedStyle . width )
760765 let height = parseFloat ( computedStyle . height )
761766 if ( width <= 0 || height <= 0 ) {
@@ -773,9 +778,8 @@ function measureLabel(labelId: string, zoom: number, reactFlowElement?: HTMLDivE
773778 return Size . ZERO
774779}
775780
776- function calculatePadding ( labelId : string , reactFlowElement ?: HTMLDivElement ) {
777- const root = getRootNode ( reactFlowElement )
778- const element = root . querySelector ( `[data-id="${ labelId } "]` ) as HTMLDivElement
781+ function calculatePadding ( labelId : string , rootElement : Element | Document | DocumentFragment ) {
782+ const element = rootElement . querySelector ( `[data-id="${ labelId } "]` ) as HTMLDivElement
779783 if ( element ) {
780784 const elementStyle = window . getComputedStyle ( element )
781785 return {
0 commit comments