From e30ae5e9721550b1b76f8944fc10698d0f832af7 Mon Sep 17 00:00:00 2001 From: qiang Date: Tue, 12 Jan 2021 17:55:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20offKeyboardHeightC?= =?UTF-8?q?hange=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis.js | 1 + lib/modules.json | 3 ++- src/core/helpers/api.js | 4 ++++ src/core/service/api/keyboard/keyboard.js | 10 +++++++++- src/core/service/bridge.js | 8 ++++++-- src/core/service/bridge/index.js | 5 +++-- src/platforms/app-plus/service/index.js | 6 ++++-- 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index e229cdce7e..9e2a783249 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -131,6 +131,7 @@ const device = [ const keyboard = [ 'hideKeyboard', 'onKeyboardHeightChange', + 'offKeyboardHeightChange', 'getSelectedTextRange' ] diff --git a/lib/modules.json b/lib/modules.json index a4d1d6dfa8..ef5153b6b1 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -162,7 +162,8 @@ "uni.createIntersectionObserver": true, "uni.createMediaQueryObserver": true, "uni.hideKeyboard": true, - "uni.onKeyboardHeightChange": true + "uni.onKeyboardHeightChange": true, + "uni.offKeyboardHeightChange": true } }, { "name": "event", diff --git a/src/core/helpers/api.js b/src/core/helpers/api.js index 6d2b15524b..22bffff77d 100644 --- a/src/core/helpers/api.js +++ b/src/core/helpers/api.js @@ -275,6 +275,10 @@ export function invokeCallbackHandler (invokeCallbackId, res, extras) { return res } +export function removeCallbackHandler (invokeCallbackId) { + delete invokeCallbacks[invokeCallbackId] +} + export function wrapperUnimplemented (name) { return function todo (args) { console.error('API `' + name + '` is not yet implemented') diff --git a/src/core/service/api/keyboard/keyboard.js b/src/core/service/api/keyboard/keyboard.js index d4f17e42e9..0b5306f732 100644 --- a/src/core/service/api/keyboard/keyboard.js +++ b/src/core/service/api/keyboard/keyboard.js @@ -1,5 +1,6 @@ import { - invoke + invoke, + remove } from 'uni-core/service/bridge' import { @@ -15,5 +16,12 @@ onMethod('onKeyboardHeightChange', res => { }) export function onKeyboardHeightChange (callbackId) { + // 与微信小程序一致仅保留最后一次监听 + remove(callback) callback = callbackId } + +export function offKeyboardHeightChange () { + // 与微信小程序一致移除最后一次监听 + callback = null +} diff --git a/src/core/service/bridge.js b/src/core/service/bridge.js index 9160f7844d..12db7f9c54 100644 --- a/src/core/service/bridge.js +++ b/src/core/service/bridge.js @@ -1,11 +1,15 @@ export function pack (args) { return args -} - +} + export function unpack (args) { return args } export function invoke (...args) { return UniServiceJSBridge.invokeCallbackHandler(...args) +} + +export function remove (args) { + return UniServiceJSBridge.removeCallbackHandler(args) } diff --git a/src/core/service/bridge/index.js b/src/core/service/bridge/index.js index 2eae012ead..42da4f5106 100644 --- a/src/core/service/bridge/index.js +++ b/src/core/service/bridge/index.js @@ -8,7 +8,8 @@ export const once = Emitter.$once.bind(Emitter) export const emit = Emitter.$emit.bind(Emitter) export { - invokeCallbackHandler + invokeCallbackHandler, + removeCallbackHandler } from 'uni-helpers/api' @@ -30,4 +31,4 @@ export function subscribeHandler (event, args, pageId) { export { publishHandler } - from 'uni-platform/service/bridge' + from 'uni-platform/service/bridge' diff --git a/src/platforms/app-plus/service/index.js b/src/platforms/app-plus/service/index.js index 2599290992..9f6eada375 100644 --- a/src/platforms/app-plus/service/index.js +++ b/src/platforms/app-plus/service/index.js @@ -3,7 +3,8 @@ import { } from 'uni-core/service/uni' import { - invokeCallbackHandler + invokeCallbackHandler, + removeCallbackHandler } from 'uni-helpers/api' import { @@ -35,6 +36,7 @@ uni.__$wx__ = wx UniServiceJSBridge.publishHandler = publishHandler UniServiceJSBridge.invokeCallbackHandler = invokeCallbackHandler +UniServiceJSBridge.removeCallbackHandler = removeCallbackHandler export default { __vuePlugin: vuePlugin, @@ -44,4 +46,4 @@ export default { uni, getApp, getCurrentPages -} +} -- GitLab