diff --git a/resources/js/chat-room.js b/resources/js/chat-room.js index 3d528a0..332fcc9 100644 --- a/resources/js/chat-room.js +++ b/resources/js/chat-room.js @@ -227,6 +227,7 @@ export { shouldMigrateLocalChatPreferences, toggleBlockMenu, toggleFeatureMenu, + toggleSoundMute, } from "./chat-room/preferences-status.js"; export { bindChatRightPanelControls } from "./chat-room/right-panel.js"; export { @@ -418,6 +419,7 @@ import { shouldMigrateLocalChatPreferences, toggleBlockMenu, toggleFeatureMenu, + toggleSoundMute, } from "./chat-room/preferences-status.js"; import { bindChatRightPanelControls } from "./chat-room/right-panel.js"; import { @@ -645,6 +647,7 @@ if (typeof window !== "undefined") { shouldMigrateLocalChatPreferences, toggleBlockMenu, toggleFeatureMenu, + toggleSoundMute, bindChatRightPanelControls, bindRoomStatusControls, normalizeRoomStatus, diff --git a/resources/js/chat-room/preferences-status.js b/resources/js/chat-room/preferences-status.js index 1c00456..503426f 100644 --- a/resources/js/chat-room/preferences-status.js +++ b/resources/js/chat-room/preferences-status.js @@ -142,6 +142,28 @@ export function setSoundMuted(muted) { return normalizedMuted; } +/** + * 切换特效音效静音状态,并按需持久化到服务端偏好。 + * + * @param {boolean} muted 是否禁音 + * @param {(muted:boolean)=>void|Promise} onChange 禁音变化回调 + * @returns {boolean} + */ +export function toggleSoundMute(muted, onChange = undefined) { + const normalizedMuted = setSoundMuted(muted); + + if (normalizedMuted && typeof window.EffectSounds !== "undefined") { + // 开启禁音时立即停止当前音效,避免状态切换后仍继续播放。 + window.EffectSounds.stop(); + } + + if (typeof onChange === "function") { + void onChange(normalizedMuted); + } + + return normalizedMuted; +} + /** * 绑定禁音复选框事件,后端保存逻辑由调用方提供。 * @@ -159,15 +181,7 @@ export function bindSoundMuteControl(onChange) { return; } - const muted = setSoundMuted(event.target.checked); - - if (muted && typeof window.EffectSounds !== "undefined") { - window.EffectSounds.stop(); - } - - if (typeof onChange === "function") { - void onChange(muted); - } + toggleSoundMute(event.target.checked, onChange); }); } diff --git a/resources/views/chat/partials/scripts.blade.php b/resources/views/chat/partials/scripts.blade.php index b2be1eb..1d0ad43 100644 --- a/resources/views/chat/partials/scripts.blade.php +++ b/resources/views/chat/partials/scripts.blade.php @@ -3169,15 +3169,15 @@ * @param {boolean} muted true = 禁音,false = 开启声音 */ function toggleSoundMute(muted) { - if (window.ChatRoomTools?.setSoundMuted) { - window.ChatRoomTools.setSoundMuted(muted); - } else { - localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, muted ? '1' : '0'); - } - if (muted && typeof EffectSounds !== 'undefined') { - EffectSounds.stop(); // 立即停止当前音效 + if (window.ChatRoomTools?.toggleSoundMute) { + window.ChatRoomTools.toggleSoundMute(muted, () => saveChatPreferences()); + return; } + localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, muted ? '1' : '0'); + if (muted && typeof EffectSounds !== 'undefined') { + EffectSounds.stop(); + } void saveChatPreferences(); } window.toggleSoundMute = toggleSoundMute;