refactor: improve the efficiency of GLightbox
switching
Repeatedly using `GLightbox.destroy()` (>= 4 times) will cause `GLightbox` to fail to generate new objects
This commit is contained in:
parent
d74bfaeda2
commit
7f83c3d00d
1 changed files with 16 additions and 6 deletions
|
@ -9,15 +9,18 @@ const lightImages = '.popup:not(.dark)';
|
||||||
const darkImages = '.popup:not(.light)';
|
const darkImages = '.popup:not(.light)';
|
||||||
let selector = lightImages;
|
let selector = lightImages;
|
||||||
|
|
||||||
function updateImages(lightbox) {
|
function updateImages(current, reverse) {
|
||||||
if (selector === lightImages) {
|
if (selector === lightImages) {
|
||||||
selector = darkImages;
|
selector = darkImages;
|
||||||
} else {
|
} else {
|
||||||
selector = lightImages;
|
selector = lightImages;
|
||||||
}
|
}
|
||||||
|
|
||||||
lightbox.destroy();
|
if (reverse === null) {
|
||||||
lightbox = GLightbox({ selector: `${selector}` });
|
reverse = GLightbox({ selector: `${selector}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
[current, reverse] = [reverse, current];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function imgPopup() {
|
export function imgPopup() {
|
||||||
|
@ -25,6 +28,11 @@ export function imgPopup() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hasDualImages = !(
|
||||||
|
document.querySelector('.popup.light') === null &&
|
||||||
|
document.querySelector('.popup.dark') === null
|
||||||
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(html.hasAttribute('data-mode') &&
|
(html.hasAttribute('data-mode') &&
|
||||||
html.getAttribute('data-mode') === 'dark') ||
|
html.getAttribute('data-mode') === 'dark') ||
|
||||||
|
@ -34,16 +42,18 @@ export function imgPopup() {
|
||||||
selector = darkImages;
|
selector = darkImages;
|
||||||
}
|
}
|
||||||
|
|
||||||
let lightbox = GLightbox({ selector: `${selector}` });
|
let current = GLightbox({ selector: `${selector}` });
|
||||||
|
|
||||||
|
if (hasDualImages && document.getElementById('mode-toggle')) {
|
||||||
|
let reverse = null;
|
||||||
|
|
||||||
if (document.getElementById('mode-toggle')) {
|
|
||||||
window.addEventListener('message', (event) => {
|
window.addEventListener('message', (event) => {
|
||||||
if (
|
if (
|
||||||
event.source === window &&
|
event.source === window &&
|
||||||
event.data &&
|
event.data &&
|
||||||
event.data.direction === ModeToggle.ID
|
event.data.direction === ModeToggle.ID
|
||||||
) {
|
) {
|
||||||
updateImages(lightbox);
|
updateImages(current, reverse);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue