keyboard.js 3.1 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
  },
  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
        this.setSoftinputNavBar()
53
        this.setSoftinputTemporary()
54
      })
55
      el.addEventListener('blur', this.onKeyboardHide.bind(this))
56 57
    },
    showSoftKeybord () {
58
      plusReady(() => {
59 60 61 62
        plus.key.showSoftKeybord()
      })
    },
    setSoftinputTemporary () {
63
      plusReady(() => {
64 65 66
        const currentWebview = plus.webview.currentWebview()
        const style = currentWebview.getStyle() || {}
        const rect = this.$el.getBoundingClientRect()
67
        currentWebview.setSoftinputTemporary && currentWebview.setSoftinputTemporary({
68
          mode: style.softinputMode === 'adjustResize' ? 'adjustResize' : (this.adjustPosition ? 'adjustPan' : 'nothing'),
69 70
          position: {
            top: rect.top,
Q
qiang 已提交
71
            height: rect.height + (Number(this.cursorSpacing) || 0)
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 103 104 105
    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
          })
        })
      }
    },
106
    onKeyboardHide () {
107
      UniViewJSBridge.unsubscribe('hideKeyboard', this.hideKeyboardTemp)
108
      document.removeEventListener('click', iosHideKeyboard, false)
109
      this.resetSoftinputNavBar()
110 111 112 113
      // 修复ios端显示与点击位置错位的Bug by:wyq
      if (document.body.scrollIntoView) {
        document.body.scrollIntoView()
      }
114 115 116
    }
  }
}