Skip to content

Commit

Permalink
fix(h5): 优化路由接口以及修复 onResize 回调参数缺失的字段 (#14967)
Browse files Browse the repository at this point in the history
Co-authored-by: Zakary <zakarycode@gmail.com>
  • Loading branch information
guoenxuan and ZakaryCode authored Feb 2, 2024
1 parent cd993e8 commit 7245127
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/taro-components/types/PageMeta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ interface PageMetaProps extends StandardProps {
}
declare namespace PageMetaProps {
interface onResizeEventDetail {
/** 设备方向 */
deviceOrientation?: 'portrait' | 'landscape'
/** 窗口尺寸 */
size: resizeType
}
Expand All @@ -73,6 +75,10 @@ declare namespace PageMetaProps {
windowWidth: number
/** 窗口高度 */
windowHeight: number
/** 屏幕宽度 */
screenWidth?: number
/** 屏幕高度 */
screenHeight?: number
}
interface onScrollEventDetail {
scrollTop: number
Expand Down
20 changes: 19 additions & 1 deletion packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
if ('url' in option) {
const pathPieces = processNavigateUrl(option)
const state = { timestamp: Date.now() }
if (pathPieces.pathname) {
const originPath = routesAlias.getOrigin(pathPieces.pathname)
const pagePath = originPath.startsWith('/') ? originPath.substring(1) : originPath
if (!RouterConfig.pages.includes(pagePath)) {
const res = { errMsg: `${method}:fail page ${pagePath} is not found` }
fail?.(res)
complete?.(res)
if (fail || complete) {
return resolve(res)
} else {
return reject(res)
}
}
}
if (method === 'navigateTo') {
history.push(pathPieces, state)
} else if (method === 'redirectTo' || method === 'switchTab') {
Expand All @@ -75,7 +89,11 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
const res = { errMsg: `${method}:fail ${error.message || error}` }
fail?.(res)
complete?.(res)
reject(res)
if (fail || complete) {
return resolve(res)
} else {
return reject(res)
}
}
})
}
Expand Down
18 changes: 12 additions & 6 deletions packages/taro-router/src/events/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ export function bindPageResize (page: PageInstance) {
pageResizeFn && window.removeEventListener('resize', pageResizeFn)

pageResizeFn = function () {
page.onResize && page.onResize({
size: {
windowHeight: window.innerHeight,
windowWidth: window.innerWidth
}
})
if (page.onResize) {
const mediaQuery = window.matchMedia('(orientation: portrait)')
page.onResize({
deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
size: {
windowHeight: window.innerHeight,
windowWidth: window.innerWidth,
screenHeight: window.screen.height,
screenWidth: window.screen.width,
}
})
}
}

window.addEventListener('resize', pageResizeFn, false)
Expand Down
3 changes: 3 additions & 0 deletions packages/taro/types/taro.lifecycle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ declare module './index' {
}

interface PageResizeObject {
deviceOrientation?: 'portrait' | 'landscape'
size: {
windowWidth: number
windowHeight: number
screenWidth?: number
screenHeight?: number
}
}

Expand Down

0 comments on commit 7245127

Please sign in to comment.