Files
chatroom/resources/js/chat-room/dialog.js
T
2026-04-25 10:09:56 +08:00

58 lines
1.6 KiB
JavaScript

// 聊天室全局弹窗事件绑定,替代 Blade 内联 onclick。
let globalDialogEventsBound = false;
/**
* 绑定全局弹窗确认与取消按钮。
*
* @returns {void}
*/
export function bindGlobalDialogControls() {
if (globalDialogEventsBound || typeof document === "undefined") {
return;
}
globalDialogEventsBound = true;
document.addEventListener("click", (event) => {
if (!(event.target instanceof Element)) {
return;
}
const actionButton = event.target.closest("[data-chat-dialog-action]");
if (!actionButton) {
return;
}
event.preventDefault();
const action = actionButton.getAttribute("data-chat-dialog-action");
// 全局弹窗只允许确认/取消两类动作,继续承接旧 Promise 回调流程。
if (action === "confirm") {
window.chatDialog?._confirm?.();
return;
}
if (action === "cancel") {
window.chatDialog?._cancel?.();
}
});
document.addEventListener("keydown", (event) => {
if (!(event.target instanceof HTMLTextAreaElement) || event.target.id !== "global-dialog-input") {
return;
}
// prompt 输入框沿用旧体验:Enter 确认,Shift+Enter 保留换行,Esc 取消。
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
window.chatDialog?._confirm?.();
return;
}
if (event.key === "Escape") {
event.preventDefault();
window.chatDialog?._cancel?.();
}
});
}