From c2bc187ce7e36bd83833058020cdfacc64de3436 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:51:59 +0900 Subject: [PATCH] fix modalImageViewer preview/result flicker (#16426) --- javascript/imageviewer.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/javascript/imageviewer.js b/javascript/imageviewer.js index 9b23f4700..979d05de5 100644 --- a/javascript/imageviewer.js +++ b/javascript/imageviewer.js @@ -13,6 +13,7 @@ function showModal(event) { if (modalImage.style.display === 'none') { lb.style.setProperty('background-image', 'url(' + source.src + ')'); } + updateModalImage(); lb.style.display = "flex"; lb.focus(); @@ -31,21 +32,26 @@ function negmod(n, m) { return ((n % m) + m) % m; } +function updateModalImage() { + const modalImage = gradioApp().getElementById("modalImage"); + let currentButton = selected_gallery_button(); + let preview = gradioApp().querySelectorAll('.livePreview > img'); + if (opts.js_live_preview_in_modal_lightbox && preview.length > 0) { + // show preview image if available + modalImage.src = preview[preview.length - 1].src; + } else if (currentButton?.children?.length > 0 && modalImage.src != currentButton.children[0].src) { + modalImage.src = currentButton.children[0].src; + if (modalImage.style.display === 'none') { + const modal = gradioApp().getElementById("lightboxModal"); + modal.style.setProperty('background-image', `url(${modalImage.src})`); + } + } +} + function updateOnBackgroundChange() { const modalImage = gradioApp().getElementById("modalImage"); if (modalImage && modalImage.offsetParent) { - let currentButton = selected_gallery_button(); - let preview = gradioApp().querySelectorAll('.livePreview > img'); - if (opts.js_live_preview_in_modal_lightbox && preview.length > 0) { - // show preview image if available - modalImage.src = preview[preview.length - 1].src; - } else if (currentButton?.children?.length > 0 && modalImage.src != currentButton.children[0].src) { - modalImage.src = currentButton.children[0].src; - if (modalImage.style.display === 'none') { - const modal = gradioApp().getElementById("lightboxModal"); - modal.style.setProperty('background-image', `url(${modalImage.src})`); - } - } + updateModalImage(); } }