From c9dc2a671a2a19cd38e7d8b00f87fb5922589f57 Mon Sep 17 00:00:00 2001 From: ppzzxx <128487648+ppzzxx@users.noreply.github.com> Date: Sun, 8 Mar 2026 20:15:11 +0800 Subject: [PATCH] Fix/chinese ime enter send (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: prevent message send when Chinese IME is composing - Add e.isComposing check to Enter key handler in ChatInput - This prevents accidental message sending when pressing Enter to confirm Chinese/Japanese input - Fixes issue where messages would be sent prematurely when using IME composition Refs: #issue * fix: prevent message send when Chinese IME is composing - Add isComposing state to track IME composition status - Add onCompositionStart/onCompositionEnd event handlers to textarea - Check isComposing state in Enter key handler to prevent accidental send This fixes the issue where pressing Enter to confirm Chinese/Japanese input would prematurely send the message instead of just confirming the IME selection. The fix uses explicit composition state tracking instead of relying solely on e.isComposing, which provides more reliable cross-browser behavior. --------- Co-authored-by: 代码专家 --- src/components/ChatInput.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/components/ChatInput.tsx b/src/components/ChatInput.tsx index c765ba2..0ccd142 100644 --- a/src/components/ChatInput.tsx +++ b/src/components/ChatInput.tsx @@ -98,6 +98,7 @@ export function ChatInput({ onSend, onNewSession, onAbort, isGenerating, disable const [files, setFiles] = useState([]); const [isDragOver, setIsDragOver] = useState(false); const [showPreview, setShowPreview] = useState(() => localStorage.getItem('pinchchat-md-preview') === '1'); + const [isComposing, setIsComposing] = useState(false); const [showSlash, setShowSlash] = useState(false); const textareaRef = useRef(null); const fileInputRef = useRef(null); @@ -207,6 +208,8 @@ export function ChatInput({ onSend, onNewSession, onAbort, isGenerating, disable const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { + // Prevent sending when IME is composing (e.g., Chinese/Japanese input) + if (isComposing) return; if (sendOnEnter) { // Enter sends, Shift+Enter for newline if (!e.shiftKey && !e.ctrlKey && !e.metaKey) { @@ -383,6 +386,8 @@ export function ChatInput({ onSend, onNewSession, onAbort, isGenerating, disable value={text} onChange={(e) => { setText(e.target.value); setShowSlash(shouldShowSlashMenu(e.target.value)); }} onKeyDown={handleKeyDown} + onCompositionStart={() => setIsComposing(true)} + onCompositionEnd={() => setIsComposing(false)} onPaste={handlePaste} placeholder={t('chat.inputPlaceholder')} aria-label={t('chat.inputLabel')}