BaseFrameElement.vue 9.1 KB

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