diff --git a/resources/views/chat/partials/scripts.blade.php b/resources/views/chat/partials/scripts.blade.php index 8f730ed..f1b7342 100644 --- a/resources/views/chat/partials/scripts.blade.php +++ b/resources/views/chat/partials/scripts.blade.php @@ -687,19 +687,39 @@ } }); - // ── 发送消息(Enter 发送) ─────────────────────── - document.getElementById('content').addEventListener('keydown', function(e) { + // ── 发送消息(Enter 发送,防 IME 输入法重复触发)──────── + // 用 isComposing 标记中文输入法的组词状态,组词期间过滤掉 Enter + let _imeComposing = false; + const _contentInput = document.getElementById('content'); + + // 中文/日文等 IME 组词开始 + _contentInput.addEventListener('compositionstart', () => { + _imeComposing = true; + }); + // 组词结束(确认候选词完成),给 10ms 缓冲让 keydown 先被过滤掉 + _contentInput.addEventListener('compositionend', () => { + setTimeout(() => { + _imeComposing = false; + }, 10); + }); + + _contentInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); + // IME 正在组词时(如选候选汉字),不触发发送 + if (_imeComposing) return; sendMessage(e); } }); /** - * 发送聊天消息 + * 发送聊天消息(内带防重入锁,避免快速连按 Enter 重复提交) */ + let _isSending = false; // 发送中防重入标记 async function sendMessage(e) { if (e) e.preventDefault(); + if (_isSending) return; // 上一次还没结束,忽略 + _isSending = true; // 前端禁言检查 if (isMutedUntil > Date.now()) { @@ -763,6 +783,7 @@ console.error(error); } finally { submitBtn.disabled = false; + _isSending = false; // 释放发送锁,允许下次发送 } }