From d5359946146b48debcbf4b241b127f8cb94e62a6 Mon Sep 17 00:00:00 2001 From: xiaowang Date: Wed, 22 Apr 2026 16:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=94=B6=E5=8F=A3?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E9=94=AE=E7=9B=98=E9=81=AE=E6=8C=A1?= =?UTF-8?q?=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeChatProgram/pages/auth/index.js | 49 ++- WeChatProgram/pages/auth/index.wxml | 410 ++++++++++++++++-------- WeChatProgram/pages/auth/index.wxss | 28 +- WeChatProgram/pages/home/index.js | 47 ++- WeChatProgram/pages/home/index.wxml | 14 +- WeChatProgram/pages/home/index.wxss | 9 + WeChatProgram/pages/products/index.js | 44 ++- WeChatProgram/pages/products/index.wxml | 9 +- WeChatProgram/pages/settings/index.js | 38 ++- WeChatProgram/pages/settings/index.wxml | 165 ++++++++-- WeChatProgram/pages/settings/index.wxss | 9 + WeChatProgram/utils/keyboard-safe.js | 243 ++++++++++++++ 12 files changed, 897 insertions(+), 168 deletions(-) create mode 100644 WeChatProgram/utils/keyboard-safe.js diff --git a/WeChatProgram/pages/auth/index.js b/WeChatProgram/pages/auth/index.js index 4315a8c1..b5057c70 100644 --- a/WeChatProgram/pages/auth/index.js +++ b/WeChatProgram/pages/auth/index.js @@ -5,11 +5,21 @@ const authStore = require('../../utils/auth') const actionGuard = require('../../utils/action-guard') const navigation = require('../../utils/navigation') const { runWithLoading } = require('../../utils/loading') +const { + createKeyboardSafeData, + attachKeyboardSafe, + detachKeyboardSafe, + handleKeyboardFocus, + handleKeyboardBlur, + resetKeyboardSafe +} = require('../../utils/keyboard-safe') const phoneRegex = /^1[3-9]\d{9}$/ Page({ - data: { + data: Object.assign(createKeyboardSafeData({ + cursorSpacing: 108 + }), { redirect: '', activeTab: 'login', loginType: 'password', @@ -59,9 +69,10 @@ Page({ verificationCode: '', newPassword: '' } - }, + }), onLoad(options) { + this.ensureKeyboardSafe() this.setData({ redirect: options && options.redirect ? options.redirect : '', authHelperText: this.buildAuthHelperText('login', 'password') @@ -73,11 +84,37 @@ Page({ }, onShow() { + this.ensureKeyboardSafe() this.consumePendingWechatMiniLogin() }, + onHide() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + + onUnload() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + noop() {}, + ensureKeyboardSafe() { + attachKeyboardSafe(this, { + cursorSpacing: 108, + spacerExtraRpx: 56 + }) + }, + + handleKeyboardFocus(event) { + handleKeyboardFocus(this, event) + }, + + handleKeyboardBlur() { + handleKeyboardBlur(this) + }, + detectWechatMiniCapability() { let supported = false let disabledReason = '' @@ -158,6 +195,7 @@ Page({ handleTabChange(event) { const tab = event.currentTarget.dataset.tab + resetKeyboardSafe(this) this.setData({ activeTab: tab, authHelperText: this.buildAuthHelperText(tab, this.data.loginType), @@ -174,6 +212,7 @@ Page({ handleLoginTypeChange(event) { const type = event.currentTarget.dataset.type + resetKeyboardSafe(this) this.setData({ loginType: type, authHelperText: this.buildAuthHelperText(this.data.activeTab, type), @@ -189,6 +228,7 @@ Page({ }, handleShowTraditionalLogin() { + resetKeyboardSafe(this) this.setData({ traditionalLoginExpanded: true, showResetPanel: false @@ -196,6 +236,7 @@ Page({ }, handleToggleResetPanel() { + resetKeyboardSafe(this) this.setData({ showResetPanel: !this.data.showResetPanel }) @@ -299,6 +340,7 @@ Page({ }, async handleAccountBlur() { + this.handleKeyboardBlur() const account = this.data.form.account.trim() if (!account) { return @@ -320,6 +362,7 @@ Page({ }, async handlePhoneBlur() { + this.handleKeyboardBlur() const phone = this.data.form.phone.trim() if (!phone) { return @@ -849,6 +892,7 @@ Page({ }, openMiniShopSelector(result) { + resetKeyboardSafe(this) const shops = Array.isArray(result && result.shops) ? result.shops : [] if (!shops.length) { wx.showToast({ @@ -887,6 +931,7 @@ Page({ }, async finishLogin(payload, successMessage) { + resetKeyboardSafe(this) getApp().setUserSession(payload) this.clearMiniShopSelector() this.setData({ diff --git a/WeChatProgram/pages/auth/index.wxml b/WeChatProgram/pages/auth/index.wxml index 0ce20078..b1ea8990 100644 --- a/WeChatProgram/pages/auth/index.wxml +++ b/WeChatProgram/pages/auth/index.wxml @@ -1,158 +1,300 @@ - + + - - - - - 登录 - 注册 - - - - + + {{authHelperText}} + + + + 昵称 + + + + + + 账号 + + {{loginAccountStatusText}} + {{registerAccountStatusText}} - - 账号密码登录 - 验证码登录 + + 手机号 + + {{registerPhoneStatusText}} + + + 密码 + + + + + 验证码 + + + {{countdown > 0 ? countdown + 's' : '发送验证码'}} + + + + + 密码 + + + + + 确认密码 + + + + + 选择门店 + + {{registerShopDisplayName || '请选择门店'}} + + + + + 选择登录门店 + + {{loginShopDisplayName || '请选择登录门店'}} + + + + + + + 忘记密码? + + + {{activeTab === 'login' ? '立即登录' : '完成注册'}} - - {{authHelperText}} + + 重置密码 + 短信验证后重置密码 - - - 昵称 - - - + + 手机号 + + - - 账号 - - {{loginAccountStatusText}} - {{registerAccountStatusText}} - + + 验证码 + + + {{forgotCountdown > 0 ? forgotCountdown + 's' : '发送验证码'}} + + - - 手机号 - - {{registerPhoneStatusText}} - + + 新密码 + + - - 密码 - - - - - 验证码 - - - {{countdown > 0 ? countdown + 's' : '发送验证码'}} + 确认重置密码 - - 密码 - - - - - 确认密码 - - - - - 选择门店 - - {{registerShopDisplayName || '请选择门店'}} - - - - - 选择登录门店 - - {{loginShopDisplayName || '请选择登录门店'}} - - - - - - - 忘记密码? - - - {{activeTab === 'login' ? '立即登录' : '完成注册'}} + - - - 重置密码 - 短信验证后重置密码 - - - 手机号 - - - - - 验证码 - - - {{forgotCountdown > 0 ? forgotCountdown + 's' : '发送验证码'}} - - - - - 新密码 - - - - 确认重置密码 - - + diff --git a/WeChatProgram/pages/auth/index.wxss b/WeChatProgram/pages/auth/index.wxss index f94f6e09..457a00f2 100644 --- a/WeChatProgram/pages/auth/index.wxss +++ b/WeChatProgram/pages/auth/index.wxss @@ -1,3 +1,19 @@ +.auth-page-root { + height: 100vh; + display: flex; + flex-direction: column; + overflow: hidden; +} + +.auth-scroll { + flex: 1; + min-height: 0; +} + +.auth-page-shell { + min-height: 100%; +} + .auth-page { background: radial-gradient(circle at top right, rgba(255, 204, 219, 0.32), transparent 30%), @@ -5,7 +21,7 @@ } .auth-content { - min-height: calc(100vh - 120rpx); + min-height: 100%; display: flex; flex-direction: column; justify-content: center; @@ -13,6 +29,11 @@ padding: 24rpx 24rpx 40rpx; } +.auth-content.keyboard-active { + justify-content: flex-start; + align-items: stretch; +} + .auth-panel, .reset-card { width: 100%; @@ -282,3 +303,8 @@ .mini-shop-action { flex: 1; } + +.keyboard-spacer { + width: 100%; + flex-shrink: 0; +} diff --git a/WeChatProgram/pages/home/index.js b/WeChatProgram/pages/home/index.js index f3260d3d..3cfa18c9 100644 --- a/WeChatProgram/pages/home/index.js +++ b/WeChatProgram/pages/home/index.js @@ -4,17 +4,28 @@ const cache = require('../../utils/cache') const { buildProductCards } = require('../../utils/product') const { resolveImageList } = require('../../utils/file') const { DEFAULT_SHOP_IMAGE, CACHE_TTL } = require('../../utils/config') +const { + createKeyboardSafeData, + attachKeyboardSafe, + detachKeyboardSafe, + handleKeyboardFocus, + handleKeyboardBlur, + resetKeyboardSafe +} = require('../../utils/keyboard-safe') const productApi = require('../../api/product') const shopApi = require('../../api/shop') const userApi = require('../../api/user') Page({ - data: { + data: Object.assign(createKeyboardSafeData({ + cursorSpacing: 96 + }), { initialized: false, loading: false, loadingMore: false, isLoggedIn: false, userInfo: null, + navTitle: '游客模式', greeting: '欢迎来到 Yumi Nail', displayName: '先看看门店和作品', searchKeyword: '', @@ -25,20 +36,32 @@ Page({ pageSize: 6, hasMore: false, popularTotal: 0 - }, + }), onLoad() { this.homeRequestId = 0 + this.ensureKeyboardSafe() this.loadHomeData() }, onShow() { + this.ensureKeyboardSafe() this.syncUserState() if (this.data.initialized && this.data.isLoggedIn) { this.loadUserProfile() } }, + onHide() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + + onUnload() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + async onPullDownRefresh() { cache.removeByPrefix('home:') await this.loadHomeData() @@ -52,10 +75,20 @@ Page({ this.loadPopularProducts(false) }, + ensureKeyboardSafe() { + attachKeyboardSafe(this, { + cursorSpacing: 96, + spacerExtraRpx: 24 + }) + }, + syncUserState() { const app = getApp() const userInfo = auth.getStoredUserInfo() const isLoggedIn = auth.isLoggedIn() + const navTitle = userInfo && String(userInfo.shopName || '').trim() + ? String(userInfo.shopName).trim() + : '游客模式' if (app) { app.globalData.userInfo = userInfo app.globalData.isLoggedIn = isLoggedIn @@ -64,6 +97,7 @@ Page({ this.setData({ isLoggedIn, userInfo, + navTitle, greeting: isLoggedIn ? '欢迎回来' : '游客模式', displayName: isLoggedIn ? ((userInfo && userInfo.nickname) || 'Yumi 用户') @@ -203,7 +237,16 @@ Page({ }) }, + handleSearchFocus(event) { + handleKeyboardFocus(this, event) + }, + + handleSearchBlur() { + handleKeyboardBlur(this) + }, + handleSearchConfirm() { + resetKeyboardSafe(this) navigation.openProducts({ keyword: this.data.searchKeyword.trim() }) diff --git a/WeChatProgram/pages/home/index.wxml b/WeChatProgram/pages/home/index.wxml index 146ecacd..d02a5953 100644 --- a/WeChatProgram/pages/home/index.wxml +++ b/WeChatProgram/pages/home/index.wxml @@ -1,8 +1,8 @@ - + - - + + {{isLoggedIn ? greeting : 'YUMI NAIL 小程序'}} @@ -14,7 +14,7 @@ 看看热门作品 - + 搜索 @@ -135,6 +140,7 @@ + diff --git a/WeChatProgram/pages/home/index.wxss b/WeChatProgram/pages/home/index.wxss index 2713d921..4f2afb01 100644 --- a/WeChatProgram/pages/home/index.wxss +++ b/WeChatProgram/pages/home/index.wxss @@ -17,6 +17,10 @@ linear-gradient(180deg, #fff7f8 0%, #fffdfb 100%); } +.home-page.keyboard-active { + padding-bottom: 0; +} + .home-hero { background: radial-gradient(circle at top right, rgba(255, 186, 207, 0.5), transparent 36%), @@ -159,3 +163,8 @@ font-size: 22rpx; color: #8d7a84; } + +.keyboard-light-spacer { + width: 100%; + flex-shrink: 0; +} diff --git a/WeChatProgram/pages/products/index.js b/WeChatProgram/pages/products/index.js index 1c66aa65..cadf1b1f 100644 --- a/WeChatProgram/pages/products/index.js +++ b/WeChatProgram/pages/products/index.js @@ -8,13 +8,23 @@ const { buildProductCards } = require('../../utils/product') const navigation = require('../../utils/navigation') const { runWithLoading } = require('../../utils/loading') const { CACHE_TTL } = require('../../utils/config') +const { + createKeyboardSafeData, + attachKeyboardSafe, + detachKeyboardSafe, + handleKeyboardFocus, + handleKeyboardBlur, + resetKeyboardSafe +} = require('../../utils/keyboard-safe') const ALL_CATEGORY = '\u5168\u90e8' const FILTER_BUSY_MESSAGE = '\u7b5b\u9009\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c' const CART_BUSY_MESSAGE = '\u8d2d\u7269\u8f66\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c' Page({ - data: { + data: Object.assign(createKeyboardSafeData({ + cursorSpacing: 96 + }), { initialized: false, loading: false, loadingMore: false, @@ -27,14 +37,16 @@ Page({ pageSize: 10, total: 0, hasMore: false - }, + }), onLoad(options) { this.filterRequestId = 0 + this.ensureKeyboardSafe() this.bootstrapPage(options || {}) }, async onShow() { + this.ensureKeyboardSafe() const app = getApp() const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null @@ -51,6 +63,16 @@ Page({ } }, + onHide() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + + onUnload() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) + }, + async onPullDownRefresh() { await this.loadProducts(true) wx.stopPullDownRefresh() @@ -63,6 +85,13 @@ Page({ this.loadProducts(false) }, + ensureKeyboardSafe() { + attachKeyboardSafe(this, { + cursorSpacing: 96, + spacerExtraRpx: 16 + }) + }, + async bootstrapPage(options) { this.setData({ loading: true @@ -263,7 +292,16 @@ Page({ }) }, + handleSearchFocus(event) { + handleKeyboardFocus(this, event) + }, + + handleSearchBlur() { + handleKeyboardBlur(this) + }, + async handleSearchConfirm() { + resetKeyboardSafe(this) try { await actionGuard.run(this, 'products:search', async () => { this.setData({ @@ -287,6 +325,7 @@ Page({ async handleSelectCategory(event) { const category = event.currentTarget.dataset.category + resetKeyboardSafe(this) try { await actionGuard.run(this, `products:category:${category}`, async () => { this.setData({ @@ -310,6 +349,7 @@ Page({ }, async handleResetFilters() { + resetKeyboardSafe(this) try { await actionGuard.run(this, 'products:reset', async () => { this.setData({ diff --git a/WeChatProgram/pages/products/index.wxml b/WeChatProgram/pages/products/index.wxml index 76ee2ff1..17ff8afb 100644 --- a/WeChatProgram/pages/products/index.wxml +++ b/WeChatProgram/pages/products/index.wxml @@ -1,7 +1,7 @@ - - + + @@ -12,6 +12,11 @@ value="{{searchKeyword}}" bindinput="handleSearchInput" bindconfirm="handleSearchConfirm" + bindfocus="handleSearchFocus" + bindblur="handleSearchBlur" + data-keyboard-anchor="products-search-anchor" + adjust-position="false" + cursor-spacing="{{keyboardCursorSpacing}}" confirm-type="search" /> 搜索 diff --git a/WeChatProgram/pages/settings/index.js b/WeChatProgram/pages/settings/index.js index 3b86e06c..a296354b 100644 --- a/WeChatProgram/pages/settings/index.js +++ b/WeChatProgram/pages/settings/index.js @@ -5,6 +5,14 @@ const navigation = require('../../utils/navigation') const { DEFAULT_AVATAR } = require('../../utils/config') const { getDraftValue, clearDraft, scheduleDraftSave, clearDraftTimer } = require('../../utils/form-draft') const { runWithLoading } = require('../../utils/loading') +const { + createKeyboardSafeData, + attachKeyboardSafe, + detachKeyboardSafe, + handleKeyboardFocus, + handleKeyboardBlur, + resetKeyboardSafe +} = require('../../utils/keyboard-safe') const phoneRegex = /^1[3-9]\d{9}$/ const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{8,}$/ @@ -41,7 +49,9 @@ const placeholderEntries = [ ] Page({ - data: { + data: Object.assign(createKeyboardSafeData({ + cursorSpacing: 108 + }), { isLoggedIn: false, loading: false, savingProfile: false, @@ -72,22 +82,46 @@ Page({ password: '', confirmPassword: '' } + }), + + onLoad() { + this.ensureKeyboardSafe() }, onShow() { + this.ensureKeyboardSafe() this.loadSettings() }, onHide() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) this.clearCountdown() clearDraftTimer(this) }, onUnload() { + resetKeyboardSafe(this) + detachKeyboardSafe(this) this.clearCountdown() clearDraftTimer(this) }, + ensureKeyboardSafe() { + attachKeyboardSafe(this, { + cursorSpacing: 108, + spacerExtraRpx: 48 + }) + }, + + handleKeyboardFocus(event) { + handleKeyboardFocus(this, event) + }, + + handleKeyboardBlur() { + handleKeyboardBlur(this) + }, + clearCountdown() { if (this.countdownTimer) { clearInterval(this.countdownTimer) @@ -252,6 +286,7 @@ Page({ handleToggleSecurityPanel(event) { const panel = event.currentTarget.dataset.panel const nextPanel = this.data.activeSecurityPanel === panel ? '' : panel + resetKeyboardSafe(this) this.clearCountdown() this.resetSecurityForms() this.setData({ @@ -262,6 +297,7 @@ Page({ }, handleCloseSecurityPanel() { + resetKeyboardSafe(this) this.resetSecurityForms() this.clearCountdown() this.setData({ diff --git a/WeChatProgram/pages/settings/index.wxml b/WeChatProgram/pages/settings/index.wxml index d813dfd7..d8ad744d 100644 --- a/WeChatProgram/pages/settings/index.wxml +++ b/WeChatProgram/pages/settings/index.wxml @@ -1,8 +1,16 @@ - - + + 登录后进入设置中心 @@ -37,14 +45,38 @@ 当前账号未设置登录密码。 - + 昵称 - + - + 个性签名 - + {{savingProfile ? '保存中...' : '保存资料'}} @@ -87,14 +119,40 @@ 输入后完成绑定 - + 手机号 - + - + 验证码 - + {{countdown > 0 ? countdown + 's' : '发送验证码'}} @@ -110,14 +168,40 @@ 当前手机号 {{userInfo.phone}} - + 新手机号 - + - + 验证码 - + {{countdown > 0 ? countdown + 's' : '发送验证码'}} @@ -131,20 +215,59 @@ 请先绑定手机号,再通过验证码设置或修改登录密码。 密码需包含字母和数字 - + 验证码 - + {{countdown > 0 ? countdown + 's' : '发送验证码'}} - + 新密码 - + - + 确认密码 - + 取消 @@ -176,6 +299,8 @@ 退出登录 + + diff --git a/WeChatProgram/pages/settings/index.wxss b/WeChatProgram/pages/settings/index.wxss index 1d1b4254..02ac7e8e 100644 --- a/WeChatProgram/pages/settings/index.wxss +++ b/WeChatProgram/pages/settings/index.wxss @@ -14,6 +14,10 @@ min-height: 100%; } +.settings-page-shell.keyboard-active { + padding-bottom: 0; +} + .settings-hero { margin-bottom: 20rpx; } @@ -225,3 +229,8 @@ font-size: 28rpx; font-weight: 700; } + +.keyboard-spacer { + width: 100%; + flex-shrink: 0; +} diff --git a/WeChatProgram/utils/keyboard-safe.js b/WeChatProgram/utils/keyboard-safe.js new file mode 100644 index 00000000..585e32a6 --- /dev/null +++ b/WeChatProgram/utils/keyboard-safe.js @@ -0,0 +1,243 @@ +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 +}