Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Fix non-Latin IME adds duplicate words when composing on macOS #368

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/components/pages/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const Home = ({
textToSpeechPlaying,
translateWhenPressingEnter,
}) => {
const onCompositionRef = useRef(false);
const inputRef = useRef(null);

const outputNode = useMemo(() => {
Expand Down Expand Up @@ -571,33 +572,21 @@ const Home = ({
e.target.selectionStart,
e.target.selectionEnd,
)}
onClick={(e) => onUpdateInputText(
e.target.value,
e.target.selectionStart,
e.target.selectionEnd,
)}
onInput={(e) => onUpdateInputText(
e.target.value,
e.target.selectionStart,
e.target.selectionEnd,
)}
// handle Chinese, Japanese, Korean IME
// https://github.com/facebook/react/issues/3926#issuecomment-929799564
// https://stackoverflow.com/a/51221639
onCompositionStart={() => {
onCompositionRef.current = true;
}}
onCompositionEnd={() => {
onCompositionRef.current = false;
}}
onKeyDown={translateWhenPressingEnter ? (e) => {
if (e.key === 'Enter') {
if (e.key === 'Enter' && !onCompositionRef.current) {
onTranslate();
e.target.blur();
}

onUpdateInputText(
e.target.value,
e.target.selectionStart,
e.target.selectionEnd,
);
} : null}
onKeyUp={(e) => onUpdateInputText(
e.target.value,
e.target.selectionStart,
e.target.selectionEnd,
)}
placeholder={getLocale('typeSomethingHere')}
spellCheck="false"
value={inputText}
Expand Down