Skip to content

Comments

fix(Drag): 解决taro不能滑动问题#3054

Merged
oasis-cloud merged 4 commits intojdf2e:feat_v3.xfrom
irisSong:bugfix_drag
Mar 5, 2025
Merged

fix(Drag): 解决taro不能滑动问题#3054
oasis-cloud merged 4 commits intojdf2e:feat_v3.xfrom
irisSong:bugfix_drag

Conversation

@irisSong
Copy link
Collaborator

@irisSong irisSong commented Mar 5, 2025

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • Style

    • 优化了页面布局,确保内容区始终至少覆盖整个视窗高度,并为滚动区域设置了合适的间距。
    • 调整并优化了拖拽展示页面的样式和触摸行为,提升了移动端用户体验。
  • Refactor

    • 简化和优化了拖拽组件的逻辑及定位计算,使组件响应更迅速,并采用了平台友好的组件结构。

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2025

Walkthrough

此次变更主要涉及样式和拖拽组件的调整。在样式部分,新增了对 .demo .drag-full 类的 min-height 及嵌套 .jd-scroll-view 的最小高度设置。在拖拽示例中,移除了部分组件的 className 属性,并调整了部分组件中位置、边界计算和事件处理逻辑。与此同时,拖拽组件的 Taro 版本更新了导入、事件处理及 JSX 结构,简化了边界计算流程。同时,在触摸控制样式中加入了 touch-action: none;

Changes

文件路径 修改摘要
packages/nutui-taro-demo/.../app.scss .demo .drag-full 添加 min-height: 100vh,新增嵌套 .jd-scroll-view,设定 min-height: calc(100vh - 57px)
src/packages/drag/demos/taro/demo2.tsx, demo3.tsx 移除 Drag 组件中 className 属性(分别移除 "drag-demo21", "drag-demo22" 和 "drag-demo3")。
src/packages/drag/demos/taro/demo4.tsx 调整 bottom 函数返回值(由 windowHeight - 501 - 57 变为 windowHeight - 559),修改 div 样式从 absolutefixed,增加 boxSizing: 'border-box',重新格式化 boundary 属性。
src/packages/drag/drag.scss .nut-drag-inner 类中新增 touch-action: none; 属性。
src/packages/drag/drag.taro.tsx 更新导入语句(引入 Viewweb),简化 getInfo 中边界计算和条件调用逻辑,调整 touchEnd 不再使用事件参数,将 <div> 替换为 <View>,并修改事件绑定。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant D as Drag组件
    participant G as getRectByTaro
    participant W as Window信息
    U->>D: 发起拖拽事件
    D->>D: 检查 dragRef 是否存在
    alt 当前为 web 平台
        D->>W: 设置延时调用 getInfo
    else
        D->>G: 直接调用 getInfo 获取区域信息
    end
    G-->>D: 返回尺寸及位置数据
    D->>D: 更新边界状态与样式
    U->>D: 触摸结束动作
    D->>D: 执行 touchEnd 逻辑
Loading

Possibly related PRs

  • fix: demos full css #3051: 该 PR 修改了 .drag-full 类的 padding 属性,与本次对 .drag-full 类样式的改动存在代码级关联。

Suggested reviewers

  • xiaoyatong
  • oasis-cloud

Poem

我是一只小兔,跳跃在代码间,
样式改变悄然生,组件舞动轻盈展。
拖拽逻辑理清晰,边界精准不迷路,
每行代码蕴灵动,创新步伐不停歇!
同伴共庆新变动,欢喜在这数字园。


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c955acf and 489c4af.

📒 Files selected for processing (1)
  • packages/nutui-taro-demo/src/app.scss (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nutui-taro-demo/src/app.scss
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: build

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) 3.x Target branch 3.x labels Mar 5, 2025
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: 1

🧹 Nitpick comments (3)
src/packages/drag/demos/taro/demo4.tsx (2)

14-14: 简化计算逻辑

windowHeight - 501 - 57简化为windowHeight - 559,计算结果相同但代码更简洁。

建议添加注释说明这个计算值的含义,以便将来维护时更容易理解。


31-36: 改进代码格式提高可读性

boundary属性从单行格式改为多行对象结构,提高了代码的可读性和可维护性,同时保持了值的一致性。

这是一个很好的实践,建议在项目中的其他地方也采用类似的格式。

src/packages/drag/drag.taro.tsx (1)

126-126: 移除未使用的参数

touchEnd函数中移除了未使用的事件参数,符合代码简洁性原则。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b8f790 and 327998e.

📒 Files selected for processing (6)
  • packages/nutui-taro-demo/src/app.scss (1 hunks)
  • src/packages/drag/demos/taro/demo2.tsx (0 hunks)
  • src/packages/drag/demos/taro/demo3.tsx (0 hunks)
  • src/packages/drag/demos/taro/demo4.tsx (1 hunks)
  • src/packages/drag/drag.scss (1 hunks)
  • src/packages/drag/drag.taro.tsx (4 hunks)
💤 Files with no reviewable changes (2)
  • src/packages/drag/demos/taro/demo2.tsx
  • src/packages/drag/demos/taro/demo3.tsx
🔇 Additional comments (6)
src/packages/drag/drag.scss (1)

13-13: 添加触摸行为控制以改善拖拽体验

.nut-drag-inner类添加touch-action: none;属性可以防止浏览器默认的触摸行为(如滚动、缩放)干扰自定义拖拽逻辑,这对解决Taro中的滑动问题至关重要。

packages/nutui-taro-demo/src/app.scss (1)

29-32: 确保滚动视图有足够的高度

.demo.full类和内部的.jd-scroll-view添加最小高度限制,确保容器元素可以填满整个视口,这有助于解决滚动区域不足导致的滑动问题。计算值calc(100vh - 57px)考虑了顶部padding的空间,保证了布局的一致性。

src/packages/drag/demos/taro/demo4.tsx (1)

21-22: 修改定位方式以优化交互体验

position'absolute'改为'fixed'并添加boxSizing: 'border-box',有助于确保拖拽边界相对于视口定位,而不是相对于其定位父元素,并且确保边框不会影响元素的尺寸计算。这些变更对解决Taro中的滑动问题有积极作用。

Also applies to: 27-27

src/packages/drag/drag.taro.tsx (3)

1-7: 优化导入结构支持跨平台兼容

更新了导入语句,添加了Taro组件库中的View组件和平台检测工具函数web,这有助于确保组件在不同平台上的一致行为。移除了不再使用的createSelectorQuery导入,减少了不必要的依赖。


64-86: 简化元素尺寸获取逻辑

重构了getInfo函数,使用getRectByTaro直接获取元素的尺寸和位置信息,简化了代码结构并提高了可读性。新的实现方式更加直观,通过解构赋值获取所需的维度,然后直接计算和设置边界状态。


143-156: 添加平台特定逻辑

引入基于平台的条件执行,使用web()函数判断当前平台并据此采取不同的初始化策略。在Web平台上增加了延时处理,非Web平台直接执行,这有助于解决不同平台下的初始化时机问题。

Comment on lines +159 to +173
<View style={style} className={`${classPrefix} ${className}`} ref={myDrag}>
<View
className={`${classPrefix}-inner`}
onTouchStart={(event) => touchStart(event)}
ref={dragRef}
catchMove
// @ts-ignore
onTouchStart={touchStart}
// @ts-ignore
onTouchMove={touchMove}
onTouchEnd={touchEnd}
// eslint-disable-next-line react/no-unknown-property
style={currstyle}
>
{children}
</div>
</div>
</View>
</View>
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

替换HTML元素为Taro组件

将传统的div元素替换为Taro的View组件,并添加catchMove属性以阻止触摸事件传播到父元素,这对于防止在拖拽操作过程中触发父容器的滚动行为至关重要,直接解决了PR中提到的Taro不能滑动的问题。

确保所有事件处理器都正确添加了// @ts-ignore注释,以避免TypeScript编译错误。当前只有onTouchStartonTouchMove添加了注释,但onTouchEnd也可能需要。

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: 1

🧹 Nitpick comments (2)
src/packages/drag/demos/taro/demo4.tsx (1)

31-36: 提高代码可读性

通过重新格式化 boundary 属性的结构,每个属性值单独一行,提高了代码的可读性和可维护性。

src/packages/drag/drag.taro.tsx (1)

143-156: 添加平台适配逻辑

针对不同平台(Web 和原生)进行了差异化处理,为 Web 环境添加了延时执行,确保 DOM 完全渲染后再获取元素尺寸。

但需要注意,依赖项 [dragRef.current] 可能会导致组件频繁重新渲染,建议改用 useLayoutEffect 或调整依赖项。

-useEffect(() => {
+useLayoutEffect(() => {
   if (dragRef.current) {
     if (web()) {
       timer.current = window.setTimeout(() => {
         getInfo()
       }, 300)
     } else {
       getInfo()
     }
   }
   return () => {
     clearTimeout(timer.current)
   }
-}, [dragRef.current])
+}, [])
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b8f790 and 327998e.

📒 Files selected for processing (6)
  • packages/nutui-taro-demo/src/app.scss (1 hunks)
  • src/packages/drag/demos/taro/demo2.tsx (0 hunks)
  • src/packages/drag/demos/taro/demo3.tsx (0 hunks)
  • src/packages/drag/demos/taro/demo4.tsx (1 hunks)
  • src/packages/drag/drag.scss (1 hunks)
  • src/packages/drag/drag.taro.tsx (4 hunks)
💤 Files with no reviewable changes (2)
  • src/packages/drag/demos/taro/demo2.tsx
  • src/packages/drag/demos/taro/demo3.tsx
🔇 Additional comments (7)
src/packages/drag/drag.scss (1)

13-13: 添加 touch-action 属性解决触摸滑动冲突

通过为 .nut-drag-inner 类添加 touch-action: none; CSS 属性,成功阻止了浏览器默认的触摸行为(如滚动、缩放等),从而避免与自定义拖拽实现的冲突。这是解决 Taro 滑动问题的关键修改。

packages/nutui-taro-demo/src/app.scss (1)

29-32: 优化滚动容器尺寸设置

通过添加 min-height: 100vh 和嵌套的 .jd-scroll-view 最小高度计算,确保了滚动区域有足够的高度。使用 calc(100vh - 57px) 考虑了顶部 57px 的间距,有效解决了在 Taro 环境中内容不能完整滚动的问题。

src/packages/drag/demos/taro/demo4.tsx (2)

14-14: 简化边界计算逻辑

将复杂的表达式 windowHeight - 501 - 57 简化为等效的 windowHeight - 559,使代码更加清晰易读。


21-22: 优化定位方式和盒模型

将定位方式从 absolute 更改为 fixed 并添加 boxSizing: 'border-box' 属性,可以确保元素相对于视口定位且边框计算更准确,这对于 Taro 环境下的拖拽边界计算至关重要。

Also applies to: 27-27

src/packages/drag/drag.taro.tsx (3)

2-2: 优化导入结构

正确引入了 Taro 的 View 组件和平台检测工具,这是适配 Taro 环境的必要步骤。

Also applies to: 6-7


126-126: 移除不必要的参数

touchEnd 函数中移除了不使用的事件参数,符合代码简洁性原则。


159-173: 使用 Taro 原生组件

将 HTML div 元素替换为 Taro 的 View 组件,并添加了 catchMove 属性,这是解决 Taro 环境中滑动冲突的关键。catchMove 可以阻止滑动事件冒泡,防止父容器捕获到子元素的滑动事件。

Comment on lines +70 to 86
const {
width,
height,
top: dragTop,
left: dragLeft,
} = await getRectByTaro(dragRef.current)

setBoundaryState({
top: -dragTop + top,
left: -dragLeft + left,
bottom: windowHeight - dragTop - bottom - Math.ceil(height),
right: screenWidth - dragLeft - right - Math.ceil(width),
})

middleLine.current =
screenWidth - rec.width - rec.left - (screenWidth - rec.width) / 2
})
.exec()
middleLine.current =
screenWidth - width - dragLeft - (screenWidth - width) / 2
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

简化边界计算逻辑

重构了边界计算方法,使用 getRectByTaro 直接获取元素尺寸和位置信息,大大简化了代码结构,减少了嵌套回调,提高了代码可读性和性能。

@codecov
Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.06%. Comparing base (3d467b1) to head (489c4af).
Report is 1 commits behind head on feat_v3.x.

Additional details and impacted files
@@            Coverage Diff             @@
##           feat_v3.x    #3054   +/-   ##
==========================================
  Coverage      86.06%   86.06%           
==========================================
  Files            275      275           
  Lines          18234    18234           
  Branches        2751     2751           
==========================================
  Hits           15694    15694           
  Misses          2535     2535           
  Partials           5        5           

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

@oasis-cloud oasis-cloud merged commit 2690af4 into jdf2e:feat_v3.x Mar 5, 2025
8 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Mar 13, 2025
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants