Skip to content

Commit aedc555

Browse files
authored
Merge pull request #1554 from wbccb/fix/1535
fix(extension): perform topGroupZIndex calibration when add node(#1535)
2 parents a971296 + 2d6e41c commit aedc555

File tree

1 file changed

+61
-0
lines changed
  • packages/extension/src/materials/group

1 file changed

+61
-0
lines changed

packages/extension/src/materials/group/index.ts

+61
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import LogicFlow, {
55
GraphConfigData,
66
EdgeConfig,
77
EventType,
8+
NodeData,
89
} from '@logicflow/core';
910
import GroupNode from './GroupNode';
1011

@@ -278,6 +279,9 @@ class Group {
278279
});
279280
}
280281
});
282+
283+
// 初始化nodes时进行this.topGroupZIndex的校准更新
284+
this.checkAndCorrectTopGroupZIndex(data.nodes);
281285
}
282286
};
283287
appendNodeToGroup = ({ data }) => {
@@ -307,6 +311,8 @@ class Group {
307311
data.children.forEach((nodeId) => {
308312
this.nodeGroupMap.set(nodeId, data.id);
309313
});
314+
// 新增node时进行this.topGroupZIndex的校准更新
315+
this.checkAndCorrectTopGroupZIndex([data]);
310316
this.nodeSelected({ data, isSelected: false, isMultiple: false });
311317
}
312318
if (!group) return;
@@ -352,6 +358,61 @@ class Group {
352358
this.activeGroup = newGroup;
353359
this.activeGroup.setAllowAppendChild(true);
354360
};
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+
};
355416
/**
356417
* 1. 分组节点默认在普通节点下面。
357418
* 2. 分组节点被选中后,会将分组节点以及其内部的其他分组节点放到其余分组节点的上面。

0 commit comments

Comments
 (0)