Browse Source

fix: 修复资源重复选择提示

jiaxing.liao 1 week ago
parent
commit
bed5db7010

+ 59 - 39
src/renderer/src/views/designer/sidebar/Resource.vue

@@ -77,6 +77,7 @@
 import type { FontResource, ImageResource, OtherResource, Resource } from '@/types/resource'
 
 import { ref, computed } from 'vue'
+import { ElMessage } from 'element-plus'
 import { SplitterCollapse, SplitterCollapseItem } from '@/components/SplitterCollapse'
 import { LuPlus, LuTrash2 } from 'vue-icons-plus/lu'
 import { useProjectStore } from '@/store/modules/project'
@@ -87,6 +88,61 @@ const projectStore = useProjectStore()
 const imageSearch = ref('')
 const fontSearch = ref('')
 const otherSearch = ref('')
+type ResourceListKey = 'images' | 'fonts' | 'others'
+type ResourceType = 'image' | 'font' | 'other'
+
+const normalizeResourcePath = (path: string) => path.replaceAll('/', '\\').toLowerCase()
+
+const isResourceExists = (type: ResourceListKey, resourcePath: string) => {
+  const normalizedPath = normalizeResourcePath(resourcePath)
+  return (
+    projectStore.project?.resources[type].some(
+      (item) => normalizeResourcePath(item.path) === normalizedPath
+    ) ?? false
+  )
+}
+
+const addResourceFile = async (
+  sourcePath: string,
+  type: ResourceType,
+  resourceListKey: ResourceListKey
+) => {
+  const fileName = sourcePath.split(/[\\/]/).pop()
+  if (!fileName) return
+
+  const resourcePath = `\\src\\assets\\${resourceListKey}\\${fileName}`
+  if (isResourceExists(resourceListKey, resourcePath)) {
+    ElMessage.warning(
+      `\u8d44\u6e90 ${fileName} \u5df2\u7ecf\u5b58\u5728\uff0c\u4e0d\u80fd\u91cd\u590d\u6dfb\u52a0`
+    )
+    return
+  }
+
+  await window.electron.ipcRenderer.invoke(
+    'copy-file',
+    sourcePath,
+    `${projectStore.projectPath}\\src\\assets\\${resourceListKey}\\${fileName}`
+  )
+
+  const resource = createFileResource(resourcePath, type)
+  if (resourceListKey === 'images') {
+    projectStore.project?.resources.images.push(resource as ImageResource)
+  } else if (resourceListKey === 'fonts') {
+    projectStore.project?.resources.fonts.push(resource as FontResource)
+  } else {
+    projectStore.project?.resources.others.push(resource as OtherResource)
+  }
+}
+
+const addResourceFiles = async (
+  paths: string[] | undefined,
+  type: ResourceType,
+  resourceListKey: ResourceListKey
+) => {
+  for (const path of paths || []) {
+    await addResourceFile(path, type, resourceListKey)
+  }
+}
 
 const getImages = computed(() => {
   return projectStore.project?.resources.images.filter((item) =>
@@ -120,19 +176,7 @@ const handleAddImage = async () => {
     ]
   })
 
-  paths?.forEach(async (path) => {
-    const fileName = path.split('\\').pop()
-    // 复制文件
-    await window.electron.ipcRenderer.invoke(
-      'copy-file',
-      path,
-      `${projectStore.projectPath}\\src\\assets\\images\\${fileName}`
-    )
-    // 记录文件
-    projectStore.project?.resources.images.push(
-      createFileResource(`\\src\\assets\\images\\${fileName}`, 'image') as ImageResource
-    )
-  })
+  await addResourceFiles(paths, 'image', 'images')
 }
 
 // 添加字体
@@ -149,19 +193,7 @@ const handleAddFont = async () => {
     ]
   })
 
-  paths?.forEach(async (path) => {
-    const fileName = path.split('\\').pop()
-    // 复制文件
-    await window.electron.ipcRenderer.invoke(
-      'copy-file',
-      path,
-      `${projectStore.projectPath}\\src\\assets\\fonts\\${fileName}`
-    )
-    // 记录文件
-    projectStore.project?.resources.fonts.push(
-      createFileResource(`\\src\\assets\\fonts\\${fileName}`, 'font') as FontResource
-    )
-  })
+  await addResourceFiles(paths, 'font', 'fonts')
 }
 
 // 添加其他资源
@@ -172,19 +204,7 @@ const handleAddOther = async () => {
     properties: ['openFile', 'multiSelections']
   })
 
-  paths?.forEach(async (path) => {
-    const fileName = path.split('\\').pop()
-    // 复制文件
-    await window.electron.ipcRenderer.invoke(
-      'copy-file',
-      path,
-      `${projectStore.projectPath}\\src\\assets\\others\\${fileName}`
-    )
-    // 记录文件
-    projectStore.project?.resources.others.push(
-      createFileResource(`\\src\\assets\\others\\${fileName}`, 'other') as OtherResource
-    )
-  })
+  await addResourceFiles(paths, 'other', 'others')
 }
 
 // 删除资源

+ 1 - 1
src/renderer/src/views/designer/sidebar/components/ThemeModal.vue

@@ -16,7 +16,7 @@
               spellcheck="false"
               clearable
               :disabled="row.__sourceKey === DEFAULT_THEME_KEY"
-              placeholder="如 daytime_theme"
+              placeholder="如 dark_theme"
               @input="sanitizeEditKey"
             />
           </template>