BaseFrameElement.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <!--
  3. width: elementInfo.width + 'px',
  4. height: elementInfo.height + 'px',
  5. -->
  6. <div
  7. class="base-element-frame"
  8. :style="{
  9. transform: isThumbnail ? 'scale(1)': `scale(${1 / props.scale})`,
  10. transformOrigin: 'top left', // 关键点
  11. top: props.elementInfo.top + 'px',
  12. left: props.elementInfo.left + 'px',
  13. width: (isThumbnail ? props.elementInfo.width : width) + 'px',
  14. height: (isThumbnail ? props.elementInfo.height : height) + 'px',
  15. overflow: 'hidden',
  16. }"
  17. >
  18. <div
  19. class="rotate-wrapper"
  20. :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
  21. >
  22. <div class="element-content">
  23. <div class="fullscreen-spin mask" v-if="(!elementInfo.isDone && !isThumbnail && isVisible) || frameloading">
  24. <div class="spin">
  25. <div class="spinner"></div>
  26. </div>
  27. </div>
  28. <!-- 视频类型(type 74):使用 video 标签 -->
  29. <video
  30. v-if="elementInfo.toolType === 74 && !isThumbnail && isVisible"
  31. :key="`video-${iframeKey}`"
  32. :src="elementInfo.url"
  33. :width="width"
  34. :height="height"
  35. controls
  36. :style="{ width: '100%', height: '100%', objectFit: 'contain' }"
  37. ></video>
  38. <!-- 英语口语预览(type 77):使用 Vue 组件直接渲染,url 字段即 configId -->
  39. <TopicDiscussionPreview
  40. v-else-if="isTopicDiscussionTool(elementInfo.toolType) && !isThumbnail && isVisible"
  41. :configId="elementInfo.url"
  42. :style="{ width: '100%', height: '100%' }"
  43. />
  44. <!-- 文章朗读预览(type 772):独立分支;缩略图仍走下方通用工具名,不挂载录音 UI -->
  45. <ArticleReadingPreview
  46. v-else-if="isArticleReadingTool(elementInfo.toolType) && !isThumbnail && isVisible"
  47. :configId="elementInfo.url"
  48. :style="{ width: '100%', height: '100%' }"
  49. />
  50. <!-- B站视频类型(type 75):使用 iframe -->
  51. <iframe
  52. v-else-if="elementInfo.toolType === 75 && !isThumbnail && isVisible"
  53. :key="`bilibili-${iframeKey}`"
  54. :src="elementInfo.url"
  55. :width="width"
  56. :height="height"
  57. :frameborder="0"
  58. :allowfullscreen="true"
  59. 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;"
  60. @load="handleIframeLoad"
  61. ></iframe>
  62. <!-- 延迟加载iframe:只有在可见且不是缩略图时才加载 -->
  63. <iframe
  64. :key="`html-${iframeKey}`"
  65. :srcdoc="elementInfo.url"
  66. v-else-if="elementInfo.isHTML && !isThumbnail && isVisible"
  67. :width="width"
  68. :height="height"
  69. :frameborder="0"
  70. :allowfullscreen="true"
  71. 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;"
  72. @load="handleIframeLoad"
  73. ></iframe>
  74. <iframe
  75. :key="`src-${iframeKey}`"
  76. v-else-if="!isThumbnail && isVisible"
  77. :src="elementInfo.url"
  78. :width="width"
  79. :height="height"
  80. :frameborder="0"
  81. :allowfullscreen="true"
  82. 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;"
  83. @load="handleIframeLoad"
  84. ></iframe>
  85. <!-- 占位符:当不可见时显示 -->
  86. <div v-else-if="!isThumbnail && !isVisible" class="iframe-placeholder">
  87. <div class="placeholder-content">
  88. <div class="placeholder-icon">🌐</div>
  89. <div class="placeholder-text">{{ lang.ssInteract }}</div>
  90. <div class="placeholder-type">
  91. ({{ getTypeLabel(Number(elementInfo.toolType)) }})
  92. </div>
  93. </div>
  94. </div>
  95. <!-- 缩略图模式 -->
  96. <div v-else-if="isThumbnail" class="thumbnail-content">
  97. <div class="thumbnail-content-inner">
  98. <div>{{ lang.ssInteract }}</div>
  99. <div>({{ getTypeLabel(Number(elementInfo.toolType)) }})</div>
  100. </div>
  101. </div>
  102. <!-- 在放映模式下不显示遮罩层,允许用户与iframe交互 -->
  103. <div class="mask" v-if="false"></div>
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. <script lang="ts" setup>
  109. import type { PropType } from 'vue'
  110. import type { PPTFrameElement } from '@/types/slides'
  111. import { lang } from '@/main'
  112. import TopicDiscussionPreview from '@/views/Editor/EnglishSpeaking/preview/TopicDiscussionPreview.vue'
  113. import ArticleReadingPreview from '@/views/Editor/EnglishSpeaking/preview/ArticleReadingPreview.vue'
  114. import {
  115. ARTICLE_READING_TOOL_TYPE,
  116. isArticleReadingTool,
  117. isTopicDiscussionTool,
  118. } from '@/configs/englishSpeakingTools'
  119. import { ref, watch, nextTick } from 'vue'
  120. import { computed } from 'vue'
  121. const props = defineProps({
  122. elementInfo: {
  123. type: Object as PropType<PPTFrameElement>,
  124. required: true,
  125. },
  126. isThumbnail: {
  127. type: Boolean,
  128. default: false,
  129. },
  130. scale: {
  131. type: Number,
  132. default: 1
  133. },
  134. isVisible: {
  135. type: Boolean,
  136. default: false,
  137. },
  138. })
  139. // 用于强制刷新iframe的key
  140. const iframeKey = ref(0)
  141. // 监听elementInfo.url的变化
  142. watch(
  143. () => props.elementInfo.url,
  144. (newUrl, oldUrl) => {
  145. if (newUrl !== oldUrl) {
  146. // 通过改变key来强制刷新iframe
  147. iframeKey.value++
  148. }
  149. }
  150. )
  151. const width = computed(() => {
  152. return props.elementInfo.width * props.scale
  153. })
  154. const height = computed(() => {
  155. return props.elementInfo.height * props.scale
  156. })
  157. const left = computed(() => {
  158. return props.elementInfo.left * props.scale
  159. })
  160. const top = computed(() => {
  161. return props.elementInfo.top * props.scale
  162. })
  163. // 获取类型标签
  164. const getTypeLabel = (type: number): string => {
  165. const typeMap: Record<number, keyof typeof lang> = {
  166. 45: 'ssChoiceQ',
  167. 15: 'ssEssayQ',
  168. 72: 'ssAIApp',
  169. 73: 'ssH5Page',
  170. 81: 'ssH5Page',
  171. 74: 'ssVideo',
  172. 75: lang.lang == 'cn' ? 'ssBiliVideo' : 'ssYouTube',
  173. 76: 'ssCreateSpace',
  174. 77: 'ssEnglishSpeakingTool',
  175. 78: 'ssVote',
  176. 79: 'ssPhoto',
  177. 84: 'ssFile',
  178. [ARTICLE_READING_TOOL_TYPE]: 'ssArticleReadingTool',
  179. 831: 'ssKwlTitle',
  180. }
  181. const key = typeMap[type]
  182. return (key ? lang[key] : lang.ssUnknown) as string
  183. }
  184. const frameloading = ref(false)
  185. // 处理iframe加载完成事件
  186. const handleIframeLoad = async (event: Event) => {
  187. const iframe = event.target as HTMLIFrameElement
  188. frameloading.value = true
  189. try {
  190. // 等待iframe完全加载
  191. await nextTick()
  192. setTimeout(async () => {
  193. // 检查iframe是否可访问(同源检查)
  194. if (iframe.contentWindow && iframe.contentDocument) {
  195. const iframeDoc = iframe.contentDocument
  196. const iframeHead =
  197. iframeDoc.head || iframeDoc.getElementsByTagName('head')[0]
  198. if (iframeHead) {
  199. // 使用动态导入获取JS文件内容
  200. const jsFiles = [
  201. {
  202. id: 'aws-sdk',
  203. importPath: () => import('./aws-sdk-2.235.1.min.js?raw'),
  204. },
  205. {
  206. id: 'jquery',
  207. importPath: () => import('./jquery-3.6.0.min.js?raw'),
  208. },
  209. { id: 'jietu', importPath: () => import('./jietu.js?raw') },
  210. ]
  211. for (const jsFile of jsFiles) {
  212. try {
  213. // 检查是否已经注入过
  214. if (!iframeDoc.getElementById(jsFile.id)) {
  215. const jsModule = await jsFile.importPath()
  216. const jsContent = jsModule.default || jsModule
  217. const scriptElement = iframeDoc.createElement('script')
  218. scriptElement.id = jsFile.id
  219. scriptElement.textContent = jsContent
  220. iframeHead.appendChild(scriptElement)
  221. console.log(`已注入 ${jsFile.id} 到iframe中`)
  222. }
  223. }
  224. catch (fetchError) {
  225. console.error(`获取 ${jsFile.id} 失败:`, fetchError)
  226. }
  227. }
  228. frameloading.value = false
  229. // 可选:在iframe中执行一些初始化代码
  230. try {
  231. iframe.contentWindow.eval(`
  232. console.log('iframe中的JS环境已准备就绪');
  233. // 这里可以添加一些初始化代码
  234. `)
  235. }
  236. catch (evalError) {
  237. frameloading.value = false
  238. console.warn('无法在iframe中执行代码:', evalError)
  239. }
  240. }
  241. }
  242. else {
  243. frameloading.value = false
  244. console.warn('无法访问iframe内容,可能是跨域限制')
  245. }
  246. }, 1000)
  247. }
  248. catch (error) {
  249. frameloading.value = false
  250. console.error('注入JS到iframe失败:', error)
  251. }
  252. }
  253. </script>
  254. <style lang="scss" scoped>
  255. .base-element-frame {
  256. position: absolute;
  257. }
  258. .element-content {
  259. width: 100%;
  260. height: 100%;
  261. overflow: hidden;
  262. video {
  263. width: 100%;
  264. height: 100%;
  265. object-fit: contain;
  266. background-color: #000;
  267. }
  268. }
  269. .mask {
  270. position: absolute;
  271. top: 0;
  272. bottom: 0;
  273. left: 0;
  274. right: 0;
  275. }
  276. .rotate-wrapper {
  277. width: 100%;
  278. height: 100%;
  279. overflow: hidden;
  280. }
  281. .thumbnail-content {
  282. width: 100%;
  283. height: 100%;
  284. background-color: #fff;
  285. }
  286. .thumbnail-content-inner {
  287. width: 100%;
  288. height: 100%;
  289. color: #ec8c00;
  290. font-size: 110px;
  291. font-weight: 600;
  292. text-align: center;
  293. line-height: 100%;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. flex-direction: column;
  298. gap: 50px;
  299. }
  300. /* iframe占位符样式 */
  301. .iframe-placeholder {
  302. width: 100%;
  303. height: 100%;
  304. background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  305. border: 2px solid #dee2e6;
  306. border-radius: 8px;
  307. display: flex;
  308. align-items: center;
  309. justify-content: center;
  310. position: relative;
  311. overflow: hidden;
  312. }
  313. .placeholder-content {
  314. text-align: center;
  315. color: #6c757d;
  316. font-family: Arial, sans-serif;
  317. }
  318. .placeholder-icon {
  319. font-size: 48px;
  320. margin-bottom: 12px;
  321. opacity: 0.7;
  322. }
  323. .placeholder-text {
  324. font-size: 16px;
  325. font-weight: 600;
  326. margin-bottom: 4px;
  327. }
  328. .placeholder-type {
  329. font-size: 12px;
  330. opacity: 0.8;
  331. }
  332. /* 添加加载动画效果 */
  333. .iframe-placeholder::before {
  334. content: "";
  335. position: absolute;
  336. top: 0;
  337. left: -100%;
  338. width: 100%;
  339. height: 100%;
  340. background: linear-gradient(
  341. 90deg,
  342. transparent,
  343. rgba(255, 255, 255, 0.4),
  344. transparent
  345. );
  346. animation: shimmer 2s infinite;
  347. }
  348. @keyframes shimmer {
  349. 0% {
  350. left: -100%;
  351. }
  352. 100% {
  353. left: 100%;
  354. }
  355. }
  356. .fullscreen-spin {
  357. position: absolute;
  358. width: 100%;
  359. height: 100%;
  360. top: 0;
  361. left: 0;
  362. z-index: 100;
  363. display: flex;
  364. justify-content: center;
  365. align-items: center;
  366. &.mask {
  367. background-color: rgba($color: #f1f1f1, $alpha: .7);
  368. }
  369. }
  370. .spin {
  371. width: 200px;
  372. height: 200px;
  373. position: absolute;
  374. top: 50%;
  375. left: 50%;
  376. margin-top: -100px;
  377. margin-left: -100px;
  378. display: flex;
  379. flex-direction: column;
  380. justify-content: center;
  381. align-items: center;
  382. }
  383. .spinner {
  384. width: 36px;
  385. height: 36px;
  386. border: 3px solid #f6c82b;
  387. border-top-color: transparent;
  388. border-radius: 50%;
  389. animation: spinner .8s linear infinite;
  390. }
  391. .text {
  392. margin-top: 20px;
  393. color: #f6c82b;
  394. }
  395. @keyframes spinner {
  396. 0% {
  397. transform: rotate(0deg);
  398. }
  399. 100% {
  400. transform: rotate(360deg);
  401. }
  402. }
  403. </style>