keyboard.js 3.5 KB
Newer Older
1 2 3 4
import {
  plusReady
} from 'uni-shared'

5 6 7
function hideKeyboard () {
  document.activeElement.blur()
}
8 9 10 11
/**
 * 保证iOS点击输入框外隐藏键盘
 */
function iosHideKeyboard () { }
12 13 14

export default {
  name: 'Keyboard',
15 16
  props: {
    cursorSpacing: {
Q
qiang 已提交
17
      type: [Number, String],
18 19
      default: 0
    },
20 21 22 23
    showConfirmBar: {
      type: [Boolean, String],
      default: 'auto'
    },
24 25 26 27 28
    adjustPosition: {
      type: Boolean,
      default: true
    }
  },
29 30
  watch: {
    focus (val) {
31
      if (val) {
32 33 34 35 36 37 38 39 40
        this.showSoftKeybord()
      }
    }
  },
  mounted () {
    if (this.autoFocus || this.focus) {
      this.showSoftKeybord()
    }
  },
41
  beforeDestroy () {
42
    this.onKeyboardHide()
43 44 45 46
  },
  methods: {
    initKeyboard (el) {
      el.addEventListener('focus', () => {
47 48 49 50
        this.hideKeyboardTemp = function () {
          hideKeyboard()
        }
        UniViewJSBridge.subscribe('hideKeyboard', this.hideKeyboardTemp)
51
        document.addEventListener('click', iosHideKeyboard, false)
52 53
        this.setSoftinputNavBar()
        this.setSoftinputTemporary()
54
      })
55
      el.addEventListener('blur', this.onKeyboardHide.bind(this))
56 57
    },
    showSoftKeybord () {
58 59 60 61 62
      if (__PLATFORM__ === 'app-plus') {
        plusReady(() => {
          plus.key.showSoftKeybord()
        })
      }
63 64
    },
    setSoftinputTemporary () {
65 66 67 68 69 70 71 72 73 74 75 76
      if (__PLATFORM__ === 'app-plus') {
        plusReady(() => {
          const currentWebview = plus.webview.currentWebview()
          const style = currentWebview.getStyle() || {}
          const rect = this.$el.getBoundingClientRect()
          currentWebview.setSoftinputTemporary && currentWebview.setSoftinputTemporary({
            mode: style.softinputMode === 'adjustResize' ? 'adjustResize' : (this.adjustPosition ? 'adjustPan' : 'nothing'),
            position: {
              top: rect.top,
              height: rect.height + (Number(this.cursorSpacing) || 0)
            }
          })
77
        })
78
      }
79
    },
80
    setSoftinputNavBar () {
81 82
      if (__PLATFORM__ === 'app-plus') {
        if (this.showConfirmBar === 'auto') {
83
          delete this.__softinputNavBar
84
          return
85 86 87
        }
        plusReady(() => {
          const currentWebview = plus.webview.currentWebview()
88 89 90 91 92 93 94 95 96 97
          const { softinputNavBar } = currentWebview.getStyle() || {}
          const showConfirmBar = softinputNavBar !== 'none'
          if (showConfirmBar !== this.showConfirmBar) {
            this.__softinputNavBar = softinputNavBar || 'auto'
            currentWebview.setStyle({
              softinputNavBar: this.showConfirmBar ? 'auto' : 'none'
            })
          } else {
            delete this.__softinputNavBar
          }
98 99 100
        })
      }
    },
101 102 103 104 105 106 107 108 109 110 111 112 113
    resetSoftinputNavBar () {
      if (__PLATFORM__ === 'app-plus') {
        const softinputNavBar = this.__softinputNavBar
        if (softinputNavBar) {
          plusReady(() => {
            const currentWebview = plus.webview.currentWebview()
            currentWebview.setStyle({
              softinputNavBar
            })
          })
        }
      }
    },
114
    onKeyboardHide () {
115
      UniViewJSBridge.unsubscribe('hideKeyboard', this.hideKeyboardTemp)
116
      document.removeEventListener('click', iosHideKeyboard, false)
117
      this.resetSoftinputNavBar()
118
      // 修复ios端显示与点击位置错位的Bug by:wyq
119 120
      if (String(navigator.vendor).indexOf('Apple') === 0) {
        document.documentElement.scrollTo(document.documentElement.scrollLeft, document.documentElement.scrollTop)
121
      }
122 123 124
    }
  }
}