index.tsx 1.8 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7
import {
  defineComponent,
  ref,
  onMounted,
  Ref,
  onActivated,
  onDeactivated,
D
DCloud_LXH 已提交
8
  Teleport,
D
DCloud_LXH 已提交
9 10 11 12
} from 'vue'
import { ResizeSensor } from '@dcloudio/uni-components'
import { getRealPath } from '@dcloudio/uni-platform'
import { updateElementStyle } from '@dcloudio/uni-shared'
D
DCloud_LXH 已提交
13
import { separateAttrs } from '../../../helpers/dom'
D
DCloud_LXH 已提交
14 15 16 17 18 19 20 21 22 23 24

const props = {
  src: {
    type: String,
    default: '',
  },
}

type RootRef = Ref<HTMLElement | null>

export default /*#__PURE__*/ defineComponent({
D
DCloud_LXH 已提交
25
  inheritAttrs: false,
D
DCloud_LXH 已提交
26 27
  name: 'WebView',
  props,
D
DCloud_LXH 已提交
28
  setup(props, { attrs }) {
D
DCloud_LXH 已提交
29
    const rootRef: RootRef = ref(null)
D
DCloud_LXH 已提交
30 31
    const iframeRef: RootRef = ref(null)
    const _resize = useWebViewSize(rootRef, iframeRef)
D
DCloud_LXH 已提交
32

D
DCloud_LXH 已提交
33 34 35
    onMounted(() => {
      _resize()
    })
D
DCloud_LXH 已提交
36

D
DCloud_LXH 已提交
37 38 39
    onActivated(() => {
      iframeRef.value && (iframeRef.value.style.display = 'block')
    })
D
DCloud_LXH 已提交
40

D
DCloud_LXH 已提交
41 42 43
    onDeactivated(() => {
      iframeRef.value && (iframeRef.value.style.display = 'none')
    })
D
DCloud_LXH 已提交
44

D
DCloud_LXH 已提交
45 46
    return () => {
      const webViewAttrs = separateAttrs(attrs)
D
DCloud_LXH 已提交
47

D
DCloud_LXH 已提交
48 49 50 51 52
      return (
        <>
          <uni-web-view {...webViewAttrs.$otherAttrs} ref={rootRef}>
            <ResizeSensor onResize={_resize} />
          </uni-web-view>
D
DCloud_LXH 已提交
53

D
DCloud_LXH 已提交
54 55 56 57 58 59 60 61 62
          <Teleport to="body">
            <iframe
              ref={iframeRef}
              src={getRealPath(props.src)}
              {...webViewAttrs.$attrs}
            ></iframe>
          </Teleport>
        </>
      )
D
DCloud_LXH 已提交
63
    }
D
DCloud_LXH 已提交
64 65
  },
})
D
DCloud_LXH 已提交
66

D
DCloud_LXH 已提交
67
function useWebViewSize(rootRef: RootRef, iframeRef: RootRef) {
D
DCloud_LXH 已提交
68 69 70
  const _resize = () => {
    const { top, left, width, height } = rootRef.value!.getBoundingClientRect()

D
DCloud_LXH 已提交
71 72 73 74 75 76 77 78 79 80
    iframeRef.value &&
      updateElementStyle(iframeRef.value, {
        position: 'absolute',
        display: 'block',
        border: '0',
        top: top + 'px',
        left: left + 'px',
        width: width + 'px',
        height: height + 'px',
      })
D
DCloud_LXH 已提交
81 82
  }

D
DCloud_LXH 已提交
83
  return _resize
D
DCloud_LXH 已提交
84
}