提交 a1c6e0ed 编写于 作者: Q qiang

feat(h5): useRem

上级 150e4bc5
...@@ -34,6 +34,10 @@ declare namespace UniApp { ...@@ -34,6 +34,10 @@ declare namespace UniApp {
globalStyle: { globalStyle: {
navigationBar: PageNavigationBar navigationBar: PageNavigationBar
refreshOptions?: PageRefreshOptions refreshOptions?: PageRefreshOptions
maxWidth?: number
rpxCalcMaxDeviceWidth?: number
rpxCalcBaseDeviceWidth?: number
// rpxCalcIncludeWidth?: number
} }
topWindow?: LayoutWindowOptions topWindow?: LayoutWindowOptions
leftWindow?: LayoutWindowOptions leftWindow?: LayoutWindowOptions
......
...@@ -19,6 +19,11 @@ function checkDeviceWidth() { ...@@ -19,6 +19,11 @@ function checkDeviceWidth() {
isIOS = platform === 'ios' isIOS = platform === 'ios'
} }
function checkValue(value: unknown, defaultValue: number) {
const newValue = Number(value) as number
return isNaN(newValue) ? defaultValue : newValue
}
export const upx2px = defineSyncApi<API_TYPE_UPX2PX>( export const upx2px = defineSyncApi<API_TYPE_UPX2PX>(
API_UPX2PX, API_UPX2PX,
(number, newDeviceWidth?: number) => { (number, newDeviceWidth?: number) => {
...@@ -34,7 +39,13 @@ export const upx2px = defineSyncApi<API_TYPE_UPX2PX>( ...@@ -34,7 +39,13 @@ export const upx2px = defineSyncApi<API_TYPE_UPX2PX>(
if (number === 0) { if (number === 0) {
return 0 return 0
} }
let result = (number / BASE_DEVICE_WIDTH) * (newDeviceWidth || deviceWidth) const config = __uniConfig.globalStyle || {}
// ignore rpxCalcIncludeWidth
const maxWidth = checkValue(config.rpxCalcMaxDeviceWidth, 960)
const baseWidth = checkValue(config.rpxCalcBaseDeviceWidth, 375)
let width = newDeviceWidth || deviceWidth
width = width <= maxWidth ? width : baseWidth
let result = (number / BASE_DEVICE_WIDTH) * width
if (result < 0) { if (result < 0) {
result = -result result = -result
} }
......
...@@ -3,11 +3,13 @@ import { App } from 'vue' ...@@ -3,11 +3,13 @@ import { App } from 'vue'
import { initLongPress } from './longPress' import { initLongPress } from './longPress'
import { initAppConfig } from './appConfig' import { initAppConfig } from './appConfig'
import { initCustomDataset } from '@dcloudio/uni-shared' import { initCustomDataset } from '@dcloudio/uni-shared'
import { useRem } from './rem'
export function initView(app: App) { export function initView(app: App) {
if (__NODE_JS__) { if (__NODE_JS__) {
return return
} }
useRem()
initCustomDataset() initCustomDataset()
if (__UNI_FEATURE_LONGPRESS__) { if (__UNI_FEATURE_LONGPRESS__) {
initLongPress() initLongPress()
......
function checkValue(value: unknown, defaultValue: number) {
const newValue = Number(value) as number
return isNaN(newValue) ? defaultValue : newValue
}
function getWindowWidth() {
const screenFix =
/^Apple/.test(navigator.vendor) && typeof window.orientation === 'number'
const landscape = screenFix && Math.abs(window.orientation as number) === 90
var screenWidth = screenFix
? Math[landscape ? 'max' : 'min'](screen.width, screen.height)
: screen.width
var windowWidth =
Math.min(
window.innerWidth,
document.documentElement.clientWidth,
screenWidth
) || screenWidth
return windowWidth
}
export function useRem() {
// TODO 与 upx2px 合并抽离逻辑到 shared,区分 service、view
function updateRem() {
const config = __uniConfig.globalStyle || {}
// ignore: rpxCalcIncludeWidth
const maxWidth = checkValue(config.rpxCalcMaxDeviceWidth, 960)
const baseWidth = checkValue(config.rpxCalcBaseDeviceWidth, 375)
let width = getWindowWidth()
width = width <= maxWidth ? width : baseWidth
document.documentElement.style.fontSize = width / 23.4375 + 'px'
}
updateRem()
document.addEventListener('DOMContentLoaded', updateRem)
window.addEventListener('load', updateRem)
window.addEventListener('resize', updateRem)
}
...@@ -967,7 +967,33 @@ function initAppConfig$1(appConfig) { ...@@ -967,7 +967,33 @@ function initAppConfig$1(appConfig) {
globalProperties.$gcd = getComponentDescriptor; globalProperties.$gcd = getComponentDescriptor;
} }
} }
function checkValue$1(value, defaultValue) {
const newValue = Number(value);
return isNaN(newValue) ? defaultValue : newValue;
}
function getWindowWidth$1() {
const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === "number";
const landscape = screenFix && Math.abs(window.orientation) === 90;
var screenWidth = screenFix ? Math[landscape ? "max" : "min"](screen.width, screen.height) : screen.width;
var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth;
return windowWidth;
}
function useRem() {
function updateRem() {
const config = __uniConfig.globalStyle || {};
const maxWidth = checkValue$1(config.rpxCalcMaxDeviceWidth, 960);
const baseWidth = checkValue$1(config.rpxCalcBaseDeviceWidth, 375);
let width = getWindowWidth$1();
width = width <= maxWidth ? width : baseWidth;
document.documentElement.style.fontSize = width / 23.4375 + "px";
}
updateRem();
document.addEventListener("DOMContentLoaded", updateRem);
window.addEventListener("load", updateRem);
window.addEventListener("resize", updateRem);
}
function initView(app) { function initView(app) {
useRem();
initCustomDataset(); initCustomDataset();
if (__UNI_FEATURE_LONGPRESS__) { if (__UNI_FEATURE_LONGPRESS__) {
initLongPress(); initLongPress();
...@@ -9881,6 +9907,10 @@ function checkDeviceWidth() { ...@@ -9881,6 +9907,10 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio2; deviceDPR = pixelRatio2;
isIOS = platform === "ios"; isIOS = platform === "ios";
} }
function checkValue(value, defaultValue) {
const newValue = Number(value);
return isNaN(newValue) ? defaultValue : newValue;
}
const upx2px = /* @__PURE__ */ defineSyncApi(API_UPX2PX, (number, newDeviceWidth) => { const upx2px = /* @__PURE__ */ defineSyncApi(API_UPX2PX, (number, newDeviceWidth) => {
if (deviceWidth === 0) { if (deviceWidth === 0) {
checkDeviceWidth(); checkDeviceWidth();
...@@ -9889,7 +9919,12 @@ const upx2px = /* @__PURE__ */ defineSyncApi(API_UPX2PX, (number, newDeviceWidth ...@@ -9889,7 +9919,12 @@ const upx2px = /* @__PURE__ */ defineSyncApi(API_UPX2PX, (number, newDeviceWidth
if (number === 0) { if (number === 0) {
return 0; return 0;
} }
let result = number / BASE_DEVICE_WIDTH * (newDeviceWidth || deviceWidth); const config = __uniConfig.globalStyle || {};
const maxWidth = checkValue(config.rpxCalcMaxDeviceWidth, 960);
const baseWidth = checkValue(config.rpxCalcBaseDeviceWidth, 375);
let width = newDeviceWidth || deviceWidth;
width = width <= maxWidth ? width : baseWidth;
let result = number / BASE_DEVICE_WIDTH * width;
if (result < 0) { if (result < 0) {
result = -result; result = -result;
} }
......
...@@ -13,6 +13,7 @@ body { ...@@ -13,6 +13,7 @@ body {
body { body {
overflow-x: hidden; overflow-x: hidden;
font-size: 16px;
} }
uni-app, uni-app,
......
...@@ -44,6 +44,7 @@ const defaultRpx2Unit = { ...@@ -44,6 +44,7 @@ const defaultRpx2Unit = {
unitPrecision: 5, unitPrecision: 5,
}; };
function createRpx2Unit(unit, unitRatio, unitPrecision) { function createRpx2Unit(unit, unitRatio, unitPrecision) {
// ignore: rpxCalcIncludeWidth
return (val) => val.replace(unitRE, (m, $1) => { return (val) => val.replace(unitRE, (m, $1) => {
if (!$1) { if (!$1) {
return m; return m;
......
...@@ -40,6 +40,7 @@ const defaultRpx2Unit = { ...@@ -40,6 +40,7 @@ const defaultRpx2Unit = {
unitPrecision: 5, unitPrecision: 5,
}; };
function createRpx2Unit(unit, unitRatio, unitPrecision) { function createRpx2Unit(unit, unitRatio, unitPrecision) {
// ignore: rpxCalcIncludeWidth
return (val) => val.replace(unitRE, (m, $1) => { return (val) => val.replace(unitRE, (m, $1) => {
if (!$1) { if (!$1) {
return m; return m;
......
...@@ -22,6 +22,7 @@ export function createRpx2Unit( ...@@ -22,6 +22,7 @@ export function createRpx2Unit(
unitRatio: number, unitRatio: number,
unitPrecision: number unitPrecision: number
) { ) {
// ignore: rpxCalcIncludeWidth
return (val: string) => return (val: string) =>
val.replace(unitRE, (m, $1) => { val.replace(unitRE, (m, $1) => {
if (!$1) { if (!$1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册