Skip to content

Commit

Permalink
fix(api): update pxtransform api fix #12475
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakaryCode committed Sep 20, 2022
1 parent 663ab56 commit fc097b6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/taro-api/src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ const defaultDesignRatio = {
750: 1,
828: 1.81 / 2
}
const defaultBaseFontSize = 20

export function getInitPxTransform (taro) {
return function (config) {
const {
designWidth = defaultDesignWidth,
deviceRatio = defaultDesignRatio
deviceRatio = defaultDesignRatio,
baseFontSize = defaultBaseFontSize
} = config
taro.config = taro.config || {}
taro.config.designWidth = designWidth
taro.config.deviceRatio = deviceRatio
taro.config.baseFontSize = baseFontSize
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/taro-h5/src/api/taro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ const initPxTransform = getInitPxTransform(taro)
const requirePlugin = permanentlyNotSupport('requirePlugin')

const pxTransform = function (size) {
// @ts-ignore
const { designWidth } = taro.config
return Math.ceil((((parseInt(size, 10) / 40) * 640) / designWidth) * 10000) / 10000 + 'rem'
const options = (taro as any).config
const baseFontSize = options.baseFontSize || 20
const designWidth = ((input = 0) => typeof options.designWidth === 'function'
? options.designWidth(input)
: options.designWidth)
const rootValue = (input = 0) => baseFontSize / options.deviceRatio[designWidth(input)] * 2
return Math.ceil((parseInt(size, 10) / rootValue(size)) * 10000) / 10000 + 'rem'
}

const canIUseWebp = function () {
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-loader/src/h5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ var inst = ${creator}(component, ${frameworkArgs})
${routerCreator}(inst, config, ${importFrameworkName})
initPxTransform({
designWidth: ${pxTransformConfig.designWidth},
deviceRatio: ${JSON.stringify(pxTransformConfig.deviceRatio)}
deviceRatio: ${JSON.stringify(pxTransformConfig.deviceRatio)},
baseFontSize: ${pxTransformConfig.baseFontSize || (pxTransformConfig.minRootSize >= 1 ? pxTransformConfig.minRootSize : 20)}
})
`
return code
Expand Down

0 comments on commit fc097b6

Please sign in to comment.