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: 解决Drag组件拖拽后会在原地留一个遮挡元素问题+解决weapp/taro-h5多个demo拖拽位置不正确问题 #2330

Merged
merged 2 commits into from
Jun 14, 2024

Conversation

irisSong
Copy link
Collaborator

@irisSong irisSong commented Jun 6, 2024

#2328

Summary by CodeRabbit

  • 新功能

    • 为拖动组件添加了异步行为,以便使用 getRectByTaro 获取元素的宽度和高度,并相应地更新 bottomright 位置的计算。
  • 样式

    • 更新了拖动组件的 CSS 样式,调整了 .nut-drag 类的 displaywidthheight 属性,并添加了嵌套的 &-inner 类以应用特定样式。
  • 修复

    • 调整了多个示例组件的 className 属性,改进了样式应用和组件的可读性。

Copy link

coderabbitai bot commented Jun 6, 2024

Walkthrough

本次更改主要涉及为多个组件添加新的 className 属性,以便更好地控制样式。此外,还更新了 Drag 组件的功能,通过异步方式获取元素的宽度和高度,并相应调整元素的位置计算。

Changes

文件路径 变更摘要
src/packages/drag/demo.taro.tsx 调整 className 属性中类名的顺序。
src/packages/drag/demos/taro/demo1.tsx <Drag> 组件添加 className 属性,值为 "drag-demo1"
src/packages/drag/demos/taro/demo2.tsx 为两个组件添加 className 属性,值分别为 "drag-demo21""drag-demo22"
src/packages/drag/demos/taro/demo3.tsx 为一个元素添加 className 属性,值为 "drag-demo3"
src/packages/drag/demos/taro/demo4.tsx <Drag> 组件添加 className 属性,值为 "drag-demo4"
src/packages/drag/drag.scss 修改 .nut-drag 类的样式属性,添加嵌套的 &-inner 类。
src/packages/drag/drag.taro.tsx 添加异步行为以获取元素宽度和高度,并相应调整 bottomright 位置的计算。
src/packages/drag/drag.tsx 修改获取元素尺寸的逻辑,使用 querySelector 定位特定类的元素,影响组件的定位行为。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DragComponent
    participant getRectByTaro
    
    User ->> DragComponent: Initialize
    DragComponent ->> getRectByTaro: Get element dimensions
    getRectByTaro -->> DragComponent: Return width and height
    DragComponent ->> DragComponent: Calculate new positions
    DragComponent -->> User: Render updated positions
Loading

Poem

代码如风轻轻吹,
样式调整细细推。
异步获取宽与高,
元素定位更可靠。
兔子欢欣跃一跃,
代码世界更美妙。


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 98fa4a0 and df3f513.

Files ignored due to path filters (1)
  • src/packages/drag/__test__/__snapshots__/drag.spec.tsx.snap is excluded by !**/*.snap
Files selected for processing (1)
  • src/packages/drag/drag.taro.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/packages/drag/drag.taro.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range and nitpick comments (2)
src/packages/drag/drag.tsx (1)

Line range hint 89-89: useEffect钩子中缺少依赖项getInfo,应将其添加到依赖数组中以避免潜在的bug。

- }, [myDrag])
+ }, [myDrag, getInfo])
src/packages/drag/drag.taro.tsx (1)

Line range hint 125-125: useEffect钩子中缺少依赖项getInfo,应将其添加到依赖数组中以避免潜在的bug。

- }, [])
+ }, [getInfo])

Comment on lines +48 to +51
const { offsetTop, offsetLeft } = el
const { offsetWidth, offsetHeight } = el.querySelector(
`.${classPrefix}-inner`
) as HTMLDivElement
Copy link

Choose a reason for hiding this comment

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

更改元素尺寸获取方式,使用特定类选择器。建议添加错误处理以防元素未找到。

- const { offsetWidth, offsetHeight } = el.querySelector(`.${classPrefix}-inner`) as HTMLDivElement
+ const element = el.querySelector(`.${classPrefix}-inner`) as HTMLDivElement
+ if (!element) {
+   throw new Error('Element not found')
+ }
+ const { offsetWidth, offsetHeight } = element
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const { offsetTop, offsetLeft } = el
const { offsetWidth, offsetHeight } = el.querySelector(
`.${classPrefix}-inner`
) as HTMLDivElement
const { offsetTop, offsetLeft } = el
const element = el.querySelector(`.${classPrefix}-inner`) as HTMLDivElement
if (!element) {
throw new Error('Element not found')
}
const { offsetWidth, offsetHeight } = element

Comment on lines 49 to 66
const getInfo = async () => {
const el = myDrag.current
if (el) {
const { top, left, bottom, right } = boundary
const { screenWidth, windowHeight } = getSystemInfoSync()

const { width, height } = await getRectByTaro(dragRef.current)
dragRef.current && dragRef.current.getBoundingClientRect()

console.log('width', width, 'height', height)
createSelectorQuery()
.select(`.${className}`)
.boundingClientRect((rec: any) => {
setBoundaryState({
top: -rec.top + top,
left: -rec.left + left,
bottom: windowHeight - rec.height - rec.top - bottom,
right: screenWidth - rec.width - rec.left - right,
bottom: windowHeight - rec.top - bottom - Math.ceil(height),
right: screenWidth - rec.left - right - Math.ceil(width),
Copy link

Choose a reason for hiding this comment

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

getInfo函数改为异步以处理尺寸获取,看起来很合理。建议在调用getBoundingClientRect时使用可选链增加代码的健壮性。

- dragRef.current && dragRef.current.getBoundingClientRect()
+ dragRef.current?.getBoundingClientRect()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const getInfo = async () => {
const el = myDrag.current
if (el) {
const { top, left, bottom, right } = boundary
const { screenWidth, windowHeight } = getSystemInfoSync()
const { width, height } = await getRectByTaro(dragRef.current)
dragRef.current && dragRef.current.getBoundingClientRect()
console.log('width', width, 'height', height)
createSelectorQuery()
.select(`.${className}`)
.boundingClientRect((rec: any) => {
setBoundaryState({
top: -rec.top + top,
left: -rec.left + left,
bottom: windowHeight - rec.height - rec.top - bottom,
right: screenWidth - rec.width - rec.left - right,
bottom: windowHeight - rec.top - bottom - Math.ceil(height),
right: screenWidth - rec.left - right - Math.ceil(width),
const getInfo = async () => {
const el = myDrag.current
if (el) {
const { top, left, bottom, right } = boundary
const { screenWidth, windowHeight } = getSystemInfoSync()
const { width, height } = await getRectByTaro(dragRef.current)
dragRef.current?.getBoundingClientRect()
console.log('width', width, 'height', height)
createSelectorQuery()
.select(`.${className}`)
.boundingClientRect((rec: any) => {
setBoundaryState({
top: -rec.top + top,
left: -rec.left + left,
bottom: windowHeight - rec.top - bottom - Math.ceil(height),
right: screenWidth - rec.left - right - Math.ceil(width),
Tools
Biome

[error] 56-56: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

Copy link

codecov bot commented Jun 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.87%. Comparing base (ad7c890) to head (df3f513).

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2330   +/-   ##
=======================================
  Coverage   85.87%   85.87%           
=======================================
  Files         217      217           
  Lines       22811    22818    +7     
  Branches     2494     2494           
=======================================
+ Hits        19589    19596    +7     
  Misses       3217     3217           
  Partials        5        5           

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

@oasis-cloud oasis-cloud merged commit 27e953d into jdf2e:next Jun 14, 2024
5 checks passed
@xm1769315774
Copy link

这个改造把初始位置改的有问题了,辛苦检查一下。具体的表现我在commit里边评论了

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