generated from electron/electron-quick-start
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
378 lines (317 loc) · 11.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const fs = require('fs');
const path = require('path');
const moveFile = require('move-file');
const isImage = require('is-image');
const open = require('open');
const os = require('check-os');
const {eventCallback, Element, node} = require('cutleryjs/dist/js/legacy.min.js');
let modIndex = 0;
const photos = [];
let folderPath = '';
window.addEventListener('DOMContentLoaded', () => {
const pathForm = node('#selectDir');
const controls = node('#controls');
const counter = node('#suggestionCounter');
pathForm.addEventListener('change', (event) => {
event.preventDefault();
let formData = new FormData(pathForm);
folderPath = formData.get('path').path;
checkUpload(folderPath)
// listPhotos(folderPath);
})
controls.addEventListener('click', (event) => {
const accept = event.target.closest('.moderate__btn--accept');
const reject = event.target.closest('.moderate__btn--reject');
if (accept && modIndex != photos.length) acceptSuggestion();
if (reject && modIndex != photos.length) rejectSuggestion();
if (accept && photos[photos.length-1].status != 'unset') node('#toStep3').classList.add('animate__zoomIn')
else if (reject && photos[photos.length-1].status != 'unset') node('#toStep3').classList.add('animate__zoomIn');
})
document.addEventListener('click', (event) => {
eventCallback('#transferPhotos', transferPhotos, false)
eventCallback('reset', resetApp, true)
eventCallback('openFolder', () => (
openPath(folderPath)
), true)
eventCallback('#path', () => {
event.preventDefault();
}, false)
eventCallback('nextSuggestion', nextSuggestion)
eventCallback('prevSuggestion', () => {
prevSuggestion(modIndex)
})
eventCallback('#updateDownload', updateUpdateMessage, false)
eventCallback('closeUpdateMessage', closeUpdateMessage, true)
eventCallback('closeToast', (target) => {
const toast = target.parentNode.closest('.toast');
fadeOutNode(toast);
})
eventCallback('moderateSkip', () => {
const accepted = getAcceptedPhotos();
if (accepted == 0) createToast({
title: 'Geen markeringen toegevoegd',
content: 'Weet je zeker dat je wil doorgaan?'
})
})
})
document.addEventListener('dragenter', (event) => {
const step1 = event.target.closest('#step1');
const dragConfirmWrapper = event.target.closest('#step1 .drag__confirm .wrapper')
if (step1 || dragConfirmWrapper) dragConfirm(step1);
})
document.addEventListener('dragleave', (event) => {
const step1 = event.target.closest('#step1');
if (step1) dragOut(step1);
})
document.addEventListener('animationend', (event) => {
// eventCallback('.toast.animate__fadeOutDown', (target) => {
// target.remove();
// }, false)
})
const photoList = node('#photoList');
const addPhotoSuggestion = (name, path) => {
const div = document.createElement('div');
div.className = 'suggestion';
div.setAttribute('data-status','unset');
div.innerHTML = `
<img class="suggestion__photo" src="${path}">
<div class="moderate__status">
<i class='bx bx-like' ></i>
<i class='bx bx-dislike' ></i>
</div>
`;
photoList.appendChild(div);
photos.push({name: name, path: path, status: 'unset', el: div});
}
const listPhotos = (uploadedFiles) => {
emptyPhotoList();
const files = uploadedFiles
for (file of files) {
file = file.name
const stat = fs.lstatSync(path.join(folderPath, file))
if (stat.isDirectory() == false && isImage(`${folderPath}/${file}`) == true) addPhotoSuggestion(file, `${folderPath}/${file}`);
}
moderate(0);
node('#step2').scrollIntoView();
dragOut(node('#step1'));
node('#controls').classList.add('animate__zoomIn');
}
const dragConfirm = (section) => {
section.classList.add('drag--over');
}
const dragOut = (section = node('#step1')) => {
section.classList.remove('drag--over');
}
const emptyPhotoList = () => {
node('#photoList').innerHTML = '';
}
const checkUpload = (folderPath) => {
const result = fs.readdirSync(folderPath, { withFileTypes: true });
const files = result.filter(file => {
const isFile = file.isDirectory();
const isHidden = file.name.startsWith('.')
return isFile == false && isHidden == false
})
if (files.length != 0) listPhotos(files);
else {
dragOut();
createToast({
title: 'Geen foto\'s gevonden',
content: 'Probeer een andere folder naar hier te slepen',
timer: 5000
});
}
}
const moderate = (index) => {
try {
const curr = node('#photoList .suggestion', true)[index];
const prev = node('#photoList .suggestion', true)[index-1];
console.log(index);
suggestionPreview(photos[index].path);
curr.scrollIntoView({inline: 'center', block: 'nearest'});
if (prev) unfocusThumbnail(prev);
focusThumbnail(curr);
node('#suggestionCounter').innerHTML = suggestionCounter();
} catch (err) {console.log(err)}
}
const nextSuggestion = () => {
try {
const current = photos[modIndex];
const next = photos[modIndex+1];
if (next) {
unfocusThumbnail(current.el);
focusThumbnail(next.el);
suggestionPreview(next.path);
next.el.scrollIntoView({inline: 'center', block: 'nearest'});
suggestionCounter();
if (modIndex < photos.length) modIndex = modIndex+1;
}
} catch (err) {console.log(err)}
}
const prevSuggestion = () => {
try {
const current = photos[modIndex];
const prev = photos[modIndex-1];
if (prev) {
unfocusThumbnail(current.el);
focusThumbnail(prev.el);
suggestionPreview(prev.path);
prev.el.scrollIntoView({inline: 'center', block: 'nearest'});
suggestionCounter();
if (modIndex >= 0) modIndex = modIndex-1;
}
} catch (err) {console.log(err)}
}
const acceptSuggestion = () => {
photos[modIndex].status = 'accept';
photos[modIndex].el.setAttribute('data-status','accept');
modIndexIncr();
moderate(modIndex);
}
const rejectSuggestion = () => {
photos[modIndex].status = 'reject';
photos[modIndex].el.setAttribute('data-status','reject');
modIndexIncr();
moderate(modIndex);
}
const suggestionCounter = () => {
return `${modIndex+1} van ${photos.length}`;
}
const getAcceptedPhotos = () => {
return photos.filter(photo => {
return photo.status == 'accept'
})
}
const transferPhotos = () => {
const foldernames = {accept: node('[name="foldername_accept"]').value, reject: node('[name="foldername_reject"]').value}
photos.forEach((photo) => {
if (photo.status == 'reject') moveFile(`${folderPath}/${photo.name}`, `${folderPath}/${foldernames.reject}/${photo.name}`)
else if (photo.status == 'accept') moveFile(`${folderPath}/${photo.name}`, `${folderPath}/${foldernames.accept}/${photo.name}`);
})
node('#step4').scrollIntoView({block: 'nearest'});
}
const resetApp = () => {
modIndex = 0;
photos.length = 0;
node('#path').value = '';
node('#step1').scrollIntoView({block: 'nearest'});
location.reload();
// app.relaunch();
}
const openPath = (path) => {
open(path)
}
const suggestionPreview = (path) => {
const preview = node('#suggestionPreview');
const animation = 'animate__zoomIn'
preview.querySelector('img').src = path;
preview.classList.add(animation);
}
const focusThumbnail = (node) => {
node.classList.add('suggestion--selected')
}
const unfocusThumbnail = (node) => {
node.classList.remove('suggestion--selected')
}
const modIndexIncr = () => {
if (modIndex < photos.length-1) modIndex = modIndex+1;
}
const getAppVersions = async () => {
let currentVersion = await fetch('https://raw.githubusercontent.com/lennertderyck/photo-sorter/master/package.json');
currentVersion = await currentVersion.json();
let thisVersion = require('./package.json');
return {
this: thisVersion.version,
current: currentVersion.version
}
}
const checkForRelease = async () => {
const versions = await getAppVersions();
let response = await fetch(`https://github.com/lennertderyck/photo-sorter/releases/tag/v${versions.current}`);
if (response.status == 404) return {
releaseAvailable: false, // with version number of most recent package.json
versions: {
this: versions.this,
current: versions.current
}
};
return {
releaseAvailable: true, // with version number of most recent package.json
versions: {
this: versions.this,
current: versions.current
}
};
}
const updateChecker = async () => {
const releaseData = await checkForRelease();
if (releaseData.versions.this !== releaseData.versions.current && releaseData.releaseAvailable == true) node('#newVersionNotify').classList.add('animate__zoomIn');
else console.log('no new version available')
node('#updateDownload').href = createUpdateLink(releaseData);
console.log(releaseData);
}
const createUpdateLink = (releaseData) => {
let name = '';
if (os.isWindows) name = 'Photo.Sorter-win32-x64.zip';
if (os.isMacOS) name = 'Photo.Sorter-darwin-x64.zip';
return `https://github.com/lennertderyck/photo-sorter/releases/download/v${releaseData.versions.current}/${name}`
}
updateChecker();
const updateUpdateMessage = () => {
node('#newVersionNotify .message').innerHTML = `
<i class='bx bx-cloud-download'></i>
<h3>De update wordt gedownload</h3>
<p><small>Meer info over updates & installatie lees je op GitHub</small></p>
<div class="btn-group">
<a href="https://github.com/lennertderyck/photo-sorter/blob/master/README.md#installeren" data-action="closeUpdateMessage" class="btn btn--simple" download><i class='bx bx-right-arrow-alt'></i> meer lezen</a>
</div>
`;
setTimeout(closeUpdateMessage, 10000);
}
const closeUpdateMessage = () => {
console.log('close');
node('#newVersionNotify').classList.remove('animate__delay-2s');
node('#newVersionNotify').classList.add('animate__zoomOut');
setTimeout(() => {
node('#newVersionNotify').classList.remove('animate__zoomIn');
}, 1000);
}
const createToast = ({title, content, timer}) => {
timer = timer || 6000;
const animateDuraction = 2000;
const toast = new Element('div');
toast.class(['toast', 'animate__animated', 'animate__fadeInUp', 'animate__faster']);
toast.inner(`
<div class="toast__wrapper">
<div class="toast__controls">
<div data-action="closeToast">
<i class='bx bx-x'></i>
</div>
</div>
<div class="toast__content">
<h5 class="toast__title">${title}</h5>
<div class="toast__body">
<p>${content}</p>
</div>
</div>
</div>
<div class="toast__timer"></div>
`);
toast.attributes([
['style', `--timer-duration: ${timer}ms`]
])
toast.append('#toasts .toasts__wrapper');
setTimeout(() => {
fadeOutNode(toast.return());
}, timer)
setTimeout(() => {
toast.return().remove();
}, timer + animateDuraction)
}
const fadeOutNode = (el) => {
el.classList.remove('animate__fadeInUp');
el.classList.add('animate__fadeOutDown');
}
})