迁移静音开关事件绑定

This commit is contained in:
2026-04-25 03:39:31 +08:00
parent c858f6af0c
commit e9c3fc989c
4 changed files with 35 additions and 1 deletions
+3
View File
@@ -7,6 +7,7 @@ export {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
bindSoundMuteControl,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
@@ -32,6 +33,7 @@ import {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
bindSoundMuteControl,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
@@ -63,6 +65,7 @@ if (typeof window !== "undefined") {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
bindSoundMuteControl,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
@@ -3,6 +3,7 @@
export const BLOCKABLE_SYSTEM_SENDERS = ["钓鱼播报", "星海小博士", "百家乐", "跑马", "神秘箱子"];
export const BLOCKED_SYSTEM_SENDERS_STORAGE_KEY = "chat_blocked_system_senders";
export const CHAT_SOUND_MUTED_STORAGE_KEY = "chat_sound_muted";
let soundMuteEventsBound = false;
/**
* 规整聊天室偏好对象,过滤非法配置并补齐默认值。
@@ -137,6 +138,35 @@ export function setSoundMuted(muted) {
return normalizedMuted;
}
/**
* 绑定禁音复选框事件,后端保存逻辑由调用方提供。
*
* @param {(muted:boolean)=>void|Promise<void>} onChange 禁音变化回调
* @returns {void}
*/
export function bindSoundMuteControl(onChange) {
if (soundMuteEventsBound || typeof document === "undefined") {
return;
}
soundMuteEventsBound = true;
document.addEventListener("change", (event) => {
if (!(event.target instanceof HTMLInputElement) || event.target.id !== "sound_muted") {
return;
}
const muted = setSoundMuted(event.target.checked);
if (muted && typeof window.EffectSounds !== "undefined") {
window.EffectSounds.stop();
}
if (typeof onChange === "function") {
void onChange(muted);
}
});
}
/**
* 当前登录账号没有服务端偏好时,判断是否需要迁移旧本地偏好。
*
@@ -129,7 +129,7 @@ $welcomeMessages = [
<div style="font-size:10px;color:#475569;padding:0 2px 8px;">可集中管理播报显示与特效声音</div>
<label
style="display:flex;align-items:center;gap:6px;font-size:12px;color:#1e293b;cursor:pointer;padding:4px 2px;">
<input type="checkbox" id="sound_muted" onchange="toggleSoundMute(this.checked)">
<input type="checkbox" id="sound_muted">
🔇 禁音
</label>
<div style="height:1px;background:#e2e8f0;margin:6px 0;"></div>
@@ -3328,6 +3328,7 @@
void saveChatPreferences();
}
window.toggleSoundMute = toggleSoundMute;
window.ChatRoomTools?.bindSoundMuteControl?.(() => saveChatPreferences());
// ── 发送消息(Enter 发送,防 IME 输入法重复触发)────────