mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-03 19:47:03 +00:00
feat(io): save and open .margin projects with cmd-s and cmd-o
This commit is contained in:
1 parent
13f2a70af2
commit
aedf8b8a63
4 files changed
+78
-1
No files matched your search
@@ -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 };
|
||||
}
|
||||
Reference in new issue
Block a user