keyboard.js 3.6 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
        this.showSoftKeybord()
      }
    }
  },
36 37 38 39 40 41 42
  directives: {
    keyboard: {
      inserted (el, binding, vnode) {
        vnode.context.initKeyboard(el)
      }
    }
  },
43 44 45 46 47
  mounted () {
    if (this.autoFocus || this.focus) {
      this.showSoftKeybord()
    }
  },
48
  beforeDestroy () {
49
    this.onKeyboardHide()
50 51 52 53
  },
  methods: {
    initKeyboard (el) {
      el.addEventListener('focus', () => {
54 55 56 57
        this.hideKeyboardTemp = function () {
          hideKeyboard()
        }
        UniViewJSBridge.subscribe('hideKeyboard', this.hideKeyboardTemp)
58
        document.addEventListener('click', iosHideKeyboard, false)
59 60
        this.setSoftinputNavBar()
        this.setSoftinputTemporary()
61
      })
62
      el.addEventListener('blur', this.onKeyboardHide.bind(this))
63 64
    },
    showSoftKeybord () {
65 66 67 68 69
      if (__PLATFORM__ === 'app-plus') {
        plusReady(() => {
          plus.key.showSoftKeybord()
        })
      }
70 71
    },
    setSoftinputTemporary () {
72 73 74 75 76 77 78 79 80 81 82 83
      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)
            }
          })
84
        })
85
      }
86
    },
87
    setSoftinputNavBar () {
88 89
      if (__PLATFORM__ === 'app-plus') {
        if (this.showConfirmBar === 'auto') {
90
          delete this.__softinputNavBar
91
          return
92 93 94
        }
        plusReady(() => {
          const currentWebview = plus.webview.currentWebview()
95 96 97 98 99 100 101 102 103 104
          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
          }
105 106 107
        })
      }
    },
108 109 110 111 112 113 114 115 116 117 118 119 120
    resetSoftinputNavBar () {
      if (__PLATFORM__ === 'app-plus') {
        const softinputNavBar = this.__softinputNavBar
        if (softinputNavBar) {
          plusReady(() => {
            const currentWebview = plus.webview.currentWebview()
            currentWebview.setStyle({
              softinputNavBar
            })
          })
        }
      }
    },
121
    onKeyboardHide () {
122
      UniViewJSBridge.unsubscribe('hideKeyboard', this.hideKeyboardTemp)
123
      document.removeEventListener('click', iosHideKeyboard, false)
124
      this.resetSoftinputNavBar()
125
      // 修复ios端显示与点击位置错位的Bug by:wyq
126 127
      if (String(navigator.vendor).indexOf('Apple') === 0) {
        document.documentElement.scrollTo(document.documentElement.scrollLeft, document.documentElement.scrollTop)
128
      }
129 130 131
    }
  }
}