🎨 Optimize storage logic
This commit is contained in:
@@ -11,6 +11,9 @@ import {
|
||||
TabType,
|
||||
UpdatesConfig,
|
||||
UpdateSourceType,
|
||||
GitSyncConfig,
|
||||
AuthMethod,
|
||||
SyncStrategy
|
||||
} from '@/../bindings/voidraft/internal/models/models';
|
||||
import {useI18n} from 'vue-i18n';
|
||||
import {ConfigUtils} from '@/utils/configUtils';
|
||||
@@ -48,6 +51,10 @@ type UpdatesConfigKeyMap = {
|
||||
readonly [K in keyof UpdatesConfig]: string;
|
||||
};
|
||||
|
||||
type SyncConfigKeyMap = {
|
||||
readonly [K in keyof GitSyncConfig]: string;
|
||||
};
|
||||
|
||||
type NumberConfigKey = 'fontSize' | 'tabSize' | 'lineHeight';
|
||||
|
||||
// 配置键映射
|
||||
@@ -88,6 +95,24 @@ const UPDATES_CONFIG_KEY_MAP: UpdatesConfigKeyMap = {
|
||||
gitea: 'updates.gitea'
|
||||
} as const;
|
||||
|
||||
const SYNC_CONFIG_KEY_MAP: SyncConfigKeyMap = {
|
||||
enabled: 'sync.enabled',
|
||||
repo_url: 'sync.repo_url',
|
||||
branch: 'sync.branch',
|
||||
auth_method: 'sync.auth_method',
|
||||
username: 'sync.username',
|
||||
password: 'sync.password',
|
||||
token: 'sync.token',
|
||||
ssh_key_path: 'sync.ssh_key_path',
|
||||
ssh_key_passphrase: 'sync.ssh_key_passphrase',
|
||||
sync_interval: 'sync.sync_interval',
|
||||
last_sync_time: 'sync.last_sync_time',
|
||||
auto_sync: 'sync.auto_sync',
|
||||
local_repo_path: 'sync.local_repo_path',
|
||||
sync_strategy: 'sync.sync_strategy',
|
||||
files_to_sync: 'sync.files_to_sync'
|
||||
} as const;
|
||||
|
||||
// 配置限制
|
||||
const CONFIG_LIMITS = {
|
||||
fontSize: {min: 12, max: 28, default: 13},
|
||||
@@ -261,6 +286,23 @@ const DEFAULT_CONFIG: AppConfig = {
|
||||
repo: "voidraft",
|
||||
}
|
||||
},
|
||||
sync: {
|
||||
enabled: false,
|
||||
repo_url: "",
|
||||
branch: "main",
|
||||
auth_method: AuthMethod.Token,
|
||||
username: "",
|
||||
password: "",
|
||||
token: "",
|
||||
ssh_key_path: "",
|
||||
ssh_key_passphrase: "",
|
||||
sync_interval: 60,
|
||||
last_sync_time: null,
|
||||
auto_sync: true,
|
||||
local_repo_path: "",
|
||||
sync_strategy: SyncStrategy.LocalFirst,
|
||||
files_to_sync: ["voidraft.db"]
|
||||
},
|
||||
metadata: {
|
||||
version: '1.0.0',
|
||||
lastUpdated: new Date().toString(),
|
||||
@@ -348,6 +390,21 @@ export const useConfigStore = defineStore('config', () => {
|
||||
state.config.updates[key] = value;
|
||||
};
|
||||
|
||||
const updateSyncConfig = async <K extends keyof GitSyncConfig>(key: K, value: GitSyncConfig[K]): Promise<void> => {
|
||||
// 确保配置已加载
|
||||
if (!state.configLoaded && !state.isLoading) {
|
||||
await initConfig();
|
||||
}
|
||||
|
||||
const backendKey = SYNC_CONFIG_KEY_MAP[key];
|
||||
if (!backendKey) {
|
||||
throw new Error(`No backend key mapping found for sync.${key.toString()}`);
|
||||
}
|
||||
|
||||
await ConfigService.Set(backendKey, value);
|
||||
state.config.sync[key] = value;
|
||||
};
|
||||
|
||||
// 加载配置
|
||||
const initConfig = async (): Promise<void> => {
|
||||
if (state.isLoading) return;
|
||||
@@ -362,6 +419,7 @@ export const useConfigStore = defineStore('config', () => {
|
||||
if (appConfig.editing) Object.assign(state.config.editing, appConfig.editing);
|
||||
if (appConfig.appearance) Object.assign(state.config.appearance, appConfig.appearance);
|
||||
if (appConfig.updates) Object.assign(state.config.updates, appConfig.updates);
|
||||
if (appConfig.sync) Object.assign(state.config.sync, appConfig.sync);
|
||||
if (appConfig.metadata) Object.assign(state.config.metadata, appConfig.metadata);
|
||||
}
|
||||
|
||||
@@ -594,6 +652,20 @@ export const useConfigStore = defineStore('config', () => {
|
||||
},
|
||||
|
||||
// 更新配置相关方法
|
||||
setAutoUpdate: async (value: boolean) => await updateUpdatesConfig('autoUpdate', value)
|
||||
setAutoUpdate: async (value: boolean) => await updateUpdatesConfig('autoUpdate', value),
|
||||
|
||||
// Git同步配置相关方法
|
||||
setGitSyncEnabled: (value: boolean) => updateSyncConfig('enabled', value),
|
||||
setGitRepoUrl: (value: string) => updateSyncConfig('repo_url', value),
|
||||
setGitBranch: (value: string) => updateSyncConfig('branch', value),
|
||||
setAuthMethod: (value: AuthMethod) => updateSyncConfig('auth_method', value),
|
||||
setGitUsername: (value: string) => updateSyncConfig('username', value),
|
||||
setGitPassword: (value: string) => updateSyncConfig('password', value),
|
||||
setGitToken: (value: string) => updateSyncConfig('token', value),
|
||||
setSSHKeyPath: (value: string) => updateSyncConfig('ssh_key_path', value),
|
||||
setSyncInterval: (value: number) => updateSyncConfig('sync_interval', value),
|
||||
setAutoSync: (value: boolean) => updateSyncConfig('auto_sync', value),
|
||||
setSyncStrategy: (value: SyncStrategy) => updateSyncConfig('sync_strategy', value),
|
||||
setFilesToSync: (value: string[]) => updateSyncConfig('files_to_sync', value),
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user