迁移聊天图片预览事件到Vite模块

This commit is contained in:
2026-04-25 03:36:30 +08:00
parent f1d8d20180
commit 2f246f9112
4 changed files with 50 additions and 9 deletions
+42 -1
View File
@@ -1,4 +1,6 @@
// 聊天室图片预览层控制,迁出 Blade 内联脚本后通过 window 全局函数兼容现有 onclick
// 聊天室图片预览层控制,迁出 Blade 内联脚本后通过事件委托接管打开和关闭
let lightboxEventsBound = false;
/**
* 打开聊天图片大图预览层。
@@ -52,3 +54,42 @@ export function closeChatImageLightbox(event = null) {
document.body.style.overflow = "";
}
/**
* 绑定聊天图片预览层事件委托。
*
* @returns {void}
*/
export function initChatImageLightboxEvents() {
if (lightboxEventsBound) {
return;
}
lightboxEventsBound = true;
document.addEventListener("click", (event) => {
if (!(event.target instanceof Element)) {
return;
}
const imagePreview = event.target.closest("[data-chat-image-lightbox-image]");
if (imagePreview) {
return;
}
const opener = event.target.closest("[data-chat-image-lightbox-open]");
if (opener) {
event.preventDefault();
openChatImageLightbox(opener.dataset.full || opener.href, opener.dataset.alt || "聊天图片");
return;
}
if (event.target.closest("[data-chat-image-lightbox-close]")) {
event.preventDefault();
closeChatImageLightbox();
}
});
}
if (typeof document !== "undefined") {
initChatImageLightboxEvents();
}