Skip to content

Commit 8ea8d36

Browse files
이미지 모달 기능 개선. 새로운 Mermaid 다이어그램이 추가될 때 자동으로 리스너를 재설정하도록 MutationObserver를 추가하였으며, 모달 요소 생성 및 리스너 설정을 위한 함수 구조를 개선하였습니다. 또한, 중복 리스너 추가를 방지하기 위해 기존 리스너 상태를 관리하는 기능을 추가하였습니다.
1 parent f865bfc commit 8ea8d36

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

assets/js/main.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,39 @@ if (document.querySelectorAll('pre code').length > 0) {
295295

296296
// Image Modal functionality
297297
function initImageModal() {
298+
// Create modal elements first
299+
createImageModal();
300+
301+
// Initialize with current elements
302+
setupImageModalListeners();
303+
304+
// Also setup a mutation observer to detect when new mermaid elements are added
305+
const observer = new MutationObserver(function(mutations) {
306+
mutations.forEach(function(mutation) {
307+
if (mutation.addedNodes.length > 0) {
308+
mutation.addedNodes.forEach(function(node) {
309+
if (node.nodeType === 1 && (node.classList.contains('mermaid') || node.querySelector('.mermaid'))) {
310+
// New mermaid diagram detected, re-setup listeners
311+
setTimeout(() => {
312+
setupImageModalListeners();
313+
}, 100);
314+
}
315+
});
316+
}
317+
});
318+
});
319+
320+
observer.observe(document.body, {
321+
childList: true,
322+
subtree: true
323+
});
324+
}
325+
326+
function createImageModal() {
298327
// Create modal elements
299328
const modal = document.createElement('div');
300329
modal.className = 'image-modal';
330+
modal.id = 'imageModal';
301331
modal.innerHTML = `
302332
<div class="image-modal-content">
303333
<button class="image-modal-close" aria-label="Close modal">
@@ -319,8 +349,13 @@ function initImageModal() {
319349
`;
320350

321351
document.body.appendChild(modal);
352+
}
353+
354+
function setupImageModalListeners() {
355+
// Get existing modal elements
356+
const modal = document.getElementById('imageModal');
357+
if (!modal) return;
322358

323-
// Get modal elements
324359
const modalContent = modal.querySelector('.image-modal-content');
325360
const modalImg = modal.querySelector('img');
326361
const modalInfo = modal.querySelector('.image-modal-info');
@@ -329,6 +364,10 @@ function initImageModal() {
329364
const modalNext = modal.querySelector('.image-modal-next');
330365
const modalLoading = modal.querySelector('.image-modal-loading');
331366

367+
// Remove existing listeners to prevent duplicates
368+
const oldListeners = modal.getAttribute('data-listeners-added');
369+
if (oldListeners === 'true') return;
370+
332371
// Get all images and mermaid diagrams in post content
333372
const images = document.querySelectorAll('.post-content img, .page-content img');
334373
const mermaidDiagrams = document.querySelectorAll('.post-content pre.mermaid, .page-content pre.mermaid');
@@ -572,4 +611,7 @@ function initImageModal() {
572611
modal.addEventListener('touchmove', function(e) {
573612
e.preventDefault();
574613
}, { passive: false });
614+
615+
// Mark listeners as added
616+
modal.setAttribute('data-listeners-added', 'true');
575617
}

0 commit comments

Comments
 (0)