<template>
  <div class="base-element-frame"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
    }"
  >
    <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      <div class="element-content">
        <iframe 
          :key="`html-${iframeKey}`"
          :srcdoc="elementInfo.url" 
          v-if="elementInfo.isHTML"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"
          allow="camera *; microphone *; display-capture; midi; encrypted-media; fullscreen; geolocation; clipboard-read; clipboard-write; accelerometer; autoplay; gyroscope; payment; picture-in-picture; usb; xr-spatial-tracking;"
        ></iframe>
        <iframe 
          :key="`src-${iframeKey}`"
          v-else
          :src="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"
          allow="camera *; microphone *; display-capture; midi; encrypted-media; fullscreen; geolocation; clipboard-read; clipboard-write; accelerometer; autoplay; gyroscope; payment; picture-in-picture; usb; xr-spatial-tracking;"
        ></iframe>

        <!-- 在放映模式下不显示遮罩层，允许用户与iframe交互 -->
        <div class="mask" v-if="false"></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { PropType } from 'vue'
import type { PPTFrameElement } from '@/types/slides'
import { ref, watch } from 'vue'

const props = defineProps({
  elementInfo: {
    type: Object as PropType<PPTFrameElement>,
    required: true,
  },
})

// 用于强制刷新iframe的key
const iframeKey = ref(0)

// 监听elementInfo.url的变化
watch(() => props.elementInfo.url, (newUrl, oldUrl) => {
  if (newUrl !== oldUrl) {
    // 通过改变key来强制刷新iframe
    iframeKey.value++
  }
})
</script>

<style lang="scss" scoped>
.base-element-frame {
  position: absolute;
}
.element-content {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.mask {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
.rotate-wrapper {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style> 