BaseFrameElement.vue 11 KB

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