Skip to content

Commit

Permalink
refactor: improve the efficiency of GLightbox switching
Browse files Browse the repository at this point in the history
Repeatedly using `GLightbox.destroy()` (>= 4 times) will cause `GLightbox` to fail to generate new objects
  • Loading branch information
cotes2020 committed Aug 19, 2024
1 parent d74bfae commit 808ef65
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions _javascript/modules/components/img-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@ const lightImages = '.popup:not(.dark)';
const darkImages = '.popup:not(.light)';
let selector = lightImages;

function updateImages(lightbox) {
function updateImages(current, reverse) {
if (selector === lightImages) {
selector = darkImages;
} else {
selector = lightImages;
}

lightbox.destroy();
lightbox = GLightbox({ selector: `${selector}` });
if (reverse === null) {
reverse = GLightbox({ selector: `${selector}` });
}

[current, reverse] = [reverse, current];
}

export function imgPopup() {
if (document.querySelector('.popup') === null) {
return;
}

const hasDualImages = !(document.querySelector('.popup.light') === null &&
document.querySelector('.popup.dark') === null);

if (
(html.hasAttribute('data-mode') &&
html.getAttribute('data-mode') === 'dark') ||
Expand All @@ -34,16 +40,18 @@ export function imgPopup() {
selector = darkImages;
}

let lightbox = GLightbox({ selector: `${selector}` });
let current = GLightbox({ selector: `${selector}` });
let reverse = null;

if (document.getElementById('mode-toggle')) {
window.addEventListener('message', (event) => {
if (
event.source === window &&
event.data &&
event.data.direction === ModeToggle.ID
event.data.direction === ModeToggle.ID &&
hasDualImages
) {
updateImages(lightbox);
updateImages(current, reverse);
}
});
}
Expand Down

0 comments on commit 808ef65

Please sign in to comment.