feat(io): save and open .margin projects with cmd-s and cmd-o

This commit is contained in:
pj committed 2026-06-20 12:54:01 -04:00
1 parent 13f2a70af2
commit aedf8b8a63
4 files changed
+78 -1

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
import { save, open } from "@tauri-apps/plugin-dialog";
import { invoke } from "@tauri-apps/api/core";
import type { Book } from "./model/book";
const FILTERS = [{ name: "margin book", extensions: ["margin"] }];
export async function chooseSavePath(book: Book): Promise<string | null> {
return save({ filters: FILTERS, defaultPath: `${book.metadata.title || "Untitled"}.margin` });
}
export async function writeBook(book: Book, path: string): Promise<void> {
await invoke("write_file", { path, contents: JSON.stringify(book, null, 2) });
}
export async function openBook(): Promise<{ book: Book; path: string } | null> {
const selected = await open({ filters: FILTERS, multiple: false, directory: false });
if (typeof selected !== "string") return null;
const contents = await invoke<string>("read_file", { path: selected });
return { book: JSON.parse(contents) as Book, path: selected };
}