Skip to content

Commit 2461f33

Browse files
committed
refactor: 重构uniPage.width、height、statusBarHeight
1 parent ac74394 commit 2461f33

File tree

2 files changed

+34
-55
lines changed

2 files changed

+34
-55
lines changed

packages/uni-h5/src/framework/setup/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import { handleBeforeEntryPageRoutes } from '../../service/api/route/utils'
5454
//#if _X_
5555
import { isDialogPageInstance } from '../../x/framework/helpers/utils'
5656
import {
57-
initPageWidthHeight,
5857
triggerDialogPageOnHide,
5958
useBackgroundColorContent,
6059
} from '../../x/framework/setup/page'
@@ -163,7 +162,6 @@ export function setupPage(comp: any) {
163162
})
164163
onMounted(() => {
165164
if (__X__) {
166-
initPageWidthHeight(instance)
167165
if (instance.subTree.el) {
168166
instance.subTree.el._page = instance.proxy?.$page as UniPage
169167
}

packages/uni-h5/src/x/framework/setup/page.ts

+34-53
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,46 @@ type PageStyle = {
5353
export const homeDialogPages: UniDialogPage[] = []
5454
export const homeSystemDialogPages: UniDialogPage[] = []
5555

56-
function isDialogPageImpl(page: UniPage): boolean {
57-
return page instanceof UniDialogPageImpl
56+
function getPageElement(page: UniPage): HTMLElement {
57+
if (__NODE_JS__) {
58+
throw new Error('Not support get page element in non-browser environment')
59+
}
60+
const currentPage = getCurrentPage() as unknown as UniPage
61+
if (page !== currentPage) {
62+
throw new Error("Can't get element of other page")
63+
}
64+
const pageEle = document.querySelector(
65+
`uni-page[data-page="${page.vm?.route}"]`
66+
)
67+
if (!pageEle) {
68+
throw new Error('page not found')
69+
}
70+
return pageEle as HTMLElement
5871
}
5972

6073
class UniPageImpl implements UniPage {
6174
route: string
6275
options: UTSJSONObject
6376
vm: ComponentPublicInstance | null
6477
$vm: ComponentPublicInstance | null
65-
width: number = 0
66-
height: number = 0
67-
statusBarHeight: number = safeAreaInsets.top
78+
79+
get statusBarHeight(): number {
80+
return safeAreaInsets.top
81+
}
82+
83+
get width(): number {
84+
return this.pageBody.width
85+
}
86+
87+
get height(): number {
88+
const pageEle = getPageElement(this)
89+
const pageHead = pageEle.querySelector('uni-page-head')
90+
return this.pageBody.height + (pageHead ? pageHead.clientHeight : 0)
91+
}
92+
6893
get pageBody(): UniPageBody {
69-
if (__NODE_JS__) {
70-
throw new Error('Not support pageBody in non-browser environment')
71-
}
72-
const currentPage = getCurrentPage() as unknown as UniPage
73-
let container: Document | Element = document
74-
if (isDialogPageImpl(this)) {
75-
const dialogPage = document.querySelector(
76-
`uni-page[data-page="${this.vm?.route}"]`
77-
)
78-
if (!dialogPage) {
79-
throw new Error('dialogPage not found')
80-
}
81-
container = dialogPage
82-
} else if (this !== currentPage) {
83-
throw new Error("Can't get pageBody of other page")
84-
}
85-
const pageBody = container.querySelector('uni-page-wrapper') as HTMLElement
94+
const pageEle = getPageElement(this)
95+
const pageBody = pageEle.querySelector('uni-page-wrapper') as HTMLElement
8696
const pageWrapperInfo = getPageWrapperInfo(pageBody)
8797
return {
8898
top: pageWrapperInfo.top,
@@ -94,23 +104,8 @@ class UniPageImpl implements UniPage {
94104
}
95105
}
96106
get safeAreaInsets(): UniSafeAreaInsets {
97-
if (__NODE_JS__) {
98-
throw new Error('Not support safeAreaInsets in non-browser environment')
99-
}
100-
const currentPage = getCurrentPage() as unknown as UniPage
101-
let container: Document | Element = document
102-
if (isDialogPageImpl(this)) {
103-
const dialogPage = document.querySelector(
104-
`uni-page[data-page="${this.vm?.route}"]`
105-
)
106-
if (!dialogPage) {
107-
throw new Error('dialogPage not found')
108-
}
109-
container = dialogPage
110-
} else if (this !== currentPage) {
111-
throw new Error("Can't get safeAreaInsets of other page")
112-
}
113-
const pageBody = container.querySelector('uni-page-wrapper') as HTMLElement
107+
const pageEle = getPageElement(this)
108+
const pageBody = pageEle.querySelector('uni-page-wrapper') as HTMLElement
114109
return getSafeAreaInsets(pageBody)
115110
}
116111
getPageStyle(): UTSJSONObject {
@@ -404,17 +399,3 @@ export function triggerDialogPageOnHide(instance: ComponentInternalInstance) {
404399
}
405400
dialogPageTriggerParentHide(instance.proxy?.$page as UniDialogPage)
406401
}
407-
export function initPageWidthHeight(instance: ComponentInternalInstance) {
408-
if (!instance.proxy) {
409-
return
410-
}
411-
const pageEl = document.querySelector(
412-
`uni-page[data-page="${instance.proxy.$vm.route}"]`
413-
) as HTMLElement
414-
if (pageEl) {
415-
// @ts-expect-error
416-
;(instance.proxy as UniPage).width = pageEl.offsetWidth
417-
// @ts-expect-error
418-
;(instance.proxy as UniPage).height = pageEl.offsetHeight
419-
}
420-
}

0 commit comments

Comments
 (0)