Skip to content

Commit

Permalink
fix: 修复导航栏配置无效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
guoenxuan committed Jun 18, 2024
1 parent 5fea6f9 commit c410398
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 78 deletions.
18 changes: 15 additions & 3 deletions packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,23 @@ export class NativeApi {
return options
}

// @ts-ignore
@(asyncAndRelease)
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setNavigationStyle (options: any): void {}

// @ts-ignore
@(asyncAndRelease)
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setCapsuleState (options: any): void {}

// @ts-ignore
@(syncAndRelease)
setNavigationStyle (options: any): any {
return options
}
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getCapsuleState (options: any): any {}

// @ts-ignore
@(syncAndRelease)
Expand Down
48 changes: 0 additions & 48 deletions packages/taro-platform-harmony-hybrid/src/api/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import Taro from '@tarojs/api'

import native from './NativeApi'

export * from './ad'
export * from './ai'
export * from './alipay'
Expand All @@ -28,47 +24,3 @@ export * from './swan'
export * from './ui'
export * from './worker'
export * from './wxml'

if (typeof window !== 'undefined') {
// @ts-ignore
window.currentNavigation = {}
}

Taro.eventCenter.on('__taroSetNavigationStyle', (style, textStyle, backgroundColor) => {
if (typeof window !== 'undefined') {
native.setNavigationStyle({ style: style, textStyle: textStyle, backgroundColor: backgroundColor })
// @ts-ignore
Object.assign(window.currentNavigation, {
style,
textStyle,
backgroundColor,
})
// @ts-ignore
if (typeof window.originCapsuleState !== 'undefined') {
// @ts-ignore
window.native?.setCapsuleState?.(window.originCapsuleState)
}
}
})

Taro.eventCenter.on('__taroEnterFullScreen', () => {
native.setNavigationStyle({ style: 'custom', textStyle: 'black', backgroundColor: '#000000' })
// @ts-ignore
if (typeof window.originCapsuleState === 'undefined') {
// @ts-ignore
window.originCapsuleState = window.native?.getCapsuleState().visible
}
// @ts-ignore
window.native?.setCapsuleState?.(false)
})

Taro.eventCenter.on('__taroExitFullScreen', () => {
// @ts-ignore
const { style, textStyle, backgroundColor } = window.currentNavigation
native.setNavigationStyle({ style: style, textStyle: textStyle, backgroundColor: backgroundColor })
// @ts-ignore
if (typeof window.originCapsuleState !== 'undefined') {
// @ts-ignore
window.native?.setCapsuleState?.(window.originCapsuleState)
}
})
5 changes: 5 additions & 0 deletions packages/taro-platform-harmony-hybrid/src/api/apis/taro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
getApp,
getCurrentInstance,
getCurrentPages,
loadNavigationStyle,
navigateBack,
navigateTo,
nextTick,
redirectTo,
registerNavigationStyleHandler,
reLaunch,
switchTab,
} from './index'
Expand All @@ -22,6 +24,9 @@ const requirePlugin = () => {
}
}

loadNavigationStyle()
registerNavigationStyleHandler()

const {
Behavior,
getEnv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import Taro from '@tarojs/api'
import { addStyle } from '@tarojs/router/dist/style'
import { setTitle } from '@tarojs/router/dist/utils/navigate'

import native from '../../NativeApi'
import { getParameterError, shouldBeObject } from '../../utils'
import { MethodHandler } from '../../utils/handler'

/**
* 导航条与Web层叠布局,导航条可见情况下Web页面顶部空出一定高度的留白
*/
function loadNavigationSytle () {
if (typeof window === 'undefined') {
return
}
// @ts-ignore
const naviHeight = window.navigationHeight ? window.navigationHeight : 0
const css = `
.taro_router .taro_page.taro_navigation_page {
padding-top: ${naviHeight}px;
}
.taro-tabbar__container .taro_page.taro_navigation_page {
max-height: calc(100vh - ${naviHeight}px);
}
.taro-tabbar__container .taro_page.taro_tabbar_page.taro_navigation_page {
max-height: calc(100vh - 50px - ${naviHeight}px);
}`
addStyle(css)
}

loadNavigationSytle()

/**
* 显示页面导航条加载图标
*
Expand Down Expand Up @@ -149,3 +122,4 @@ export const setNavigationBarColor: typeof Taro.setNavigationBarColor = (options
* @null_implementation
*/
export const hideHomeButton = () => Promise.resolve()
export { loadNavigationStyle, registerNavigationStyleHandler } from './style-event'
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Taro from '@tarojs/api'

import native from '../../NativeApi'

/**
* 导航条与Web层叠布局,导航条可见情况下Web页面顶部空出一定高度的留白
*/
export function loadNavigationStyle () {
if (window === undefined) {
return
}
// @ts-ignore
const naviHeight = window.navigationHeight ? window.navigationHeight : 0
const css = `
.taro_router .taro_page.taro_navigation_page {
padding-top: ${naviHeight}px;
}
.taro-tabbar__container .taro_page.taro_navigation_page {
max-height: calc(100vh - ${naviHeight}px);
}
.taro-tabbar__container .taro_page.taro_tabbar_page.taro_navigation_page {
max-height: calc(100vh - 50px - ${naviHeight}px);
}`
const style = document.createElement('style')
style.innerHTML = css
document.getElementsByTagName('head')[0].appendChild(style)
}

/**
* 监听导航栏设置、进入/退出全屏事件,更改导航栏样式以及显示/隐藏胶囊按钮
*/
export function registerNavigationStyleHandler () {
if (window !== undefined) {
// @ts-ignore
window.currentNavigation = {}
}

Taro.eventCenter.on('__taroSetNavigationStyle', (style, textStyle, backgroundColor) => {
if (window !== undefined) {
native.setNavigationStyle({ style: style, textStyle: textStyle, backgroundColor: backgroundColor })
// @ts-ignore
Object.assign(window.currentNavigation, {
style,
textStyle,
backgroundColor,
})
// @ts-ignore
const { originCapsuleVisible } = window
if (originCapsuleVisible !== undefined) {
native.setCapsuleState({ visible: originCapsuleVisible })
}
}
})

Taro.eventCenter.on('__taroEnterFullScreen', () => {
native.setNavigationStyle({ style: 'custom', textStyle: 'black', backgroundColor: '#000000' })
// @ts-ignore
if (window.originCapsuleVisible === undefined) {
// @ts-ignore
window.originCapsuleVisible = native.getCapsuleState().visible
}
native.setCapsuleState({ visible: false })
})

Taro.eventCenter.on('__taroExitFullScreen', () => {
// @ts-ignore
const { style, textStyle, backgroundColor } = window.currentNavigation
native.setNavigationStyle({ style: style, textStyle: textStyle, backgroundColor: backgroundColor })
// @ts-ignore
const { originCapsuleVisible } = window
if (originCapsuleVisible !== undefined) {
native.setCapsuleState({ visible: originCapsuleVisible })
}
})
}

0 comments on commit c410398

Please sign in to comment.