From d723a255a316407ba215d38956334371ea06a66c Mon Sep 17 00:00:00 2001 From: yayunhuang Date: Wed, 28 Aug 2024 17:28:22 +0800 Subject: [PATCH] Save images to localforage & redirect to download page when all mockups generated --- public/scripts/models/image-upload.js | 1 + public/scripts/upload.js | 51 +++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/public/scripts/models/image-upload.js b/public/scripts/models/image-upload.js index 7b7afb9..1c490b3 100644 --- a/public/scripts/models/image-upload.js +++ b/public/scripts/models/image-upload.js @@ -25,6 +25,7 @@ class ImageUpload { message = null; ulid = null; previewUrl = null; + generatedMockups = []; loadDimensionPromise = null; diff --git a/public/scripts/upload.js b/public/scripts/upload.js index f5c1f7a..9d6c7f3 100644 --- a/public/scripts/upload.js +++ b/public/scripts/upload.js @@ -30,7 +30,7 @@ function getMaxWorkers() { // browser does not support navigator.hardwareConcurrency, fallback to default return DEFAULT_MAX_WEB_WORKERS; } - return navigator.hardwareConcurrency; + return 4; } function runWorker(worker, imageUpload, orientationIndex, mode) { @@ -100,7 +100,13 @@ function runWorker(worker, imageUpload, orientationIndex, mode) { ImageUploadState.ReadSuccess, ); } else if (mode === "mockup") { - console.log("generate mockup", imageUpload.file.name, orientationIndex); + const ulid = e.data["ulid"]; + const results = e.data["results"]; + + window.viewModel.fileList.addGeneratedMockupToImageUploadByULID( + ulid, + results, + ); } window.viewModel.idleWorker(worker); @@ -187,6 +193,15 @@ class FileListViewModel { return imageUpload; }); } + + addGeneratedMockupToImageUploadByULID(ulid, generatedMockup) { + this._imageUploads = this._imageUploads.map((imageUpload) => { + if (imageUpload.ulid == ulid) { + imageUpload.generatedMockups.push(generatedMockup); + } + return imageUpload; + }); + } } class RootViewModel { @@ -788,6 +803,38 @@ function main() { }, ); + // observe fileListViewModel: imageUploads[].generatedMockups[].length + mobx.reaction( + () => + viewModel.fileList.imageUploads.map( + (imageUpload) => imageUpload.generatedMockups.length, + ), + async () => { + if (viewModel.isGeneratingMockup) { + const imageUploads = viewModel.fileList.imageUploads; + let generatedMockups = []; + for (let i = 0; i < imageUploads.length; i += 1) { + generatedMockups = [ + ...generatedMockups, + ...imageUploads[i].generatedMockups, + ]; + if ( + imageUploads[i].generatedMockups.length < viewModel.orientationsNum + ) { + return; + } + } + + window.localforage + .setItem("pictureArray", generatedMockups) + .then(() => { + window.location.href = + "/download/?deviceId=" + window.workerDeviceId; + }); + } + }, + ); + // observe viewModel: selectedPreviewImageULID mobx.reaction( () => viewModel.selectedPreviewImageULID,