-
Notifications
You must be signed in to change notification settings - Fork 33
/
renderer.js
334 lines (289 loc) · 8.37 KB
/
renderer.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const { ipcRenderer, shell } = require('electron');
const settings = require('electron-settings');
const { dialog } = require('electron').remote;
const fs = require('fs');
const path = require('path');
const log = require('electron-log');
const dragzone = document.getElementById('dragzone'),
resultBox = document.getElementById('result'),
btnOpenSettings = document.getElementById('btnOpenSettings'),
btnCloseSettings = document.getElementById('btnCloseSettings'),
menuSettings = document.getElementById('menuSettings'),
switches = document.getElementsByTagName('input'),
openInBrowserLink = document.getElementsByClassName('openInBrowser'),
btnSavepath = document.getElementById('btnSavepath'),
wrapperSavePath = document.getElementById('wrapperSavePath'),
folderswitch = document.getElementById('folderswitch'),
clearlist = document.getElementById('clearlist'),
updatecheck = document.getElementById('updatecheck'),
suffix = document.getElementById('suffix'),
subfolder = document.getElementById('subfolder'),
notification = document.getElementById('notification');
/*
* Settings
*/
let userSetting = settings.getSync();
notification.checked = userSetting.notification;
clearlist.checked = userSetting.clearlist;
updatecheck.checked = userSetting.updatecheck;
suffix.checked = userSetting.suffix;
subfolder.checked = userSetting.subfolder;
if (userSetting.folderswitch === false)
{
folderswitch.checked = false;
wrapperSavePath.classList.remove('d-none');
} else
{
folderswitch.checked = true;
}
/*
* Cut path from beginning, if necessary
* @param {string} path Filepath
* @param {integer} length max length
* @return {string} truncated string
*/
const cutFolderName = (path, length = 20) => {
return path.length >= length ? '... ' + path.substr(path.length - length) : path;
};
/**
* @param {{savepath:string}} userSetting
*/
if (userSetting.savepath)
btnSavepath.innerText = cutFolderName(userSetting.savepath[0], 48);
/*
* Open filepicker
*/
dragzone.onclick = () => {
dialog.showOpenDialog(
{
properties: ['openFile', 'multiSelections']
}).then(result => {
if (result.canceled)
{
return;
}
// Add loader
dragzone.classList.add('is--processing');
if (settings.getSync('clearlist') === true)
{
resultBox.innerHTML = '';
}
for (let f of result.filePaths)
{
let filename = path.parse(f).base;
ipcRenderer.send('shrinkImage', filename, f);
}
}).catch(err => {
log.error(err);
});
};
document.ondragover = () => {
dragzone.classList.add('drag-active');
return false;
};
document.ondragleave = () => {
dragzone.classList.remove('drag-active');
return false;
};
document.ondragend = () => {
dragzone.classList.remove('drag-active');
return false;
};
/*
* Action on drag drop
*/
document.ondrop = e => {
e.preventDefault();
let items = e.dataTransfer.items;
for (let i = 0; i < items.length; i++)
{
// webkitGetAsEntry is where the magic happens
let item = items[i].webkitGetAsEntry();
if (item)
{
traverseFileTree(item);
}
}
if (settings.getSync('clearlist'))
{
resultBox.innerHTML = '';
}
dragzone.classList.add('is--processing');
dragzone.classList.remove('drag-active');
return false;
};
/*
* Choose folder for saving shrunken images
*/
btnSavepath.onclick = () => {
dialog.showOpenDialog(
{
properties: ['openDirectory', 'createDirectory']
}).then(result => {
if (result.filePaths)
{
btnSavepath.innerText = cutFolderName(result.filePaths[0], 48);
settings.set('savepath', result.filePaths);
}
}).catch(err => {
log.error(err);
});
};
/*
* Save settings
*/
Array.from(switches).forEach(switchEl => {
switchEl.onchange = e => {
settings.setSync(e.target['name'], e.target['checked']);
if (e.target['name'] === 'folderswitch')
{
if (!e.target['checked'])
{
wrapperSavePath.classList.remove('d-none');
} else
{
wrapperSavePath.classList.add('d-none');
}
}
};
});
/*
* Settings menu
*/
// Open
btnOpenSettings.onclick = e => {
e.preventDefault();
menuSettings.classList.add('is--open');
};
// Close on pressing close icon
btnCloseSettings.onclick = e => {
e.preventDefault();
menuSettings.classList.remove('is--open');
};
// Close on pressing ESC
document.onkeyup = e => {
if (e.key === 'Escape')
{
menuSettings.classList.remove('is--open');
}
};
/*
* Renderer process
*/
ipcRenderer.on('isShrinked', (event, path, sizeBefore, sizeAfter) => {
const percent = Math.round((100 / sizeBefore) * (sizeBefore - sizeAfter));
// Remove loader
dragzone.classList.remove('is--processing');
// Create container
const resContainer = document.createElement('div');
resContainer.className = 'resLine';
resContainer.innerHTML =
'<span>You saved ' +
percent +
'%. Your shrunken image is here:</span><br>';
// Create link
let resElement = document.createElement('a');
resElement.setAttribute('href', '#');
let resText = document.createTextNode(path);
resElement.appendChild(resText);
// Add click event
resElement.onclick = el => {
el.preventDefault();
shell.showItemInFolder(path);
};
resContainer.appendChild(resElement);
resultBox.prepend(resContainer);
// Notification
if (settings.getSync('notification'))
{
new window.Notification('Image shrunk, pal!', {
body: path,
silent: true
});
}
}).on('openSettings', () => {
menuSettings.classList.add('is--open');
}).on('error', () => {
// Remove loader
dragzone.classList.remove('is--processing');
});
/*
* Parallax background
*/
let bg = document.getElementById('background'),
winX = window.innerWidth / 2,
winY = window.innerHeight / 2;
// Fix window size on resize
window.onresize = () => {
setTimeout(() => {
winX = window.innerWidth / 2;
winY = window.innerHeight / 2;
}, 700);
};
// Let's do some parallax stuff
document.onmousemove = e => {
let transX = e.clientX - winX,
transY = e.clientY - winY,
tiltX = transX / winY,
tiltY = -(transY / winX),
radius = Math.sqrt(Math.pow(tiltX, 2) + Math.pow(tiltY, 2)),
transformX = Math.floor(tiltX * Math.PI),
transformY = Math.floor(tiltY * Math.PI),
degree = radius * 15,
transform;
transform = 'scale(1.15)';
transform += ' rotate3d(' + tiltX + ', ' + tiltY + ', 0, ' + degree + 'deg)';
transform += ' translate3d(' + transformX + 'px, ' + transformY + 'px, 0)';
bg.style.transform = transform;
};
// Reset, if mouse leaves window
document.onmouseleave = () => {
bg.style.transform = '';
};
// (opt) event, text as return value
ipcRenderer.on('updateReady', () => {
// changes the text of the button
const container = document.getElementById('ready');
container.innerHTML = 'new version available!';
});
/*
* Open external links in browser
*/
Array.from(openInBrowserLink).forEach((el) => {
el.addEventListener('click', (event) => {
event.preventDefault();
shell.openExternal(el.getAttribute('href')).catch((error) => {
log.error(error);
});
});
});
/**
* Traverse down the folders and exclude files in `exclude` array
*/
function traverseFileTree(item, path)
{
const exclude = ['.DS_Store'];
path = path || '';
if (item.isFile)
{
// Get file
item.file(function(f) {
if (fs.statSync(f.path).isDirectory() || exclude.includes(f.name))
{
dragzone.classList.remove('drag-active');
return false;
}
ipcRenderer.send('shrinkImage', f.name, f.path, f.lastModified);
});
} else if (item.isDirectory)
{
// Get folder contents
const dirReader = item.createReader();
dirReader.readEntries(function(entries) {
for (let i in entries)
{
traverseFileTree(entries[i], path + item.name + '/');
}
});
}
}