-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
114 lines (110 loc) · 3.08 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!doctype html>
<html>
<head>
<title>Invoice Compiler</title>
<style>
body,
html {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
#root {
width: calc(100vw - 4em);
height: calc(100vh - 4em);
margin: 2em;
border: 4px dashed lightgrey;
}
#root.dragover {
border-color: green;
}
#root.processing {
border-color: yellow;
}
#root p {
padding: 4em;
text-align: center;
font-family: sans-serif;
font-size: 20px;
line-height: 1.5;
}
</style>
</head>
<body>
<div id="root"><p>Please drag an invoice YAML here.</p></div>
<a id="download" style="display: none;"></a>
<input type="file" id="upload" name="file" />
<script>
const root = document.getElementById("root");
const upload = document.getElementById("upload");
function readFile(f) {
return new Promise((resolve, reject) => {
const fr = new FileReader();
fr.onload = () => {
resolve(fr.result);
};
fr.onerror = reject;
fr.readAsText(f);
});
}
async function process(file) {
const fileName = file.name;
root.innerHTML = `<p>Processing ${fileName}</p>`;
const data = await readFile(file);
root.className = "processing";
const res = await fetch("/compile", {
method: "POST",
body: data,
});
if (res.ok) {
root.className = "";
const blob = await res.blob();
const url = window.URL.createObjectURL(blob);
const a = document.getElementById("download");
a.href = url;
a.download = fileName.replace(".yaml", ".pdf");
a.click();
root.innerHTML = `<p>Processing complete.<br /><a href="${url}" target="_blank">Click here to download.</a></p>`;
} else {
root.className = "error";
root.innerHTML = `<p>${await res.text()}</p>`;
}
}
upload.addEventListener("change", (ev) => {
if (ev.target.files.length === 1) {
process(ev.target.files[0]);
}
});
root.addEventListener("click", (ev) => {
if (root.className === "") {
upload.click();
}
});
root.addEventListener("dragover", (ev) => {
ev.preventDefault();
ev.stopPropagation();
console.log(ev.dataTransfer.items);
if (
ev.dataTransfer.items.length === 1 &&
ev.dataTransfer.items[0].kind === "file"
) {
ev.dataTransfer.dropEffect = "copy";
root.className = "dragover";
}
});
root.addEventListener("dragleave", (ev) => {
ev.preventDefault();
ev.stopPropagation();
root.className = "";
});
root.addEventListener("drop", async (ev) => {
ev.preventDefault();
ev.stopPropagation();
const file = ev.dataTransfer.files[0];
await process(file);
});
</script>
</body>
</html>