forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addDropUpload.js
53 lines (41 loc) 路 1.23 KB
/
addDropUpload.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
import { dispatch } from "../dispatch.js";
import { global_state } from "../global_state.js";
function upload(state, files, extensions = []) {
let file = files[0];
let fileName = file.name.split(".");
let name = fileName[0];
const extension = fileName[fileName.length - 1];
if (extensions.length > 0 && extensions.includes(enxtension))
throw "Extension not recongized: " + fileName;
readFile(state, file);
// if (["json"].includes(extension)) readFile(file);
// else console.log("Unknown extension:", extension);
}
function readFile(state, file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onloadend = (event) => {
let text = reader.result;
try {
dispatch("LOAD_NEW_GAME", { code: text });
} catch (err) {}
};
}
export function addDropUpload(state, bodyListener) {
bodyListener("drop", "", function (evt) {
let dt = evt.dataTransfer;
let files = dt.files;
upload(state, files);
pauseEvent(evt);
});
bodyListener("dragover", "", function (evt) {
pauseEvent(evt);
});
}
function pauseEvent(e) {
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.returnValue = false;
return false;
}