File tree Expand file tree Collapse file tree 4 files changed +30
-7
lines changed Expand file tree Collapse file tree 4 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ class GraphModel {
37
37
topElement : BaseNodeModel | BaseEdgeModel ; // 当前位于顶部的元素
38
38
selectElement : BaseNodeModel | BaseEdgeModel ; // 当前位于顶部的元素
39
39
selectElements = new Map < string , BaseElementModel > ( ) ; // 多选
40
+ idGenerator : ( ) => number | string ;
40
41
@observable selectElementSize = 0 ;
41
42
@observable edgeType : string ;
42
43
@observable nodes : BaseNodeModel [ ] = [ ] ;
@@ -61,6 +62,7 @@ class GraphModel {
61
62
grid : { size = 1 } = { } ,
62
63
isSilentMode = false ,
63
64
eventCenter,
65
+ idGenerator,
64
66
} = config ;
65
67
this . background = background ;
66
68
this . isSlient = isSilentMode ;
@@ -73,6 +75,7 @@ class GraphModel {
73
75
this . width = config . width ;
74
76
this . height = config . height ;
75
77
this . partial = config . partial ;
78
+ this . idGenerator = idGenerator ;
76
79
}
77
80
@computed get nodesMap ( ) : { [ key : string ] : { index : number , model : BaseNodeModel } } {
78
81
return this . nodes . reduce ( ( nMap , model , index ) => {
Original file line number Diff line number Diff line change @@ -90,9 +90,22 @@ class BaseEdgeModel implements IBaseModel {
90
90
if ( ! data . properties ) {
91
91
data . properties = { } ;
92
92
}
93
+
94
+ if ( ! data . id ) {
95
+ const { idGenerator } = this . graphModel ;
96
+ const globalId = idGenerator && idGenerator ( ) ;
97
+ if ( globalId ) data . id = globalId ;
98
+ const nodeId = this . createId ( ) ;
99
+ if ( nodeId ) data . id = nodeId ;
100
+ }
101
+
93
102
assign ( this , pickEdgeConfig ( data ) ) ;
94
103
}
95
104
105
+ createId ( ) {
106
+ return null ;
107
+ }
108
+
96
109
setAttributes ( ) { }
97
110
98
111
@computed get sourceNode ( ) {
Original file line number Diff line number Diff line change @@ -104,14 +104,23 @@ export default class BaseNodeModel implements IBaseModel {
104
104
if ( ! data . properties ) {
105
105
data . properties = { } ;
106
106
}
107
- const id = this . createUniqueId ( ) ;
108
- if ( ! data . id && id ) {
109
- data . id = id ;
107
+
108
+ if ( ! data . id ) {
109
+ const { idGenerator } = this . graphModel ;
110
+ const globalId = idGenerator && idGenerator ( ) ;
111
+ if ( globalId ) data . id = globalId ;
112
+ const nodeId = this . createId ( ) ;
113
+ if ( nodeId ) data . id = nodeId ;
110
114
}
115
+
111
116
this . formatText ( data ) ;
112
117
assign ( this , pickNodeConfig ( data ) ) ;
113
118
}
114
119
120
+ createId ( ) {
121
+ return null ;
122
+ }
123
+
115
124
// 格式化text参数,未修改observable不作为action
116
125
formatText ( data ) : void {
117
126
if ( ! data . text ) {
@@ -247,10 +256,6 @@ export default class BaseNodeModel implements IBaseModel {
247
256
return [ ] ;
248
257
}
249
258
250
- createUniqueId ( ) {
251
- return null ;
252
- }
253
-
254
259
@action
255
260
move ( deltaX , deltaY ) : void {
256
261
this . x += deltaX ;
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ export type Definition = {
54
54
hideAnchors ?: boolean ; // 是否隐藏anchor
55
55
56
56
hoverOutline ?: boolean ; // 是否显示节点hover时的outline
57
+
58
+ idGenerator ?: ( ) => number | string ; // 元素id生成器
57
59
} ;
58
60
59
61
export interface GuardsTypes {
You can’t perform that action at this time.
0 commit comments