import { defineStore } from 'pinia' import { ref } from 'vue' // 定义树状结构的节点 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 // 使用树状结构存储 treeStructure: TreeNode[] } export const useTemplateStore = defineStore('template', () => { const templates = ref([]) const dataFile = 'templates.json' // Load templates from local storage or initialize async function loadTemplates() { try { const stored = await window.mainApi.invoke('msgLoadData', dataFile) if (stored) { templates.value = JSON.parse(stored as string) || [] } else { // Initialize with sample data if no stored data templates.value = [] } } catch (error) { console.error('Failed to load templates:', error) templates.value = [] } } // Save templates to local storage function saveTemplates() { try { window.mainApi.invoke( 'msgSaveData', dataFile, JSON.stringify(templates.value) ) } catch (error) { console.error('Failed to save templates:', error) } } // Get all templates function getTemplates() { return templates.value } // Get template by ID function getTemplateById(id: string) { return templates.value.find((template) => template.id === id) } // Add new template function addTemplate(template: Omit) { const newTemplate: Template = { id: `template_${Date.now()}`, ...template } templates.value.push(newTemplate) saveTemplates() } // Update existing template function updateTemplate(updatedTemplate: Template) { const index = templates.value.findIndex((t) => t.id === updatedTemplate.id) if (index !== -1) { templates.value[index] = updatedTemplate saveTemplates() } } // Delete template function deleteTemplate(id: string) { templates.value = templates.value.filter((template) => template.id !== id) saveTemplates() } // Get template names for dropdowns function getTemplateNames() { return templates.value.map((template) => ({ text: template.name, value: template.id })) } return { templates, loadTemplates, getTemplates, getTemplateById, addTemplate, updateTemplate, deleteTemplate, getTemplateNames } })