fix gallery modal next/prev button navigation

This commit is contained in:
Ryan Meador
2026-01-08 17:31:21 -05:00
parent ae357a5e49
commit 383898eae7
3 changed files with 82 additions and 5 deletions
+17 -1
View File
@@ -32,6 +32,7 @@ function closeModal(evt, force = false) {
}
function modalImageSwitch(offset) {
const negmod = (n, m) => ((n % m) + m) % m;
const galleryButtons = all_gallery_buttons();
if (galleryButtons.length > 1) {
const currentButton = selected_gallery_button();
@@ -39,7 +40,6 @@ function modalImageSwitch(offset) {
galleryButtons.forEach((v, i) => {
if (v === currentButton) result = i;
});
const negmod = (n, m) => ((n % m) + m) % m;
if (result !== -1) {
const nextButton = galleryButtons[negmod((result + offset), galleryButtons.length)];
nextButton.click();
@@ -47,8 +47,24 @@ function modalImageSwitch(offset) {
const modal = gradioApp().getElementById('lightboxModal');
modalImage.src = nextButton.children[0].src;
if (modalImage.style.display === 'none') modal.style.setProperty('background-image', `url(${modalImage.src})`);
return;
}
}
const galleryFilesContainer = gradioApp().getElementById('tab-gallery-files');
if (!galleryFilesContainer || !galleryFilesContainer.offsetParent) return;
const gallerySelection = window.getGallerySelection?.();
if (!gallerySelection?.files?.length || gallerySelection.files.length <= 1) return;
const baseIndex = gallerySelection.index >= 0 ? gallerySelection.index : 0;
const nextIndex = negmod((baseIndex + offset), gallerySelection.files.length);
window.setGallerySelection?.(nextIndex, { send: true, emit: true });
const modalImage = gradioApp().getElementById('modalImage');
const modal = gradioApp().getElementById('lightboxModal');
const directSrc = window.getGallerySelectedUrl?.() || new URL(`/file=${encodeURI(window.currentImage)}`, window.location.origin).toString();
if (modalImage && modal) {
modalImage.src = directSrc;
if (modalImage.style.display === 'none') modal.style.setProperty('background-image', `url(${directSrc})`);
}
}
function modalSaveImage(event) {