mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
fix: Safari IME enter key triggering message send
Made-with: Cursor
This commit is contained in:
@@ -306,11 +306,7 @@ const sendBtn = document.getElementById('send-btn');
|
|||||||
const messagesDiv = document.getElementById('chat-messages');
|
const messagesDiv = document.getElementById('chat-messages');
|
||||||
|
|
||||||
chatInput.addEventListener('compositionstart', () => { isComposing = true; });
|
chatInput.addEventListener('compositionstart', () => { isComposing = true; });
|
||||||
// Safari fires compositionend *before* the confirming keydown event, so if we
|
chatInput.addEventListener('compositionend', () => { setTimeout(() => { isComposing = false; }, 100); });
|
||||||
// reset isComposing synchronously the keydown handler sees !isComposing and
|
|
||||||
// sends the message prematurely. A setTimeout(0) defers the reset until after
|
|
||||||
// keydown has been processed, fixing the Safari IME Enter-to-confirm bug.
|
|
||||||
chatInput.addEventListener('compositionend', () => { setTimeout(() => { isComposing = false; }, 0); });
|
|
||||||
|
|
||||||
chatInput.addEventListener('input', function() {
|
chatInput.addEventListener('input', function() {
|
||||||
this.style.height = '42px';
|
this.style.height = '42px';
|
||||||
@@ -322,6 +318,8 @@ chatInput.addEventListener('input', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
chatInput.addEventListener('keydown', function(e) {
|
chatInput.addEventListener('keydown', function(e) {
|
||||||
|
// keyCode 229 indicates an IME is processing the keystroke (reliable across browsers)
|
||||||
|
if (e.keyCode === 229 || e.isComposing || isComposing) return;
|
||||||
if ((e.ctrlKey || e.shiftKey) && e.key === 'Enter') {
|
if ((e.ctrlKey || e.shiftKey) && e.key === 'Enter') {
|
||||||
const start = this.selectionStart;
|
const start = this.selectionStart;
|
||||||
const end = this.selectionEnd;
|
const end = this.selectionEnd;
|
||||||
@@ -329,7 +327,7 @@ chatInput.addEventListener('keydown', function(e) {
|
|||||||
this.selectionStart = this.selectionEnd = start + 1;
|
this.selectionStart = this.selectionEnd = start + 1;
|
||||||
this.dispatchEvent(new Event('input'));
|
this.dispatchEvent(new Event('input'));
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
} else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !isComposing) {
|
} else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey) {
|
||||||
sendMessage();
|
sendMessage();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user