提交 9151b16f 编写于 作者: fxy060608's avatar fxy060608

feat: add rpx2px

上级 5e7465a9
declare var tt: any
declare var qa: any
declare var swan: any
declare var qq: any
declare var __PLATFORM__: UniApp.PLATFORM
declare var __PLATFORM_PREFIX__: 'wx' | 'qq' | 'my' | 'swan' | 'tt' | 'qa'
declare var __GLOBAL__: Record<string, any>
......
declare namespace Page {
interface PageInstance {
$page: {
id: number
path: string
route: string
fullPath: string
options: Record<string, any>
meta: UniApp.PageRouteMeta
}
}
}
declare namespace UniApp {
type ClassObj = Record<string, boolean>
type StyleObj = Record<string, any>
......
import { getBaseSystemInfo } from '@dcloudio/uni-platform'
import { createSyncApi } from '../../helpers/api'
import { Upx2pxProtocol } from '../../protocols/base/upx2px'
......@@ -8,7 +9,7 @@ let deviceWidth = 0
let deviceDPR = 0
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = __GLOBAL__.getSystemInfoSync()
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo()
deviceWidth = windowWidth as number
deviceDPR = pixelRatio as number
isIOS = platform === 'ios'
......
......@@ -8084,7 +8084,7 @@ function patchStyle(el, prev, next) {
else if (isString(next)) {
if (prev !== next) {
const current = style.display;
style.cssText = next;
style.cssText = normalizeRpx(next);
// indicates that the `display` of the element is controlled by `v-show`,
// so we always keep the current `display` value regardless of the `style` value,
// thus handing over control to `v-show`.
......@@ -8112,6 +8112,7 @@ function setStyle(style, name, val) {
val.forEach(v => setStyle(style, name, v));
}
else {
val = normalizeRpx(val);
if (name.startsWith('--')) {
// custom property definition
style.setProperty(name, val);
......@@ -8147,7 +8148,19 @@ function autoPrefix(style, rawName) {
}
}
return rawName;
}
}
// fixed by xxxxxx
// upx,rpx
const rpxRE = /\b([+-]?\d+(\.\d+)?)[r|u]px\b/g;
const normalizeRpx = (val) => {
if (isString(val)) {
return val.replace(rpxRE, (a, b) => {
// @ts-ignore
return rpx2px(b) + 'px';
});
}
return val;
};
const xlinkNS = 'http://www.w3.org/1999/xlink';
function patchAttr(el, key, value, isSVG) {
......
......@@ -8084,7 +8084,7 @@ function patchStyle(el, prev, next) {
else if (isString(next)) {
if (prev !== next) {
const current = style.display;
style.cssText = next;
style.cssText = normalizeRpx(next);
// indicates that the `display` of the element is controlled by `v-show`,
// so we always keep the current `display` value regardless of the `style` value,
// thus handing over control to `v-show`.
......@@ -8112,6 +8112,7 @@ function setStyle(style, name, val) {
val.forEach(v => setStyle(style, name, v));
}
else {
val = normalizeRpx(val);
if (name.startsWith('--')) {
// custom property definition
style.setProperty(name, val);
......@@ -8147,7 +8148,19 @@ function autoPrefix(style, rawName) {
}
}
return rawName;
}
}
// fixed by xxxxxx
// upx,rpx
const rpxRE = /\b([+-]?\d+(\.\d+)?)[r|u]px\b/g;
const normalizeRpx = (val) => {
if (isString(val)) {
return val.replace(rpxRE, (a, b) => {
// @ts-ignore
return rpx2px(b) + 'px';
});
}
return val;
};
const xlinkNS = 'http://www.w3.org/1999/xlink';
function patchAttr(el, key, value, isSVG) {
......
......@@ -3339,6 +3339,33 @@ function getRealPath$1(filePath) {
}
return filePath;
}
const ua = navigator.userAgent;
const isAndroid = /android/i.test(ua);
const isIOS$1 = /iphone|ipad|ipod/i.test(ua);
function getScreenFix() {
return /^Apple/.test(navigator.vendor) && typeof window.orientation === "number";
}
function isLandscape(screenFix) {
return screenFix && Math.abs(window.orientation) === 90;
}
function getScreenWidth(screenFix, landscape) {
return screenFix ? Math[landscape ? "max" : "min"](screen.width, screen.height) : screen.width;
}
function getScreenHeight(screenFix, landscape) {
return screenFix ? Math[landscape ? "min" : "max"](screen.height, screen.width) : screen.height;
}
function getWindowWidth(screenWidth) {
return Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth;
}
function getBaseSystemInfo() {
const screenFix = getScreenFix();
const windowWidth = getWindowWidth(getScreenWidth(screenFix, isLandscape(screenFix)));
return {
platform: isIOS$1 ? "ios" : "other",
pixelRatio: window.devicePixelRatio,
windowWidth
};
}
var index_vue_vue_type_style_index_0_lang$d = "\nuni-image {\r\n width: 320px;\r\n height: 240px;\r\n display: inline-block;\r\n overflow: hidden;\r\n position: relative;\n}\nuni-image[hidden] {\r\n display: none;\n}\nuni-image > div {\r\n width: 100%;\r\n height: 100%;\n}\nuni-image > img {\r\n -webkit-touch-callout: none;\r\n -webkit-user-select: none;\r\n -moz-user-select: none;\r\n display: block;\r\n position: absolute;\r\n top: 0;\r\n left: 0;\r\n width: 100%;\r\n height: 100%;\r\n opacity: 0;\n}\nuni-image > .uni-image-will-change {\r\n will-change: transform;\n}\r\n";
const _sfc_main$j = {
name: "Image",
......@@ -7736,14 +7763,14 @@ const Upx2pxProtocol = [
];
const EPS = 1e-4;
const BASE_DEVICE_WIDTH = 750;
let isIOS$1 = false;
let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const {platform, pixelRatio: pixelRatio2, windowWidth} = __GLOBAL__.getSystemInfoSync();
const {platform, pixelRatio: pixelRatio2, windowWidth} = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio2;
isIOS$1 = platform === "ios";
isIOS = platform === "ios";
}
const upx2px = /* @__PURE__ */ createSyncApi("upx2px", (number, newDeviceWidth) => {
if (deviceWidth === 0) {
......@@ -7759,7 +7786,7 @@ const upx2px = /* @__PURE__ */ createSyncApi("upx2px", (number, newDeviceWidth)
}
result = Math.floor(result + EPS);
if (result === 0) {
if (deviceDPR === 1 || !isIOS$1) {
if (deviceDPR === 1 || !isIOS) {
result = 1;
} else {
result = 0.5;
......@@ -7984,24 +8011,20 @@ const canIUse = /* @__PURE__ */ createSyncApi("canIUse", (schema) => {
const makePhoneCall = /* @__PURE__ */ createAsyncApi("makePhoneCall", (option) => {
window.location.href = `tel:${option.phoneNumber}`;
}, MakePhoneCallProtocol);
const ua = navigator.userAgent;
const isAndroid = /android/i.test(ua);
const isIOS = /iphone|ipad|ipod/i.test(ua);
const getSystemInfoSync = /* @__PURE__ */ createSyncApi("getSystemInfoSync", () => {
var screen = window.screen;
var pixelRatio2 = window.devicePixelRatio;
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 screenHeight = screenFix ? Math[landscape ? "min" : "max"](screen.height, screen.width) : screen.height;
var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth;
var windowHeight = window.innerHeight;
var language = navigator.language;
var statusBarHeight = out.top;
var osname;
var osversion;
var model;
if (isIOS) {
const pixelRatio2 = window.devicePixelRatio;
const screenFix = getScreenFix();
const landscape = isLandscape(screenFix);
const screenWidth = getScreenWidth(screenFix, landscape);
const screenHeight = getScreenHeight(screenFix, landscape);
const windowWidth = getWindowWidth(screenWidth);
let windowHeight = window.innerHeight;
const language = navigator.language;
const statusBarHeight = out.top;
let osname;
let osversion;
let model;
if (isIOS$1) {
osname = "iOS";
const osversionFind = ua.match(/OS\s([\w_]+)\slike/);
if (osversionFind) {
......@@ -8053,9 +8076,9 @@ const getSystemInfoSync = /* @__PURE__ */ createSyncApi("getSystemInfoSync", ()
osname = "Other";
osversion = "0";
}
var system = `${osname} ${osversion}`;
var platform = osname.toLocaleLowerCase();
var safeArea = {
const system = `${osname} ${osversion}`;
const platform = osname.toLocaleLowerCase();
const safeArea = {
left: out.left,
right: windowWidth - out.right,
top: out.top,
......
......@@ -49,7 +49,7 @@ function initPublicPage(route: RouteLocationNormalizedLoaded) {
}
export function initPage(vm: ComponentPublicInstance) {
currentPages.push(vm as Page.PageInstance)
currentPages.push((vm as unknown) as Page.PageInstance)
const route = vm.$route
;(vm as any).$page = initPublicPage(route)
}
......
export const ua = navigator.userAgent
export const isAndroid = /android/i.test(ua)
export const isIOS = /iphone|ipad|ipod/i.test(ua)
export function getScreenFix() {
return (
/^Apple/.test(navigator.vendor) && typeof window.orientation === 'number'
)
}
export function isLandscape(screenFix: boolean) {
return screenFix && Math.abs(window.orientation as number) === 90
}
export function getScreenWidth(screenFix: boolean, landscape: boolean) {
return screenFix
? Math[landscape ? 'max' : 'min'](screen.width, screen.height)
: screen.width
}
export function getScreenHeight(screenFix: boolean, landscape: boolean) {
return screenFix
? Math[landscape ? 'min' : 'max'](screen.height, screen.width)
: screen.height
}
export function getWindowWidth(screenWidth: number) {
return (
Math.min(
window.innerWidth,
document.documentElement.clientWidth,
screenWidth
) || screenWidth
)
}
/**
* 简易版systemInfo,主要为upx2px服务
* @returns
*/
export function getBaseSystemInfo() {
const screenFix = getScreenFix()
const windowWidth = getWindowWidth(
getScreenWidth(screenFix, isLandscape(screenFix))
)
return {
platform: isIOS ? 'ios' : 'other',
pixelRatio: window.devicePixelRatio,
windowWidth,
}
}
export * from './getRealPath'
export { getBaseSystemInfo } from './getBaseSystemInfo'
......@@ -4,45 +4,36 @@ import { createSyncApi } from '@dcloudio/uni-api'
import { getWindowOffset } from '@dcloudio/uni-core'
const ua = navigator.userAgent
/**
* 是否安卓设备
*/
const isAndroid = /android/i.test(ua)
/**
* 是否iOS设备
*/
const isIOS = /iphone|ipad|ipod/i.test(ua)
import {
ua,
isIOS,
isAndroid,
isLandscape,
getScreenFix,
getScreenWidth,
getWindowWidth,
getScreenHeight,
} from '../../../platform/getBaseSystemInfo'
/**
* 获取系统信息-同步
*/
export const getSystemInfoSync = createSyncApi<typeof uni.getSystemInfoSync>(
'getSystemInfoSync',
() => {
var screen = window.screen
var pixelRatio = window.devicePixelRatio
const pixelRatio = window.devicePixelRatio
// 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
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 screenHeight = screenFix
? Math[landscape ? 'min' : 'max'](screen.height, screen.width)
: screen.height
var windowWidth =
Math.min(
window.innerWidth,
document.documentElement.clientWidth,
screenWidth
) || screenWidth
var windowHeight = window.innerHeight
var language = navigator.language
var statusBarHeight = safeAreaInsets.top
var osname
var osversion
var model
const screenFix = getScreenFix()
const landscape = isLandscape(screenFix)
const screenWidth = getScreenWidth(screenFix, landscape)
const screenHeight = getScreenHeight(screenFix, landscape)
const windowWidth = getWindowWidth(screenWidth)
let windowHeight = window.innerHeight
const language = navigator.language
const statusBarHeight = safeAreaInsets.top
let osname
let osversion
let model
if (isIOS) {
osname = 'iOS'
......@@ -99,9 +90,9 @@ export const getSystemInfoSync = createSyncApi<typeof uni.getSystemInfoSync>(
osversion = '0'
}
var system = `${osname} ${osversion}`
var platform = osname.toLocaleLowerCase()
var safeArea = {
const system = `${osname} ${osversion}`
const platform = osname.toLocaleLowerCase()
const safeArea = {
left: safeAreaInsets.left,
right: windowWidth - safeAreaInsets.right,
top: safeAreaInsets.top,
......
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-mp-alipay/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "my",
"__PLATFORM__": "\"mp-alipay\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return my.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = my.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return my.getSystemInfoSync()
}
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-mp-baidu/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "swan",
"__PLATFORM__": "\"mp-baidu\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return swan.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = swan.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return swan.getSystemInfoSync()
}
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-mp-qq/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "qq",
"__PLATFORM__": "\"mp-qq\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return qq.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = qq.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return qq.getSystemInfoSync()
}
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-mp-toutiao/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "tt",
"__PLATFORM__": "\"mp-toutiao\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return tt.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = tt.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return tt.getSystemInfoSync()
}
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-mp-weixin/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "wx",
"__PLATFORM__": "\"mp-weixin\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return wx.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = wx.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return wx.getSystemInfoSync()
}
......@@ -3,6 +3,14 @@
"src/runtime/index.ts": "dist/uni.mp.esm.js",
"src/api/index.ts": "dist/uni.api.esm.js"
},
"alias": {
"entries": [
{
"find": "@dcloudio/uni-platform",
"replacement": "packages/uni-quickapp-webview/src/platform/index.ts"
}
]
},
"replacements": {
"__GLOBAL__": "qa",
"__PLATFORM__": "\"quickapp-webview\"",
......
......@@ -301,6 +301,10 @@ function createApi(type, name, fn, protocol, options) {
}
}
function getBaseSystemInfo() {
return qa.getSystemInfoSync()
}
const Upx2pxProtocol = [
{
name: 'upx',
......@@ -315,7 +319,7 @@ let isIOS = false;
let deviceWidth = 0;
let deviceDPR = 0;
function checkDeviceWidth() {
const { platform, pixelRatio, windowWidth } = qa.getSystemInfoSync();
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
deviceWidth = windowWidth;
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
......
export function getBaseSystemInfo() {
return qa.getSystemInfoSync()
}
......@@ -51,11 +51,11 @@ const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
const uniInjectPluginOptions: Partial<InjectOptions> = {
exclude: [...COMMON_EXCLUDE],
'__GLOBAL__.': '@dcloudio/uni-h5',
'uni.': '@dcloudio/uni-h5',
getApp: ['@dcloudio/uni-h5', 'getApp'],
getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
UniViewJSBridge: ['@dcloudio/uni-h5', 'UniViewJSBridge'],
}
export function resolvePlugins(
......
......@@ -27,7 +27,8 @@ export function uniPagesJsonPlugin(
if (id.endsWith(PAGES_JSON_JS)) {
return {
code:
(options.command === 'serve' ? registerGlobalCode : '') +
registerGlobalCode +
(options.command === 'serve' ? registerDevServerGlobalCode : '') +
parsePagesJson(code, config, options),
map: { mappings: '' },
}
......@@ -79,10 +80,14 @@ const hmrCode = `if(import.meta.hot){
})
}`
const registerGlobalCode = `import {uni,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5'
const registerGlobalCode = `import {upx2px} from '@dcloudio/uni-h5'
window.rpx2px = upx2px
`
const registerDevServerGlobalCode = `import {uni,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5'
window.getApp = getApp
window.getCurrentPages = getCurrentPages
window.uni = window.__GLOBAL__ = uni
window.uni = uni
window.UniViewJSBridge = UniViewJSBridge
window.UniServiceJSBridge = UniServiceJSBridge
`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册