From 094b049c9442a26f85e22581929abb81b1ea5a3e Mon Sep 17 00:00:00 2001 From: Good Wood Date: Thu, 27 Mar 2025 15:41:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20fix=20the=20bug=20where=20pressing=20Ent?= =?UTF-8?q?er=20in=20Chinese=20input=20mode=20on=20Safari=E2=80=A6=20(#169?= =?UTF-8?q?14)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/chat/chat/chat-input-area/index.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/web/app/components/base/chat/chat/chat-input-area/index.tsx b/web/app/components/base/chat/chat/chat-input-area/index.tsx index d7b5f8b39e..14d8185f99 100644 --- a/web/app/components/base/chat/chat/chat-input-area/index.tsx +++ b/web/app/components/base/chat/chat/chat-input-area/index.tsx @@ -80,6 +80,7 @@ const ChatInputArea = ({ const { checkInputsForm } = useCheckInputsForms() const historyRef = useRef(['']) const [currentIndex, setCurrentIndex] = useState(-1) + const isComposingRef = useRef(false) const handleSend = () => { if (isResponding) { notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) @@ -103,8 +104,21 @@ const ChatInputArea = ({ } } } + const handleCompositionStart = () => { + // e: React.CompositionEvent + isComposingRef.current = true + } + const handleCompositionEnd = () => { + // safari or some browsers will trigger compositionend before keydown. + // delay 50ms for safari. + setTimeout(() => { + isComposingRef.current = false + }, 50) + } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) { + // if isComposing, exit + if (isComposingRef.current) return e.preventDefault() setQuery(query.replace(/\n$/, '')) historyRef.current.push(query) @@ -188,6 +202,8 @@ const ChatInputArea = ({ setTimeout(handleTextareaResize, 0) }} onKeyDown={handleKeyDown} + onCompositionStart={handleCompositionStart} + onCompositionEnd={handleCompositionEnd} onPaste={handleClipboardPasteFile} onDragEnter={handleDragFileEnter} onDragLeave={handleDragFileLeave}