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: ✨ Upload 添加 use-preview 属性支持关闭文件预览点击文件替换上传(#664) #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kqswzhyb
Copy link
Contributor

@kqswzhyb kqswzhyb commented Nov 22, 2024

🤔 这个 PR 的性质是?(至少选择一个)

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

🔗 相关 Issue

[<!--

  1. 描述相关需求的来源,如相关的 issue 讨论链接。
    -->]([新功能需求] upload组件增加参数让已上传成功的图片点击后不弹出预览而是调起选择文件 #664)

💡 需求背景和解决方案

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

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能

    • 更新了上传组件文档,增加了“关闭预览点击文件替换”功能,允许用户通过设置 use-preview 属性为 false 来点击已上传文件进行替换。
    • 在上传组件的示例中添加了新的演示块,展示如何禁用预览功能。
  • 文档

    • 更新了上传组件的文档,增加了新属性 use-preview 的说明,并更新了属性表以反映其类型和默认值。

Copy link

vercel bot commented Nov 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 3:18am

Copy link

coderabbitai bot commented Nov 22, 2024

Walkthrough

此拉取请求对上传组件的文档和实现进行了更新,增加了新的功能和说明。文档中新增了“关闭预览点击文件替换”部分,介绍了如何通过设置 use-preview 属性为 false 来禁用文件预览并启用文件替换。组件的实现中,添加了 usePreview 属性,并修改了多个函数以支持文件替换和预览机制的灵活控制。整体结构保持不变,但功能性得到了显著增强。

Changes

文件路径 更改摘要
docs/component/upload.md 更新文档,添加“关闭预览点击文件替换”部分,新增 use-preview 属性的说明。
src/pages/upload/Index.vue 在 Vue 模板中添加新的演示块,展示如何禁用预览功能并替换文件,新增 fileList17 变量。
src/uni_modules/wot-design-uni/components/wd-upload/types.ts uploadProps 中添加 usePreview 属性,类型为布尔值,默认值为 true
src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue 更新多个函数以支持文件替换,添加 currentIndex 参数,增强预览处理逻辑,改进错误处理。

Possibly related PRs

Suggested reviewers

  • Moonofweisheng

兔子跳跃在草地上,
上传功能新变化,
文件替换不再难,
预览关闭真简单。
欢庆更新乐无边,
兔子欢舞乐无边! 🐇✨


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

netlify bot commented Nov 22, 2024

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f77db15
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/673ff7a74bc1cb0008623c58
😎 Deploy Preview https://deploy-preview-728--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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

🧹 Outside diff range and nitpick comments (4)
src/uni_modules/wot-design-uni/components/wd-upload/types.ts (1)

319-323: 属性定义和文档说明完善

属性定义清晰,类型安全性好,文档注释完整。建议补充默认值说明,以保持与其他属性文档风格一致。

建议更新注释,添加默认值说明:

  /**
   * 是否点击已上传的图片或视频可预览,值为false的情况下再次弹出上传
   * 类型:boolean
+  * 默认值:true
   */
  usePreview: makeBooleanProp(true),
src/pages/upload/Index.vue (1)

127-131: 建议添加文件名称以提高示例的完整性

为了使示例更加完整,建议为文件对象添加 name 属性,这样可以更好地展示实际使用场景。

 const fileList17 = ref<UploadFile[]>([
   {
-    url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/panda.jpg'
+    url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/panda.jpg',
+    name: 'panda'
   }
 ])
docs/component/upload.md (1)

617-617: 建议优化属性描述的表达方式

当前描述:

| use-preview                 | 是否点击已上传的图片或视频可预览,值为false的情况下再次弹出上传                                                                                                                                                     | boolean                                | -                                              | true                       | 1.3.15 |

建议修改为更简洁的表达:

| use-preview                 | 是否启用预览模式,false 时点击已上传文件将触发重新上传                                                                                                                                                    | boolean                                | -                                              | true                       | 1.3.15 |
src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue (1)

623-693: 建议优化代码以减少重复

onPreviewImageonPreviewVideoonPreviewFile函数中存在大量相似的逻辑,可以考虑将这些函数合并或提取公共部分,提升代码的可维护性和可读性。

示例:

function onPreviewMedia(file: UploadFileItem, isMediaType: (file: UploadFileItem) => boolean) {
  const { beforePreview, usePreview } = props
  const index: number = uploadFiles.value.findIndex((item) => item.url === file.url)
  if (usePreview) {
    if (beforePreview) {
      beforePreview({
        file,
        index,
        imgList: [],
        resolve: (isPass: boolean) => {
          isPass && handlePreviewMedia(file, isMediaType)
        }
      })
    } else {
      handlePreviewMedia(file, isMediaType)
    }
  } else {
    handleChoose(index)
  }
}

然后针对不同类型调用:

function onPreviewImage(file: UploadFileItem) {
  onPreviewMedia(file, isImage)
}

function onPreviewVideo(file: UploadFileItem) {
  onPreviewMedia(file, isVideo)
}

function onPreviewFile(file: UploadFileItem) {
  onPreviewMedia(file, (file) => !isImage(file) && !isVideo(file))
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4bf8d1e and f77db15.

📒 Files selected for processing (4)
  • docs/component/upload.md (2 hunks)
  • src/pages/upload/Index.vue (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-upload/types.ts (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue (7 hunks)
🔇 Additional comments (3)
src/uni_modules/wot-design-uni/components/wd-upload/types.ts (1)

319-323: 验证预览相关功能的完整性

新增的 usePreview 属性与现有的预览相关功能(如 beforePreview 钩子和 onPreviewFail 事件)存在关联,需要确保功能协调。

✅ Verification successful

Let me gather more context about the preview functionality implementation.


usePreview 属性集成验证完成

usePreview 属性已正确集成到预览功能中:

  • 在图片、视频和文件预览的处理函数中都有正确的条件判断
  • beforePreview 钩子和 onPreviewFail 事件配合良好
  • 提供了完整的预览控制流程:预览判断 -> 前置钩子 -> 预览执行 -> 失败处理
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查预览相关的功能实现

# 搜索预览相关的方法实现
echo "检查预览相关方法:"
ast-grep --pattern 'onPreview$_($_) {
  $$$
}'

# 搜索使用 usePreview 属性的地方
echo "检查 usePreview 使用:"
rg "usePreview|use-preview"

Length of output: 1594


Script:

#!/bin/bash
# 检查 wd-upload 组件中预览相关的具体实现
echo "检查预览相关的具体实现:"
rg -A 10 "const.*beforePreview.*usePreview" src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue

# 检查组件中的预览事件处理
echo "检查预览事件处理:"
rg -A 5 "onPreview" src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue

# 检查组件中的预览失败处理
echo "检查预览失败处理:"
rg "onPreviewFail|preview-fail" src/uni_modules/wot-design-uni/components/wd-upload/

Length of output: 5480

src/pages/upload/Index.vue (1)

14-16: 示例代码实现正确且清晰!

新增的演示模块结构合理,清晰地展示了 use-preview 属性的使用方法。

docs/component/upload.md (1)

100-112: 文档结构和示例代码符合规范

新增的"关闭预览点击文件替换"章节清晰地说明了功能用途,并提供了完整的示例代码。

Comment on lines +679 to +691
if (usePreview) {
if (beforePreview) {
beforePreview({
file,
index,
imgList: [],
resolve: (isPass: boolean) => {
isPass && handlePreviewFile(file)
}
})
} else {
handlePreviewFile(file)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复替换文件时的索引匹配错误

在函数onPreviewFile中,同样需要确保indexuploadFiles.value中获取,以避免索引不匹配导致的文件替换错误。

建议修改如下:

function onPreviewFile(file: UploadFileItem) {
  const { beforePreview, usePreview } = props
- const lists = uploadFiles.value.filter((file) => {
-   return !isVideo(file) && !isImage(file)
- })
- const index: number = lists.findIndex((item) => item.url === file.url)
+ const index: number = uploadFiles.value.findIndex((item) => item.url === file.url)
  if (usePreview) {
    // 原有逻辑
  } else {
    handleChoose(index)
  }
}

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +626 to +645
if (usePreview) {
if (beforePreview) {
beforePreview({
file,
index,
imgList: lists.map((file) => file.url),
resolve: (isPass: boolean) => {
isPass &&
handlePreviewImage(
index,
lists.map((file) => file.url)
)
}
})
} else {
handlePreviewImage(
index,
lists.map((file) => file.url)
)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复替换文件时的索引匹配错误

在函数onPreviewImage中,您使用了过滤后的lists数组来获取index,但在调用handleChoose(index)时,index应该对应于uploadFiles.value中的索引。由于listsuploadFiles.value的子集,这可能导致索引不匹配,进而替换错误的文件。

建议修改如下:

function onPreviewImage(file: UploadFileItem) {
  const { beforePreview, usePreview } = props
- const lists = uploadFiles.value.filter((file) => isImage(file))
- const index: number = lists.findIndex((item) => item.url === file.url)
+ const index: number = uploadFiles.value.findIndex((item) => item.url === file.url)
  if (usePreview) {
    // 原有逻辑
  } else {
    handleChoose(index)
  }
}

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +655 to +667
if (usePreview) {
if (beforePreview) {
beforePreview({
file,
index,
imgList: [],
resolve: (isPass: boolean) => {
isPass && handlePreviewVieo(index, lists)
}
})
} else {
handlePreviewVieo(index, lists)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复替换文件时的索引匹配错误

在函数onPreviewVideo中,与onPreviewImage类似,您使用了过滤后的lists数组来获取index,但在调用handleChoose(index)时,index应对应于uploadFiles.value中的索引。这可能导致索引不匹配的问题。

建议修改如下:

function onPreviewVideo(file: UploadFileItem) {
  const { beforePreview, usePreview } = props
- const lists = uploadFiles.value.filter((file) => isVideo(file))
- const index: number = lists.findIndex((item) => item.url === file.url)
+ const index: number = uploadFiles.value.findIndex((item) => item.url === file.url)
  if (usePreview) {
    // 原有逻辑
  } else {
    handleChoose(index)
  }
}

Committable suggestion skipped: line range outside the PR's diff.

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.

1 participant