From 033a70c0d069c3ff224f1952322ad323d61f01f3 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 15 Mar 2023 12:08:24 -0700 Subject: [PATCH] viewer: ximg for live photo Signed-off-by: Varun Patil --- src/components/viewer/PsImage.ts | 49 ++++++++++++++++------------ src/components/viewer/PsLivePhoto.ts | 6 ++-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/components/viewer/PsImage.ts b/src/components/viewer/PsImage.ts index a498aff5..6823d50c 100644 --- a/src/components/viewer/PsImage.ts +++ b/src/components/viewer/PsImage.ts @@ -3,6 +3,32 @@ import { isVideoContent } from "./PsVideo"; import { isLiveContent } from "./PsLivePhoto"; import { fetchImage } from "../frame/XImgCache"; +export function getXImgElem( + content: any, + onLoad: () => void +): HTMLImageElement { + const img = document.createElement("img"); + img.classList.add("pswp__img"); + img.style.visibility = "hidden"; + + // Fetch with Axios + fetchImage(content.data.src).then((blob) => { + // Check if destroyed already + if (!content.element) return; + + // Insert image + const blobUrl = URL.createObjectURL(blob); + img.src = blobUrl; + img.onerror = img.onload = () => { + img.style.visibility = "visible"; + onLoad(); + URL.revokeObjectURL(blobUrl); + }; + }); + + return img; +} + export default class ImageContentSetup { constructor(lightbox: PhotoSwipe) { this.initLightboxEvents(lightbox); @@ -16,28 +42,9 @@ export default class ImageContentSetup { onContentLoad(e) { if (isVideoContent(e.content) || isLiveContent(e.content)) return; - // Don't insert default image + // Insert image throgh XImgCache e.preventDefault(); - const content = e.content; - const img = document.createElement("img"); - img.classList.add("pswp__img"); - img.style.visibility = "hidden"; - content.element = img; - - // Fetch with Axios - fetchImage(content.data.src).then((blob) => { - // Check if destroyed already - if (!content.element) return; - - // Insert image - const blobUrl = URL.createObjectURL(blob); - img.src = blobUrl; - img.onerror = img.onload = () => { - img.style.visibility = "visible"; - content.onLoaded(); - URL.revokeObjectURL(blobUrl); - }; - }); + e.content.element = getXImgElem(e.content, () => e.content.onLoaded()); } onContentLoadImage(e) { diff --git a/src/components/viewer/PsLivePhoto.ts b/src/components/viewer/PsLivePhoto.ts index 4ca87858..2d3a107f 100644 --- a/src/components/viewer/PsLivePhoto.ts +++ b/src/components/viewer/PsLivePhoto.ts @@ -1,4 +1,5 @@ import PhotoSwipe from "photoswipe"; +import { getXImgElem } from "./PsImage"; import * as utils from "../../services/Utils"; export function isLiveContent(content): boolean { @@ -46,10 +47,7 @@ class LivePhotoContentSetup { utils.setupLivePhotoHooks(video); - const img = document.createElement("img"); - img.classList.add("pswp__img"); - img.src = content.data.src; - img.onload = () => content.onLoaded(); + const img = getXImgElem(content, () => content.onLoaded()); div.appendChild(img); content.element = div;