mirror of
https://github.com/coalaura/whiskr.git
synced 2025-09-09 01:09:54 +00:00
file attachments
This commit is contained in:
@@ -55,10 +55,7 @@ function make(tag, ...classes) {
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
return text
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">");
|
||||
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||
}
|
||||
|
||||
function formatMilliseconds(ms) {
|
||||
@@ -101,8 +98,26 @@ function download(name, type, data) {
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function selectFile(accept) {
|
||||
return new Promise((resolve) => {
|
||||
function lines(text) {
|
||||
let count = 0,
|
||||
index = 0;
|
||||
|
||||
while (index < text.length) {
|
||||
index = text.indexOf("\n", index);
|
||||
|
||||
if (index === -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
count++;
|
||||
index++;
|
||||
}
|
||||
|
||||
return count + 1;
|
||||
}
|
||||
|
||||
function selectFile(accept, asJson = false) {
|
||||
return new Promise(resolve => {
|
||||
const input = make("input");
|
||||
|
||||
input.type = "file";
|
||||
@@ -120,13 +135,22 @@ function selectFile(accept) {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = () => {
|
||||
try {
|
||||
const data = JSON.parse(reader.result);
|
||||
let content = reader.result;
|
||||
|
||||
resolve(data);
|
||||
} catch {
|
||||
resolve(false);
|
||||
if (asJson) {
|
||||
try {
|
||||
content = JSON.parse(content);
|
||||
} catch {
|
||||
resolve(false);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
resolve({
|
||||
name: file.name,
|
||||
content: content,
|
||||
});
|
||||
};
|
||||
|
||||
reader.onerror = () => resolve(false);
|
||||
|
Reference in New Issue
Block a user