Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extension): 【dynamic-group】componentWillUnmount移除事件监听,解决折叠再展开的子元素移动错乱问题 #1919

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wbccb
Copy link
Contributor

@wbccb wbccb commented Oct 17, 2024

fix #1912

问题发生的原因

下面视频展示的关系为:dynamicGroupA嵌套dynamicGroupB,dynamicGroupB嵌套普通Node

由于dynamicGroupA嵌套dynamicGroupB,在dynamicGroupA进行折叠时,会触发dynamicGroupB的隐藏,从而触发dynamicGroupB的componentWillUnmount()销毁,但是如下面视频所示,dynamicGroupB的componentDidMount()注册了很多事件:

  componentDidMount() {
    super.componentDidMount()
    const { eventCenter } = this.props.graphModel
    // 在 group 旋转时,对组内的所有子节点也进行对应的旋转计算
    eventCenter.on('node:rotate', this.onNodeRotate)
    // 在 group 缩放时,对组内的所有子节点也进行对应的缩放计算
    eventCenter.on('node:resize', this.onNodeResize)
    // 在 group 移动时,对组内的所有子节点也进行对应的移动计算
    eventCenter.on('node:mousemove', this.onNodeMouseMove)
  }

但是忘记执行对应的事件移除,因此导致每一次折叠=>展开的时候,都会重复注册一次事件,因此导致下面视频所展示触发多次 moveNodes,也就是dynamicGroupB移动1px,会触发多次它的children的1px移动,从而造成移动错乱问题

on-off.mp4

解决方法

将componentDidMount注册的监听事件方法抽离出来,为后面解除事件监听做准备

提交记录:refactor(extension): 【dynamic-group】将componentDidMount注册的监听事件方法抽离出来,为后面解除事件监听做准备

交互逻辑无变化,只是将注册方法抽离为this.xxxx方法,放在外部

componentWillUnmount移除监听

  componentWillUnmount() {
    super.componentWillUnmount()
    const { eventCenter } = this.props.graphModel
    eventCenter.off('node:rotate', this.onNodeRotate)
    eventCenter.off('node:resize', this.onNodeResize)
    eventCenter.off('node:mousemove', this.onNodeMouseMove)
  }

Copy link

changeset-bot bot commented Oct 17, 2024

⚠️ No Changeset found

Latest commit: d54c861

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report]: 分组嵌套之后,元素会出现错位
1 participant