修改发布球局
This commit is contained in:
98
src/store/keyboardStore.ts
Normal file
98
src/store/keyboardStore.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { create } from 'zustand'
|
||||
import Taro from '@tarojs/taro'
|
||||
|
||||
interface KeyboardState {
|
||||
keyboardHeight: number
|
||||
isKeyboardVisible: boolean
|
||||
listeners: Set<(height: number, visible: boolean) => void>
|
||||
isInitialized: boolean
|
||||
}
|
||||
|
||||
interface KeyboardActions {
|
||||
setKeyboardHeight: (height: number) => void
|
||||
setKeyboardVisible: (visible: boolean) => void
|
||||
addListener: (listener: (height: number, visible: boolean) => void) => () => void
|
||||
initializeKeyboardListener: () => void
|
||||
cleanup: () => void
|
||||
}
|
||||
|
||||
type KeyboardStore = KeyboardState & KeyboardActions
|
||||
|
||||
export const useKeyboardStore = create<KeyboardStore>((set, get) => ({
|
||||
keyboardHeight: 0,
|
||||
isKeyboardVisible: false,
|
||||
listeners: new Set(),
|
||||
isInitialized: false,
|
||||
|
||||
setKeyboardHeight: (height: number) => {
|
||||
set({ keyboardHeight: height })
|
||||
const { listeners } = get()
|
||||
listeners.forEach(listener => listener(height, get().isKeyboardVisible))
|
||||
},
|
||||
|
||||
setKeyboardVisible: (visible: boolean) => {
|
||||
set({ isKeyboardVisible: visible })
|
||||
const { listeners } = get()
|
||||
listeners.forEach(listener => listener(get().keyboardHeight, visible))
|
||||
},
|
||||
|
||||
addListener: (listener: (height: number, visible: boolean) => void) => {
|
||||
const { listeners } = get()
|
||||
listeners.add(listener)
|
||||
|
||||
// 返回取消监听的函数
|
||||
return () => {
|
||||
listeners.delete(listener)
|
||||
}
|
||||
},
|
||||
|
||||
initializeKeyboardListener: () => {
|
||||
const { isInitialized } = get()
|
||||
if (isInitialized) return
|
||||
|
||||
console.log('初始化全局键盘监听器')
|
||||
|
||||
Taro.onKeyboardHeightChange?.((res: any) => {
|
||||
const height = Number(res?.height || 0)
|
||||
console.log('全局键盘高度变化:', height)
|
||||
|
||||
const store = get()
|
||||
if (height > 0) {
|
||||
store.setKeyboardVisible(true)
|
||||
store.setKeyboardHeight(height)
|
||||
} else {
|
||||
store.setKeyboardVisible(false)
|
||||
store.setKeyboardHeight(0)
|
||||
}
|
||||
})
|
||||
|
||||
set({ isInitialized: true })
|
||||
},
|
||||
|
||||
cleanup: () => {
|
||||
console.log('清理全局键盘监听器')
|
||||
// @ts-ignore
|
||||
if (typeof Taro.offKeyboardHeightChange === 'function') {
|
||||
// @ts-ignore
|
||||
Taro.offKeyboardHeightChange()
|
||||
}
|
||||
set({
|
||||
isInitialized: false,
|
||||
keyboardHeight: 0,
|
||||
isKeyboardVisible: false,
|
||||
listeners: new Set()
|
||||
})
|
||||
}
|
||||
}))
|
||||
|
||||
// 导出便捷的 hooks
|
||||
export const useKeyboardHeight = () => {
|
||||
const { keyboardHeight, isKeyboardVisible, addListener, initializeKeyboardListener } = useKeyboardStore()
|
||||
|
||||
return {
|
||||
keyboardHeight,
|
||||
isKeyboardVisible,
|
||||
addListener,
|
||||
initializeKeyboardListener
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user