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

feat: imagepreview taro 增加缩放, 优化代码 #2941

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

xiaoyatong
Copy link
Collaborator

@xiaoyatong xiaoyatong commented Jan 14, 2025

🤔 这个变动的性质是?

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

🔗 相关 Issue

#784
#1255

💡 需求背景和解决方案

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

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

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

Summary by CodeRabbit

  • 样式更新

    • 优化了图片预览组件的样式布局
    • 调整了关闭图标和图像/视频容器的显示方式
    • 确保图片预览组件宽度自适应且居中
  • 组件改进

    • 增强了图片预览组件的类型安全性
    • 简化了触摸事件和图像缩放的处理逻辑
    • 优化了图像和视频的渲染方式
  • 测试调整

    • 增强了图片预览组件的测试覆盖率,特别是交互和状态管理
    • 更新了自动播放和关闭图标相关的测试用例
    • 调整了测试中的超时时间和预期行为

Copy link

coderabbitai bot commented Jan 14, 2025

概述

演练

这个拉取请求主要修改了图像预览组件的样式和代码实现。在 imagepreview.scss 中,更新了 .nut-close.nut-pop.nut-imagepreview-swiper-item.nut-swiper-item 的样式,增加了嵌套类和响应式属性。在 imagepreview.taro.tsximagepreview.tsx 中,优化了类型定义、状态管理和事件处理逻辑,提高了组件的类型安全性和代码可读性。

变更

文件 变更摘要
src/packages/imagepreview/imagepreview.scss 新增嵌套类 .nut-icon,调整 .nut-pop 的样式,更新 .nut-imagepreview-swiper-item.nut-swiper-item 的嵌套样式
src/packages/imagepreview/imagepreview.taro.tsx 更新 onChange 方法签名,优化 useRef 类型,简化状态管理,重构触摸事件处理
src/packages/imagepreview/imagepreview.tsx 修改 ImagePreviewProps 接口,更新 defaultProps,简化 maxNo 计算,重构渲染逻辑
src/packages/imagepreview/__test__/imagepreview.spec.tsx 更新测试用例以反映组件行为的变化,包括 autoPlay 属性的移除和样式的修改

可能相关的问题

可能相关的 PR

建议标签

size/L

建议审阅者

  • xiaoyatong
  • oasis-cloud

诗歌

兔子的图像预览魔法,
样式优化,代码轻盈,
缩放自如,布局精妙,
响应迅速,性能飞扬,
预览世界,尽在掌握!🐰✨


📜 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 03236a3 and 7444846.

📒 Files selected for processing (1)
  • src/packages/imagepreview/__test__/imagepreview.spec.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/packages/imagepreview/test/imagepreview.spec.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build

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:

  • 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. (Beta)
  • @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.

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

Attention: Patch coverage is 96.36364% with 2 lines in your changes missing coverage. Please review.

Project coverage is 84.51%. Comparing base (2571c8d) to head (7444846).

Files with missing lines Patch % Lines
src/packages/imagepreview/imagepreview.tsx 96.36% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2941      +/-   ##
==========================================
+ Coverage   84.02%   84.51%   +0.49%     
==========================================
  Files         220      220              
  Lines       17917    17872      -45     
  Branches     2628     2639      +11     
==========================================
+ Hits        15055    15105      +50     
+ Misses       2857     2762      -95     
  Partials        5        5              

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

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

🧹 Nitpick comments (4)
src/packages/imagepreview/imagepreview.tsx (2)

111-167: 建议优化缩放性能

当前缩放实现逻辑清晰,但建议以下优化:

  1. 考虑使用 requestAnimationFrame 来优化缩放动画性能
  2. 添加防抖处理,避免频繁的状态更新
  3. 考虑使用 CSS transform 的 scale3d 来启用硬件加速
  const scaleNow = () => {
    if (ref.current) {
-     ref.current.style.transform = `scale(${store.scale})`
+     requestAnimationFrame(() => {
+       ref.current.style.transform = `scale3d(${store.scale}, ${store.scale}, 1)`
+     })
    }
  }

265-286: 建议增强类型安全性

当前实现的类型判断可以更严谨,建议以下改进:

  1. 创建统一的联合类型来处理图片和视频项
  2. 使用类型守卫函数来确保类型安全
+ type MediaItem = 
+   | { type: 'video'; data: VideoOption }
+   | { type: 'image'; data: ImageOption }

+ const isVideo = (item: MediaItem): item is { type: 'video'; data: VideoOption } => {
+   return item.type === 'video'
+ }

  {[
-   ...videos.map((item) => ({ type: 'video', data: item })),
-   ...images.map((item) => ({ type: 'image', data: item })),
+   ...videos.map((item): MediaItem => ({ type: 'video', data: item })),
+   ...images.map((item): MediaItem => ({ type: 'image', data: item })),
  ]
src/packages/imagepreview/imagepreview.taro.tsx (1)

269-297: 建议优化平台特定实现

针对不同平台的处理已经很好,但还可以进一步优化:

  1. 建议将平台特定的逻辑抽离到单独的 hooks 中
  2. 考虑使用条件编译来减少打包体积
  3. 为小程序平台添加更多的性能优化
+ const usePlatformFeatures = () => {
+   const isWeb = Taro.getEnv() === 'WEB'
+   return {
+     showMenuByLongpress: !isWeb,
+     imageProps: !isWeb ? { showMenuByLongpress } : {}
+   }
+ }

- {...(Taro.getEnv() !== 'WEB' && {
-   showMenuByLongpress,
- })}
+ {...imageProps}
src/packages/imagepreview/imagepreview.scss (1)

81-87: 建议增强响应式设计

当前的弹性布局实现良好,建议添加以下响应式优化:

  1. 添加媒体查询以适应不同屏幕尺寸
  2. 考虑使用 viewport 单位来优化大屏幕显示
  3. 添加最小/最大尺寸限制
  .nut-image,
  .nut-video {
    display: flex;
    justify-content: center;
    align-items: center;
+   min-width: 280px;
+   max-width: 90vw;
+   @media (min-width: 768px) {
+     max-width: 80vw;
+   }
  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2571c8d and 33fe815.

📒 Files selected for processing (3)
  • src/packages/imagepreview/imagepreview.scss (3 hunks)
  • src/packages/imagepreview/imagepreview.taro.tsx (8 hunks)
  • src/packages/imagepreview/imagepreview.tsx (7 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/packages/imagepreview/imagepreview.taro.tsx

[error] 238-240: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 245-246: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (1)
src/packages/imagepreview/imagepreview.tsx (1)

46-47: 类型定义优化值得称赞!

使用 ImageOption[]VideoOption[] 类型别名替代内联类型定义,提高了代码的可维护性和类型安全性。

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

🔭 Outside diff range comments (1)
src/packages/imagepreview/__test__/imagepreview.spec.tsx (1)

Line range hint 1-144: 缺少缩放功能的测试用例

根据 PR 描述,此次改动增加了图片预览的缩放功能,但测试文件中没有相关的测试用例。建议添加以下测试场景:

  1. 基本缩放功能
  2. 最大/最小缩放限制
  3. 缩放手势操作
  4. 双击缩放
  5. 缩放状态下的拖动
// 示例测试用例
test('zoom functionality', async () => {
  const { container } = render(
    <ImagePreview
      images={images}
      visible
      defaultZoom={1}
      minZoom={0.5}
      maxZoom={3}
    />
  )
  
  // 测试缩放相关的交互和状态
})
🧹 Nitpick comments (2)
src/packages/imagepreview/__test__/imagepreview.spec.tsx (2)

100-100: 建议增加样式自定义的测试用例

当前测试仅验证了指示器的颜色和底部位置,建议增加更多样式属性的测试覆盖:

  • 指示器的大小
  • 指示器的形状
  • 指示器的透明度
  • 其他可自定义的样式属性
test('customize indicator styles', async () => {
  const { container } = render(
    <ImagePreview
      images={images}
      visible
      indicator
      indicatorColor="red"
      indicatorSize={20}
      indicatorShape="square"
      indicatorOpacity={0.8}
    />
  )
  // 添加相应的样式验证
})

Line range hint 1-144: 建议优化测试结构和组织

当前测试文件的组织结构可以进行以下改进:

  1. 使用 describe 块按功能模块组织测试:

    • 基础功能
    • 自动播放
    • 指示器
    • 关闭按钮
    • 缩放功能
    • 视频播放
  2. 测试描述应更具体,包含:

    • 测试场景
    • 预期行为
    • 边界条件
describe('ImagePreview', () => {
  describe('Basic Features', () => {
    test('should render correctly with default props', () => {
      // ...
    })
  })

  describe('Zoom Features', () => {
    test('should zoom in/out with gesture controls', () => {
      // ...
    })
  })
  
  // 其他功能模块...
})
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 33fe815 and 03236a3.

📒 Files selected for processing (1)
  • src/packages/imagepreview/__test__/imagepreview.spec.tsx (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (1)
src/packages/imagepreview/__test__/imagepreview.spec.tsx (1)

61-61: 测试稳定性和覆盖率需要改进

这个测试用例存在以下问题:

  1. 移除了 autoPlay 属性但没有相应的测试来验证默认行为
  2. 延迟时间从 1100ms 增加到 4600ms,超时时间从 2000ms 增加到 5000ms,这可能表明测试不够稳定
  3. 只验证了初始状态的索引显示(1/4),没有验证自动播放过程中的状态变化

建议添加以下测试场景:

  • 验证默认的自动播放行为
  • 验证切换过程中的索引变化
  • 使用更可靠的方式来等待状态变化,而不是依赖固定的延迟时间

Also applies to: 68-68, 72-73, 76-76

@Alex-huxiyang
Copy link
Collaborator

@coderabbitai

Copy link

coderabbitai bot commented Jan 15, 2025

@Alex-huxiyang i'll review the changes in your pr that adds zoom functionality and optimizes the imagepreview component for taro.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@@ -75,7 +76,7 @@ const defaultProps = {
closeIcon: false,
closeIconPosition: 'top-right',
showMenuByLongpress: false,
onChange: (value: number) => {},
onChange: () => {},
Copy link
Collaborator

Choose a reason for hiding this comment

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

属性类型有改动,文档需要更新一下

@oasis-cloud
Copy link
Collaborator

image
应该类似图中的缩放,只控制 img 或它的直接父亲容器。

小程序和 H5 中快速滑动会触发双击放大的交互

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

Successfully merging this pull request may close these issues.

使用 ImagePreview 组件放大的图片不能缩放
3 participants