index.vue 2.5 KB
Newer Older
M
mehaotian 已提交
1
<template>
Q
qiang 已提交
2
  <uni-web-view v-on="$listeners"/>
M
mehaotian 已提交
3
</template>
Q
qiang 已提交
4 5 6
<script>
import {
  WEBVIEW_INSERTED,
雪洛's avatar
雪洛 已提交
7 8
  WEBVIEW_REMOVED
} from '../../../constants'
M
mehaotian 已提交
9 10 11 12 13 14 15 16 17 18 19
let webview = false
const insertHTMLWebView = ({
  htmlId
}) => {
  const parentWebview = plus.webview.currentWebview()
  // fixed by hxy web-view 组件所在的 webview 不注入 uni-app 框架
  const styles = {
    'uni-app': 'none'
  }
  const parentTitleNView = parentWebview.getTitleNView()
  if (parentTitleNView) {
20 21 22 23 24
    if (plus.navigator.isImmersedStatusbar()) {
      styles.top = 44 + plus.navigator.getStatusbarHeight()
    } else {
      styles.top = 44
    }
M
mehaotian 已提交
25 26 27 28 29 30 31 32 33 34 35 36
    styles.bottom = 0
  }
  webview = plus.webview.create('', htmlId, styles)
  if (parentTitleNView) {
    webview.addEventListener('titleUpdate', function () {
      let title = webview.getTitle()
      parentWebview.setStyle({
        titleNView: {
          titleText: (!title || title === 'null') ? '' : title
        }
      })
    })
Q
qiang 已提交
37
  }
M
mehaotian 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  plus.webview.currentWebview().append(webview)
}

const updateHTMLWebView = ({
  htmlId,
  src,
  webviewStyles
}) => {
  // fixed by xxx 非空时才执行更新操作
  const realPath = src || ''
  if (!realPath) {
    return
  }
  if (/^(http|https):\/\//.test(realPath) && webviewStyles.progress) {
    webview.setStyle({
      progress: {
        color: webviewStyles.progress.color
      }
    })
Q
qiang 已提交
57
  }
M
mehaotian 已提交
58
  webview.loadURL(realPath)
Q
qiang 已提交
59
}
M
mehaotian 已提交
60 61 62 63 64 65 66 67 68 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

const removeHTMLWebView = () => {
  plus.webview.currentWebview().remove(webview)
  webview.close('none')
  webview = false
}

export default {
  name: 'WebView',
  props: {
    src: {
      type: String,
      default: ''
    },
    webviewStyles: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    src (val, oldVal) {
      webview && updateHTMLWebView({
        src: this.$getRealPath(val),
        webviewStyles: this.webviewStyles
      })
    }
  },
  mounted () {
    this.htmlId = 'webviewId' + this.$page.id
    insertHTMLWebView({
      htmlId: this.htmlId
    })
    updateHTMLWebView({
      src: this.$getRealPath(this.src),
      webviewStyles: this.webviewStyles
Q
qiang 已提交
97 98
    })
    UniViewJSBridge.publishHandler(WEBVIEW_INSERTED, {}, this.$page.id)
M
mehaotian 已提交
99 100
  },
  beforeDestroy () {
Q
qiang 已提交
101 102
    removeHTMLWebView()
    UniViewJSBridge.publishHandler(WEBVIEW_REMOVED, {}, this.$page.id)
M
mehaotian 已提交
103 104 105 106 107 108 109 110 111 112 113
  }
}
</script>
<style>
  uni-web-view {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
Q
qiang 已提交
114
</style>