BaseFrameElement.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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="isTopicDiscussionTool(elementInfo.toolType) && !isThumbnail && isVisible"
  41. :configId="elementInfo.url"
  42. :style="{ width: '100%', height: '100%' }"
  43. />
  44. <!-- 文章朗读预览(type 772):独立分支;缩略图仍走下方通用工具名,不挂载录音 UI -->
  45. <ArticleReadingPreview
  46. v-else-if="isArticleReadingTool(elementInfo.toolType) && !isThumbnail && isVisible"
  47. :configId="elementInfo.url"
  48. :style="{ width: '100%', height: '100%' }"
  49. />
  50. <!-- B站视频类型(type 75):使用 iframe -->
  51. <iframe
  52. v-else-if="elementInfo.toolType === 75 && !isThumbnail && isVisible"
  53. :key="`bilibili-${iframeKey}`"
  54. :src="elementInfo.url"
  55. :width="width"
  56. :height="height"
  57. :frameborder="0"
  58. :allowfullscreen="true"
  59. 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;"
  60. @load="handleIframeLoad"
  61. ></iframe>
  62. <!-- 延迟加载iframe:只有在可见且不是缩略图时才加载 -->
  63. <iframe
  64. :key="`html-${iframeKey}`"
  65. :srcdoc="processedSrcDoc"
  66. v-else-if="elementInfo.isHTML && !isThumbnail && isVisible"
  67. :width="width"
  68. :height="height"
  69. :frameborder="0"
  70. :allowfullscreen="true"
  71. 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;"
  72. @load="handleIframeLoad"
  73. ></iframe>
  74. <iframe
  75. :key="`src-${iframeKey}`"
  76. v-else-if="!isThumbnail && isVisible"
  77. :src="elementInfo.url"
  78. :width="width"
  79. :height="height"
  80. :frameborder="0"
  81. :allowfullscreen="true"
  82. 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;"
  83. @load="handleIframeLoad"
  84. ></iframe>
  85. <!-- 占位符:当不可见时显示 -->
  86. <div v-else-if="!isThumbnail && !isVisible" class="iframe-placeholder">
  87. <div class="placeholder-content">
  88. <div class="placeholder-icon">🌐</div>
  89. <div class="placeholder-text">{{ lang.ssInteract }}</div>
  90. <div class="placeholder-type">
  91. ({{ getTypeLabel(Number(elementInfo.toolType)) }})
  92. </div>
  93. </div>
  94. </div>
  95. <!-- 缩略图模式 -->
  96. <div v-else-if="isThumbnail" class="thumbnail-content">
  97. <div class="thumbnail-content-inner">
  98. <div>{{ lang.ssInteract }}</div>
  99. <div>({{ getTypeLabel(Number(elementInfo.toolType)) }})</div>
  100. </div>
  101. </div>
  102. <!-- 在放映模式下不显示遮罩层,允许用户与iframe交互 -->
  103. <div class="mask" v-if="false"></div>
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. <script lang="ts" setup>
  109. import type { PropType } from 'vue'
  110. import type { PPTFrameElement } from '@/types/slides'
  111. import { lang } from '@/main'
  112. import TopicDiscussionPreview from '@/views/Editor/EnglishSpeaking/preview/TopicDiscussionPreview.vue'
  113. import ArticleReadingPreview from '@/views/Editor/EnglishSpeaking/preview/ArticleReadingPreview.vue'
  114. import {
  115. ARTICLE_READING_TOOL_TYPE,
  116. isArticleReadingTool,
  117. isTopicDiscussionTool,
  118. } from '@/configs/englishSpeakingTools'
  119. import { ref, watch, nextTick } from 'vue'
  120. import { computed } from 'vue'
  121. const props = defineProps({
  122. elementInfo: {
  123. type: Object as PropType<PPTFrameElement>,
  124. required: true,
  125. },
  126. isThumbnail: {
  127. type: Boolean,
  128. default: false,
  129. },
  130. scale: {
  131. type: Number,
  132. default: 1
  133. },
  134. isVisible: {
  135. type: Boolean,
  136. default: false,
  137. },
  138. })
  139. // 用于强制刷新iframe的key
  140. const iframeKey = ref(0)
  141. // 监听elementInfo.url的变化
  142. watch(
  143. () => props.elementInfo.url,
  144. (newUrl, oldUrl) => {
  145. if (newUrl !== oldUrl) {
  146. // 通过改变key来强制刷新iframe
  147. iframeKey.value++
  148. }
  149. }
  150. )
  151. const width = computed(() => {
  152. return props.elementInfo.width * props.scale
  153. })
  154. // srcdoc 外链 CDN 镜像替换
  155. const CDN_RULES: [RegExp, string][] = [
  156. // ===== 1. lucide 特殊处理(必须放在最前,且指向 UMD)=====
  157. [/unpkg\.com\/lucide@latest/g,
  158. 'cdn.jsdmirror.com/npm/lucide@1.47.0/dist/umd/lucide.min.js'],
  159. [/unpkg\.com\/lucide@([\d.]+)/g,
  160. 'cdn.jsdmirror.com/npm/lucide@$1/dist/umd/lucide.min.js'],
  161. [/cdn\.jsdelivr\.net\/npm\/lucide@([\d.]+)(?!\/dist\/umd)/g,
  162. 'cdn.jsdmirror.com/npm/lucide@$1/dist/umd/lucide.min.js'],
  163. // ===== 2. unpkg → JSDMirror(一步到位,避免链式替换)=====
  164. [/unpkg\.com/g, 'cdn.jsdmirror.com/npm'],
  165. // ===== 3. jsDelivr 全系列 → JSDMirror =====
  166. [/fastly\.jsdelivr\.net/g, 'cdn.jsdmirror.com'],
  167. [/gcore\.jsdelivr\.net/g, 'cdn.jsdmirror.com'],
  168. [/cdn\.jsdelivr\.net/g, 'cdn.jsdmirror.com'],
  169. // ===== 4. Google Hosted Libraries → JSDMirror(按路径精确映射)=====
  170. [/ajax\.googleapis\.com\/ajax\/libs\/jquery\/([\d.]+)\/jquery(\.min)?\.js/g,
  171. 'cdn.jsdmirror.com/npm/jquery@$1/dist/jquery$2.js'],
  172. [/ajax\.googleapis\.com\/ajax\/libs\/([^\/]+)\/([\d.]+)\/(.+)/g,
  173. 'cdn.jsdmirror.com/npm/$1@$2/$3'],
  174. [/ajax\.googleapis\.com\/ajax\/libs\/([^\/]+)\/([\d.]+)/g,
  175. 'cdn.jsdmirror.com/npm/$1@$2'],
  176. // ===== 5. Google Fonts =====
  177. [/fonts\.googleapis\.com/g, 'fonts.loli.net'],
  178. [/fonts\.gstatic\.com/g, 'gstatic.loli.net'],
  179. [/themes\.googleusercontent\.com/g, 'gstatic.loli.net'],
  180. // ===== 6. cdnjs =====
  181. [/cdnjs\.cloudflare\.com/g, 'cdnjs.loli.net'],
  182. [/cdnjs\.loli\.net/g, 'cdnjs.loli.net'], // 幂等,防止重复替换
  183. // ===== 7. Font Awesome =====
  184. [/kit\.fontawesome\.com/g, 'cdn.jsdmirror.com/npm/@fortawesome/fontawesome-free@6/js/all.min.js'],
  185. [/use\.fontawesome\.com\/releases\/([^\/]+)\/css\/all\.min\.css/g,
  186. 'cdn.jsdmirror.com/npm/@fortawesome/fontawesome-free@$1/css/all.min.css'],
  187. // ===== 8. Bootstrap CDN =====
  188. [/stackpath\.bootstrapcdn\.com\/bootstrap\/([\d.]+)\/css\/bootstrap(\.min)?\.css/g,
  189. 'cdn.jsdmirror.com/npm/bootstrap@$1/dist/css/bootstrap$2.css'],
  190. [/stackpath\.bootstrapcdn\.com\/bootstrap\/([\d.]+)\/js\/bootstrap(\.min)?\.js/g,
  191. 'cdn.jsdmirror.com/npm/bootstrap@$1/dist/js/bootstrap$2.js'],
  192. [/maxcdn\.bootstrapcdn\.com\/bootstrap\/([\d.]+)\/css\/bootstrap(\.min)?\.css/g,
  193. 'cdn.jsdmirror.com/npm/bootstrap@$1/dist/css/bootstrap$2.css'],
  194. [/maxcdn\.bootstrapcdn\.com\/bootstrap\/([\d.]+)\/js\/bootstrap(\.min)?\.js/g,
  195. 'cdn.jsdmirror.com/npm/bootstrap@$1/dist/js/bootstrap$2.js'],
  196. // ===== 9. polyfill =====
  197. [/polyfill\.io/g, 'cdn.jsdmirror.com/npm/polyfill-library@3/polyfill.min.js'],
  198. [/polyfill-fastly\.io/g, 'cdn.jsdmirror.com/npm/polyfill-library@3/polyfill.min.js'],
  199. // ===== 10. Tailwind Play CDN =====
  200. [/https?:\/\/cdn\.tailwindcss\.com[\d.\/]*/g,
  201. 'https://cdn.jsdmirror.com/npm/@tailwindcss/browser@4.3.3'],
  202. // ===== 11. jQuery 官方 CDN =====
  203. [/code\.jquery\.com\/jquery-([\d.]+)(\.min)?\.js/g,
  204. 'cdn.jsdmirror.com/npm/jquery@$1/dist/jquery$2.js'],
  205. // ===== 12. GitHub Raw =====
  206. [/raw\.githubusercontent\.com\/([^\/]+)\/([^\/]+)\/([^\/]+)\/(.+)/g,
  207. 'cdn.jsdmirror.com/gh/$1/$2@$3/$4'],
  208. [/raw\.githack\.com\/([^\/]+)\/([^\/]+)\/([^\/]+)\/(.+)/g,
  209. 'cdn.jsdmirror.com/gh/$1/$2@$3/$4'],
  210. // ===== 13. Gravatar(这个真没有靠谱国内镜像,建议保留原样)=====
  211. // 如果必须替换,用下面这条,但不保证可用
  212. // [/www\.gravatar\.com\/avatar/g, 'sdn.geekzu.org/avatar'],
  213. ]
  214. const processedSrcDoc = computed(() => {
  215. let html = props.elementInfo.url || ''
  216. if (!html || typeof html !== 'string') return html
  217. for (const [pattern, replacement] of CDN_RULES) {
  218. html = html.replace(pattern, replacement)
  219. }
  220. return html
  221. })
  222. const height = computed(() => {
  223. return props.elementInfo.height * props.scale
  224. })
  225. const left = computed(() => {
  226. return props.elementInfo.left * props.scale
  227. })
  228. const top = computed(() => {
  229. return props.elementInfo.top * props.scale
  230. })
  231. // 获取类型标签
  232. const getTypeLabel = (type: number): string => {
  233. const typeMap: Record<number, keyof typeof lang> = {
  234. 45: 'ssChoiceQ',
  235. 15: 'ssEssayQ',
  236. 72: 'ssAIApp',
  237. 73: 'ssH5Page',
  238. 81: 'ssH5Page',
  239. 74: 'ssVideo',
  240. 75: lang.lang == 'cn' ? 'ssBiliVideo' : 'ssYouTube',
  241. 76: 'ssCreateSpace',
  242. 77: 'ssEnglishSpeakingTool',
  243. 78: 'ssVote',
  244. 79: 'ssPhoto',
  245. 84: 'ssFile',
  246. [ARTICLE_READING_TOOL_TYPE]: 'ssArticleReadingTool',
  247. 831: 'ssKwlTitle',
  248. 832: 'ssSixHatTitle',
  249. }
  250. const key = typeMap[type]
  251. return (key ? lang[key] : lang.ssUnknown) as string
  252. }
  253. const frameloading = ref(false)
  254. // 处理iframe加载完成事件
  255. const handleIframeLoad = async (event: Event) => {
  256. const iframe = event.target as HTMLIFrameElement
  257. frameloading.value = true
  258. try {
  259. // 等待iframe完全加载
  260. await nextTick()
  261. setTimeout(async () => {
  262. // 检查iframe是否可访问(同源检查)
  263. if (iframe.contentWindow && iframe.contentDocument) {
  264. const iframeDoc = iframe.contentDocument
  265. const iframeHead =
  266. iframeDoc.head || iframeDoc.getElementsByTagName('head')[0]
  267. if (iframeHead) {
  268. // 使用动态导入获取JS文件内容
  269. const jsFiles = [
  270. {
  271. id: 'aws-sdk',
  272. importPath: () => import('./aws-sdk-2.235.1.min.js?raw'),
  273. },
  274. {
  275. id: 'jquery',
  276. importPath: () => import('./jquery-3.6.0.min.js?raw'),
  277. },
  278. { id: 'jietu', importPath: () => import('./jietu.js?raw') },
  279. ]
  280. for (const jsFile of jsFiles) {
  281. try {
  282. // 检查是否已经注入过
  283. if (!iframeDoc.getElementById(jsFile.id)) {
  284. const jsModule = await jsFile.importPath()
  285. const jsContent = jsModule.default || jsModule
  286. const scriptElement = iframeDoc.createElement('script')
  287. scriptElement.id = jsFile.id
  288. scriptElement.textContent = jsContent
  289. iframeHead.appendChild(scriptElement)
  290. console.log(`已注入 ${jsFile.id} 到iframe中`)
  291. }
  292. }
  293. catch (fetchError) {
  294. console.error(`获取 ${jsFile.id} 失败:`, fetchError)
  295. }
  296. }
  297. frameloading.value = false
  298. // 可选:在iframe中执行一些初始化代码
  299. try {
  300. iframe.contentWindow.eval(`
  301. console.log('iframe中的JS环境已准备就绪');
  302. // 这里可以添加一些初始化代码
  303. `)
  304. }
  305. catch (evalError) {
  306. frameloading.value = false
  307. console.warn('无法在iframe中执行代码:', evalError)
  308. }
  309. }
  310. }
  311. else {
  312. frameloading.value = false
  313. console.warn('无法访问iframe内容,可能是跨域限制')
  314. }
  315. }, 1000)
  316. }
  317. catch (error) {
  318. frameloading.value = false
  319. console.error('注入JS到iframe失败:', error)
  320. }
  321. }
  322. </script>
  323. <style lang="scss" scoped>
  324. .base-element-frame {
  325. position: absolute;
  326. }
  327. .element-content {
  328. width: 100%;
  329. height: 100%;
  330. overflow: hidden;
  331. video {
  332. width: 100%;
  333. height: 100%;
  334. object-fit: contain;
  335. background-color: #000;
  336. }
  337. }
  338. .mask {
  339. position: absolute;
  340. top: 0;
  341. bottom: 0;
  342. left: 0;
  343. right: 0;
  344. }
  345. .rotate-wrapper {
  346. width: 100%;
  347. height: 100%;
  348. overflow: hidden;
  349. }
  350. .thumbnail-content {
  351. width: 100%;
  352. height: 100%;
  353. background-color: #fff;
  354. }
  355. .thumbnail-content-inner {
  356. width: 100%;
  357. height: 100%;
  358. color: #ec8c00;
  359. font-size: 110px;
  360. font-weight: 600;
  361. text-align: center;
  362. line-height: 100%;
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. flex-direction: column;
  367. gap: 50px;
  368. }
  369. /* iframe占位符样式 */
  370. .iframe-placeholder {
  371. width: 100%;
  372. height: 100%;
  373. background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  374. border: 2px solid #dee2e6;
  375. border-radius: 8px;
  376. display: flex;
  377. align-items: center;
  378. justify-content: center;
  379. position: relative;
  380. overflow: hidden;
  381. }
  382. .placeholder-content {
  383. text-align: center;
  384. color: #6c757d;
  385. font-family: Arial, sans-serif;
  386. }
  387. .placeholder-icon {
  388. font-size: 48px;
  389. margin-bottom: 12px;
  390. opacity: 0.7;
  391. }
  392. .placeholder-text {
  393. font-size: 16px;
  394. font-weight: 600;
  395. margin-bottom: 4px;
  396. }
  397. .placeholder-type {
  398. font-size: 12px;
  399. opacity: 0.8;
  400. }
  401. /* 添加加载动画效果 */
  402. .iframe-placeholder::before {
  403. content: "";
  404. position: absolute;
  405. top: 0;
  406. left: -100%;
  407. width: 100%;
  408. height: 100%;
  409. background: linear-gradient(
  410. 90deg,
  411. transparent,
  412. rgba(255, 255, 255, 0.4),
  413. transparent
  414. );
  415. animation: shimmer 2s infinite;
  416. }
  417. @keyframes shimmer {
  418. 0% {
  419. left: -100%;
  420. }
  421. 100% {
  422. left: 100%;
  423. }
  424. }
  425. .fullscreen-spin {
  426. position: absolute;
  427. width: 100%;
  428. height: 100%;
  429. top: 0;
  430. left: 0;
  431. z-index: 100;
  432. display: flex;
  433. justify-content: center;
  434. align-items: center;
  435. &.mask {
  436. background-color: rgba($color: #f1f1f1, $alpha: .7);
  437. }
  438. }
  439. .spin {
  440. width: 200px;
  441. height: 200px;
  442. position: absolute;
  443. top: 50%;
  444. left: 50%;
  445. margin-top: -100px;
  446. margin-left: -100px;
  447. display: flex;
  448. flex-direction: column;
  449. justify-content: center;
  450. align-items: center;
  451. }
  452. .spinner {
  453. width: 36px;
  454. height: 36px;
  455. border: 3px solid #f6c82b;
  456. border-top-color: transparent;
  457. border-radius: 50%;
  458. animation: spinner .8s linear infinite;
  459. }
  460. .text {
  461. margin-top: 20px;
  462. color: #f6c82b;
  463. }
  464. @keyframes spinner {
  465. 0% {
  466. transform: rotate(0deg);
  467. }
  468. 100% {
  469. transform: rotate(360deg);
  470. }
  471. }
  472. </style>