1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-09 01:09:54 +00:00

import and export

This commit is contained in:
Laura
2025-08-16 14:54:27 +02:00
parent 566996a728
commit 66cf5011a5
6 changed files with 155 additions and 6 deletions

View File

@@ -74,3 +74,66 @@ function formatMilliseconds(ms) {
function fixed(num, decimals = 0) {
return num.toFixed(decimals).replace(/\.?0+$/m, "");
}
function download(name, type, data) {
let blob;
if (data instanceof Blob) {
blob = data;
} else {
blob = new Blob([data], {
type: type,
});
}
const a = document.createElement("a"),
url = URL.createObjectURL(blob);
a.setAttribute("download", name);
a.style.display = "none";
a.href = url;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function selectFile(accept) {
return new Promise((resolve) => {
const input = make("input");
input.type = "file";
input.accept = accept;
input.onchange = () => {
const file = input.files[0];
if (!file) {
resolve(false);
return;
}
const reader = new FileReader();
reader.onload = () => {
try {
const data = JSON.parse(reader.result);
resolve(data);
} catch {
resolve(false);
}
};
reader.onerror = () => resolve(false);
reader.readAsText(file);
};
input.click();
});
}