keyboard.js 5.6 KB
Newer Older
1 2 3
import {
  plusReady
} from 'uni-shared'
4
import emitter from './emitter'
5

6 7 8 9
/**
 * 保证iOS点击输入框外隐藏键盘
 */
function iosHideKeyboard () { }
10

11
function setSoftinputTemporary (vm, reset) {
12
  plusReady(() => {
13 14 15
    const MODE_ADJUSTRESIZE = 'adjustResize'
    const MODE_ADJUSTPAN = 'adjustPan'
    const MODE_NOTHING = 'nothing'
16 17
    const currentWebview = plus.webview.currentWebview()
    const style = currentWebview.getStyle() || {}
18 19
    const options = {
      mode: (reset || style.softinputMode === MODE_ADJUSTRESIZE) ? MODE_ADJUSTRESIZE : (vm.adjustPosition ? MODE_ADJUSTPAN : MODE_NOTHING),
20
      position: {
21 22
        top: 0,
        height: 0
23
      }
24 25 26 27 28 29 30
    }
    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)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  })
}

function setSoftinputNavBar (vm) {
  if (vm.showConfirmBar === 'auto') {
    delete vm.__softinputNavBar
    return
  }
  plusReady(() => {
    const currentWebview = plus.webview.currentWebview()
    const { softinputNavBar } = currentWebview.getStyle() || {}
    const showConfirmBar = softinputNavBar !== 'none'
    if (showConfirmBar !== vm.showConfirmBar) {
      vm.__softinputNavBar = softinputNavBar || 'auto'
      currentWebview.setStyle({
        softinputNavBar: vm.showConfirmBar ? 'auto' : 'none'
      })
    } else {
      delete vm.__softinputNavBar
    }
  })
}

function resetSoftinputNavBar (vm) {
  const softinputNavBar = vm.__softinputNavBar
  if (softinputNavBar) {
    plusReady(() => {
      const currentWebview = plus.webview.currentWebview()
      currentWebview.setStyle({
        softinputNavBar
      })
    })
  }
}

66
let resetTimer
67 68
let isAndroid
let osVersion
69 70
let keyboardHeight
let keyboardChangeCallback
71 72 73 74 75
if (__PLATFORM__ === 'app-plus') {
  plusReady(() => {
    isAndroid = plus.os.name.toLowerCase() === 'android'
    osVersion = plus.os.version
  })
76 77 78 79
  document.addEventListener('keyboardchange', function (event) {
    keyboardHeight = event.height
    keyboardChangeCallback && keyboardChangeCallback()
  }, false)
80 81
}

82 83
export default {
  name: 'Keyboard',
84
  mixins: [emitter],
85 86
  props: {
    cursorSpacing: {
Q
qiang 已提交
87
      type: [Number, String],
88 89
      default: 0
    },
90 91 92 93
    showConfirmBar: {
      type: [Boolean, String],
      default: 'auto'
    },
94
    adjustPosition: {
95 96 97 98 99
      type: [Boolean, String],
      default: true
    },
    autoBlur: {
      type: [Boolean, String],
100
      default: false
101 102
    }
  },
103 104 105 106 107 108 109
  directives: {
    keyboard: {
      inserted (el, binding, vnode) {
        vnode.context.initKeyboard(el)
      }
    }
  },
110 111
  methods: {
    initKeyboard (el) {
112 113
      let focus

114 115 116 117 118
      const keyboardChange = () => {
        this.$trigger('keyboardheightchange', {}, {
          height: keyboardHeight,
          duration: 0.25
        })
119 120 121
        // 安卓切换不同键盘类型时会导致键盘收回,需重新设置
        if (focus && keyboardHeight === 0) {
          setSoftinputTemporary(this)
122
        }
123 124 125 126 127 128 129 130
        // 安卓/iOS13收起键盘时主动失去焦点
        if (this.autoBlur && focus && keyboardHeight === 0 && (isAndroid || parseInt(osVersion) >= 13)) {
          document.activeElement.blur()
        }
      }

      el.addEventListener('focus', () => {
        focus = true
131
        clearTimeout(resetTimer)
132
        document.addEventListener('click', iosHideKeyboard, false)
133 134

        if (__PLATFORM__ === 'app-plus') {
135 136 137 138 139 140 141
          keyboardChangeCallback = keyboardChange
          if (keyboardHeight) {
            this.$trigger('keyboardheightchange', {}, {
              height: keyboardHeight,
              duration: 0
            })
          }
142 143 144
          setSoftinputNavBar(this)
          setSoftinputTemporary(this)
        }
145
      })
146

147
      if (__PLATFORM__ === 'app-plus') {
148
        // 安卓单独隐藏键盘后点击输入框不会触发 focus 事件
149
        el.addEventListener('click', () => {
150
          if (!this.disabled && focus && keyboardHeight === 0) {
151
            setSoftinputTemporary(this)
152
          }
153
        })
154
        if (!isAndroid && parseInt(osVersion) < 12) {
155
          // iOS12 以下系统 focus 事件设置较迟,改在 touchstart 设置
156 157 158 159 160 161
          el.addEventListener('touchstart', () => {
            if (!this.disabled && !focus) {
              setSoftinputTemporary(this)
            }
          })
        }
162
      }
163 164 165 166 167

      const onKeyboardHide = () => {
        document.removeEventListener('click', iosHideKeyboard, false)

        if (__PLATFORM__ === 'app-plus') {
168 169 170 171 172 173 174
          keyboardChangeCallback = null
          if (keyboardHeight) {
            this.$trigger('keyboardheightchange', {}, {
              height: 0,
              duration: 0
            })
          }
175
          resetSoftinputNavBar(this)
176 177 178 179 180 181
          if (isAndroid) {
            // 还原安卓软键盘配置,避免影响 web-view 组件
            resetTimer = setTimeout(() => {
              setSoftinputTemporary(this, true)
            }, 300)
          }
182 183 184 185 186
        }

        // 修复ios端显示与点击位置错位的Bug by:wyq
        if (String(navigator.vendor).indexOf('Apple') === 0) {
          document.documentElement.scrollTo(document.documentElement.scrollLeft, document.documentElement.scrollTop)
187 188
        }
      }
189 190

      el.addEventListener('blur', () => {
191 192 193
        // 在iOS设备上,手动调用uni.hideKeyboard(),键盘收起并且触发blur,但实际并没有blur。
        // 此时如果再点击页面其他地方会重新聚焦,此处做处理
        el.blur()
194 195 196
        focus = false
        onKeyboardHide()
      })
197 198 199
    }
  }
}