index.vue 2.8 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 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 = {
Q
qiang 已提交
20 21
    'uni-app': 'none',
    isUniH5: true
M
mehaotian 已提交
22 23 24
  }
  const parentTitleNView = parentWebview.getTitleNView()
  if (parentTitleNView) {
Q
qiang 已提交
25
    if (plus.navigator.isImmersedStatusbar()) {
26
      styles.top = NAVBAR_HEIGHT + plus.navigator.getStatusbarHeight()
Q
qiang 已提交
27
    } else {
28
      styles.top = NAVBAR_HEIGHT
29
    }
M
mehaotian 已提交
30 31 32 33 34
    styles.bottom = 0
  }
  webview = plus.webview.create('', htmlId, styles)
  if (parentTitleNView) {
    webview.addEventListener('titleUpdate', function () {
fxy060608's avatar
fxy060608 已提交
35
      const title = webview.getTitle()
M
mehaotian 已提交
36 37
      parentWebview.setStyle({
        titleNView: {
38 39
          // iOS titleText 为空字符串时 按钮会隐藏
          titleText: (!title || title === 'null') ? ' ' : title
M
mehaotian 已提交
40 41 42
        }
      })
    })
Q
qiang 已提交
43
  }
M
mehaotian 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  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 已提交
63
  }
M
mehaotian 已提交
64
  webview.loadURL(realPath)
Q
qiang 已提交
65
}
M
mehaotian 已提交
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

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 () {
D
DCloud_LXH 已提交
96 97 98 99 100 101 102 103 104 105
    this._onParentReady(() => {
      this.htmlId = WEBVIEW_ID_PREFIX + this.$page.id
      insertHTMLWebView({
        htmlId: this.htmlId
      })
      updateHTMLWebView({
        src: this.$getRealPath(this.src),
        webviewStyles: this.webviewStyles
      })
      UniViewJSBridge.publishHandler(WEBVIEW_INSERTED, {}, this.$page.id)
Q
qiang 已提交
106
    })
M
mehaotian 已提交
107 108
  },
  beforeDestroy () {
Q
qiang 已提交
109 110
    removeHTMLWebView()
    UniViewJSBridge.publishHandler(WEBVIEW_REMOVED, {}, this.$page.id)
M
mehaotian 已提交
111 112 113 114 115 116 117 118 119 120 121
  }
}
</script>
<style>
  uni-web-view {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
Q
qiang 已提交
122
</style>