Skip to content

Commit

Permalink
Preview first image after updating image
Browse files Browse the repository at this point in the history
  • Loading branch information
YayunHuang committed Aug 19, 2024
1 parent 9ef10cc commit 068ce80
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
2 changes: 2 additions & 0 deletions public/scripts/models/image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ImageUpload {
signedData = null;
readState = ReadState.ReadyForRead;
message = null;
ulid = null;
previewUrl = null;

loadDimensionPromise = null;

Expand Down
130 changes: 130 additions & 0 deletions public/scripts/ulid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions public/scripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,48 @@ async function runWorker(worker) {
.then(function (pictureArray) {
window.location.href = "/download/?deviceId=" + window.workerDeviceId;
})
.catch(function (err) {
// TODO: Handle preview error in widget
console.error("Get error while storing images to localforage:", err);
});
},
false,
);
}

function runPreviewWorker(worker, imageUpload) {
const imageUploadFile = imageUpload.file;
worker.postMessage({
imageUpload: imageUploadFile,
location: window.location.toString(),
deviceId: window.workerDeviceId,
deviceInfo: window.deviceInfo,
});
worker.addEventListener(
"message",
function (e) {
window.localforage
.setItem(`previewImage-${imageUpload.ulid}`, e.data[1])
.then(function () {
const imageContainer = document.querySelector(
".upload__device-image-rect",
);

/* Put first generated mockup to preview area */
if (!imageContainer.style.backgroundImage) {
imageContainer.style.backgroundImage = `url(${e.data[1]})`;
imageContainer.style.backgroundSize = "cover";
imageContainer.style.backgroundPosition = "center";

const imageUploadHints = document.querySelectorAll(
".upload__device-hint",
);
imageUploadHints.forEach((imageUploadHint) => {
imageUploadHint.innerHTML = "";
imageUploadHint.style.background = "transparent";
});
}
})
.catch(function (err) {
console.error("Get error while storing images to localforage:", err);
});
Expand Down Expand Up @@ -88,7 +130,9 @@ class FileListViewModel {
for (const file of files) {
const imageUpload = new ImageUpload(file, MAX_FILE_SIZE_BYTE);
await imageUpload.read();
imageUpload.ulid = ULID.ulid();
this._imageUploads.push(imageUpload);
window.viewModel.generatePreviewMockup(imageUpload);
}
}

Expand All @@ -109,6 +153,7 @@ class RootViewModel {
_socket = null;
_redirectTimer = null;
worker = new Worker("/scripts/web_worker.js");
previewWorker = new Worker("/scripts/preview_worker.js");
selectedColorId = null;

constructor(maxMockupWaitSec, fileListViewModel, selectedColorId) {
Expand All @@ -129,6 +174,10 @@ class RootViewModel {
return this._isGeneratingMockup;
}

async generatePreviewMockup(imageUpload) {
runPreviewWorker(this.previewWorker, imageUpload);
}

async generateMockup() {
if (!this.fileList.isReadyForMockup) {
console.warn("Cannot generate mockup at this moment");
Expand Down
1 change: 1 addition & 0 deletions src/pages/model/[model].astro
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ if (deviceDetail.imagePath != null && deviceDetail.imagePath.length >= 1) {
<script type="text/javascript" src="/scripts/utils/scroll.js"></script>
<script type="text/javascript" src="/scripts/models/image-upload.js"></script>
<script type="text/javascript" src="/scripts/upload.js"></script>
<script type="text/javascript" src="/scripts/ulid.min.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"
></script>
<div class="mockup-content">
Expand Down
8 changes: 4 additions & 4 deletions src/styles/upload.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ main {
width: 100%;
height: 100%;
object-fit: contain;

/* Overlay the preview image with the color device image */
position: relative;
z-index: 1;
}

.upload__device-image-rect-wrapper {
Expand All @@ -160,10 +164,6 @@ main {
justify-content: center;
}

.upload__device-image-rect {
position: relative;
}

.upload__device-image-rect__screen-rect {
position: absolute;
}
Expand Down

0 comments on commit 068ce80

Please sign in to comment.