const DEFAULT_CURSOR_SPACING = 96 const DEFAULT_SPACER_EXTRA_RPX = 48 const DEFAULT_SCROLL_DELAY = 32 const MAX_CURSOR_SPACING = 180 function getWindowWidth() { try { if (typeof wx.getWindowInfo === 'function') { const info = wx.getWindowInfo() if (info && info.windowWidth) { return info.windowWidth } } const systemInfo = wx.getSystemInfoSync() return (systemInfo && systemInfo.windowWidth) || 375 } catch (error) { return 375 } } function pxToRpx(px) { return Math.round((Number(px) || 0) * 750 / getWindowWidth()) } function createKeyboardSafeData(options = {}) { const cursorSpacing = Number(options.cursorSpacing) > 0 ? Math.round(Number(options.cursorSpacing)) : DEFAULT_CURSOR_SPACING return { keyboardVisible: false, keyboardHeightPx: 0, keyboardInsetRpx: 0, keyboardAnchorId: '', keyboardCursorSpacing: cursorSpacing } } function clearKeyboardTimers(state) { if (!state) { return } if (state.anchorTimer) { clearTimeout(state.anchorTimer) state.anchorTimer = null } if (state.blurTimer) { clearTimeout(state.blurTimer) state.blurTimer = null } } function queueAnchorScroll(page, anchorId) { const state = page && page.__keyboardSafe if (!page || !state || !anchorId) { return } if (state.anchorTimer) { clearTimeout(state.anchorTimer) } const nextDelay = Number(state.options.scrollDelay) || DEFAULT_SCROLL_DELAY const updateAnchor = () => { if (!page.__keyboardSafe) { return } page.setData({ keyboardAnchorId: anchorId }) } if (page.data && page.data.keyboardAnchorId === anchorId) { page.setData({ keyboardAnchorId: '' }) state.anchorTimer = setTimeout(updateAnchor, nextDelay) return } state.anchorTimer = setTimeout(updateAnchor, 0) } function attachKeyboardSafe(page, options = {}) { if (!page) { return } detachKeyboardSafe(page) const state = { activeAnchorId: '', anchorTimer: null, blurTimer: null, options: { cursorSpacing: Number(options.cursorSpacing) > 0 ? Math.round(Number(options.cursorSpacing)) : DEFAULT_CURSOR_SPACING, spacerExtraRpx: Number(options.spacerExtraRpx) >= 0 ? Number(options.spacerExtraRpx) : DEFAULT_SPACER_EXTRA_RPX, scrollDelay: Number(options.scrollDelay) > 0 ? Number(options.scrollDelay) : DEFAULT_SCROLL_DELAY }, listener: null } state.listener = result => { const nextHeight = Math.max(0, Number(result && result.height) || 0) const keyboardVisible = nextHeight > 0 const keyboardInsetRpx = keyboardVisible ? pxToRpx(nextHeight) + state.options.spacerExtraRpx : 0 const keyboardCursorSpacing = keyboardVisible ? Math.max( state.options.cursorSpacing, Math.min(MAX_CURSOR_SPACING, Math.round(nextHeight * 0.38)) ) : state.options.cursorSpacing page.setData({ keyboardVisible, keyboardHeightPx: nextHeight, keyboardInsetRpx, keyboardCursorSpacing, keyboardAnchorId: keyboardVisible ? page.data.keyboardAnchorId : (state.activeAnchorId ? page.data.keyboardAnchorId : '') }) if (keyboardVisible && state.activeAnchorId) { queueAnchorScroll(page, state.activeAnchorId) } } page.__keyboardSafe = state if (typeof wx.onKeyboardHeightChange === 'function') { wx.onKeyboardHeightChange(state.listener) } } function detachKeyboardSafe(page) { if (!page || !page.__keyboardSafe) { return } const state = page.__keyboardSafe clearKeyboardTimers(state) if (typeof wx.offKeyboardHeightChange === 'function' && state.listener) { try { wx.offKeyboardHeightChange(state.listener) } catch (error) {} } delete page.__keyboardSafe } function handleKeyboardFocus(page, event) { const state = page && page.__keyboardSafe if (!page || !state) { return } if (state.blurTimer) { clearTimeout(state.blurTimer) state.blurTimer = null } const anchorId = String( event && event.currentTarget && event.currentTarget.dataset && event.currentTarget.dataset.keyboardAnchor ? event.currentTarget.dataset.keyboardAnchor : '' ) state.activeAnchorId = anchorId if (anchorId) { queueAnchorScroll(page, anchorId) } if (!page.data.keyboardCursorSpacing || page.data.keyboardCursorSpacing < state.options.cursorSpacing) { page.setData({ keyboardCursorSpacing: state.options.cursorSpacing }) } } function resetKeyboardSafe(page, options = {}) { if (!page) { return } const state = page.__keyboardSafe if (state) { clearKeyboardTimers(state) if (options.clearActiveAnchor !== false) { state.activeAnchorId = '' } } const fallbackCursorSpacing = state ? state.options.cursorSpacing : DEFAULT_CURSOR_SPACING page.setData({ keyboardVisible: false, keyboardHeightPx: 0, keyboardInsetRpx: 0, keyboardAnchorId: options.keepAnchor ? (page.data.keyboardAnchorId || '') : '', keyboardCursorSpacing: fallbackCursorSpacing }) } function handleKeyboardBlur(page) { const state = page && page.__keyboardSafe if (!page || !state) { return } if (state.blurTimer) { clearTimeout(state.blurTimer) } state.blurTimer = setTimeout(() => { if (!page.__keyboardSafe) { return } resetKeyboardSafe(page) }, 120) } module.exports = { createKeyboardSafeData, attachKeyboardSafe, detachKeyboardSafe, handleKeyboardFocus, handleKeyboardBlur, resetKeyboardSafe }