BaseFrameElement.vue 11 KB

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