From c858f6af0cb610091fb676969282eee1ad40ee56 Mon Sep 17 00:00:00 2001 From: lkddi Date: Sat, 25 Apr 2026 03:38:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E8=81=8A=E5=A4=A9=E5=AE=A4?= =?UTF-8?q?=E9=9D=99=E9=9F=B3=E5=81=8F=E5=A5=BD=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/js/chat-room.js | 15 ++++ resources/js/chat-room/preferences-status.js | 78 +++++++++++++++++++ .../views/chat/partials/scripts.blade.php | 39 ++++++++-- 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/resources/js/chat-room.js b/resources/js/chat-room.js index a681207..5c791ee 100644 --- a/resources/js/chat-room.js +++ b/resources/js/chat-room.js @@ -7,9 +7,14 @@ export { BLOCKABLE_SYSTEM_SENDERS, BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, CHAT_SOUND_MUTED_STORAGE_KEY, + isSoundMuted, + loadBlockedSystemSenders, normalizeChatPreferences, normalizeDailyStatus, parseDailyStatusExpiry, + persistBlockedSystemSenders, + setSoundMuted, + shouldMigrateLocalChatPreferences, } from "./chat-room/preferences-status.js"; export { normalizeRoomStatus, @@ -27,9 +32,14 @@ import { BLOCKABLE_SYSTEM_SENDERS, BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, CHAT_SOUND_MUTED_STORAGE_KEY, + isSoundMuted, + loadBlockedSystemSenders, normalizeChatPreferences, normalizeDailyStatus, parseDailyStatusExpiry, + persistBlockedSystemSenders, + setSoundMuted, + shouldMigrateLocalChatPreferences, } from "./chat-room/preferences-status.js"; import { normalizeRoomStatus, @@ -53,9 +63,14 @@ if (typeof window !== "undefined") { BLOCKABLE_SYSTEM_SENDERS, BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, CHAT_SOUND_MUTED_STORAGE_KEY, + isSoundMuted, + loadBlockedSystemSenders, normalizeChatPreferences, normalizeDailyStatus, parseDailyStatusExpiry, + persistBlockedSystemSenders, + setSoundMuted, + shouldMigrateLocalChatPreferences, normalizeRoomStatus, renderRoomStatusRow, renderRoomsOnlineStatus, diff --git a/resources/js/chat-room/preferences-status.js b/resources/js/chat-room/preferences-status.js index 5a861e6..f451a11 100644 --- a/resources/js/chat-room/preferences-status.js +++ b/resources/js/chat-room/preferences-status.js @@ -73,3 +73,81 @@ export function normalizeDailyStatus(raw, nowTimestamp = Date.now()) { expires_at: parsedExpiry.toISOString(), }; } + +/** + * 从本地缓存读取已屏蔽的系统播报发送者列表。 + * + * @param {string[]} blockableSystemSenders + * @returns {string[]} + */ +export function loadBlockedSystemSenders(blockableSystemSenders = BLOCKABLE_SYSTEM_SENDERS) { + try { + const saved = JSON.parse(localStorage.getItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY) || "[]"); + + if (!Array.isArray(saved)) { + return []; + } + + return saved.filter((sender) => blockableSystemSenders.includes(sender)); + } catch (error) { + return []; + } +} + +/** + * 将屏蔽的系统播报发送者列表写入本地缓存。 + * + * @param {Iterable} senders + * @returns {void} + */ +export function persistBlockedSystemSenders(senders) { + localStorage.setItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, JSON.stringify(Array.from(senders))); +} + +/** + * 判断当前禁音开关是否处于打开状态。 + * + * @returns {boolean} + */ +export function isSoundMuted() { + const muteCheckbox = document.getElementById("sound_muted"); + + if (muteCheckbox) { + return Boolean(muteCheckbox.checked); + } + + return localStorage.getItem(CHAT_SOUND_MUTED_STORAGE_KEY) === "1"; +} + +/** + * 同步禁音复选框和本地缓存。 + * + * @param {boolean} muted 是否禁音 + * @returns {boolean} + */ +export function setSoundMuted(muted) { + const normalizedMuted = Boolean(muted); + localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, normalizedMuted ? "1" : "0"); + + const muteCheckbox = document.getElementById("sound_muted"); + if (muteCheckbox) { + muteCheckbox.checked = normalizedMuted; + } + + return normalizedMuted; +} + +/** + * 当前登录账号没有服务端偏好时,判断是否需要迁移旧本地偏好。 + * + * @param {{blocked_system_senders?:string[],sound_muted?:boolean}} serverPreferences + * @param {string[]} localBlockedSenders + * @param {boolean} localMuted + * @returns {boolean} + */ +export function shouldMigrateLocalChatPreferences(serverPreferences, localBlockedSenders, localMuted) { + const hasServerPreferences = (serverPreferences?.blocked_system_senders || []).length > 0 + || Boolean(serverPreferences?.sound_muted); + + return !hasServerPreferences && (localBlockedSenders.length > 0 || localMuted); +} diff --git a/resources/views/chat/partials/scripts.blade.php b/resources/views/chat/partials/scripts.blade.php index 9d10e96..f42a22a 100644 --- a/resources/views/chat/partials/scripts.blade.php +++ b/resources/views/chat/partials/scripts.blade.php @@ -1047,6 +1047,10 @@ * @returns {string[]} */ function loadBlockedSystemSenders() { + if (window.ChatRoomTools?.loadBlockedSystemSenders) { + return window.ChatRoomTools.loadBlockedSystemSenders(BLOCKABLE_SYSTEM_SENDERS); + } + try { const saved = JSON.parse(localStorage.getItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY) || '[]'); if (!Array.isArray(saved)) { @@ -1064,6 +1068,11 @@ * 将当前屏蔽配置持久化到 localStorage。 */ function persistBlockedSystemSenders() { + if (window.ChatRoomTools?.persistBlockedSystemSenders) { + window.ChatRoomTools.persistBlockedSystemSenders(blockedSystemSenders); + return; + } + localStorage.setItem( BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, JSON.stringify(Array.from(blockedSystemSenders)) @@ -1076,6 +1085,10 @@ * @returns {boolean} */ function isSoundMuted() { + if (window.ChatRoomTools?.isSoundMuted) { + return window.ChatRoomTools.isSoundMuted(); + } + const muteCheckbox = document.getElementById('sound_muted'); if (muteCheckbox) { @@ -1102,6 +1115,11 @@ */ function persistChatPreferencesToLocal() { persistBlockedSystemSenders(); + if (window.ChatRoomTools?.setSoundMuted) { + window.ChatRoomTools.setSoundMuted(isSoundMuted()); + return; + } + localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, isSoundMuted() ? '1' : '0'); } @@ -3264,9 +3282,10 @@ const storedBlockedSystemSenders = loadBlockedSystemSenders(); const mutedFromLocal = localStorage.getItem(CHAT_SOUND_MUTED_STORAGE_KEY) === '1'; - const hasServerPreferences = initialChatPreferences.blocked_system_senders.length > 0 || initialChatPreferences.sound_muted; - const shouldMigrateLocalPreferences = !hasServerPreferences - && (storedBlockedSystemSenders.length > 0 || mutedFromLocal); + const shouldMigrateLocalPreferences = window.ChatRoomTools?.shouldMigrateLocalChatPreferences + ? window.ChatRoomTools.shouldMigrateLocalChatPreferences(initialChatPreferences, storedBlockedSystemSenders, mutedFromLocal) + : !(initialChatPreferences.blocked_system_senders.length > 0 || initialChatPreferences.sound_muted) + && (storedBlockedSystemSenders.length > 0 || mutedFromLocal); if (shouldMigrateLocalPreferences) { blockedSystemSenders = new Set(storedBlockedSystemSenders); @@ -3274,8 +3293,12 @@ // 恢复禁音复选框状态;默认一律为未禁音。 const muted = shouldMigrateLocalPreferences ? mutedFromLocal : initialChatPreferences.sound_muted; - const muteChk = document.getElementById('sound_muted'); - if (muteChk) muteChk.checked = muted; + if (window.ChatRoomTools?.setSoundMuted) { + window.ChatRoomTools.setSoundMuted(muted); + } else { + const muteChk = document.getElementById('sound_muted'); + if (muteChk) muteChk.checked = muted; + } syncBlockedSystemSenderCheckboxes(); if (shouldMigrateLocalPreferences) { @@ -3293,7 +3316,11 @@ * @param {boolean} muted true = 禁音,false = 开启声音 */ function toggleSoundMute(muted) { - localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, muted ? '1' : '0'); + 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(); // 立即停止当前音效 }