| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <template>
- <!--
-
- width: elementInfo.width + 'px',
- height: elementInfo.height + 'px',
-
- -->
- <div
- class="base-element-frame"
- :style="{
- transform: isThumbnail ? 'scale(1)': `scale(${1 / props.scale})`,
- transformOrigin: 'top left', // 关键点
- top: props.elementInfo.top + 'px',
- left: props.elementInfo.left + 'px',
- width: (isThumbnail ? props.elementInfo.width : width) + 'px',
- height: (isThumbnail ? props.elementInfo.height : height) + 'px',
- overflow: 'hidden',
- }"
- >
- <div
- class="rotate-wrapper"
- :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
- >
- <div class="element-content">
- <div class="fullscreen-spin mask" v-if="(!elementInfo.isDone && !isThumbnail && isVisible) || frameloading">
- <div class="spin">
- <div class="spinner"></div>
- </div>
- </div>
- <!-- 视频类型(type 74):使用 video 标签 -->
- <video
- v-if="elementInfo.toolType === 74 && !isThumbnail && isVisible"
- :key="`video-${iframeKey}`"
- :src="elementInfo.url"
- :width="width"
- :height="height"
- controls
- :style="{ width: '100%', height: '100%', objectFit: 'contain' }"
- ></video>
- <!-- 英语口语预览(type 77):使用 Vue 组件直接渲染,url 字段即 configId -->
- <TopicDiscussionPreview
- v-else-if="Number(elementInfo.toolType) === 77 && !isThumbnail && isVisible"
- :configId="elementInfo.url"
- :style="{ width: '100%', height: '100%' }"
- />
- <!-- B站视频类型(type 75):使用 iframe -->
- <iframe
- v-else-if="elementInfo.toolType === 75 && !isThumbnail && isVisible"
- :key="`bilibili-${iframeKey}`"
- :src="elementInfo.url"
- :width="width"
- :height="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;"
- @load="handleIframeLoad"
- ></iframe>
- <!-- 延迟加载iframe:只有在可见且不是缩略图时才加载 -->
- <iframe
- :key="`html-${iframeKey}`"
- :srcdoc="elementInfo.url"
- v-else-if="elementInfo.isHTML && !isThumbnail && isVisible"
- :width="width"
- :height="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;"
- @load="handleIframeLoad"
- ></iframe>
- <iframe
- :key="`src-${iframeKey}`"
- v-else-if="!isThumbnail && isVisible"
- :src="elementInfo.url"
- :width="width"
- :height="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;"
- @load="handleIframeLoad"
- ></iframe>
- <!-- 占位符:当不可见时显示 -->
- <div v-else-if="!isThumbnail && !isVisible" class="iframe-placeholder">
- <div class="placeholder-content">
- <div class="placeholder-icon">🌐</div>
- <div class="placeholder-text">{{ lang.ssInteract }}</div>
- <div class="placeholder-type">
- ({{ getTypeLabel(Number(elementInfo.toolType)) }})
- </div>
- </div>
- </div>
- <!-- 缩略图模式 -->
- <div v-else-if="isThumbnail" class="thumbnail-content">
- <div class="thumbnail-content-inner">
- <div>{{ lang.ssInteract }}</div>
- <div>({{ getTypeLabel(Number(elementInfo.toolType)) }})</div>
- </div>
- </div>
- <!-- 在放映模式下不显示遮罩层,允许用户与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 { lang } from '@/main'
- import TopicDiscussionPreview from '@/views/Editor/EnglishSpeaking/preview/TopicDiscussionPreview.vue'
- import { ref, watch, nextTick } from 'vue'
- import { computed } from 'vue'
- const props = defineProps({
- elementInfo: {
- type: Object as PropType<PPTFrameElement>,
- required: true,
- },
- isThumbnail: {
- type: Boolean,
- default: false,
- },
- scale: {
- type: Number,
- default: 1
- },
- isVisible: {
- type: Boolean,
- default: false,
- },
- })
- // 用于强制刷新iframe的key
- const iframeKey = ref(0)
- // 监听elementInfo.url的变化
- watch(
- () => props.elementInfo.url,
- (newUrl, oldUrl) => {
- if (newUrl !== oldUrl) {
- // 通过改变key来强制刷新iframe
- iframeKey.value++
- }
- }
- )
- const width = computed(() => {
- return props.elementInfo.width * props.scale
- })
- const height = computed(() => {
- return props.elementInfo.height * props.scale
- })
- const left = computed(() => {
- return props.elementInfo.left * props.scale
- })
- const top = computed(() => {
- return props.elementInfo.top * props.scale
- })
- // 获取类型标签
- const getTypeLabel = (type: number): string => {
- const typeMap: Record<number, keyof typeof lang> = {
- 45: 'ssChoiceQ',
- 15: 'ssEssayQ',
- 72: 'ssAIApp',
- 73: 'ssH5Page',
- 74: 'ssVideo',
- 75: lang.lang == 'cn' ? 'ssBiliVideo' : 'ssYouTube',
- 76: 'ssCreateSpace',
- 77: 'ssEnglishSpeakingTool',
- 78: 'ssVote',
- }
- const key = typeMap[type]
- return (key ? lang[key] : lang.ssUnknown) as string
- }
- const frameloading = ref(false)
- // 处理iframe加载完成事件
- const handleIframeLoad = async (event: Event) => {
- const iframe = event.target as HTMLIFrameElement
- frameloading.value = true
- try {
- // 等待iframe完全加载
- await nextTick()
- setTimeout(async () => {
- // 检查iframe是否可访问(同源检查)
- if (iframe.contentWindow && iframe.contentDocument) {
- const iframeDoc = iframe.contentDocument
- const iframeHead =
- iframeDoc.head || iframeDoc.getElementsByTagName('head')[0]
- if (iframeHead) {
- // 使用动态导入获取JS文件内容
- const jsFiles = [
- {
- id: 'aws-sdk',
- importPath: () => import('./aws-sdk-2.235.1.min.js?raw'),
- },
- {
- id: 'jquery',
- importPath: () => import('./jquery-3.6.0.min.js?raw'),
- },
- { id: 'jietu', importPath: () => import('./jietu.js?raw') },
- ]
- for (const jsFile of jsFiles) {
- try {
- // 检查是否已经注入过
- if (!iframeDoc.getElementById(jsFile.id)) {
- const jsModule = await jsFile.importPath()
- const jsContent = jsModule.default || jsModule
- const scriptElement = iframeDoc.createElement('script')
- scriptElement.id = jsFile.id
- scriptElement.textContent = jsContent
- iframeHead.appendChild(scriptElement)
- console.log(`已注入 ${jsFile.id} 到iframe中`)
- }
- }
- catch (fetchError) {
- console.error(`获取 ${jsFile.id} 失败:`, fetchError)
- }
- }
- frameloading.value = false
- // 可选:在iframe中执行一些初始化代码
- try {
- iframe.contentWindow.eval(`
- console.log('iframe中的JS环境已准备就绪');
- // 这里可以添加一些初始化代码
- `)
- }
- catch (evalError) {
- frameloading.value = false
- console.warn('无法在iframe中执行代码:', evalError)
- }
- }
- }
- else {
- frameloading.value = false
- console.warn('无法访问iframe内容,可能是跨域限制')
- }
- }, 1000)
- }
- catch (error) {
- frameloading.value = false
- console.error('注入JS到iframe失败:', error)
- }
- }
- </script>
-
- <style lang="scss" scoped>
- .base-element-frame {
- position: absolute;
- }
- .element-content {
- width: 100%;
- height: 100%;
- overflow: hidden;
- video {
- width: 100%;
- height: 100%;
- object-fit: contain;
- background-color: #000;
- }
- }
- .mask {
- position: absolute;
- top: 0;
- bottom: 0;
- left: 0;
- right: 0;
- }
- .rotate-wrapper {
- width: 100%;
- height: 100%;
- overflow: hidden;
- }
- .thumbnail-content {
- width: 100%;
- height: 100%;
- background-color: #fff;
- }
- .thumbnail-content-inner {
- width: 100%;
- height: 100%;
- color: #ec8c00;
- font-size: 110px;
- font-weight: 600;
- text-align: center;
- line-height: 100%;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- gap: 50px;
- }
- /* iframe占位符样式 */
- .iframe-placeholder {
- width: 100%;
- height: 100%;
- background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
- border: 2px solid #dee2e6;
- border-radius: 8px;
- display: flex;
- align-items: center;
- justify-content: center;
- position: relative;
- overflow: hidden;
- }
- .placeholder-content {
- text-align: center;
- color: #6c757d;
- font-family: Arial, sans-serif;
- }
- .placeholder-icon {
- font-size: 48px;
- margin-bottom: 12px;
- opacity: 0.7;
- }
- .placeholder-text {
- font-size: 16px;
- font-weight: 600;
- margin-bottom: 4px;
- }
- .placeholder-type {
- font-size: 12px;
- opacity: 0.8;
- }
- /* 添加加载动画效果 */
- .iframe-placeholder::before {
- content: "";
- position: absolute;
- top: 0;
- left: -100%;
- width: 100%;
- height: 100%;
- background: linear-gradient(
- 90deg,
- transparent,
- rgba(255, 255, 255, 0.4),
- transparent
- );
- animation: shimmer 2s infinite;
- }
- @keyframes shimmer {
- 0% {
- left: -100%;
- }
- 100% {
- left: 100%;
- }
- }
- .fullscreen-spin {
- position: absolute;
- width: 100%;
- height: 100%;
- top: 0;
- left: 0;
- z-index: 100;
- display: flex;
- justify-content: center;
- align-items: center;
- &.mask {
- background-color: rgba($color: #f1f1f1, $alpha: .7);
- }
- }
- .spin {
- width: 200px;
- height: 200px;
- position: absolute;
- top: 50%;
- left: 50%;
- margin-top: -100px;
- margin-left: -100px;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- }
- .spinner {
- width: 36px;
- height: 36px;
- border: 3px solid #f6c82b;
- border-top-color: transparent;
- border-radius: 50%;
- animation: spinner .8s linear infinite;
- }
- .text {
- margin-top: 20px;
- color: #f6c82b;
- }
- @keyframes spinner {
- 0% {
- transform: rotate(0deg);
- }
- 100% {
- transform: rotate(360deg);
- }
- }
- </style>
|