-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (49 loc) · 1.42 KB
/
script.js
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
async function getZip (files, elt) {
let process = async () => {
elt.setAttribute("data-progress", Math.round((i / l) * 100));
if (i < l) {
await addFile(zip, files[i][0], files[i][1]);
i++;
await process();
}
};
let zip = new JSZip();
let l = files.length;
let i = 0;
await process();
return zip;
}
async function addFile (zip, filename, filepath) {
let contents = await getFileContents(filepath);
zip.file(filename, contents);
}
async function getFileContents (filepath) {
let response = await fetch(filepath);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
}
document.querySelector(".download-grammars").addEventListener("click", async ({ target }) => {
let btn = target;
if (btn.classList.contains("loading")) {
return;
}
btn.classList.add("loading");
btn.setAttribute("data-progress", 0);
let files = [];
for (let id in components.languages) {
if (id === "meta") {
continue;
}
let basepath =
"https://dev.prismjs.com/" + components.languages.meta.path.replace(/\{id}/g, id);
let basename = basepath.substring(basepath.lastIndexOf("/") + 1);
files.push([basename + ".js", basepath + ".js"]);
files.push([basename + ".min.js", basepath + ".min.js"]);
}
let zip = await getZip(files, btn);
btn.classList.remove("loading");
let blob = await zip.generateAsync({ type: "blob" });
saveAs(blob, "prism-components.zip");
});