feat: 添加重复名称检查功能,防止在同一目录下创建同名文件或目录,优化用户体验
This commit is contained in:
1 parent
5ca31a5348
commit
ac5488a21d
3 files changed
+149
-189
No files matched your search
@@ -1,17 +1,6 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
// 保持旧的接口用于数据迁移
|
||||
interface OldTemplateFile {
|
||||
path: string
|
||||
content?: string // Content for text files that are created directly
|
||||
sourceFilePath?: string // Path to copy from for file copying
|
||||
}
|
||||
|
||||
interface OldTemplateDirectory {
|
||||
path: string
|
||||
}
|
||||
|
||||
// 定义树状结构的节点
|
||||
export interface TreeNode {
|
||||
id: string
|
||||
@@ -108,92 +97,6 @@ export const useTemplateStore = defineStore('template', () => {
|
||||
}))
|
||||
}
|
||||
|
||||
// 将扁平结构转换为树状结构
|
||||
function convertFlatToTreeStructure(files: OldTemplateFile[], directories: OldTemplateDirectory[]): TreeNode[] {
|
||||
// 创建根节点
|
||||
const root: TreeNode = {
|
||||
id: 'root',
|
||||
name: 'root',
|
||||
path: '',
|
||||
type: 'directory',
|
||||
children: []
|
||||
}
|
||||
|
||||
// 先处理所有目录
|
||||
directories.forEach(dir => {
|
||||
const pathParts = dir.path.split('/').filter(part => part !== '') // Split path and remove empty parts
|
||||
let currentLevel = root.children || []
|
||||
|
||||
// Navigate/create the directory structure
|
||||
for (let i = 0; i < pathParts.length; i++) {
|
||||
const part = pathParts[i]
|
||||
const isLastPart = i === pathParts.length - 1
|
||||
|
||||
// Check if this part already exists in the current level
|
||||
let existingItem = currentLevel.find(item => item.name === part)
|
||||
|
||||
if (!existingItem) {
|
||||
// Create new directory item
|
||||
const newDir: TreeNode = {
|
||||
id: `dir-${part}-${Date.now()}-${Math.random()}`, // Unique ID
|
||||
name: part,
|
||||
path: dir.path.substring(0, dir.path.indexOf(part) + part.length), // Full path up to this part
|
||||
type: 'directory',
|
||||
children: []
|
||||
}
|
||||
currentLevel.push(newDir)
|
||||
existingItem = newDir
|
||||
}
|
||||
|
||||
// Move to the next level if not the last part
|
||||
if (!isLastPart && existingItem.children) {
|
||||
currentLevel = existingItem.children
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Process each file and add to the appropriate directory
|
||||
files.forEach(file => {
|
||||
const pathParts = file.path.split('/').filter(part => part !== '')
|
||||
const fileName = pathParts.pop() // Get the last part as filename
|
||||
|
||||
if (!fileName) return // Skip if no filename
|
||||
|
||||
let currentLevel = root.children || []
|
||||
|
||||
// Navigate to the appropriate directory for the file
|
||||
for (let i = 0; i < pathParts.length; i++) {
|
||||
const part = pathParts[i]
|
||||
const dirItem = currentLevel.find(item => item.name === part && item.type === 'directory')
|
||||
|
||||
if (dirItem && dirItem.children) {
|
||||
currentLevel = dirItem.children
|
||||
} else {
|
||||
// If the directory doesn't exist, create it
|
||||
const newDir: TreeNode = {
|
||||
id: `dir-${part}-${Date.now()}-${Math.random()}`,
|
||||
name: part,
|
||||
path: pathParts.slice(0, i+1).join('/'),
|
||||
type: 'directory',
|
||||
children: []
|
||||
}
|
||||
currentLevel.push(newDir)
|
||||
currentLevel = newDir.children || []
|
||||
}
|
||||
}
|
||||
|
||||
// Add the file to the current level
|
||||
currentLevel.push({
|
||||
id: `file-${fileName}-${Date.now()}-${Math.random()}`,
|
||||
name: fileName,
|
||||
path: file.path,
|
||||
type: 'file'
|
||||
})
|
||||
})
|
||||
|
||||
return root.children || []
|
||||
}
|
||||
|
||||
return {
|
||||
templates,
|
||||
loadTemplates,
|
||||
|
||||
Reference in new issue
Block a user