feat: 重构模板管理,采用树状结构存储文件和目录,优化模板创建和编辑流程
This commit is contained in:
1 parent
f8890ff1e9
commit
5ca31a5348
4 files changed
+351
-246
No files matched your search
@@ -1,22 +1,35 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
interface TemplateFile {
|
||||
// 保持旧的接口用于数据迁移
|
||||
interface OldTemplateFile {
|
||||
path: string
|
||||
content?: string // Content for text files that are created directly
|
||||
sourceFilePath?: string // Path to copy from for file copying
|
||||
}
|
||||
|
||||
interface TemplateDirectory {
|
||||
interface OldTemplateDirectory {
|
||||
path: string
|
||||
}
|
||||
|
||||
// 定义树状结构的节点
|
||||
export interface TreeNode {
|
||||
id: string
|
||||
name: string
|
||||
path: string // 完整路径
|
||||
type: 'directory' | 'file'
|
||||
children?: TreeNode[]
|
||||
// 文件特定属性
|
||||
content?: string
|
||||
sourceFilePath?: string
|
||||
}
|
||||
|
||||
export interface Template {
|
||||
id: string
|
||||
name: string
|
||||
description: string
|
||||
files: TemplateFile[]
|
||||
directories: TemplateDirectory[]
|
||||
// 使用树状结构存储
|
||||
treeStructure: TreeNode[]
|
||||
}
|
||||
|
||||
export const useTemplateStore = defineStore('template', () => {
|
||||
@@ -95,6 +108,92 @@ 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