提交 441e8e07 编写于 作者: Q qiang

fix: 修复安卓平台 web-view 组件内 input 被软键盘遮挡的问题 question/92008

上级 fdf0df0e
...@@ -13,18 +13,26 @@ function showSoftKeybord () { ...@@ -13,18 +13,26 @@ function showSoftKeybord () {
}) })
} }
function setSoftinputTemporary (vm) { function setSoftinputTemporary (vm, reset) {
plusReady(() => { plusReady(() => {
const MODE_ADJUSTRESIZE = 'adjustResize'
const MODE_ADJUSTPAN = 'adjustPan'
const MODE_NOTHING = 'nothing'
const currentWebview = plus.webview.currentWebview() const currentWebview = plus.webview.currentWebview()
const style = currentWebview.getStyle() || {} const style = currentWebview.getStyle() || {}
const rect = vm.$el.getBoundingClientRect() const options = {
currentWebview.setSoftinputTemporary && currentWebview.setSoftinputTemporary({ mode: (reset || style.softinputMode === MODE_ADJUSTRESIZE) ? MODE_ADJUSTRESIZE : (vm.adjustPosition ? MODE_ADJUSTPAN : MODE_NOTHING),
mode: style.softinputMode === 'adjustResize' ? 'adjustResize' : (vm.adjustPosition ? 'adjustPan' : 'nothing'),
position: { position: {
top: rect.top, top: 0,
height: rect.height + (Number(vm.cursorSpacing) || 0) height: 0
} }
}) }
if (options.mode === MODE_ADJUSTPAN) {
const rect = vm.$el.getBoundingClientRect()
options.position.top = rect.top
options.position.height = rect.height + (Number(vm.cursorSpacing) || 0)
}
currentWebview.setSoftinputTemporary(options)
}) })
} }
...@@ -60,6 +68,7 @@ function resetSoftinputNavBar (vm) { ...@@ -60,6 +68,7 @@ function resetSoftinputNavBar (vm) {
} }
} }
let resetTimer
let isAndroid let isAndroid
let osVersion let osVersion
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
...@@ -127,6 +136,7 @@ export default { ...@@ -127,6 +136,7 @@ export default {
el.addEventListener('focus', () => { el.addEventListener('focus', () => {
focus = true focus = true
clearTimeout(resetTimer)
document.addEventListener('click', iosHideKeyboard, false) document.addEventListener('click', iosHideKeyboard, false)
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
...@@ -137,12 +147,14 @@ export default { ...@@ -137,12 +147,14 @@ export default {
}) })
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
// 安卓单独隐藏键盘后点击输入框不会触发 focus 事件
el.addEventListener('click', () => { el.addEventListener('click', () => {
if (!this.disabled && focus && keyboardHeight === 0) { if (!this.disabled && focus && keyboardHeight === 0) {
setSoftinputTemporary(this) setSoftinputTemporary(this)
} }
}) })
if (!isAndroid && parseInt(osVersion) < 12) { if (!isAndroid && parseInt(osVersion) < 12) {
// iOS12 以下系统 focus 事件设置较迟,改在 touchstart 设置
el.addEventListener('touchstart', () => { el.addEventListener('touchstart', () => {
if (!this.disabled && !focus) { if (!this.disabled && !focus) {
setSoftinputTemporary(this) setSoftinputTemporary(this)
...@@ -157,6 +169,12 @@ export default { ...@@ -157,6 +169,12 @@ export default {
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
document.removeEventListener('keyboardchange', keyboardChange, false) document.removeEventListener('keyboardchange', keyboardChange, false)
resetSoftinputNavBar(this) resetSoftinputNavBar(this)
if (isAndroid) {
// 还原安卓软键盘配置,避免影响 web-view 组件
resetTimer = setTimeout(() => {
setSoftinputTemporary(this, true)
}, 300)
}
} }
// 修复ios端显示与点击位置错位的Bug by:wyq // 修复ios端显示与点击位置错位的Bug by:wyq
...@@ -169,10 +187,6 @@ export default { ...@@ -169,10 +187,6 @@ export default {
focus = false focus = false
onKeyboardHide() onKeyboardHide()
}) })
this.$on('hook:beforeDestroy', () => {
onKeyboardHide()
})
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册