BaseFrameElement.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. // 注入 <base href>:srcdoc 内相对路径资源需要基于原始页面 URL 解析
  221. const baseHref = props.elementInfo.curl
  222. if (baseHref && !/<base\s/i.test(html)) {
  223. const safeHref = baseHref.replace(/"/g, '&quot;')
  224. const baseTag = `<base href="${safeHref}">`
  225. if (/<head[^>]*>/i.test(html)) {
  226. html = html.replace(/<head[^>]*>/i, match => `${match}${baseTag}`)
  227. }
  228. else if (/<html[^>]*>/i.test(html)) {
  229. html = html.replace(/<html[^>]*>/i, match => `${match}<head>${baseTag}</head>`)
  230. }
  231. else {
  232. html = `<head>${baseTag}</head>${html}`
  233. }
  234. }
  235. return html
  236. })
  237. const height = computed(() => {
  238. return props.elementInfo.height * props.scale
  239. })
  240. const left = computed(() => {
  241. return props.elementInfo.left * props.scale
  242. })
  243. const top = computed(() => {
  244. return props.elementInfo.top * props.scale
  245. })
  246. // 获取类型标签
  247. const getTypeLabel = (type: number): string => {
  248. const typeMap: Record<number, keyof typeof lang> = {
  249. 45: 'ssChoiceQ',
  250. 15: 'ssEssayQ',
  251. 72: 'ssAIApp',
  252. 73: 'ssH5Page',
  253. 81: 'ssH5Page',
  254. 74: 'ssVideo',
  255. 75: lang.lang == 'cn' ? 'ssBiliVideo' : 'ssYouTube',
  256. 76: 'ssCreateSpace',
  257. 77: 'ssEnglishSpeakingTool',
  258. 78: 'ssVote',
  259. 79: 'ssPhoto',
  260. 84: 'ssFile',
  261. [ARTICLE_READING_TOOL_TYPE]: 'ssArticleReadingTool',
  262. 831: 'ssKwlTitle',
  263. 832: 'ssSixHatTitle',
  264. }
  265. const key = typeMap[type]
  266. return (key ? lang[key] : lang.ssUnknown) as string
  267. }
  268. const frameloading = ref(false)
  269. // 处理iframe加载完成事件
  270. const handleIframeLoad = async (event: Event) => {
  271. const iframe = event.target as HTMLIFrameElement
  272. frameloading.value = true
  273. try {
  274. // 等待iframe完全加载
  275. await nextTick()
  276. setTimeout(async () => {
  277. // 检查iframe是否可访问(同源检查)
  278. if (iframe.contentWindow && iframe.contentDocument) {
  279. const iframeDoc = iframe.contentDocument
  280. const iframeHead =
  281. iframeDoc.head || iframeDoc.getElementsByTagName('head')[0]
  282. if (iframeHead) {
  283. // 使用动态导入获取JS文件内容
  284. const jsFiles = [
  285. {
  286. id: 'aws-sdk',
  287. importPath: () => import('./aws-sdk-2.235.1.min.js?raw'),
  288. },
  289. {
  290. id: 'jquery',
  291. importPath: () => import('./jquery-3.6.0.min.js?raw'),
  292. },
  293. { id: 'jietu', importPath: () => import('./jietu.js?raw') },
  294. ]
  295. for (const jsFile of jsFiles) {
  296. try {
  297. // 检查是否已经注入过
  298. if (!iframeDoc.getElementById(jsFile.id)) {
  299. const jsModule = await jsFile.importPath()
  300. const jsContent = jsModule.default || jsModule
  301. const scriptElement = iframeDoc.createElement('script')
  302. scriptElement.id = jsFile.id
  303. scriptElement.textContent = jsContent
  304. iframeHead.appendChild(scriptElement)
  305. console.log(`已注入 ${jsFile.id} 到iframe中`)
  306. }
  307. }
  308. catch (fetchError) {
  309. console.error(`获取 ${jsFile.id} 失败:`, fetchError)
  310. }
  311. }
  312. frameloading.value = false
  313. // 可选:在iframe中执行一些初始化代码
  314. try {
  315. iframe.contentWindow.eval(`
  316. console.log('iframe中的JS环境已准备就绪');
  317. // 这里可以添加一些初始化代码
  318. `)
  319. }
  320. catch (evalError) {
  321. frameloading.value = false
  322. console.warn('无法在iframe中执行代码:', evalError)
  323. }
  324. }
  325. }
  326. else {
  327. frameloading.value = false
  328. console.warn('无法访问iframe内容,可能是跨域限制')
  329. }
  330. }, 1000)
  331. }
  332. catch (error) {
  333. frameloading.value = false
  334. console.error('注入JS到iframe失败:', error)
  335. }
  336. }
  337. </script>
  338. <style lang="scss" scoped>
  339. .base-element-frame {
  340. position: absolute;
  341. }
  342. .element-content {
  343. width: 100%;
  344. height: 100%;
  345. overflow: hidden;
  346. video {
  347. width: 100%;
  348. height: 100%;
  349. object-fit: contain;
  350. background-color: #000;
  351. }
  352. }
  353. .mask {
  354. position: absolute;
  355. top: 0;
  356. bottom: 0;
  357. left: 0;
  358. right: 0;
  359. }
  360. .rotate-wrapper {
  361. width: 100%;
  362. height: 100%;
  363. overflow: hidden;
  364. }
  365. .thumbnail-content {
  366. width: 100%;
  367. height: 100%;
  368. background-color: #fff;
  369. }
  370. .thumbnail-content-inner {
  371. width: 100%;
  372. height: 100%;
  373. color: #ec8c00;
  374. font-size: 110px;
  375. font-weight: 600;
  376. text-align: center;
  377. line-height: 100%;
  378. display: flex;
  379. align-items: center;
  380. justify-content: center;
  381. flex-direction: column;
  382. gap: 50px;
  383. }
  384. /* iframe占位符样式 */
  385. .iframe-placeholder {
  386. width: 100%;
  387. height: 100%;
  388. background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  389. border: 2px solid #dee2e6;
  390. border-radius: 8px;
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. position: relative;
  395. overflow: hidden;
  396. }
  397. .placeholder-content {
  398. text-align: center;
  399. color: #6c757d;
  400. font-family: Arial, sans-serif;
  401. }
  402. .placeholder-icon {
  403. font-size: 48px;
  404. margin-bottom: 12px;
  405. opacity: 0.7;
  406. }
  407. .placeholder-text {
  408. font-size: 16px;
  409. font-weight: 600;
  410. margin-bottom: 4px;
  411. }
  412. .placeholder-type {
  413. font-size: 12px;
  414. opacity: 0.8;
  415. }
  416. /* 添加加载动画效果 */
  417. .iframe-placeholder::before {
  418. content: "";
  419. position: absolute;
  420. top: 0;
  421. left: -100%;
  422. width: 100%;
  423. height: 100%;
  424. background: linear-gradient(
  425. 90deg,
  426. transparent,
  427. rgba(255, 255, 255, 0.4),
  428. transparent
  429. );
  430. animation: shimmer 2s infinite;
  431. }
  432. @keyframes shimmer {
  433. 0% {
  434. left: -100%;
  435. }
  436. 100% {
  437. left: 100%;
  438. }
  439. }
  440. .fullscreen-spin {
  441. position: absolute;
  442. width: 100%;
  443. height: 100%;
  444. top: 0;
  445. left: 0;
  446. z-index: 100;
  447. display: flex;
  448. justify-content: center;
  449. align-items: center;
  450. &.mask {
  451. background-color: rgba($color: #f1f1f1, $alpha: .7);
  452. }
  453. }
  454. .spin {
  455. width: 200px;
  456. height: 200px;
  457. position: absolute;
  458. top: 50%;
  459. left: 50%;
  460. margin-top: -100px;
  461. margin-left: -100px;
  462. display: flex;
  463. flex-direction: column;
  464. justify-content: center;
  465. align-items: center;
  466. }
  467. .spinner {
  468. width: 36px;
  469. height: 36px;
  470. border: 3px solid #f6c82b;
  471. border-top-color: transparent;
  472. border-radius: 50%;
  473. animation: spinner .8s linear infinite;
  474. }
  475. .text {
  476. margin-top: 20px;
  477. color: #f6c82b;
  478. }
  479. @keyframes spinner {
  480. 0% {
  481. transform: rotate(0deg);
  482. }
  483. 100% {
  484. transform: rotate(360deg);
  485. }
  486. }
  487. </style>