<template>
  <iframe v-if="src" ref="iframe" class="comp-webview-default" :src="src" :allow="allow"  frameborder="0" @load="handleLoad"></iframe>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watchEffect } from 'vue';

const props = defineProps({
  url: {
    type: String,
    default: ''
  },
  isComputeHeight: {
    type: Boolean,
    default: false
  }
});

const emits = defineEmits(['init'])
const iframe = ref()
const src = ref('');
const allow = 'autoplay; fullscreen'

const handleMessage = (event) => {
  if (iframe.value && event.source !== iframe.value.contentWindow) {
    return;
  }
  const { type, data } = event.data;
  console.log(type, data)
};

const handleLoad = () => {
  emits('init')
};

onMounted(() => {
  window.addEventListener('message', handleMessage);
});

onBeforeUnmount(() => {
  window.removeEventListener('message', handleMessage);
});

watchEffect(() => {
  src.value = props.url;
});

</script>

<style scoped>
.comp-webview-default {
  border: 0;
  display: block;
}
</style>
