-
Notifications
You must be signed in to change notification settings - Fork 1
/
preload.js
84 lines (70 loc) · 2.18 KB
/
preload.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
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
const { ipcRenderer } = require("electron");
window.addEventListener("DOMContentLoaded", () => {
let pending = false;
const dropzone = document.getElementById("dropzone");
const text = document.getElementById("dropzone-text");
dropzone.addEventListener("dragover", (event) => {
event.preventDefault();
if (pending) {
return;
}
dropzone.classList.add("dragging-over");
});
dropzone.addEventListener("dragleave", (event) => {
if (pending) {
return;
}
dropzone.classList.remove("dragging-over");
});
dropzone.addEventListener("drop", (event) => {
if (pending) {
return;
}
event.preventDefault();
dropzone.classList.remove("dragging-over");
dropzone.classList.add("dragging-pending");
text.innerHTML = `<img class="loading" src="./assets/images/loader.svg" alt="" />`;
pending = true;
// 发送文件路径到主进程
if (event.dataTransfer.items) {
// 获取文件路径
const file = event.dataTransfer.items[0].getAsFile();
const filePath = file.path;
// 发送文件路径到主进程
ipcRenderer.on("drop-file-message", (event, data) => {
dropzone.classList.remove("dragging-pending");
pending = false;
console.log(
`Received response from main process: ${data.code} ${data.message}`
);
const reset = () =>
setTimeout(
() =>
(text.innerHTML = `<img src="./assets/images/upload.svg" alt="" />`),
5000
);
if (data && data.code == 400) {
text.innerHTML = "不支持的文件类型";
reset();
return;
}
if (data && data.code == 409) {
text.innerHTML = "文件已存在";
reset();
return;
}
if (data && data.code == 500) {
text.innerHTML = `<img src="./assets/images/error.svg" alt="" />`;
reset();
return;
}
if (data && data.code == 200) {
text.innerHTML = `<img src="./assets/images/upload.svg" alt="" />`;
reset();
return;
}
});
ipcRenderer.send("drop-file", filePath);
}
});
});