@@ -5,6 +5,7 @@ import LogicFlow, {
5
5
GraphConfigData ,
6
6
EdgeConfig ,
7
7
EventType ,
8
+ NodeData ,
8
9
} from '@logicflow/core' ;
9
10
import GroupNode from './GroupNode' ;
10
11
@@ -278,6 +279,9 @@ class Group {
278
279
} ) ;
279
280
}
280
281
} ) ;
282
+
283
+ // 初始化nodes时进行this.topGroupZIndex的校准更新
284
+ this . checkAndCorrectTopGroupZIndex ( data . nodes ) ;
281
285
}
282
286
} ;
283
287
appendNodeToGroup = ( { data } ) => {
@@ -307,6 +311,8 @@ class Group {
307
311
data . children . forEach ( ( nodeId ) => {
308
312
this . nodeGroupMap . set ( nodeId , data . id ) ;
309
313
} ) ;
314
+ // 新增node时进行this.topGroupZIndex的校准更新
315
+ this . checkAndCorrectTopGroupZIndex ( [ data ] ) ;
310
316
this . nodeSelected ( { data, isSelected : false , isMultiple : false } ) ;
311
317
}
312
318
if ( ! group ) return ;
@@ -352,6 +358,61 @@ class Group {
352
358
this . activeGroup = newGroup ;
353
359
this . activeGroup . setAllowAppendChild ( true ) ;
354
360
} ;
361
+ findNodeAndChildMaxZIndex = ( nodeModel : BaseNodeModel ) => {
362
+ let maxZIndex = DEFAULT_BOTTOM_Z_INDEX ;
363
+ if ( nodeModel . isGroup ) {
364
+ maxZIndex = nodeModel . zIndex > maxZIndex ? nodeModel . zIndex : maxZIndex ;
365
+ }
366
+ if ( nodeModel . children ) {
367
+ nodeModel . children . forEach ( ( nodeId : string ) => {
368
+ if ( typeof nodeId === 'object' ) {
369
+ // 正常情况下, GroupNodeModel.children是一个id数组,这里只是做个兼容
370
+ // @ts -ignore
371
+ nodeId = nodeId . id ;
372
+ }
373
+ const child = this . lf . getNodeModelById ( nodeId ) ;
374
+ if ( child . isGroup ) {
375
+ const childMaxZIndex = this . findNodeAndChildMaxZIndex ( child ) ;
376
+ maxZIndex = childMaxZIndex > maxZIndex ? childMaxZIndex : maxZIndex ;
377
+ }
378
+ } ) ;
379
+ }
380
+ return maxZIndex ;
381
+ } ;
382
+ checkAndCorrectTopGroupZIndex = ( nodes : NodeData [ ] ) => {
383
+ // 初始化时/增加新节点时,找出新增nodes的最大zIndex
384
+ let maxZIndex = DEFAULT_BOTTOM_Z_INDEX ;
385
+ nodes . forEach ( ( node : NodeData ) => {
386
+ const nodeModel = this . lf . getNodeModelById ( node . id ) ;
387
+ const currentNodeMaxZIndex = this . findNodeAndChildMaxZIndex ( nodeModel ) ;
388
+ if ( currentNodeMaxZIndex > maxZIndex ) {
389
+ maxZIndex = currentNodeMaxZIndex ;
390
+ }
391
+ } ) ;
392
+
393
+ if ( this . topGroupZIndex >= maxZIndex ) {
394
+ // 一般是初始化时/增加新节点时发生,因为外部强行设置了一个很大的zIndex
395
+ // 删除节点不会影响目前最高zIndex的赋值
396
+ return ;
397
+ }
398
+ // 新增nodes中如果存在zIndex比this.topGroupZIndex大
399
+ // 说明this.topGroupZIndex已经失去意义,代表不了目前最高zIndex的group,需要重新校准
400
+
401
+ // https://github.com/didi/LogicFlow/issues/1535
402
+ // 当外部直接设置多个BaseNode.zIndex=1时
403
+ // 当点击某一个node时,由于这个this.topGroupZIndex是从-10000开始计算的,this.topGroupZIndex+1也就是-9999
404
+ // 这就造成当前点击的node的zIndex远远比其它node的zIndex小,因此造成zIndex错乱问题
405
+ const allGroupNodes = this . lf . graphModel . nodes
406
+ . filter ( ( node : BaseNodeModel ) => node . isGroup ) ;
407
+ let max = this . topGroupZIndex ;
408
+ for ( let i = 0 ; i < allGroupNodes . length ; i ++ ) {
409
+ const groupNode = allGroupNodes [ i ] ;
410
+ if ( groupNode . zIndex > max ) {
411
+ max = groupNode . zIndex ;
412
+ }
413
+ }
414
+ this . topGroupZIndex = max ;
415
+ } ;
355
416
/**
356
417
* 1. 分组节点默认在普通节点下面。
357
418
* 2. 分组节点被选中后,会将分组节点以及其内部的其他分组节点放到其余分组节点的上面。
0 commit comments