feat: optimize auto-save logic

This commit is contained in:
purocean
2024-03-08 15:02:17 +08:00
parent bfd512b2ef
commit b4c6acccc6
+38 -29
View File
@@ -559,37 +559,46 @@ export async function ensureCurrentFileSaved () {
try {
const autoSave = !isEncrypted(currentFile) && getSetting('auto-save', 2000)
if (autoSave) {
await saveContent()
} else {
const confirm = await useModal().confirm({
title: t('save-check-dialog.title'),
content: t('save-check-dialog.desc'),
action: h(Fragment, [
h('button', {
onClick: async () => {
await saveContent()
useModal().ok()
}
}, t('save')),
h('button', {
onClick: () => useModal().ok()
}, t('discard')),
h('button', {
onClick: () => useModal().cancel()
}, t('cancel')),
])
})
checkFile()
if (confirm) {
if (!store.getters.isSaved && currentFile.content) {
store.state.currentContent = currentFile.content!
}
} else {
throw new Error('Document not saved')
try {
await saveContent()
return
} catch (error: any) {
useToast().show('warning', error.message)
}
}
const confirm = await useModal().confirm({
title: t('save-check-dialog.title'),
content: t('save-check-dialog.desc'),
action: h(Fragment, [
h('button', {
onClick: async () => {
await saveContent().catch(error => {
useToast().show('warning', error.message)
throw error
})
useModal().ok()
}
}, t('save')),
h('button', {
onClick: () => useModal().ok()
}, t('discard')),
h('button', {
onClick: () => useModal().cancel()
}, t('cancel')),
])
})
checkFile()
if (confirm) {
if (!store.getters.isSaved && currentFile.content) {
store.state.currentContent = currentFile.content!
}
} else {
throw new Error('Document not saved')
}
} catch (error: any) {
useToast().show('warning', error.message)
throw error