Skip to content

Commit

Permalink
Fix app data saving on Tauri client
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Oct 30, 2024
1 parent 6ffdbf4 commit 6588c94
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
-->

<script setup lang="ts">
import { onMounted, ref, watch, watchEffect } from 'vue';
import { h, onMounted, ref, watch, watchEffect } from 'vue';
import { useColorMode, usePreferredLanguages, useCssVar, useMediaQuery } from '@vueuse/core';
import { ElConfigProvider, ElIcon } from 'element-plus';
import { ElConfigProvider, ElIcon, ElMessage } from 'element-plus';
import { Operation } from '@element-plus/icons-vue'
import { useFluent } from 'fluent-vue';
import { isTauri } from './libs/Consts';
if (isTauri) {
var pkgTauriFs = import('@tauri-apps/plugin-fs')
var pkgTauri = import("@tauri-apps/api/core")
var pkgTauriPath = import("@tauri-apps/api/path")
}

import Menu from '@/components/Menu.vue';
Expand Down Expand Up @@ -82,14 +83,23 @@ loadStorages()

async function writeJson(name: string, jsonStr: string) {
if (isTauri) {
const { BaseDirectory, writeTextFile } = await pkgTauriFs
const { BaseDirectory, writeTextFile, exists, mkdir } = await pkgTauriFs;
const { appDataDir } = await pkgTauriPath;
try {
await writeTextFile(name, jsonStr, { baseDir: BaseDirectory.AppData })
const appDataPath = await appDataDir();
if (!await exists(appDataPath)) {
await mkdir(appDataPath, { recursive: true });
}
await writeTextFile(name, jsonStr, { baseDir: BaseDirectory.AppData });
} catch (err) {
console.error(err)
console.error(err);
ElMessage({
type: 'error',
message: $t('error-save-file', { file: name, err: String(err) })
});
}
} else {
window.localStorage.setItem(name, jsonStr)
window.localStorage.setItem(name, jsonStr);
}
}
settingStore.$subscribe(() => writeJson('settings.json', settingStore.toJson))
Expand Down Expand Up @@ -317,3 +327,7 @@ watchEffect(async () => {
}
}
</style>

<fluent locale="zh-CN">
error-save-file = 保存文件 { $file } 失败:{ $err }
</fluent>

0 comments on commit 6588c94

Please sign in to comment.