Skip to content

Commit 9912c7f

Browse files
committed
🚑 给草稿文件增加自动备份路径
1 parent 580a02f commit 9912c7f

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

app/src/core/service/Settings.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Store } from "@tauri-apps/plugin-store";
22
import { useEffect, useState } from "react";
33
import { createStore } from "../../utils/store";
4+
import { appCacheDir } from "@tauri-apps/api/path";
5+
import { PathString } from "../../utils/pathString";
6+
import { isWeb } from "../../utils/platform";
47

58
/**
69
* 设置相关的操作
@@ -238,6 +241,17 @@ export namespace Settings {
238241
githubUser: "",
239242
};
240243

244+
/**
245+
* 获取默认的草稿备份路径
246+
* @returns
247+
*/
248+
async function getDefaultDraftBackupPath() {
249+
if (isWeb) {
250+
return "web-drafts-backup"; // 随便设置一个非空字符串
251+
}
252+
return (await appCacheDir()) + PathString.getSep() + "drafts-backup";
253+
}
254+
241255
export async function init() {
242256
store = await createStore("settings.json");
243257
// 调用所有watcher
@@ -253,6 +267,14 @@ export namespace Settings {
253267
set("theme", "dark");
254268
remove("uiTheme");
255269
}
270+
271+
// 1.7.4 之前的草稿默认备份路径都是空,现在自动改成appCacheDir默认路径
272+
if (!isWeb) {
273+
if ((await get("autoBackupDraftPath")) === "") {
274+
set("autoBackupDraftPath", await getDefaultDraftBackupPath());
275+
}
276+
defaultSettings.autoBackupDraftPath = await getDefaultDraftBackupPath();
277+
}
256278
}
257279

258280
export function has(key: string): Promise<boolean> {

app/src/core/service/dataFileService/StageSaveManager.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ export namespace StageSaveManager {
4848
* @param errorCallback
4949
* @returns
5050
*/
51-
export async function backupHandle(path: string, data: Serialized.File) {
51+
export async function backupHandle(path: string, data: Serialized.File, addFlashEffect = true) {
5252
const backupFolderPath = PathString.dirPath(path);
5353
const isExists = await exists(backupFolderPath);
5454
if (!isExists) {
55-
throw new Error("备份文件路径错误:" + backupFolderPath);
55+
// throw new Error("备份文件路径不存在:" + backupFolderPath);
56+
// 创建备份文件夹
57+
await createFolder(backupFolderPath);
5658
}
5759

5860
await writeTextFile(path, JSON.stringify(data));
59-
Stage.effectMachine.addEffect(ViewFlashEffect.SaveFile());
61+
if (addFlashEffect) {
62+
Stage.effectMachine.addEffect(ViewFlashEffect.SaveFile());
63+
}
6064
}
6165
/**
6266
* 备份,会在工程文件夹的内部新建一个backup文件夹,并在里面生成一个类似的json文件

app/src/core/service/dataFileService/autoSaveBackupEngine/autoBackupEngine.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class AutoBackupEngine {
4444
if (now - this.lastAutoBackupTime > this.autoBackupInterval) {
4545
if (Stage.path.isDraft()) {
4646
const backupDraftPath = `${this.autoBackupDraftPath}${PathString.getSep()}${PathString.getTime()}.json`;
47-
StageSaveManager.backupHandle(backupDraftPath, StageDumper.dump());
47+
StageSaveManager.backupHandle(backupDraftPath, StageDumper.dump(), false);
4848
} else {
4949
StageSaveManager.backupHandleWithoutCurrentPath(StageDumper.dump(), false);
5050
this.limitBackupFilesAndDeleteOld(this.autoBackupLimitCount);

app/src/locales/zh_CN.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ settings:
297297
请填写绝对路径,草稿将统一备份到此文件夹下
298298
留空或路径错误时不进行备份
299299
windows系统注意使用反斜杠
300-
例如:C:\\Users\\username\\Documents\\DraftBackup
300+
例如:C:\Users\username\Documents\DraftBackup
301301
结尾不要带路径分隔符
302302
autoBackupLimitCount:
303303
title: 自动备份最大数量
@@ -581,11 +581,14 @@ appMenu:
581581
title: 位置
582582
items:
583583
openDataFolder:
584-
title: 软件数据
585-
description: 打开软件数据文件夹,内部存放用户自定义设置等数据
584+
title: 配置数据
585+
description: 打开软件用户配置数据文件夹,内部存放用户自定义设置等数据
586586
openProjectFolder:
587587
title: 当前位置
588588
description: 打开当前编辑的 工程文件 所在的文件夹
589+
openCacheFolder:
590+
title: 缓存文件夹
591+
description: 打开软件缓存文件夹,内部存放软件运行时产生的缓存数据,以及默认的草稿备份文件
589592
export:
590593
title: 导出
591594
items:

app/src/pages/_app_menu.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
FilePlus2,
1515
FileType,
1616
Folder,
17+
FolderClock,
1718
FolderCog,
1819
FolderOpen,
1920
Fullscreen,
@@ -57,7 +58,7 @@ import {
5758
import { cn } from "../utils/cn";
5859
import { getCurrentWindow, isDesktop, isWeb } from "../utils/platform";
5960
// import { writeTextFile } from "@tauri-apps/plugin-fs";
60-
import { dataDir } from "@tauri-apps/api/path";
61+
import { appCacheDir, dataDir } from "@tauri-apps/api/path";
6162
import { useState } from "react";
6263
import { useTranslation } from "react-i18next";
6364
import { Dialog } from "../components/dialog";
@@ -494,6 +495,16 @@ export default function AppMenu({ className = "", open = false }: { className?:
494495
>
495496
{t("location.items.openDataFolder.title")}
496497
</Col>
498+
<Col
499+
icon={<FolderClock />}
500+
onClick={async () => {
501+
const path = await appCacheDir();
502+
openFilePath(path);
503+
}}
504+
details={t("location.items.openCacheFolder.description")}
505+
>
506+
{t("location.items.openCacheFolder.title")}
507+
</Col>
497508
<Col
498509
icon={<FolderOpen />}
499510
onClick={() => {

0 commit comments

Comments
 (0)