39
src/utils/hostNavigationTitle.ts
Normal file
39
src/utils/hostNavigationTitle.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'
|
||||
|
||||
export const MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE = 'museum-guide:navigation-title'
|
||||
|
||||
type MiniProgramBridge = {
|
||||
postMessage?: (options: {
|
||||
data: {
|
||||
type: typeof MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE
|
||||
title: string
|
||||
}
|
||||
}) => void
|
||||
}
|
||||
|
||||
const getMiniProgramBridge = () => {
|
||||
if (typeof window === 'undefined') return undefined
|
||||
|
||||
return (window as Window & {
|
||||
wx?: {
|
||||
miniProgram?: MiniProgramBridge
|
||||
}
|
||||
}).wx?.miniProgram
|
||||
}
|
||||
|
||||
export const syncHostNavigationTitle = (title: string) => {
|
||||
if (typeof document !== 'undefined') document.title = title
|
||||
|
||||
if (typeof uni !== 'undefined' && typeof uni.setNavigationBarTitle === 'function') {
|
||||
uni.setNavigationBarTitle({ title })
|
||||
}
|
||||
|
||||
if (!isEmbeddedInWechatMiniProgram()) return
|
||||
|
||||
getMiniProgramBridge()?.postMessage?.({
|
||||
data: {
|
||||
type: MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE,
|
||||
title
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user