BaseFrameElement.vue 7.1 KB

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