fix(frontend): 修复文件树与编辑器状态一致性

This commit is contained in:
2026-08-29 23:59:11 +08:00
parent 5aa0026bb9
commit afd49ba00f
4 changed files with 90 additions and 13 deletions
+23 -7
View File
@@ -30,9 +30,7 @@ export const useEditorStore = defineStore('editor', () => {
function updateContent(newContent: string) {
content.value = newContent
if (saveStatus.value === 'saved' || saveStatus.value === 'idle') {
saveStatus.value = 'dirty'
}
saveStatus.value = 'dirty'
}
let saveTimer: ReturnType<typeof setTimeout> | null = null
@@ -47,24 +45,34 @@ export const useEditorStore = defineStore('editor', () => {
async function save() {
if (!currentFilePath.value) return
if (saveStatus.value === 'saving') return
const targetPath = currentFilePath.value
const snapshot = content.value
saveStatus.value = 'saving'
try {
await workspaceService.saveFileContent(currentFilePath.value, content.value)
saveStatus.value = 'saved'
lastSavedAt.value = new Date().toISOString()
await workspaceService.saveFileContent(targetPath, snapshot)
if (currentFilePath.value === targetPath) {
saveStatus.value = content.value === snapshot ? 'saved' : 'dirty'
lastSavedAt.value = new Date().toISOString()
}
} catch {
saveStatus.value = 'save_failed'
}
}
let loadVersion = 0
async function loadFile(filePath: string) {
const version = ++loadVersion
currentFilePath.value = filePath
saveStatus.value = 'saving'
try {
content.value = await workspaceService.readFileContent(filePath)
const loadedContent = await workspaceService.readFileContent(filePath)
if (version !== loadVersion || currentFilePath.value !== filePath) return
content.value = loadedContent
saveStatus.value = 'saved'
lastSavedAt.value = new Date().toISOString()
} catch {
if (version !== loadVersion || currentFilePath.value !== filePath) return
content.value = ''
saveStatus.value = 'idle'
}
@@ -89,6 +97,7 @@ export const useEditorStore = defineStore('editor', () => {
}
function closeFile() {
loadVersion++
if (saveTimer) clearTimeout(saveTimer)
currentFilePath.value = null
currentNoteId.value = null
@@ -98,6 +107,12 @@ export const useEditorStore = defineStore('editor', () => {
highlightBlockId.value = null
}
function renameFilePath(oldPath: string, newPath: string) {
if (currentFilePath.value === oldPath || currentFilePath.value?.startsWith(`${oldPath}/`)) {
currentFilePath.value = `${newPath}${currentFilePath.value.slice(oldPath.length)}`
}
}
return {
mode,
content,
@@ -118,5 +133,6 @@ export const useEditorStore = defineStore('editor', () => {
highlightBlock,
setExternalChanged,
closeFile,
renameFilePath,
}
})