keyboard.js 1.7 KB
Newer Older
1 2 3 4 5 6
function hideKeyboard () {
  document.activeElement.blur()
}

export default {
  name: 'Keyboard',
7 8 9 10 11 12 13 14 15 16
  props: {
    cursorSpacing: {
      type: Number,
      default: 0
    },
    adjustPosition: {
      type: Boolean,
      default: true
    }
  },
17 18 19 20 21 22 23 24 25 26 27 28
  watch: {
    focus (val) {
      if (val) {
        this.showSoftKeybord()
      }
    }
  },
  mounted () {
    if (this.autoFocus || this.focus) {
      this.showSoftKeybord()
    }
  },
29 30 31 32
  beforeDestroy () {
    UniViewJSBridge.unsubscribe('hideKeyboard', hideKeyboard)
  },
  methods: {
33 34 35 36 37 38 39 40 41
    plusReady (callback) {
      if (!callback) {
        return
      }
      if (window.plus) {
        return callback()
      }
      document.addEventListener('plusready', callback)
    },
42 43 44
    initKeyboard (el) {
      el.addEventListener('focus', () => {
        UniViewJSBridge.subscribe('hideKeyboard', hideKeyboard)
45
        this.setSoftinputTemporary()
46 47 48 49
      })
      el.addEventListener('blur', () => {
        UniViewJSBridge.unsubscribe('hideKeyboard', hideKeyboard)
      })
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    },
    showSoftKeybord () {
      this.plusReady(() => {
        plus.key.showSoftKeybord()
      })
    },
    setSoftinputTemporary () {
      this.plusReady(() => {
        var currentWebview = plus.webview.currentWebview()
        var style = currentWebview.getStyle() || {}
        if (style.softinputMode === 'adjustResize') {
          return
        }
        var rect = this.$el.getBoundingClientRect()
        currentWebview.setSoftinputTemporary && currentWebview.setSoftinputTemporary({
          mode: this.adjustPosition ? 'adjustPan' : 'nothing',
          position: {
            top: rect.top,
            height: rect.height + this.cursorSpacing
69 70 71
          }
        })
      })
72 73 74
    }
  }
}