2024-04-17 14:29:47 -04:00
|
|
|
/*
|
|
|
|
|
* Vencord, a Discord client mod
|
|
|
|
|
* Copyright (c) 2024 Vendicated and contributors
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { createStore } from "@api/DataStore";
|
|
|
|
|
import { DataStore } from "@api/index";
|
|
|
|
|
import { Toasts } from "@webpack/common";
|
|
|
|
|
|
2025-10-14 16:41:32 -04:00
|
|
|
import { noteHandler, noteHandlerCache } from "./NoteHandler";
|
2026-01-30 14:10:22 +01:00
|
|
|
import { Notebook } from "./types";
|
2024-04-17 14:29:47 -04:00
|
|
|
|
|
|
|
|
export const HolyNoteStore = createStore("HolyNoteData", "HolyNoteStore");
|
|
|
|
|
|
2026-01-30 14:10:22 +01:00
|
|
|
export function saveCacheToDataStore(key: string, value: Notebook) {
|
|
|
|
|
DataStore.set(key, value, HolyNoteStore);
|
2024-04-17 14:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:10:22 +01:00
|
|
|
export function deleteCacheFromDataStore(key: string) {
|
|
|
|
|
DataStore.del(key, HolyNoteStore);
|
2024-04-17 14:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DataStoreToCache() {
|
2026-01-30 14:10:22 +01:00
|
|
|
const entries = await DataStore.entries(HolyNoteStore);
|
|
|
|
|
for (const [key, value] of entries) {
|
|
|
|
|
noteHandlerCache.set(String(key), value as Notebook);
|
|
|
|
|
}
|
2024-04-17 14:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DeleteEntireStore() {
|
|
|
|
|
await DataStore.clear(HolyNoteStore);
|
2026-01-30 14:10:22 +01:00
|
|
|
noteHandler.newNoteBook("Main", true);
|
2024-04-17 14:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function downloadNotes() {
|
|
|
|
|
const filename = "notes.json";
|
2026-01-30 14:10:22 +01:00
|
|
|
const data = JSON.stringify(noteHandler.exportNotes(), null, 2);
|
2024-04-17 14:29:47 -04:00
|
|
|
|
2026-01-30 14:10:22 +01:00
|
|
|
if (IS_VESKTOP || IS_EQUIBOP || IS_WEB) {
|
2024-04-17 14:29:47 -04:00
|
|
|
const file = new File([data], filename, { type: "application/json" });
|
|
|
|
|
const a = document.createElement("a");
|
|
|
|
|
a.href = URL.createObjectURL(file);
|
|
|
|
|
a.download = filename;
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
a.click();
|
2026-01-30 14:10:22 +01:00
|
|
|
setTimeout(() => {
|
2024-04-17 14:29:47 -04:00
|
|
|
URL.revokeObjectURL(a.href);
|
|
|
|
|
document.body.removeChild(a);
|
2026-01-30 14:10:22 +01:00
|
|
|
}, 0);
|
2024-04-17 14:29:47 -04:00
|
|
|
} else {
|
|
|
|
|
DiscordNative.fileManager.saveWithDialog(data, filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function uploadNotes() {
|
2026-01-30 14:10:22 +01:00
|
|
|
if (IS_VESKTOP || IS_EQUIBOP || IS_WEB) {
|
2024-04-17 14:29:47 -04:00
|
|
|
const input = document.createElement("input");
|
|
|
|
|
input.type = "file";
|
|
|
|
|
input.accept = "application/json";
|
2026-01-30 14:10:22 +01:00
|
|
|
input.style.display = "none";
|
|
|
|
|
input.onchange = () => {
|
2024-04-17 14:29:47 -04:00
|
|
|
const file = input.files?.[0];
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
2026-01-30 14:10:22 +01:00
|
|
|
reader.onload = () => {
|
2024-04-17 14:29:47 -04:00
|
|
|
try {
|
2026-01-30 14:10:22 +01:00
|
|
|
noteHandler.importNotes(reader.result as string);
|
|
|
|
|
} catch {
|
2024-04-17 14:29:47 -04:00
|
|
|
Toasts.show({
|
|
|
|
|
id: Toasts.genId(),
|
2026-01-30 14:10:22 +01:00
|
|
|
message: "Failed to import notes",
|
2024-04-17 14:29:47 -04:00
|
|
|
type: Toasts.Type.FAILURE,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
|
};
|
|
|
|
|
document.body.appendChild(input);
|
|
|
|
|
input.click();
|
2026-01-30 14:10:22 +01:00
|
|
|
setTimeout(() => document.body.removeChild(input), 0);
|
2024-04-17 14:29:47 -04:00
|
|
|
} else {
|
2026-01-30 14:10:22 +01:00
|
|
|
const [file] = await DiscordNative.fileManager.openFiles({
|
|
|
|
|
filters: [{ name: "notes", extensions: ["json"] }]
|
|
|
|
|
});
|
2024-04-17 14:29:47 -04:00
|
|
|
if (file) {
|
2026-01-30 14:10:22 +01:00
|
|
|
noteHandler.importNotes(new TextDecoder().decode(file.data));
|
2024-04-17 14:29:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|