Skip to content

Commit

Permalink
fix(uni-app-x web): 修复page对象innerWidth、innerHeight、safeAreaInsets实现错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Jan 14, 2025
1 parent 4e38d1b commit 5386285
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions packages/uni-h5/src/x/framework/setup/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ import {
} from '../../../framework/setup/page'
import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router'
import { isDialogPageInstance } from '../helpers/utils'
import { getWindowInfo } from '../../../service/api/device/getWindowInfo'
import type { UniSafeAreaInsets } from '@dcloudio/uni-app-x/types/native/UniSafeAreaInsets'
import safeAreaInsets from 'safe-area-insets'

const getSystemSafeAreaInsets = function () {
return {
top: safeAreaInsets.top,
right: safeAreaInsets.right,
bottom: safeAreaInsets.bottom,
left: safeAreaInsets.left,
}
}

let escBackPageNum = 0
type PageStyle = {
Expand All @@ -44,13 +53,51 @@ class UniPageImpl implements UniPage {
vm: ComponentPublicInstance | null
$vm: ComponentPublicInstance | null
get innerWidth(): number {
return getWindowInfo().windowWidth
const currentPage = getCurrentPage() as unknown as UniPage
if (currentPage !== this) {
throw new Error("Can't get innerWidth of other page")
}
// 非uni-app-x uni-page-body是自动高度
const pageWrapper = document.querySelector('uni-page-wrapper')
return pageWrapper!.clientWidth
}
get innerHeight(): number {
return getWindowInfo().windowHeight
const currentPage = getCurrentPage() as unknown as UniPage
if (currentPage !== this) {
throw new Error("Can't get innerHeight of other page")
}
// 非uni-app-x uni-page-body是自动高度
const pageWrapper = document.querySelector('uni-page-wrapper')
return pageWrapper!.clientHeight
}
get safeAreaInsets(): UniSafeAreaInsets {
return getWindowInfo().safeAreaInsets
const currentPage = getCurrentPage() as unknown as UniPage
if (currentPage !== this) {
throw new Error("Can't get safeAreaInsets of other page")
}
const pageWrapper = document.querySelector(
'uni-page-wrapper'
) as HTMLElement
const pageWrapperRect = pageWrapper.getBoundingClientRect()
const systemSafeAreaInsets = getSystemSafeAreaInsets()

const bodyRect = document.body.getBoundingClientRect()
const pageWrapperEdge = {
top: pageWrapperRect.top,
left: pageWrapperRect.left,
right: bodyRect.right - pageWrapperRect.right,
bottom: bodyRect.bottom - pageWrapperRect.bottom,
}

const computeEdge = (bodyEdge: number, nativeEdge: number) => {
return Math.max(0, nativeEdge - bodyEdge)
}
return {
top: computeEdge(pageWrapperEdge.top, systemSafeAreaInsets.top),
left: computeEdge(pageWrapperEdge.left, systemSafeAreaInsets.left),
right: computeEdge(pageWrapperEdge.right, systemSafeAreaInsets.right),
bottom: computeEdge(pageWrapperEdge.bottom, systemSafeAreaInsets.bottom),
}
}
getPageStyle(): UTSJSONObject {
const pageMeta = this.vm?.$basePage.meta
Expand Down

0 comments on commit 5386285

Please sign in to comment.