diff --git a/resources/js/chat-room.js b/resources/js/chat-room.js index 7526304..a94ffa3 100644 --- a/resources/js/chat-room.js +++ b/resources/js/chat-room.js @@ -24,6 +24,7 @@ export { } from "./chat-room/mobile-drawer.js"; export { bindMarriageStatusControls } from "./chat-room/marriage-status.js"; export { bindToolbarControls, runFeatureShortcut, runToolbarAction } from "./chat-room/toolbar.js"; +export { bindUserTargetActions, openUserCard, switchTarget } from "./chat-room/user-target-actions.js"; export { bindWelcomeMenuControls } from "./chat-room/welcome-menu.js"; export { bindAdminMenuControls } from "./chat-room/admin-menu.js"; export { @@ -102,6 +103,7 @@ import { } from "./chat-room/mobile-drawer.js"; import { bindMarriageStatusControls } from "./chat-room/marriage-status.js"; import { bindToolbarControls, runFeatureShortcut, runToolbarAction } from "./chat-room/toolbar.js"; +import { bindUserTargetActions, openUserCard, switchTarget } from "./chat-room/user-target-actions.js"; import { bindWelcomeMenuControls } from "./chat-room/welcome-menu.js"; import { bindAdminMenuControls } from "./chat-room/admin-menu.js"; import { @@ -189,6 +191,9 @@ if (typeof window !== "undefined") { bindToolbarControls, runFeatureShortcut, runToolbarAction, + bindUserTargetActions, + openUserCard, + switchTarget, bindWelcomeMenuControls, bindAdminMenuControls, bindBaccaratLossCoverAdminControls, @@ -254,10 +259,12 @@ if (typeof window !== "undefined") { window.closeMobileDrawer = closeMobileDrawer; window.loadMobileRoomList = loadMobileRoomList; window.openMobileDrawer = openMobileDrawer; + window.openUserCard = openUserCard; window.renderMobileRoomList = renderMobileRoomList; window.renderMobileUserList = renderMobileUserList; window.scheduleRenderMobileUserList = scheduleRenderMobileUserList; window.switchMobileTab = switchMobileTab; + window.switchTarget = switchTarget; window.runFeatureShortcut = runFeatureShortcut; window.runToolbarAction = runToolbarAction; window.openHolidayRunFromSystemMessage = openHolidayRunFromSystemMessage; @@ -286,6 +293,7 @@ if (typeof window !== "undefined") { bindChatToast(); bindFriendPanelControls(); bindToolbarControls(); + bindUserTargetActions(); bindAdminMenuControls(); bindBaccaratLossCoverAdminControls(); bindBaccaratLossCoverControls(); diff --git a/resources/js/chat-room/user-target-actions.js b/resources/js/chat-room/user-target-actions.js new file mode 100644 index 0000000..2bd0e88 --- /dev/null +++ b/resources/js/chat-room/user-target-actions.js @@ -0,0 +1,78 @@ +// 聊天室用户名快捷操作,提供 switchTarget/openUserCard 全局兼容函数。 + +/** + * 将聊天目标切换到指定用户,离线用户会临时补入下拉框。 + * + * @param {string} username + * @returns {void} + */ +export function switchTarget(username) { + const toUserSelect = document.getElementById("to_user"); + if (!toUserSelect) { + return; + } + + const options = toUserSelect.options; + let found = false; + + for (let index = 0; index < options.length; index += 1) { + if (options[index].value === username) { + toUserSelect.value = username; + found = true; + break; + } + } + + // 已离线用户不在下拉列表时,临时加入一次,方便直接发起悄悄话。 + if (!found && username !== "大家") { + const option = document.createElement("option"); + option.value = username; + option.textContent = username; + toUserSelect.appendChild(option); + toUserSelect.value = username; + } + + document.getElementById("content")?.focus(); +} + +/** + * 打开指定用户的名片弹窗。 + * + * @param {string} username + * @returns {void} + */ +export function openUserCard(username) { + const normalizedUsername = String(username) + .replace(/^[\u3010\[【\s]+|[\u3011\]】\s]+$/g, "") + .trim(); + + if (!normalizedUsername) { + return; + } + + if (!window.Alpine) { + console.warn("[openUserCard] Alpine.js 尚未初始化,请稍后再试"); + return; + } + + const container = document.getElementById("user-modal-container"); + if (!container) { + return; + } + + window.Alpine.$data(container)?.fetchUser?.(normalizedUsername); +} + +/** + * 暴露用户名快捷操作给存量 Blade 调用点。 + * + * @returns {void} + */ +export function bindUserTargetActions() { + if (typeof window === "undefined") { + return; + } + + window.switchTarget = switchTarget; + window.openUserCard = openUserCard; +} diff --git a/resources/views/chat/partials/user-actions.blade.php b/resources/views/chat/partials/user-actions.blade.php index 06aa7c6..fcf3393 100644 --- a/resources/views/chat/partials/user-actions.blade.php +++ b/resources/views/chat/partials/user-actions.blade.php @@ -12,63 +12,7 @@ @version 1.0.0 --}} -{{-- ═══════════ 全局交互函数 ═══════════ --}} - +{{-- 全局 switchTarget/openUserCard 已迁移到 resources/js/chat-room/user-target-actions.js --}} {{-- ═══════════ 用户名片弹窗 (Alpine.js) ═══════════ --}} @php $gifts = \App\Models\Gift::activeList(); @endphp