keyboard.js 2.9 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 31 32 33 34 35 36 37 38 39 40
  watch: {
    focus (val) {
      if (val) {
        this.showSoftKeybord()
      }
    }
  },
  mounted () {
    if (this.autoFocus || this.focus) {
      this.showSoftKeybord()
    }
  },
41
  beforeDestroy () {
42
    this.onKeyboardHide()
43 44 45 46 47
  },
  methods: {
    initKeyboard (el) {
      el.addEventListener('focus', () => {
        UniViewJSBridge.subscribe('hideKeyboard', hideKeyboard)
48
        document.addEventListener('click', iosHideKeyboard, false)
49
        this.setSoftinputNavBar()
50
        this.setSoftinputTemporary()
51
      })
52
      el.addEventListener('blur', this.onKeyboardHide.bind(this))
53 54
    },
    showSoftKeybord () {
55
      plusReady(() => {
56 57 58 59
        plus.key.showSoftKeybord()
      })
    },
    setSoftinputTemporary () {
60
      plusReady(() => {
61 62 63
        const currentWebview = plus.webview.currentWebview()
        const style = currentWebview.getStyle() || {}
        const rect = this.$el.getBoundingClientRect()
64
        currentWebview.setSoftinputTemporary && currentWebview.setSoftinputTemporary({
65
          mode: style.softinputMode === 'adjustResize' ? 'adjustResize' : (this.adjustPosition ? 'adjustPan' : 'nothing'),
66 67
          position: {
            top: rect.top,
Q
qiang 已提交
68
            height: rect.height + (Number(this.cursorSpacing) || 0)
69 70 71
          }
        })
      })
72
    },
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    setSoftinputNavBar () {
      if (this.showConfirmBar === 'auto') {
        delete this.__softinputNavBar
        return
      }
      plusReady(() => {
        const currentWebview = plus.webview.currentWebview()
        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
        }
      })
    },
    resetSoftinputNavBar () {
      const softinputNavBar = this.__softinputNavBar
      if (softinputNavBar) {
        plusReady(() => {
          const currentWebview = plus.webview.currentWebview()
          currentWebview.setStyle({
            softinputNavBar
          })
        })
      }
    },
103 104 105
    onKeyboardHide () {
      UniViewJSBridge.unsubscribe('hideKeyboard', hideKeyboard)
      document.removeEventListener('click', iosHideKeyboard, false)
106
      this.resetSoftinputNavBar()
107 108 109
    }
  }
}