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(rn): 修复previewImage 预览图片API的current必须是urls其中的一个 #11632

Open
wants to merge 8 commits into
base: 3.x
Choose a base branch
from

Conversation

WhiteSean
Copy link
Contributor

这个 PR 做了什么? (简要描述所做更改)
修复previewImage 预览图片API的current必须是urls其中的一个,当current不是urls当中一个时,程序抛出异常,这个pr解决这个bug,去掉这个校验

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue id #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)

@taro-bot2
Copy link

taro-bot2 bot commented Apr 13, 2022

欢迎提交 PR~ Taro 非常感谢您对开源事业做出的贡献!🌷🌷🌷

一般 PR 会在一到两周内进行 review,成功合入后会随下一个版本进行发布。

Review 需要耗费大量时间,所以请遵循以下规范,协助我们提高 review 效率🙏🙏🙏

  1. 详细介绍 PR 的背景(非常重要,例如解决了什么问题,该问题如何复现等)
  2. 确保 CI 顺利运行。
  3. 最好能提供对应的测试用例。

为了更好地进行沟通,请加入 Taro 开发者微信群:

@zhiqingchen zhiqingchen requested a review from iChengbo April 13, 2022 08:19
@zhiqingchen
Copy link
Member

3.4.5-zeta.1

@zhiqingchen zhiqingchen added the T-rn Target - 编译到 React Native label Apr 13, 2022
Copy link
Collaborator

@iChengbo iChengbo left a comment

Choose a reason for hiding this comment

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

这不是 bug,是为了提醒使用者需要校验好这两个属性的,不然没法 previewImage

@WhiteSean
Copy link
Contributor Author

这不是 bug,是为了提醒使用者需要校验好这两个属性的,不然没法 previewImage

current控制显示任何一张图片,urls用于控制需要预览的一些图片(可能预览除cunrrent显示之外的图片,也存在这样的真实场景),当urls里不包括current的图片时 我试过是可以正常预览的,而且不做这样的校验更灵活,且其他端侧也没有这样的限制

@iChengbo
Copy link
Collaborator

iChengbo commented Apr 15, 2022

这不是 bug,是为了提醒使用者需要校验好这两个属性的,不然没法 previewImage

current控制显示任何一张图片,urls用于控制需要预览的一些图片(可能预览除cunrrent显示之外的图片,也存在这样的真实场景),当urls里不包括current的图片时 我试过是可以正常预览的,而且不做这样的校验更灵活,且其他端侧也没有这样的限制

删除这段限制后,不包括 current 也可以正常预览是因为做了兼容。

index={index === -1 ? 0 : index}

此外,这块可以确实可以优化一下:处理成 current 非必填,此时默认取第一个。但是,如果没有 urls 的限制还是要有的。参考如下:

let index = 0
if (urls.length === 0) {
  throw new Error('"current" or "urls" is invalid')
} else {
 index = Math.max(current, urls.indexOf(current))
}

@WhiteSean
Copy link
Contributor Author

这不是 bug,是为了提醒使用者需要校验好这两个属性的,不然没法 previewImage

current控制显示任何一张图片,urls用于控制需要预览的一些图片(可能预览除cunrrent显示之外的图片,也存在这样的真实场景),当urls里不包括current的图片时 我试过是可以正常预览的,而且不做这样的校验更灵活,且其他端侧也没有这样的限制

删除这段限制后,不包括 current 也可以正常预览是因为做了兼容。

index={index === -1 ? 0 : index}

此外,这块可以确实可以优化一下:处理成 current 非必填,此时默认取第一个。但是,如果没有 urls 的限制还是要有的。参考如下:

let index = 0
if (urls.length === 0) {
  throw new Error('"current" or "urls" is invalid')
} else {
 index = Math.max(current, urls.indexOf(current))
}

可以的

@zhiqingchen
Copy link
Member

@iChengbo 再review一个


let index = 0
if (urls.length === 0) {
throw new Error('"current" or "urls" is invalid')
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里把提示文案给改下吧

if (urls.length === 0) {
throw new Error('"current" or "urls" is invalid')
} else {
index = Math.max(current, urls.indexOf(current))
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里的 urls.indexOf(current) 可以先声明出来,然后 === -1 时给个 warning 信息(console.warn('xxxx xxxx xxxx')),不等于 -1 时再取 max。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

突然发现不能取max,因为curreny是一个字符串,取max是返回的NaN
改成这样看是否可以?

let index = 0
if (!urls || urls.length === 0) {
  throw new Error('待预览的图片列表"urls"不能为空')
} else {
  urls.indexOf(current) === -1 ? console.warn('显示的图片不在预览列表当中') : ''
  index = Math.max(0, urls.indexOf(current))
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. 提示文案翻译成英文;
  2. 下面一段你看看这样写呢?少执行一次 indexOf
const _index = urls.indexOf(current)
if (_index === -1) {
  console.warn('xxxx')
}
index = Math.max(0, _index)

@iChengbo iChengbo changed the title fix: 修复previewImage 预览图片API的current必须是urls其中的一个 fix(rn): 修复previewImage 预览图片API的current必须是urls其中的一个 Apr 21, 2022

let index = 0
if (!urls || urls.length === 0) {
throw new Error('the list of images to be previewed "urls" cannot be empty')
Copy link
Collaborator

Choose a reason for hiding this comment

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

the "urls" cannot be empty

throw new Error('the list of images to be previewed "urls" cannot be empty')
} else {
const _index = urls.indexOf(current)
_index === -1 ? console.warn('the displayed image is not in the preview list') : ''
Copy link
Collaborator

Choose a reason for hiding this comment

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

_index === -1 && console.warning('"current" is not listed in the "urls"')

} else {
const _index = urls.indexOf(current)
_index === -1 ? console.warn('the displayed image is not in the preview list') : ''
index = Math.max(0, _index)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

修改后把 commit 合并成一个就行了

Copy link
Member

Choose a reason for hiding this comment

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

这个我们 squash and merge 就会是一个commit

Copy link
Collaborator

Choose a reason for hiding this comment

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

哦哦 我以为要先合并呢😂

@liqinuo liqinuo changed the base branch from main to 3.x July 16, 2024 06:56
@ZEJIA-LIU ZEJIA-LIU deleted the branch NervJS:3.x July 16, 2024 07:15
@ZEJIA-LIU ZEJIA-LIU closed this Jul 16, 2024
@ZEJIA-LIU ZEJIA-LIU reopened this Jul 16, 2024
@ZEJIA-LIU ZEJIA-LIU closed this Jul 16, 2024
@ZEJIA-LIU ZEJIA-LIU reopened this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-rn Target - 编译到 React Native
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants