-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.js
116 lines (93 loc) · 2.37 KB
/
action.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
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
115
116
const Vue = require('vue/dist/vue.common.js');
const { remote, ipcRenderer } = require('electron');
const desc = {
data: {
status: 0,
disabled: false,
},
mounted() {
this.init();
},
methods: {
init() {
this.disabled = ipcRenderer.sendSync('isServerRunning');
},
idrag() {
if(this.disabled) return;
if(this.status === 0) this.status = 1;
},
iundrag() {
if(this.status === 1) this.status = 0;
},
ofilename() {
const now = new Date();
return `clexport.${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.tar`;
},
odrag() {
if(this.disabled) return;
this.status = -1;
},
odragstart(e) {
if(this.disabled) return;
e.dataTransfer.setData('DownloadURL',
`application/x-tar:${this.ofilename()}:clexport://down`); // eslint-disable-line max-len
},
oundrag() {
if(this.status === -1) this.status = 0;
},
odirect() {
const opath = remote.dialog.showSaveDialog({
defaultPath: this.ofilename(),
});
if(!opath) return; // Cancelled
ipcRenderer.send('directExport', opath);
ipcRenderer.once('exportCb', (ev, err) => {
if(err) {
alert('导出失败');
console.error(err);
} else {
alert('导出成功!');
this.exit();
}
});
},
drop(e) {
if(this.disabled) return;
if(this.status !== 1) return;
this.status = 0;
const dt = e.dataTransfer;
if(dt.files.length !== 1) {
alert('请只添加一个文件');
return;
}
console.log(dt);
const { type } = dt.files[0];
if(type !== 'application/x-tar') {
alert('请添加一个 tar 文件');
return;
}
ipcRenderer.send('doImport', dt.files[0].path);
ipcRenderer.once('importCb', (ev, err) => {
if(err) {
alert('导入失败');
console.error(err);
} else {
alert('导入成功!');
this.exit();
}
});
},
exit() {
remote.getCurrentWindow().close();
},
},
};
// eslint-disable-next-line no-unused-vars
function setup() {
const inst = new Vue(desc);
inst.$mount('#app');
window.addEventListener('keydown', ev => {
if(ev.key === 'Escape') inst.exit();
else if(ev.key === 'e') inst.odirect();
});
}