fix(extension): perform topGroupZIndex calibration when add node(#1535) #1554
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.
fix #1535
问题发生的原因
在https://examples.logic-flow.cn/demo/dist/pool的调试中,发现两个
GroupNode
类型的Node
数据的初始化zIndex=1
当点击右边的矩形时,会触发
node:click
方法,一共有两个地方由于
GroupNode
类型的Node
默认autoToFront=false
,因此不会触发上面代码中的graphModel.toFront(model.id)
除了
BaseNode.tsx
监听node:click
之外,还有Group
也监听node:click
从上面代码我们可以清晰知道,当触发
toFrontGroup()
时,由于GroupNode
类型isGroup=true
,因此会继续往下走this.topGroupZIndex=-10000
,this.topGroupZIndex++
之后就是-9999
model.setZIndex(this.topGroupZIndex)
等于model.setZIndex(-9999)
但是我们要记住一个东西,两个
GroupNode
类型的Node
数据的初始化zIndex=1
,也就是说点击的Node.zIndex变为-999
,但是未点击的Node.zIndex还是1
,因此就造成了#1535
视频中的假选状态,本质就是点击Node的zIndex
低于未点击Node的zIndex
,被盖住了解决方法
初步方案
我们从
this.topGroupZIndex
的字面含义就可以知道,其值代表着最顶部的group
的zIndex
,如果用户不在外部随便设置zIndex
,GroupNode
类型的Node
数据默认初始化就是this.zIndex = DEFAULT_BOTTOM_Z_INDEX=-10000
,也就是this.topGroupZIndex
的初始化值而
this.topGroupZIndex
的初始化值是-10000
,外部设置了zIndex=1
,因此导致了this.topGroupZIndex
的作用消失了因此我们增加一个校准方法
把它放在
toFrontGroup()
之前,如下面所示,这样通过checkAndCorrectTopGroupZIndex()
的校准,this.topGroupZIndex
就等于所有group
类型节点中最高的zIndex
的值,然后再进行this.toFrontGroup()
就可以完美将点击的node的zIndex再增加1!优化:去除判断条件
this.topGroupZIndex=-10000
node.zIndex=1
,一个node.zIndex=-10000
,一个node.zIndex=-9999
node.zIndex=-10000
时,此时node会变为node.zIndex=-9999
nodeZIndex > DEFAULT_TOP_Z_INDEX
或者nodeZIndex > DEFAULT_BOTTOM_Z_INDEX
或者nodeZIndex > this.topGroupZIndex
需要每次触发
node:click
进行checkAndCorrectTopGroupZIndex()
的校验再度优化:初始化时/新增node时进行topGroupZIndex的校准
this.topGroupZIndex
代表着目前所有group
中最大zIndex
,那么按照常理来说,应该在初始化的时候,遍历所有group
类型节点进行this.topGroupZIndex
的校准当有新增元素时,由于存在强制构建一个
group
类型节点的zIndex
很大很大,因此我们在每一次新增元素时都得进行一次this.topGroupZIndex
的校准当删除元素时,不会影响
this.topGroupZIndex
代表所有group
类型节点最大zIndex
,因此不做处理而
this.topGroupZIndex
的校准新增对目前nodes最大的zIndex的遍历这样子,我们就可以去除
nodeSelected()
的this.topGroupZIndex
校准,因为this.topGroupZIndex
总是对的!单元测试(暂无)
=_=一直报错,无法调通,有空再补,先把bug修复了