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

Watermark container size changes, resulting in incomplete display or disappearance of watermarks #48660

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

Conversation

765477020
Copy link

@765477020 765477020 commented Apr 26, 2024

中文版模板 / Chinese template

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

🔗 Related issue link

在浏览器控制台修改Watermark组件的父div尺寸时,水印显示不全或消失

💡 Background and solution

通过MutateObserver API来监听container dom的属性值是否发生变化
当dom属性值发生变化时,更新container的key值,利用react的diff算法key值发生变化重新渲染

📝 Changelog

Language Changelog
🇺🇸 English Watermark container size changes, resulting in incomplete display or disappearance of watermarks
🇨🇳 Chinese Watermark容器大小变化,导致水印显示不全或消失

☑️ Self-Check before Merge

⚠️ Please check all items below before requesting a reviewing. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

Copy link

stackblitz bot commented Apr 26, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link
Contributor

github-actions bot commented Apr 26, 2024

Preview is ready

Copy link
Contributor

github-actions bot commented Apr 26, 2024

👁 Visual Regression Report for PR #48660 Passed ✅

🎯 Target branch: master (0f06598)
📖 View Full Report ↗︎

🎊 Congrats! No visual-regression diff found.

@zombieJ
Copy link
Member

zombieJ commented Apr 26, 2024

What's related issue or reproduce?

Copy link

codesandbox-ci bot commented Apr 26, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@765477020
Copy link
Author

What's related issue or reproduce?

Have been updated

Copy link

codecov bot commented Apr 26, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 99.99%. Comparing base (0f06598) to head (afc9818).

Files Patch % Lines
components/watermark/index.tsx 75.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##            master   #48660      +/-   ##
===========================================
- Coverage   100.00%   99.99%   -0.01%     
===========================================
  Files          746      746              
  Lines        12994    12998       +4     
  Branches      3411     3412       +1     
===========================================
+ Hits         12994    12997       +3     
- Misses           0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -222,6 +224,8 @@ const Watermark: React.FC<WatermarkProps> = (props) => {
mutations.forEach((mutation) => {
if (reRendering(mutation, isWatermarkEle)) {
syncWatermark();
} else if (reRendering(mutation, isWatermarkContainer)) {
setContainerKey(containerKey + 1);
Copy link
Member

Choose a reason for hiding this comment

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

感觉搞复杂了,如果只是需要更新视图的话,直接用 forceUpdate 就可以了吧:

import useForceUpdate from '../_util/hooks/useForceUpdate';

const forceUpdate = useForceUpdate();

if (xxxx) {
  forceUpdate();
}

Copy link
Member

Choose a reason for hiding this comment

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

用时间戳做 key 的初始值感觉不是很好的解法,初始值还不如直接写 0 好了,另外 new Date().getTime() 可以简写为 Date.now()

Copy link
Author

Choose a reason for hiding this comment

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

我曾经尝试过使用useForceUpdate(useReducer)方法来触发视图更新,但是没有解决这个问题

但是我采用了您key初始值为0的建议

**解决思路:**父元素旧的dom替换成新的dom,如果不换成新的dom浏览器控制台设置的属性将不会消失(如果您有更好的解决思路可以提出)

**解决方案:**将父元素div的key进行更新,react的diff算法检测到前后key不一致,会调用ReactElement方法来生成新的fiberNode,最终产生一个新的真实dom

为什么useReducer方法无法解决这个问题
useReducer只是触发视图的更新,父元素的key或type没有发生改变,react会复用这个老的真实dom,导致浏览器控制台设置的CSS属性依旧存在

@zombieJ
Copy link
Member

zombieJ commented Apr 28, 2024

Have been updated

@765477020 录个视频看看?

@zombieJ
Copy link
Member

zombieJ commented May 7, 2024

@765477020 录个视频看看?

@765477020,想看看说的问题具体是什么样子的。

@765477020
Copy link
Author

@765477020 录个视频看看?

@765477020,想看看说的问题具体是什么样子的。

您好,研究了半天搞了一个录屏,但是分辨率不是很高,抱歉

2024-05-07.10-05-24.mp4

@@ -222,6 +224,8 @@ const Watermark: React.FC<WatermarkProps> = (props) => {
mutations.forEach((mutation) => {
if (reRendering(mutation, isWatermarkEle)) {
syncWatermark();
} else if (reRendering(mutation, isWatermarkContainer)) {
setContainerKey((x) => x + 1);
Copy link
Member

Choose a reason for hiding this comment

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

不能用 key 来做重新渲染,这样内部的子元素也会全部重新渲染。这里应该是只有 container 和 watermark 保活才行。

Copy link
Member

Choose a reason for hiding this comment

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

这里需要检测一下 heightscrollHeight 变化,然后重新设置一下 div 的 style。

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.

None yet

3 participants