feat: auto-save version history with 3s debounce
This commit is contained in:
@@ -161,6 +161,19 @@ export const useEditorStore = defineStore('editor', () => {
|
|||||||
|
|
||||||
function clearAISession() { aiSessionId.value = ''; localStorage.removeItem('editor_ai_session') }
|
function clearAISession() { aiSessionId.value = ''; localStorage.removeItem('editor_ai_session') }
|
||||||
|
|
||||||
|
let saveVersionTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let lastSavedContent = ''
|
||||||
|
|
||||||
|
function debouncedSaveVersion() {
|
||||||
|
const current = JSON.stringify(gameData.value)
|
||||||
|
if (current === lastSavedContent) return
|
||||||
|
if (saveVersionTimer) clearTimeout(saveVersionTimer)
|
||||||
|
saveVersionTimer = setTimeout(async () => {
|
||||||
|
lastSavedContent = current
|
||||||
|
await saveVersion('手动编辑')
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
|
||||||
async function autoSave() {
|
async function autoSave() {
|
||||||
try {
|
try {
|
||||||
await fetch('/api/save', {
|
await fetch('/api/save', {
|
||||||
@@ -169,6 +182,7 @@ export const useEditorStore = defineStore('editor', () => {
|
|||||||
body: JSON.stringify({ path: sourcePath.value, data: gameData.value }),
|
body: JSON.stringify({ path: sourcePath.value, data: gameData.value }),
|
||||||
})
|
})
|
||||||
} catch { /* dev server not running */ }
|
} catch { /* dev server not running */ }
|
||||||
|
debouncedSaveVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveVersion(label: string) {
|
async function saveVersion(label: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user