webview-screenshot-comparison.uvue 2.5 KB
Newer Older
1
<template>
2 3
  <web-view id="webview-screenshot-comparison" class="webview-screenshot-comparison" :webview-styles="webviewStyles"
    :src="src" @loaded="loaded" @error="error">
4 5 6 7
  </web-view>
</template>

<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9
  import { state } from '@/store/index.uts'

10 11 12 13 14 15 16
  export default {
    data() {
      return {
        baseSrc: '',
        src: '',
        webviewContext: null as WebviewContext | null,
        isLoaded: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18
        headerHeight: state.headerHeight,
        devicePixelRatio: state.devicePixelRatio,
19 20 21
        webviewStyles: {
          progress: false
        }
22 23 24
      }
    },
    onReady() {
25
      this.webviewContext = uni.createWebviewContext('webview-screenshot-comparison', this)
26 27 28
    },
    methods: {
      appendWebHeadPlaceholder() {
雪洛's avatar
雪洛 已提交
29
        if (this.src.indexOf('pages/template/navbar-lite/navbar-lite') > -1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
30
          this.webviewContext?.evalJS(`
31 32 33
          const uniNavbar = document.querySelector('.uni-navbar');
          uniNavbar.style.paddingTop = '${state.headerHeight - 44}px';
        `)
雪洛's avatar
雪洛 已提交
34
        } else if (this.src.indexOf('pages/template/scroll-fold-nav/scroll-fold-nav') > -1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
35
          this.webviewContext?.evalJS(`
36 37 38 39 40 41 42 43
          const heightSeat = document.querySelector('.height-seat');
          heightSeat.style.height = '125px';
          heightSeat.style.backgroundColor = '#f0f8ff';
          const topBox = document.querySelector('.top-box');
          topBox.style.top = '35px';
        `)
        } else if (this.src.indexOf('pages/template/pull-zoom-image/pull-zoom-image') > -1) {
          return
DCloud-WZF's avatar
DCloud-WZF 已提交
44 45
        } else {
          this.webviewContext?.evalJS(`
46 47 48 49 50 51 52 53 54 55 56 57
const hasWebHeadPlaceholder = document.querySelector('.web-head-placeholder-for-screenshot-comparison');
if (hasWebHeadPlaceholder) {
  return;
}
const webHeadPlaceholder = document.createElement('div');
webHeadPlaceholder.style.height = '${state.headerHeight - 44}px';
webHeadPlaceholder.style.backgroundColor = '#007aff';
webHeadPlaceholder.classList.add('web-head-placeholder-for-screenshot-comparison');
const uniPage = document.querySelector('uni-page');
const uniPageHead = document.querySelector('uni-page-head');
uniPage.insertBefore(webHeadPlaceholder, uniPageHead);
`);
DCloud-WZF's avatar
DCloud-WZF 已提交
58
        }
59 60 61
      },
      loaded() {
        this.isLoaded = true
DCloud-WZF's avatar
DCloud-WZF 已提交
62
        this.appendWebHeadPlaceholder();
63 64 65 66 67 68 69 70
      },
      error(event : WebViewErrorEvent) {
        console.log('webview load error', JSON.stringify(event.detail));
      }
    }
  }
</script>
<style>
71
  .webview-screenshot-comparison {
72 73 74
    flex: 1;
  }
</style>