fix(frontend): 修复文件树与编辑器状态一致性
This commit is contained in:
@@ -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,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -69,6 +69,7 @@ export const useWorkspaceStore = defineStore('workspace', () => {
|
||||
vaultName.value = info.name
|
||||
fileTree.value = await workspaceService.getFileTree()
|
||||
hasVault.value = true
|
||||
localStorage.setItem('last-vault-path', info.path)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
@@ -82,6 +83,7 @@ export const useWorkspaceStore = defineStore('workspace', () => {
|
||||
vaultName.value = info.name
|
||||
fileTree.value = await workspaceService.getFileTree()
|
||||
hasVault.value = true
|
||||
localStorage.setItem('last-vault-path', info.path)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
@@ -113,6 +115,32 @@ export const useWorkspaceStore = defineStore('workspace', () => {
|
||||
remove(fileTree.value)
|
||||
}
|
||||
|
||||
function renamePath(oldPath: string, newPath: string, newName: string) {
|
||||
const node = findNodeByPath(fileTree.value, oldPath)
|
||||
if (!node) return
|
||||
const updateNodePath = (current: FileNode) => {
|
||||
if (current.path === oldPath) current.name = newName
|
||||
if (current.path === oldPath || current.path.startsWith(`${oldPath}/`)) {
|
||||
current.path = `${newPath}${current.path.slice(oldPath.length)}`
|
||||
}
|
||||
current.children?.forEach(updateNodePath)
|
||||
}
|
||||
updateNodePath(node)
|
||||
openFiles.value = openFiles.value.map((path) =>
|
||||
path === oldPath || path.startsWith(`${oldPath}/`) ? `${newPath}${path.slice(oldPath.length)}` : path
|
||||
)
|
||||
if (activeFilePath.value && (activeFilePath.value === oldPath || activeFilePath.value.startsWith(`${oldPath}/`))) {
|
||||
activeFilePath.value = `${newPath}${activeFilePath.value.slice(oldPath.length)}`
|
||||
}
|
||||
}
|
||||
|
||||
function closePath(path: string) {
|
||||
const activeWasRemoved = Boolean(activeFilePath.value && (activeFilePath.value === path || activeFilePath.value.startsWith(`${path}/`)))
|
||||
openFiles.value = openFiles.value.filter((openPath) => openPath !== path && !openPath.startsWith(`${path}/`))
|
||||
if (activeWasRemoved) activeFilePath.value = openFiles.value.at(-1) ?? null
|
||||
return activeWasRemoved
|
||||
}
|
||||
|
||||
return {
|
||||
vaultPath,
|
||||
vaultName,
|
||||
@@ -132,5 +160,7 @@ export const useWorkspaceStore = defineStore('workspace', () => {
|
||||
createVault,
|
||||
addFileToTree,
|
||||
removeFromTree,
|
||||
renamePath,
|
||||
closePath,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user