Skip to content

Commit

Permalink
fix(size): 优化方法默认值,避免提前调用导致报错 fix #14360
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakaryCode committed Sep 5, 2023
1 parent 22183d6 commit 7b5e869
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
12 changes: 7 additions & 5 deletions packages/taro-api/src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ export function getInitPxTransform (taro) {
export function getPxTransform (taro) {
return function (size) {
const config = taro.config || {}
const deviceRatio = config.deviceRatio || defaultDesignRatio
const baseFontSize = config.baseFontSize
const deviceRatio = config.deviceRatio || defaultDesignRatio
const designWidth = (((input = 0) => isFunction(config.designWidth)
? config.designWidth(input)
: config.designWidth || defaultDesignWidth))(size)
if (!(designWidth in deviceRatio)) {
throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`)
}
const targetUnit = config.targetUnit || defaultTargetUnit
const unitPrecision = config.unitPrecision || defaultUnitPrecision
const formatSize = ~~size
let rootValue = 1 / deviceRatio[designWidth]
switch (config.targetUnit) {
switch (targetUnit) {
case 'rem':
rootValue *= baseFontSize * 2
break
Expand All @@ -64,9 +66,9 @@ export function getPxTransform (taro) {
break
}
let val = formatSize / rootValue
if (config.unitPrecision >= 0 && config.unitPrecision <= 100) {
val = Number(val.toFixed(config.unitPrecision))
if (unitPrecision >= 0 && unitPrecision <= 100) {
val = Number(val.toFixed(unitPrecision))
}
return val + config.targetUnit
return val + targetUnit
}
}
39 changes: 24 additions & 15 deletions packages/taro-h5/src/api/taro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,22 @@ function getConfig (): Record<string, any> {
return ((taro as any).config ||= {})
}

const defaultDesignWidth = 750
const defaultDesignRatio: TaroGeneral.TDeviceRatio = {
640: 2.34 / 2,
750: 1,
828: 1.81 / 2
}
const defaultBaseFontSize = 20
const defaultUnitPrecision = 5
const defaultTargetUnit = 'rem'

const initPxTransform = function ({
designWidth = 750,
deviceRatio = {
640: 2.34 / 2,
750: 1,
828: 1.81 / 2
} as TaroGeneral.TDeviceRatio,
baseFontSize = 20,
unitPrecision = 5,
targetUnit = 'rem'
designWidth = defaultDesignWidth,
deviceRatio = defaultDesignRatio,
baseFontSize = defaultBaseFontSize,
unitPrecision = defaultUnitPrecision,
targetUnit = defaultTargetUnit
}) {
const config = getConfig.call(this)
config.designWidth = designWidth
Expand All @@ -71,16 +77,19 @@ const initPxTransform = function ({

const pxTransform = function (size = 0) {
const config = getConfig.call(this)
const baseFontSize = config.baseFontSize || 20
const baseFontSize = config.baseFontSize || defaultBaseFontSize
const deviceRatio = config.deviceRatio || defaultDesignRatio
const designWidth = (((input = 0) => isFunction(config.designWidth)
? config.designWidth(input)
: config.designWidth))(size)
if (!(designWidth in config.deviceRatio)) {
throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`)
}
const targetUnit = config.targetUnit || defaultTargetUnit
const unitPrecision = config.unitPrecision || defaultUnitPrecision
const formatSize = ~~size
let rootValue = 1 / config.deviceRatio[designWidth]
switch (config?.targetUnit) {
let rootValue = 1 / deviceRatio[designWidth]
switch (targetUnit) {
case 'vw':
rootValue = designWidth / 100
break
Expand All @@ -92,11 +101,11 @@ const pxTransform = function (size = 0) {
rootValue *= baseFontSize * 2
}
let val: number | string = formatSize / rootValue
if (config.unitPrecision >= 0 && config.unitPrecision <= 100) {
if (unitPrecision >= 0 && unitPrecision <= 100) {
// Number(val): 0.50000 => 0.5
val = Number(val.toFixed(config.unitPrecision))
val = Number(val.toFixed(unitPrecision))
}
return val + config?.targetUnit
return val + targetUnit
}

const canIUseWebp = function () {
Expand Down

0 comments on commit 7b5e869

Please sign in to comment.