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 a8a8f37
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions _javascript/modules/components/img-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ 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 +42,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 a8a8f37

Please sign in to comment.