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: 兼容不设置高度时,窗口变化后更新画布尺寸 #1900

Merged
merged 5 commits into from
Dec 11, 2024

Conversation

menghany
Copy link
Contributor

解决resize传递宽高后渲染无效,并兼容窗口变化事件。

Copy link

changeset-bot bot commented Oct 10, 2024

⚠️ No Changeset found

Latest commit: 28893d8

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

@menghany
Copy link
Contributor Author

menghany commented Oct 10, 2024

#1889

@boyongjiong
Copy link
Collaborator

你好,这个功能都做了什么测试呢?是怎么测试的

@boyongjiong
Copy link
Collaborator

  1. 这个的实现是否有必要引入该额外的变量记录状态(isContainerWidth/isContainerHeight)。

  2. graphModel 传入 Graph 组件,组件会根据 props 变化自动更新,下面 needUpdate 的状态值我也没看到使用的地方。

  3. 只看代码 diff,style 是在哪儿定义的变量,没看到定义怎么就直接使用了呢

那个 issue 的 codesandbox 我没打开。我现在的理解,如果 resize 未生效,先确认 resize 后,graphModel 中的 width/height 是否变成了预期的值。1. 如果值变了,容器样式没有变,那根据 width、height 值更新容器样式即可;2. 如果值没有变,先定位问题(为什么没变),再按 1 的逻辑处理。

@menghany
Copy link
Contributor Author

menghany commented Oct 12, 2024

@boyongjiong
我观察git提交记录,resize没有变化是因为此次提交 d3d1cbb,增加了自适应的同时删除了style属性引用
image
实际上当前代码存在style声明,只是去掉了属性的使用。

我目前想到的能够兼容不配置宽或者高度时重新resize及配置时不清空resize,加入了两个属性记录。

@menghany
Copy link
Contributor Author

你好,这个功能都做了什么测试呢?是怎么测试的

image
我使用此测试demo,增加了变更尺寸的方法。但是此提交我没有增加。如果这种测试方式正确,我可以补充commit

@boyongjiong
Copy link
Collaborator

你好,这个功能都做了什么测试呢?是怎么测试的

image 我使用此测试demo,增加了变更尺寸的方法。但是此提交我没有增加。如果这种测试方式正确,我可以补充commit

是这样的,提交一下吧,我看一下。建议后续 PR 也是提供测试的代码,就跟你现在做的这个一样

@boyongjiong
Copy link
Collaborator

image
靓仔,首先感谢你的 PR,我仔细看了一下代码,如果是需要 resize,我只修改上图这个地方,把 style 用上之后,好像就符合预期了

效果如下:
2024-10-30 15 16 22
我把代码传上去,你看下是否符合预期。因为我感觉你这块的代码中 isContainerWidth 好像没啥意义,实现的略微有点复杂

@menghany
Copy link
Contributor Author

刚刚review发现,
window.addEventListener('resize', this.throttleResize),
throttleResize方法有问题,没有正确的创建导致window的resize方法一直没有生效。我修改完成之后,如果不增加父容器判断,窗口大小变化了自定义resize就失效了。

@menghany
Copy link
Contributor Author

@boyongjiong 请问为什么关闭这个 pr 呢,即使可能不是最优解,但是我认为目前 resize 方法现在是无效的,如果不兼容 resize 监听,也需要移除 resize 监听事件吧。

@boyongjiong
Copy link
Collaborator

我当时测试了

image 靓仔,首先感谢你的 PR,我仔细看了一下代码,如果是需要 resize,我只修改上图这个地方,把 style 用上之后,好像就符合预期了

效果如下: 2024-10-30 15 16 22 2024-10-30 15 16 22 我把代码传上去,你看下是否符合预期。因为我感觉你这块的代码中 isContainerWidth 好像没啥意义,实现的略微有点复杂

你看下这个?我当时测试之后发现,好像直接把 style 应用到画布上之后,resize 的功能就符合预期了。

我先把这个 PR 打开,我再看一下。

@boyongjiong boyongjiong reopened this Nov 26, 2024
@@ -56,7 +56,7 @@ class Graph extends Component<IGraphProps> {
this.props.options.width = this.props.graphModel.width
this.props.options.height = this.props.graphModel.height
}
private throttleResize = () => throttle(this.handleResize, 200)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个位置的节流没生效,代码有点小问题,如果生效了的话。只补充style,视口尺寸变了的话。style会被重置。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see see

Copy link
Collaborator

@boyongjiong boyongjiong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好,你看看我的评论?有问题在评论中讨论


this.width = options.width || this.rootEl.getBoundingClientRect().width
this.height = options.height || this.rootEl.getBoundingClientRect().height
if (options.width) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我突然发现这个写法的问题,如果我把 options.width 设置成 0,是不是就去拿容器的大小了

这段的代码是不是可以写成下面这种,当 options.width 是 undefined 时,取 container 宽度

// 初始化宽度 + 设置是否使用容器宽度标志位
this.width = options.width ?? this.rootEl.getBoundingClientRect().width
this.isContainerWidth = isNil(options.width)

// 高度同上
this.height = options.height ?? this.rootEl.getBoundingClientRect().height
this.isContainerHeight = isNil(options.height)

@@ -1411,8 +1432,20 @@ export class GraphModel {
* 重新设置画布的宽高
*/
@action resize(width?: number, height?: number): void {
this.width = width || this.rootEl.getBoundingClientRect().width
this.height = height || this.rootEl.getBoundingClientRect().height
if (width) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码是否可以同样简单写

// 更新宽度 + 更新标志信息
this.width = width ?? this.rootEl.getBoundingClientRect().width
this.isContainerWidth = isNil(width)

this.height = height ?? this.rootEl.getBoundingClientRect().height
this.isContainerHeight = isNil(height)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已优化此处代码

@@ -39,9 +39,24 @@ type ContainerStyle = {
@observer
class Graph extends Component<IGraphProps> {
private handleResize = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段逻辑我有些没看太懂的点:

  1. 是只有当画布是容器的宽或高时,才更新画布吗?
  2. 为什么需要重新设置 options.width、options.height,这个是用户外部传入的,是否有必要再更新呢?它是不是只在初始化时用一次

另外,就是代码的问题:代码中 通过 this.props.graphModel 读取值的频率太高了,可以提前解构出来下面用

const { graphModel, options } = this.props
const { width, height, isContainerWidth, isContainerHeight } = graphModel

let nextWidth: number | undefined = width
let nextHeight: number | undefined = height

const needUpdate: boolean = isContainerWidth || isContainerHeight

if (isContainerWidth) {
    nextWidth = undefined
}
if (isContainerHeight) {
    nextHeight = undefined
}
if (needUpdate) {
    graphModel.resize(nextWidth, nextHeight)
}

options.width = graphModel.width
options.height = graphModel.height

Copy link
Contributor Author

@menghany menghany Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.没错,考虑到如果用户设置了宽高,那么视口变化时就不需要重复更新尺寸了
2.关于重新设置option的宽高,我这边考虑时外部能拿到这个和获取这个属性,我在测试用例里面也打印了这个测试,所以给他重新更新为新的属性。如果不太需要的话可以移除。
最后优化部分:感谢提醒,已优化为解构方式

lf.graphModel.isContainerHeight,
lf.graphModel.isContainerWidth,
)
console.log('current option size', lf.options.width, lf.options.height)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处可以获取到options里面的宽高,所以进行了更新

@boyongjiong
Copy link
Collaborator

本 PR 代码跟现在 master 上代码已经有冲突了,建议拉一下最新代码,然后基于 master rebase ,更新一下当前 pr 代码哈

@boyongjiong
Copy link
Collaborator

你的操作好像有点问题 😂,在你的分支,基于原项目 master 分支 rebase 之后,在你的分支 git push -f ,这个 PR 的内容就更新了

@boyongjiong
Copy link
Collaborator

git reflog 撤回一下你在我评论之后的操作,重新整一下

@menghany
Copy link
Contributor Author

git reflog 撤回一下你在我评论之后的操作,重新整一下

感谢,这次呢?

@boyongjiong boyongjiong merged commit 5773099 into didi:master Dec 11, 2024
@boyongjiong
Copy link
Collaborator

git reflog 撤回一下你在我评论之后的操作,重新整一下

感谢,这次呢?

好了哈,代码已合并,感谢 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.

2 participants