Fix: 防止Enter重复发送(IME输入法防穿透+_isSending防重入锁)
This commit is contained in:
@@ -687,19 +687,39 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── 发送消息(Enter 发送) ───────────────────────
|
// ── 发送消息(Enter 发送,防 IME 输入法重复触发)────────
|
||||||
document.getElementById('content').addEventListener('keydown', function(e) {
|
// 用 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) {
|
if (e.key === 'Enter' && !e.shiftKey) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
// IME 正在组词时(如选候选汉字),不触发发送
|
||||||
|
if (_imeComposing) return;
|
||||||
sendMessage(e);
|
sendMessage(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送聊天消息
|
* 发送聊天消息(内带防重入锁,避免快速连按 Enter 重复提交)
|
||||||
*/
|
*/
|
||||||
|
let _isSending = false; // 发送中防重入标记
|
||||||
async function sendMessage(e) {
|
async function sendMessage(e) {
|
||||||
if (e) e.preventDefault();
|
if (e) e.preventDefault();
|
||||||
|
if (_isSending) return; // 上一次还没结束,忽略
|
||||||
|
_isSending = true;
|
||||||
|
|
||||||
// 前端禁言检查
|
// 前端禁言检查
|
||||||
if (isMutedUntil > Date.now()) {
|
if (isMutedUntil > Date.now()) {
|
||||||
@@ -763,6 +783,7 @@
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
submitBtn.disabled = false;
|
submitBtn.disabled = false;
|
||||||
|
_isSending = false; // 释放发送锁,允许下次发送
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user