From 1197521921f98e92e7c89b91dbcbb6b981908ec6 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 5 Jul 2023 22:50:12 +0800 Subject: [PATCH] fix: #2252 polyfill composing for old safari browsers --- app/components/chat.tsx | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 74c872de..26716150 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -172,10 +172,29 @@ function PromptToast(props: { function useSubmitHandler() { const config = useAppConfig(); const submitKey = config.submitKey; + const isComposing = useRef(false); + + useEffect(() => { + const onCompositionStart = () => { + isComposing.current = true; + }; + const onCompositionEnd = () => { + isComposing.current = false; + }; + + window.addEventListener("compositionstart", onCompositionStart); + window.addEventListener("compositionend", onCompositionEnd); + + return () => { + window.removeEventListener("compositionstart", onCompositionStart); + window.removeEventListener("compositionend", onCompositionEnd); + }; + }, []); const shouldSubmit = (e: React.KeyboardEvent) => { if (e.key !== "Enter") return false; - if (e.key === "Enter" && e.nativeEvent.isComposing) return false; + if (e.key === "Enter" && (e.nativeEvent.isComposing || isComposing.current)) + return false; return ( (config.submitKey === SubmitKey.AltEnter && e.altKey) || (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||