优化:移除重复逻辑和语法高亮支持
- 提取文件列表排序公共函数 sortFileList - 统一应用文件夹优先排序规则 - 移除生产环境 source map,减小打包体积 - 提升代码可维护性
This commit is contained in:
@@ -216,7 +216,7 @@ import {
|
|||||||
|
|
||||||
// 导入公共工具函数和常量
|
// 导入公共工具函数和常量
|
||||||
import { STORAGE_KEYS, DEFAULTS } from '@/utils/constants'
|
import { STORAGE_KEYS, DEFAULTS } from '@/utils/constants'
|
||||||
import { formatBytes } from '@/utils/fileUtils'
|
import { formatBytes, sortFileList } from '@/utils/fileUtils'
|
||||||
|
|
||||||
// 导入 composables
|
// 导入 composables
|
||||||
import { useFileOperations } from '@/composables/useFileOperations'
|
import { useFileOperations } from '@/composables/useFileOperations'
|
||||||
@@ -350,6 +350,7 @@ const listDirectory = async () => {
|
|||||||
fileLoading.value = true
|
fileLoading.value = true
|
||||||
try {
|
try {
|
||||||
fileList.value = await listDir(filePath.value)
|
fileList.value = await listDir(filePath.value)
|
||||||
|
fileList.value = sortFileList(fileList.value)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('列出目录失败:', error)
|
console.error('列出目录失败:', error)
|
||||||
Message.error('列出目录失败: ' + (error.message || error))
|
Message.error('列出目录失败: ' + (error.message || error))
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, onMounted } from 'vue'
|
import { ref, computed, onMounted } from 'vue'
|
||||||
import { Message } from '@arco-design/web-vue'
|
import { Message } from '@arco-design/web-vue'
|
||||||
|
import { sortFileList } from '@/utils/fileUtils'
|
||||||
|
|
||||||
// 导入子组件
|
// 导入子组件
|
||||||
import Toolbar from './components/Toolbar.vue'
|
import Toolbar from './components/Toolbar.vue'
|
||||||
@@ -138,6 +139,7 @@ const loadDirectory = async (path: string) => {
|
|||||||
fileLoading.value = true
|
fileLoading.value = true
|
||||||
try {
|
try {
|
||||||
fileList.value = await listDirectory(path)
|
fileList.value = await listDirectory(path)
|
||||||
|
fileList.value = sortFileList(fileList.value)
|
||||||
console.log('Files loaded:', fileList.value.length)
|
console.log('Files loaded:', fileList.value.length)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Load directory error:', error)
|
console.error('Load directory error:', error)
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ import { useFileEdit } from './composables/useFileEdit'
|
|||||||
import { useCommonPaths } from './composables/useCommonPaths'
|
import { useCommonPaths } from './composables/useCommonPaths'
|
||||||
|
|
||||||
// 导入工具函数
|
// 导入工具函数
|
||||||
import { getFileName } from '@/utils/fileUtils'
|
import { getFileName, sortFileList } from '@/utils/fileUtils'
|
||||||
import { isImageFile, isVideoFile, isAudioFile, isPdfFile, isHtmlFile, isMarkdownFile } from '@/utils/fileTypeHelpers'
|
import { isImageFile, isVideoFile, isAudioFile, isPdfFile, isHtmlFile, isMarkdownFile } from '@/utils/fileTypeHelpers'
|
||||||
import { STORAGE_KEYS, DEFAULTS, UI_TEXT, VALIDATION_RULES, FILE_EXTENSIONS } from '@/utils/constants'
|
import { STORAGE_KEYS, DEFAULTS, UI_TEXT, VALIDATION_RULES, FILE_EXTENSIONS } from '@/utils/constants'
|
||||||
|
|
||||||
@@ -980,6 +980,7 @@ const loadDirectory = async (path: string) => {
|
|||||||
fileLoading.value = true
|
fileLoading.value = true
|
||||||
try {
|
try {
|
||||||
fileList.value = await fileOps.listDirectory(path)
|
fileList.value = await fileOps.listDirectory(path)
|
||||||
|
fileList.value = sortFileList(fileList.value)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Message.error(`加载目录失败: ${error}`)
|
Message.error(`加载目录失败: ${error}`)
|
||||||
} finally {
|
} finally {
|
||||||
@@ -1009,7 +1010,7 @@ const loadZipDirectoryContents = async (zipPath: string, currentDir: string): Pr
|
|||||||
filtered = allFiles.filter((f: any) => !f.path.includes('/'))
|
filtered = allFiles.filter((f: any) => !f.path.includes('/'))
|
||||||
}
|
}
|
||||||
|
|
||||||
return filtered.map((f: any) => ({
|
const result = filtered.map((f: any) => ({
|
||||||
name: f.name,
|
name: f.name,
|
||||||
path: f.path,
|
path: f.path,
|
||||||
is_dir: f.is_dir,
|
is_dir: f.is_dir,
|
||||||
@@ -1017,6 +1018,8 @@ const loadZipDirectoryContents = async (zipPath: string, currentDir: string): Pr
|
|||||||
mod_time: f.mod_time || '',
|
mod_time: f.mod_time || '',
|
||||||
is_favorite: false
|
is_favorite: false
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
return sortFileList(result)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('加载 ZIP 目录失败:', error)
|
console.error('加载 ZIP 目录失败:', error)
|
||||||
Message.error(`加载 ZIP 目录失败: ${error}`)
|
Message.error(`加载 ZIP 目录失败: ${error}`)
|
||||||
|
|||||||
@@ -320,3 +320,25 @@ export function sanitizeFileName(filename, replacement = '_') {
|
|||||||
|
|
||||||
return filename.replace(illegalChars, replacement)
|
return filename.replace(illegalChars, replacement)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件列表排序:文件夹优先,同类型按名称排序
|
||||||
|
* @param {Array} fileList - 文件列表
|
||||||
|
* @returns {Array} 排序后的文件列表
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* sortFileList([{name: 'b.txt', is_dir: false}, {name: 'a', is_dir: true}])
|
||||||
|
* // [{name: 'a', is_dir: true}, {name: 'b.txt', is_dir: false}]
|
||||||
|
*/
|
||||||
|
export function sortFileList(fileList) {
|
||||||
|
if (!Array.isArray(fileList)) return fileList
|
||||||
|
|
||||||
|
return fileList.sort((a, b) => {
|
||||||
|
// 如果都是目录或都是文件,按名称排序
|
||||||
|
if (a.is_dir === b.is_dir) {
|
||||||
|
return a.name.localeCompare(b.name)
|
||||||
|
}
|
||||||
|
// 目录优先
|
||||||
|
return a.is_dir ? -1 : 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export default defineConfig({
|
|||||||
build: {
|
build: {
|
||||||
outDir: 'dist',
|
outDir: 'dist',
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
sourcemap: true, // 启用 source map 用于调试
|
sourcemap: false, // 生产环境禁用 source map,减小打包体积
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
output: {
|
output: {
|
||||||
manualChunks: {
|
manualChunks: {
|
||||||
|
|||||||
Reference in New Issue
Block a user