1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-25 16:56:23 +00:00

add import/export functionality for notes

This commit is contained in:
Wolfie 2024-03-16 22:32:32 -04:00
parent 2e9e14b447
commit 97242d5cd9
No known key found for this signature in database
GPG key ID: DE384EE9BF2D909A
3 changed files with 107 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, M
import { findByProps } from "@webpack";
import { Button, Forms, Text } from "@webpack/common";
import noteHandler from "plugins/holynotes/noteHandler";
import { downloadNotes, uploadNotes } from "plugins/holynotes/utils";
export default ({ onClose, ...modalProps }: ModalProps & { onClose: () => void; }) => {
const { colorStatusGreen } = findByProps("colorStatusGreen");
@ -60,6 +61,21 @@ export default ({ onClose, ...modalProps }: ModalProps & { onClose: () => void;
onClick={() => {
noteHandler.refreshAvatars();
}}>Refresh Avatars</Button>
<Button
look={Button.Looks.FILLED}
color={Button.Colors.GREEN}
style={{ marginRight: "10px" }}
onClick={() => {
uploadNotes();
}}>Import Notes</Button>
<Button
look={Button.Looks.FILLED}
color={Button.Colors.GREEN}
style={{ marginRight: "70px" }}
onClick={() => {
downloadNotes();
}}>Export Notes</Button>
<Button
look={Button.Looks.FILLED}
color={Button.Colors.RED}

View file

@ -181,4 +181,34 @@ export default new (class NoteHandler {
type: Toasts.Type.SUCCESS,
});
};
public exportNotes = async () => {
return this.getAllNotes();
};
public importNotes = async (notes: HolyNotes.Note[]) => {
try {
var parseNotes = JSON.parse(notes as unknown as string);
} catch (e) {
console.log(e);
return Toasts.show({
id: Toasts.genId(),
message: "Invalid JSON.",
type: Toasts.Type.FAILURE,
});
}
for (const notebook in parseNotes) {
noteHandlerCache.set(notebook, parseNotes[notebook]);
saveCacheToDataStore(notebook, parseNotes[notebook] as unknown as HolyNotes.Note[]);
}
Toasts.show({
id: Toasts.genId(),
message: "Successfully imported notes.",
type: Toasts.Type.SUCCESS,
});
};
});

View file

@ -9,6 +9,7 @@ import { DataStore } from "@api/index";
import noteHandler, { noteHandlerCache } from "./noteHandler";
import { HolyNotes } from "./types";
import { Toasts } from "@webpack/common";
export const HolyNoteStore = createStore("HolyNoteData", "HolyNoteStore");
@ -42,3 +43,63 @@ export async function DeleteEntireStore() {
await DataStore.clear(HolyNoteStore);
return noteHandler.newNoteBook("Main", true);
}
export async function downloadNotes() {
const filename = "notes.json";
const exportData = await noteHandler.exportNotes();
const data = JSON.stringify(exportData, null, 2);
if (IS_VESKTOP || IS_WEB) {
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();
setImmediate(() => {
URL.revokeObjectURL(a.href);
document.body.removeChild(a);
});
} else {
DiscordNative.fileManager.saveWithDialog(data, filename);
}
}
export async function uploadNotes() {
if (IS_VESKTOP || IS_WEB) {
const input = document.createElement("input");
input.type = "file";
input.style.display = "none";
input.accept = "application/json";
input.onchange = async () => {
const file = input.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = async () => {
try {
await noteHandler.importNotes(reader.result as unknown as HolyNotes.Note[]);
} catch (err) {
console.error(err);
Toasts.show({
id: Toasts.genId(),
message: "Invalid JSON.",
type: Toasts.Type.FAILURE,
});
}
};
reader.readAsText(file);
};
document.body.appendChild(input);
input.click();
setImmediate(() => document.body.removeChild(input));
} else {
const [file] = await DiscordNative.fileManager.openFiles({ filters: [{ name: "notes", extensions: ["json"] }] });
if (file) {
await noteHandler.importNotes(new TextDecoder().decode(file.data) as unknown as HolyNotes.Note[]);
}
}
}