BaseFrameElement.vue 6.9 KB

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