@@ -53,36 +53,46 @@ type PageStyle = {
53
53
export const homeDialogPages : UniDialogPage [ ] = [ ]
54
54
export const homeSystemDialogPages : UniDialogPage [ ] = [ ]
55
55
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
58
71
}
59
72
60
73
class UniPageImpl implements UniPage {
61
74
route : string
62
75
options : UTSJSONObject
63
76
vm : ComponentPublicInstance | null
64
77
$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
+
68
93
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
86
96
const pageWrapperInfo = getPageWrapperInfo ( pageBody )
87
97
return {
88
98
top : pageWrapperInfo . top ,
@@ -94,23 +104,8 @@ class UniPageImpl implements UniPage {
94
104
}
95
105
}
96
106
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
114
109
return getSafeAreaInsets ( pageBody )
115
110
}
116
111
getPageStyle ( ) : UTSJSONObject {
@@ -404,17 +399,3 @@ export function triggerDialogPageOnHide(instance: ComponentInternalInstance) {
404
399
}
405
400
dialogPageTriggerParentHide ( instance . proxy ?. $page as UniDialogPage )
406
401
}
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