Skip to content

Commit 0a6f41f

Browse files
committed
fix: dir preloading and support none-exif file
1 parent bdf0965 commit 0a6f41f

File tree

5 files changed

+100
-20
lines changed

5 files changed

+100
-20
lines changed

main.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { LocalFileData } = require("get-file-object-from-local-path");
1414
const exec = require("child_process").exec;
1515
const { default: axios } = require("axios");
1616

17-
let readinPath = "";
17+
let readinPath = undefined;
1818

1919
function createWindow() {
2020
// 創建瀏覽器窗口
@@ -81,7 +81,10 @@ function createWindow() {
8181

8282
// console.log(appPath);
8383

84-
const d = readinPath ? readinPath : appPath;
84+
console.log(`readinPath = ${readinPath}`);
85+
console.log(`appPath = ${appPath}`);
86+
87+
const d = readinPath ?? appPath;
8588
const fileList = fs.readdirSync(d);
8689
const files = [];
8790

@@ -183,8 +186,10 @@ function createWindow() {
183186
}
184187

185188
app.on("ready", function () {
189+
// console.log(" ==================== ready ====================");
186190
if (process.argv.length > 1) {
187191
const temp = process.argv[1];
192+
// const temp = "C:\\Users\\narut\\Desktop\\test2";
188193

189194
if (fs.existsSync(temp) && fs.lstatSync(temp).isDirectory()) {
190195
readinPath = temp;
@@ -197,6 +202,14 @@ app.on("ready", function () {
197202
// 部分 API 在 ready 事件觸發後才能使用。
198203

199204
app.whenReady().then(() => {
205+
// if (process.argv.length > 1) {
206+
// const temp = process.argv[1];
207+
208+
// if (fs.existsSync(temp) && fs.lstatSync(temp).isDirectory()) {
209+
// readinPath = temp;
210+
// }
211+
// }
212+
200213
createWindow();
201214

202215
app.on("activate", function () {

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "photolisting",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "main.js",
55
"scripts": {
6-
"dev": "cross-env NODE_ENV=dev vite",
6+
"vite-dev": "cross-env NODE_ENV=dev vite",
77
"build": "cross-env NODE_ENV=production vite build",
88
"github:build": "cross-env NODE_ENV=github vite build --mode github",
99
"serve": "vite preview",
1010
"electron:serve": "cross-env NODE_ENV=dev electron .",
1111
"pack": "electron-builder --dir",
1212
"pack-dist": "electron-builder",
1313
"full-build": "npm-run-all -s build pack-dist",
14-
"github-full-build": "npm-run-all -s github:build pack-dist"
14+
"github-full-build": "npm-run-all -s github:build pack-dist",
15+
"dev": "npm-run-all -p vite-dev electron:serve"
1516
},
1617
"dependencies": {
1718
"axios": "^1.4.0",

preload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
1515
}
1616
return [
1717
data.map((f) => {
18-
console.log(f);
18+
// console.log(f);
1919
return constructFileFromLocalFileData(f);
2020
}),
2121
// data,

src/App.vue

+78-12
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default {
206206
this.menuSize.width = contextMenu?.offsetWidth;
207207
this.menuSize.height = contextMenu?.offsetHeight;
208208
// this.processForm = true;
209-
// this.preloadDirectory();
209+
this.preloadDirectory();
210210
211211
// window.addEventListener("keydown", this.handelKeypress);
212212
},
@@ -216,15 +216,21 @@ export default {
216216
console.log(checkPath);
217217
if (checkPath) {
218218
this.fileListScreen = true;
219-
this.processForm = true;
219+
// this.processForm = true;
220220
this.preloadDirectory();
221221
}
222222
},
223223
imgOnLoad(e, index) {
224-
this.files[index].width = e.target.naturalWidth;
225-
this.files[index].height = e.target.naturalHeight;
226-
if (!Number.isInteger(this.files[index].rating)) {
227-
this.files[index].rating = this.files[index].exif?.Rating;
224+
const f = this.files[index];
225+
226+
f.width = e.target.naturalWidth;
227+
f.height = e.target.naturalHeight;
228+
229+
console.log(f.rating);
230+
231+
if (!Number.isInteger(f.rating)) {
232+
console.log(f.exif?.Rating);
233+
f.rating = f.exif?.Rating ?? 5;
228234
}
229235
},
230236
getFilesByRating(rating) {
@@ -290,25 +296,26 @@ export default {
290296
}
291297
},
292298
async preloadDirectory() {
299+
this.processForm = true;
293300
this.files = [];
294301
295302
const [files, dir] = await window.electronAPI.preloadDirectory();
296303
297304
this.savePath = dir;
298305
299-
if (!files.length) {
306+
if (!files?.length) {
300307
this.fileListScreen = false;
301308
this.processForm = false;
309+
return;
302310
} else {
303311
this.fileTotalCount = files.length;
304312
}
305313
306-
const fileLoadPromise = [];
307-
308314
for (let i = 0; i < files.length; i++) {
309315
this.files.push(await this.createFile(files[i], i));
310316
}
311317
318+
this.fileListScreen = true;
312319
this.processForm = false;
313320
},
314321
changeRating(nextRating) {
@@ -319,17 +326,25 @@ export default {
319326
async createFile(file, index) {
320327
// console.log(file);
321328
322-
return {
329+
// console.log(file.name, await exifr.thumbnailUrl(file));
330+
331+
const item = {
323332
index: index,
324333
name: file.name,
325-
url: await exifr.thumbnailUrl(file),
334+
url:
335+
(await exifr.thumbnailUrl(file)) ??
336+
(await this.fileToBase64WithResize(file)),
326337
exif: await exifr.parse(file, true),
327338
width: 1,
328339
height: 1,
329340
checked: false,
330-
rating: undefined,
341+
rating: (await exifr.parse(file, true)?.rating) ?? 5,
331342
raw: file,
332343
};
344+
345+
console.log(item);
346+
347+
return item;
333348
},
334349
listingConfirm() {
335350
const finalList = this.files
@@ -346,6 +361,8 @@ export default {
346361
},
347362
checkUpdate() {
348363
window.electronAPI.checkUpdate((evt, arg) => {
364+
if (!arg) return;
365+
349366
this.updateInfo = arg;
350367
this.updateForm = arg.currentVersion < arg.targetVersion;
351368
// console.log(this.updateForm);
@@ -383,6 +400,55 @@ export default {
383400
handelKeypress(event) {
384401
console.log(event);
385402
},
403+
fileToBase64WithResize(file, maxSize = 640) {
404+
return new Promise((resolve, reject) => {
405+
const reader = new FileReader();
406+
407+
reader.onload = () => {
408+
const img = new Image();
409+
img.onload = () => {
410+
const canvas = document.createElement("canvas");
411+
let width = img.width;
412+
let height = img.height;
413+
414+
if (width > maxSize || height > maxSize) {
415+
const aspectRatio = width / height;
416+
if (height >= width) {
417+
width = maxSize;
418+
height = width / aspectRatio;
419+
}
420+
if (width > height) {
421+
height = maxSize;
422+
width = height * aspectRatio;
423+
}
424+
}
425+
426+
canvas.width = width;
427+
canvas.height = height;
428+
429+
const ctx = canvas.getContext("2d");
430+
ctx.drawImage(img, 0, 0, width, height);
431+
432+
ctx.canvas.toBlob((blob) => {
433+
const url = URL.createObjectURL(blob);
434+
resolve(url);
435+
}, file.type);
436+
};
437+
438+
img.onerror = (error) => {
439+
reject(undefined);
440+
};
441+
442+
img.src = reader.result;
443+
};
444+
445+
reader.onerror = (error) => {
446+
reject(undefined);
447+
};
448+
449+
reader.readAsDataURL(file);
450+
});
451+
},
386452
},
387453
watch: {
388454
menuOpened(value) {

src/components/Lightbox.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default {
139139
if (scaledWidth > screen.width) {
140140
const maxMove = (scaledWidth - screen.width) / 2 + 16;
141141
142-
this.movement.x += movementX;
142+
this.movement.x += movementX / 2;
143143
144144
if (movementX > 0 && this.movement.x > maxMove) {
145145
this.movement.x = maxMove;
@@ -154,7 +154,7 @@ export default {
154154
if (scaledHeight > screen.height) {
155155
const maxMove = (scaledHeight - screen.height) / 2 + 16;
156156
157-
this.movement.y += movementY;
157+
this.movement.y += movementY / 2;
158158
159159
if (movementY > 0 && this.movement.y > maxMove) {
160160
this.movement.y = maxMove;

0 commit comments

Comments
 (0)