1
+ import React , { useEffect , useRef , useState } from 'react' ;
2
+
3
+ const isBrowser = typeof window !== 'undefined' ;
4
+ const G6 = isBrowser ? window . g6v5 : null ;
5
+
6
+ let graph ;
7
+
8
+ const V5Combo : React . FC = ( ) => {
9
+ const ref = useRef < HTMLDivElement > ( null ) ;
10
+ const [ updateComboStructure , setUpdateComboStructure ] = useState ( false ) ;
11
+ useEffect ( ( ) => {
12
+ graph = new G6 . Graph ( {
13
+ container : ref . current ,
14
+ width : 1000 ,
15
+ height : 1000 ,
16
+ type : 'graph' ,
17
+ node : {
18
+ labelShape : {
19
+ position : 'bottom' ,
20
+ text : {
21
+ fields : [ 'id' ] ,
22
+ formatter : ( model ) => model . id ,
23
+ } ,
24
+ } ,
25
+ labelBackgroundShape : { } ,
26
+ animates : {
27
+ update : [
28
+ {
29
+ fields : [ 'opacity' ] ,
30
+ shapeId : 'haloShape' ,
31
+ } ,
32
+ ] ,
33
+ } ,
34
+ } ,
35
+ combo : ( model ) => {
36
+ return {
37
+ id : model . id ,
38
+ data : {
39
+ ...model . data ,
40
+ keyShape : {
41
+ padding : [ 10 , 10 , 10 , 10 ] ,
42
+ r : 50 ,
43
+ } ,
44
+ labelShape : {
45
+ text : model . id ,
46
+ } ,
47
+
48
+ animates : {
49
+ buildIn : [
50
+ {
51
+ fields : [ 'opacity' ] ,
52
+ duration : 500 ,
53
+ delay : 500 + Math . random ( ) * 500 ,
54
+ } ,
55
+ ] ,
56
+ buildOut : [
57
+ {
58
+ fields : [ 'opacity' ] ,
59
+ duration : 200 ,
60
+ } ,
61
+ ] ,
62
+ update : [
63
+ {
64
+ fields : [ 'lineWidth' , 'r' ] ,
65
+ shapeId : 'keyShape' ,
66
+ duration : 800 ,
67
+ } ,
68
+ {
69
+ fields : [ 'opacity' ] ,
70
+ shapeId : 'haloShape' ,
71
+ duration : 500 ,
72
+ } ,
73
+ ] ,
74
+ } ,
75
+ } ,
76
+ } ;
77
+ } ,
78
+ data : {
79
+ nodes : [
80
+ { id : 'node1' , data : { parentId : 'combo1' , x : 250 , y : 100 } } ,
81
+ { id : 'node2' , data : { parentId : 'combo1' , x : 350 , y : 100 } } ,
82
+ { id : 'node3' , data : { parentId : 'combo2' , x : 100 , y : 300 } } ,
83
+ { id : 'node4' , data : { parentId : 'combo1' , x : 350 , y : 200 } } ,
84
+ { id : 'node5' , data : { x : 600 , y : 300 } } ,
85
+ ] ,
86
+ edges : [
87
+ { id : 'edge1' , source : 'node1' , target : 'node2' , data : { } } ,
88
+ { id : 'edge2' , source : 'node1' , target : 'node3' , data : { } } ,
89
+ { id : 'edge3' , source : 'node1' , target : 'node4' , data : { } } ,
90
+ { id : 'edge4' , source : 'node2' , target : 'node4' , data : { } } ,
91
+ { id : 'edge5' , source : 'node3' , target : 'node4' , data : { } } ,
92
+ { id : 'edge6' , source : 'node4' , target : 'node5' , data : { } } ,
93
+ ] ,
94
+ combos : [
95
+ { id : 'combo1' , data : { parentId : 'combo2' } } ,
96
+ { id : 'combo2' , data : { } } ,
97
+ ] ,
98
+ } ,
99
+ modes : {
100
+ default : [
101
+ 'collapse-expand-combo' ,
102
+ {
103
+ type : 'drag-node' ,
104
+ key : 'drag-node-behavior' ,
105
+ enableTransient : false
106
+ } ,
107
+ 'drag-canvas' ,
108
+ 'zoom-canvas' ,
109
+ {
110
+ type : 'click-select' ,
111
+ itemTypes : [ 'node' , 'edge' , 'combo' ] ,
112
+ } ,
113
+ {
114
+ type : 'hover-activate' ,
115
+ itemTypes : [ 'node' , 'edge' ] ,
116
+ } ,
117
+ {
118
+ type : 'drag-combo' ,
119
+ key : 'drag-combo-behavior' ,
120
+ enableTransient : false
121
+ }
122
+ ] ,
123
+ } ,
124
+ } ) ;
125
+ } , [ ] ) ;
126
+
127
+ useEffect ( ( ) => {
128
+ if ( ! graph ) return ;
129
+ debugger
130
+ graph . updateBehavior ( {
131
+ type : 'drag-combo' ,
132
+ key : 'drag-node-behavior' ,
133
+ updateComboStructure
134
+ } , 'default' ) ;
135
+ graph . updateBehavior ( {
136
+ type : 'drag-combo' ,
137
+ key : 'drag-combo-behavior' ,
138
+ updateComboStructure
139
+ } , 'default' ) ;
140
+ } , [ updateComboStructure ] )
141
+
142
+ return (
143
+ < div ref = { ref } >
144
+ < div style = { { display : 'inline-flex' , position : 'absolute' , left : '16px' , top : '16px' , zIndex : 1 } } >
145
+ < div > 拖拽 combo 时:</ div >
146
+ < select
147
+ className = 'update-combo-structure'
148
+ onChange = { val => setUpdateComboStructure ( val . target ?. value === 'structure' ) }
149
+ >
150
+ < option value = 'structure' > 更新结构</ option >
151
+ < option value = 'size' > 更新大小</ option >
152
+ </ select >
153
+ </ div >
154
+ </ div >
155
+ ) ;
156
+ } ;
157
+
158
+ export default V5Combo ;
0 commit comments