fix(extension): nodeGroupMap was not set correctly when adding a group #1268
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
close #1261
问题发生的原因
如issues/1261重现代码所示,当我们框选元素时,会触发
lf.addNode()
会触发下面代码的执行在上面这段代码中,由于
this.getGroup()
会一直返回null
,因此会一直导致this.nodeGroupMap.set()
这段代码无法执行而
this.getGroup()
会一直返回null
是因为遍历所有nodes
时,会筛选非自己本身的分组,即node.id !== nodeData.id
,比如将issues/1261重现代码所有分组都删除了,然后我们框选一个分组,最终const { nodes } = this.lf.graphModel
永远无法得到groups.length>0
,因为唯一一个分组(node.isGroup
)虽然满足node.isInRange(bounds)
,但是无法满足node.id !== nodeData.id
如下面代码所示,由于
this.nodeGroupMap
没有正常将新增的group数据添加进去,因此当我们使用this.graphModel.group.getNodeGroup(targetNodeId)
总是会拿到空的值
,从而不得不使用graphModel.getNodeModelById(targetNodeId)
获取targetNodeIdGroup.id
以及sourceNodeIdGroup.id
,这就会导致了下面this.createVirtualEdge(data)
语句地执行。本质就是拿不到
node
对应的groupId
,所以只能拿它自己的nodeId
,最终错误认为该node
不属于分组,从而创建出虚拟边初始化数据时,会触发
graphRendered()
方法,在这个方法中,会进行this.nodeGroupMap.set(nodeId, node.id)
的设置,因此不会产生上面的问题解决方法
由于上面这段代码跟
group
没有关联关系,因此我们将它提前执行,如下面所示,我们在if (!group) return;
之前提前执行,当添加group
数据时,虽然我们通过this.getGroup(bounds, data)
拿到的数据为空,但是我们也可以正确进行this.nodeGroupMap.set(nodeId, data.id)
操作,从而避免了添加group
数据没有正确设置this.nodeGroupMap
的问题