1- import React , { useEffect } from 'react' ;
2- import { Graph , TGraphConfig , GraphState } from "@gravity-ui/graph" ;
3- import { GraphCanvas , GraphBlock , useGraph , useElk , TBlock , TConnection , useGraphEvent , MultipointConnection } from "@gravity-ui/graph/react" ;
4- import ELK , { ElkNode , ElkExtendedEdge } from 'elkjs' ;
5-
6- import type { Data , GraphNode , Options , Shapes } from '@gravity-ui/paranoid' ;
1+ import React , { useEffect , useMemo } from 'react' ;
2+
3+ import type { TBlock , TGraphConfig } from '@gravity-ui/graph' ;
4+ import { Graph , GraphState } from '@gravity-ui/graph' ;
5+ import {
6+ GraphBlock ,
7+ GraphCanvas ,
8+ MultipointConnection ,
9+ TConnection ,
10+ useElk ,
11+ useGraph ,
12+ useGraphEvent ,
13+ } from '@gravity-ui/graph/react' ;
14+ import type { Data , GraphNode , Options , Shapes } from '@gravity-ui/paranoid' ;
15+ import type { ElkExtendedEdge , ElkNode } from 'elkjs' ;
16+ import ELK from 'elkjs' ;
17+
18+ import { prepareBlocks , prepareChildren , prepareConnections , prepareEdges } from './utils' ;
719
820interface Props < T > {
921 data : Data < T > ;
1022}
1123
1224const config = {
1325 settings : {
14- connection : MultipointConnection
15- }
26+ connection : MultipointConnection ,
27+ } ,
1628} ;
1729const elk = new ELK ( ) ;
1830
1931const renderBlockFn = ( graph , block ) => {
20- return < GraphBlock graph = { graph } block = { block } > { block . id } </ GraphBlock > ;
21- } ;
22-
23- const prepareChildren = ( blocks : TGraphConfig [ "blocks" ] ) => {
24- return blocks . map ( ( b ) => {
25- return {
26- id : b . id as string ,
27- width : b . width ,
28- height : b . height ,
29- } satisfies ElkNode ;
30- } ) ;
31- } ;
32-
33- const prepareEdges = ( connections : TGraphConfig [ "connections" ] , skipLabels ?: boolean ) => {
34- return connections . map ( ( c , i ) => {
35- const labelText = `label ${ i } ` ;
36-
37- return {
38- id : c . id as string ,
39- sources : [ c . sourceBlockId as string ] ,
40- targets : [ c . targetBlockId as string ] ,
41- // labels: skipLabels
42- // ? []
43- // : [{ text: labelText, width: measureText(labelText, `${FONT_SIZE}px sans-serif`), height: FONT_SIZE }],
44- } satisfies ElkExtendedEdge ;
45- } ) ;
32+ return (
33+ < GraphBlock graph = { graph } block = { block } >
34+ { block . id }
35+ </ GraphBlock >
36+ ) ;
4637} ;
4738
48-
49-
50- const _blocks = [
51- {
52- width : 200 ,
53- height : 160 ,
54- id : "Left" ,
55- is : "Block" ,
56- selected : false ,
57- name : "Left block" ,
58- anchors : [ ] ,
59- } ,
60- {
61- width : 200 ,
62- height : 160 ,
63- id : "Right" ,
64- is : "Block" ,
65- selected : false ,
66- name : "Right block" ,
67- anchors : [ ] ,
68- }
69- ]
70-
71- const _connections = [
72- {
73- id : "c1" ,
74- sourceBlockId : "Left" ,
75- targetBlockId : "Right" ,
76- }
77- ]
78-
79- const elkConfig = {
80- id : "root" ,
81- children : prepareChildren ( _blocks ) ,
82- edges : prepareEdges ( _connections ) ,
39+ // const _blocks: TBlock[] = [
40+ // {
41+ // width: 200,
42+ // height: 160,
43+ // id: 'Left',
44+ // is: 'block-action',
45+ // selected: false,
46+ // name: 'Left block',
47+ // anchors: [],
48+ // },
49+ // {
50+ // width: 200,
51+ // height: 160,
52+ // id: 'Right',
53+ // is: 'block-action',
54+ // selected: false,
55+ // name: 'Right block',
56+ // anchors: [],
57+ // },
58+ // ];
59+
60+ // const _connections = [
61+ // {
62+ // id: 'c1',
63+ // sourceBlockId: 'Left',
64+ // targetBlockId: 'Right',
65+ // },
66+ // ];
67+
68+ const baseElkConfig = {
69+ id : 'root' ,
8370 layoutOptions : {
84- 'elk.algorithm' : 'mrtree ' ,
71+ 'elk.algorithm' : 'layered ' ,
8572 'elk.direction' : 'DOWN' ,
86- 'elk.spacing.edgeNode' : '50' ,
87- 'elk.spacing.nodeNode' : '50'
88- }
73+ // 'elk.spacing.edgeNode': '50',
74+ 'elk.layered.spacing.nodeNodeBetweenLayers' : '50' ,
75+ 'elk.layered.nodePlacement.bk.fixedAlignment' : 'BALANCED' ,
76+ 'elk.layered.nodePlacement.bk.ordering' : 'INTERACTIVE' ,
77+ 'elk.debugMode' : true
78+ // 'elk.alignment': 'CENTER'
79+ } ,
8980} ;
9081
91- export function GravityGraph < T > ( { data } : Props < T > ) {
92- console . log ( data ) ;
93-
94- const { graph, start } = useGraph ( config ) ;
95- const { isLoading, result } = useElk ( elkConfig , elk ) ;
82+ export function GravityGraph < T > ( { data} : Props < T > ) {
83+ // console.log('997', data);
84+
85+ const _blocks = useMemo ( ( ) => prepareBlocks ( data . nodes ) , [ data . nodes ] ) ;
86+ const _connections = useMemo ( ( ) => prepareConnections ( data . links ) , [ data . links ] ) ;
87+ const elkConfig = useMemo (
88+ ( ) => ( {
89+ ...baseElkConfig ,
90+ children : prepareChildren ( _blocks ) ,
91+ edges : prepareEdges ( _connections ) ,
92+ } ) ,
93+ [ _blocks , _connections ] ,
94+ ) ;
95+ const { graph, start} = useGraph ( config ) ;
96+ const { isLoading, result} = useElk ( elkConfig , elk ) ;
9697
9798 React . useEffect ( ( ) => {
98-
9999 if ( isLoading || ! result ) {
100100 return ;
101101 }
102102
103- console . log ( 'result' , result ) ;
104-
103+ // console.log('result', result);
105104
106105 const blocks = _blocks . map ( ( block ) => {
107106 return {
@@ -120,21 +119,20 @@ export function GravityGraph<T>({ data }: Props<T>) {
120119 return acc ;
121120 } , [ ] ) ;
122121
123- console . log ( 'connections' , connections ) ;
124-
122+ // console.log('connections', connections);
125123
126124 graph . setEntities ( {
127125 blocks,
128126 connections,
129127 } ) ;
130128 } , [ isLoading , result , graph ] ) ;
131129
132- useGraphEvent ( graph , " state-change" , ( { state } ) => {
130+ useGraphEvent ( graph , ' state-change' , ( { state} ) => {
133131 if ( state === GraphState . ATTACHED ) {
134- console . log ( 'start' )
132+ console . log ( 'start' ) ;
135133 start ( ) ;
136134 }
137135 } ) ;
138136
139- return < GraphCanvas graph = { graph } renderBlock = { renderBlockFn } />
137+ return < GraphCanvas graph = { graph } renderBlock = { renderBlockFn } /> ;
140138}
0 commit comments