Просмотр исходного кода

refactor(useImport): 优化图片导出状态共享与代码清理

1.  将imgExporting提取为模块级共享状态,确保所有useImport调用共用该ref
2.  简化window对象挂载imgExporting的逻辑,移除重复判断
3.  清理代码中的多余分号与注释格式,统一代码风格
lsc 3 дней назад
Родитель
Сommit
0f18325f43
1 измененных файлов с 16 добавлено и 13 удалено
  1. 16 13
      src/hooks/useImport.ts

+ 16 - 13
src/hooks/useImport.ts

@@ -15,6 +15,9 @@ import { showConfirmDialog } from '@/utils/confirmDialog'
 import { EMFJS, WMFJS } from 'rtf.js'
 import { EMFJS, WMFJS } from 'rtf.js'
 import * as UTIF from 'utif2'
 import * as UTIF from 'utif2'
 
 
+// 模块级共享状态,确保所有 useImport() 调用共用同一个 ref
+const _imgExporting = ref(false)
+
 /**
 /**
  * 获取 PPT 文件的基本信息
  * 获取 PPT 文件的基本信息
  */
  */
@@ -90,7 +93,7 @@ export default () => {
   const { isEmptySlide } = useSlideHandler()
   const { isEmptySlide } = useSlideHandler()
 
 
   const exporting = ref(false)
   const exporting = ref(false)
-  const imgExporting = ref(false)
+  const imgExporting = _imgExporting
 
 
   // 导入JSON文件
   // 导入JSON文件
   const importJSON = (files: FileList, cover = false) => {
   const importJSON = (files: FileList, cover = false) => {
@@ -246,14 +249,14 @@ export default () => {
     return json
     return json
   }
   }
 
 
-  // 优化暴露到 window 对象的方式,避免重复赋值
+  // 暴露到 window 对象
   if (typeof window !== 'undefined') {
   if (typeof window !== 'undefined') {
     const win = window as any
     const win = window as any
     if (!win.exportJSON) win.exportJSON = exportJSON2
     if (!win.exportJSON) win.exportJSON = exportJSON2
     if (!win.readJSON) win.readJSON = readJSON
     if (!win.readJSON) win.readJSON = readJSON
-    if (!win.imgExporting) win.imgExporting = () => {
-      console.log(imgExporting.value)
-      return imgExporting.value
+    win.imgExporting = () => {
+      console.log('imgExporting:', _imgExporting.value)
+      return _imgExporting.value
     }
     }
   }
   }
 
 
@@ -2055,22 +2058,22 @@ export default () => {
 
 
               const pattern: string | undefined = el.fill?.type === 'image' ? el.fill.value.picBase64 : undefined
               const pattern: string | undefined = el.fill?.type === 'image' ? el.fill.value.picBase64 : undefined
               const fill = el.fill?.type === 'color' ? el.fill.value : el.fill || 'none'
               const fill = el.fill?.type === 'color' ? el.fill.value : el.fill || 'none'
-              //const style = getStyle(convertFontSizePtToPx(el.content, ratio, el.autoFit)) + (el.pathBBox?.pWidth ? ';width:' + (el.pathBBox?.pWidth * ratio) + 'px;height:' + (el.pathBBox?.pHeight * ratio) + 'px;' : '') // 设置字体的样式等,这里由于不支持的样式在里面会过滤
-              const baseStyle = getStyle(convertFontSizePtToPx(el.content, ratio, el.autoFit));
+              // const style = getStyle(convertFontSizePtToPx(el.content, ratio, el.autoFit)) + (el.pathBBox?.pWidth ? ';width:' + (el.pathBBox?.pWidth * ratio) + 'px;height:' + (el.pathBBox?.pHeight * ratio) + 'px;' : '') // 设置字体的样式等,这里由于不支持的样式在里面会过滤
+              const baseStyle = getStyle(convertFontSizePtToPx(el.content, ratio, el.autoFit))
               const isVertical =
               const isVertical =
                 (typeof el.content === 'string' && el.content.includes('writing-mode: vertical-rl')) ||
                 (typeof el.content === 'string' && el.content.includes('writing-mode: vertical-rl')) ||
-                baseStyle.includes('writing-mode: vertical-rl');
+                baseStyle.includes('writing-mode: vertical-rl')
 
 
-              let extraStyle = '';
+              let extraStyle = ''
               if (el.pathBBox?.pWidth) {
               if (el.pathBBox?.pWidth) {
-                const w = el.pathBBox.pWidth * ratio;
-                const h = el.pathBBox.pHeight * ratio;
+                const w = el.pathBBox.pWidth * ratio
+                const h = el.pathBBox.pHeight * ratio
                 extraStyle = isVertical
                 extraStyle = isVertical
                   ? `;width:${h}px;height:${w}px;`
                   ? `;width:${h}px;height:${w}px;`
-                  : `;width:${w}px;height:${h}px;`;
+                  : `;width:${w}px;height:${h}px;`
               }
               }
 
 
-              const style = baseStyle + extraStyle;
+              const style = baseStyle + extraStyle
               const element: PPTShapeElement = {
               const element: PPTShapeElement = {
                 type: 'shape',
                 type: 'shape',
                 id: nanoid(10),
                 id: nanoid(10),