mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-05 00:34:22 +08:00
feat(config): add JSON import/export support for user preferences and configurations
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { saveAs } from "file-saver";
|
||||
|
||||
import { useConfigStore, type AiConfig, type WebdavSyncConfig } from "@/stores/use-config-store";
|
||||
import { usePromptSourceStore, type PromptSourceSchedule } from "@/stores/use-prompt-source-store";
|
||||
import type { PromptSource } from "@/services/api/prompt-source-presets";
|
||||
|
||||
type AppConfigFile = {
|
||||
app: "infinite-canvas";
|
||||
version: 1;
|
||||
exportedAt: string;
|
||||
config: AiConfig;
|
||||
webdav: WebdavSyncConfig;
|
||||
promptSources: {
|
||||
sources: PromptSource[];
|
||||
schedule: PromptSourceSchedule;
|
||||
};
|
||||
};
|
||||
|
||||
export function exportAppConfig() {
|
||||
const { config, webdav } = useConfigStore.getState();
|
||||
const { sources, schedule } = usePromptSourceStore.getState();
|
||||
const data: AppConfigFile = { app: "infinite-canvas", version: 1, exportedAt: new Date().toISOString(), config, webdav, promptSources: { sources, schedule } };
|
||||
saveAs(new Blob([JSON.stringify(data, null, 2)], { type: "application/json;charset=utf-8" }), "infinite-canvas-config.json");
|
||||
}
|
||||
|
||||
export async function importAppConfig(file: File) {
|
||||
let data: AppConfigFile;
|
||||
try {
|
||||
data = JSON.parse(await file.text()) as AppConfigFile;
|
||||
} catch {
|
||||
throw new Error("配置文件格式不正确");
|
||||
}
|
||||
if (data.app !== "infinite-canvas" || data.version !== 1 || !data.config || !data.webdav || !data.promptSources) throw new Error("配置文件格式不正确");
|
||||
useConfigStore.setState({ config: data.config, webdav: data.webdav });
|
||||
usePromptSourceStore.setState(data.promptSources);
|
||||
}
|
||||
Reference in New Issue
Block a user