index.vue 3.0 KB
Newer Older
M
mehaotian 已提交
1
<template>
fxy060608's avatar
fxy060608 已提交
2
  <uni-web-view v-on="$listeners" />
M
mehaotian 已提交
3
</template>
Q
qiang 已提交
4 5 6
<script>
import {
  WEBVIEW_INSERTED,
7 8
  WEBVIEW_REMOVED,
  WEBVIEW_ID_PREFIX
雪洛's avatar
雪洛 已提交
9
} from '../../../constants'
10 11 12

import { NAVBAR_HEIGHT } from 'uni-helpers/constants'

M
mehaotian 已提交
13 14
let webview = false
const insertHTMLWebView = ({
15
  webviewStyles,
16 17
  htmlId,
  updateTitle
M
mehaotian 已提交
18 19 20
}) => {
  const parentWebview = plus.webview.currentWebview()
  // fixed by hxy web-view 组件所在的 webview 不注入 uni-app 框架
21 22 23 24 25 26 27 28 29
  const styles = Object.assign(
    {
      'uni-app': 'none',
      isUniH5: true,
      // ios 默认绘制到安全区外
      contentAdjust: false
    },
    webviewStyles
  )
M
mehaotian 已提交
30 31
  const parentTitleNView = parentWebview.getTitleNView()
  if (parentTitleNView) {
Q
qiang 已提交
32
    if (plus.navigator.isImmersedStatusbar()) {
33
      styles.top = NAVBAR_HEIGHT + plus.navigator.getStatusbarHeight()
Q
qiang 已提交
34
    } else {
35
      styles.top = NAVBAR_HEIGHT
36
    }
M
mehaotian 已提交
37 38 39 40 41
    styles.bottom = 0
  }
  webview = plus.webview.create('', htmlId, styles)
  if (parentTitleNView) {
    webview.addEventListener('titleUpdate', function () {
42
      if (!updateTitle) return
fxy060608's avatar
fxy060608 已提交
43
      const title = webview.getTitle()
M
mehaotian 已提交
44 45
      parentWebview.setStyle({
        titleNView: {
46 47
          // iOS titleText 为空字符串时 按钮会隐藏
          titleText: (!title || title === 'null') ? ' ' : title
M
mehaotian 已提交
48 49 50
        }
      })
    })
Q
qiang 已提交
51
  }
M
mehaotian 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  plus.webview.currentWebview().append(webview)
}

const updateHTMLWebView = ({
  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 已提交
70
  }
M
mehaotian 已提交
71
  webview.loadURL(realPath)
Q
qiang 已提交
72
}
M
mehaotian 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86

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

export default {
  name: 'WebView',
  props: {
    src: {
      type: String,
      default: ''
    },
87 88 89 90
    updateTitle: {
      type: Boolean,
      default: true
    },
M
mehaotian 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    webviewStyles: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    src (val, oldVal) {
      webview && updateHTMLWebView({
        src: this.$getRealPath(val),
        webviewStyles: this.webviewStyles
      })
    }
  },
  mounted () {
D
DCloud_LXH 已提交
107 108
    this.htmlId = WEBVIEW_ID_PREFIX + this.$page.id
    insertHTMLWebView({
109
      webviewStyles: this.webviewStyles,
110 111
      htmlId: this.htmlId,
      updateTitle: this.updateTitle
D
DCloud_LXH 已提交
112 113 114 115
    })
    updateHTMLWebView({
      src: this.$getRealPath(this.src),
      webviewStyles: this.webviewStyles
Q
qiang 已提交
116
    })
D
DCloud_LXH 已提交
117
    UniViewJSBridge.publishHandler(WEBVIEW_INSERTED, {}, this.$page.id)
M
mehaotian 已提交
118 119
  },
  beforeDestroy () {
Q
qiang 已提交
120 121
    removeHTMLWebView()
    UniViewJSBridge.publishHandler(WEBVIEW_REMOVED, {}, this.$page.id)
M
mehaotian 已提交
122 123 124 125 126 127 128 129 130 131 132
  }
}
</script>
<style>
  uni-web-view {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
Q
qiang 已提交
133
</style>