Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pasting in composition mode #599

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface MarkdownTextInputProps extends TextInputProps, InlineImagesInputProps

interface MarkdownNativeEvent extends Event {
inputType?: string;
isComposing?: boolean;
keyCode?: number;
}

type MarkdownTextInput = TextInput & React.Component<MarkdownTextInputProps>;
Expand Down Expand Up @@ -120,7 +122,6 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
throw new Error('[react-native-live-markdown] `parser` is not a function');
}

const compositionRef = useRef<boolean>(false);
const divRef = useRef<MarkdownTextInputElement | null>(null);
const currentlyFocusedField = useRef<HTMLDivElement | null>(null);
const contentSelection = useRef<Selection | null>(null);
Expand Down Expand Up @@ -349,6 +350,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
}
const nativeEvent = e.nativeEvent as MarkdownNativeEvent;
const inputType = nativeEvent.inputType;
const isComposing = isEventComposing(nativeEvent);

updateTextColor(divRef.current, e.target.textContent ?? '');
const previousText = divRef.current.value;
Expand All @@ -370,7 +372,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
? Math.max(contentSelection.current.start, 0) // Don't move the caret when deleting forward with no characters selected
: Math.max(Math.max(contentSelection.current.end, 0) + (parsedText.length - previousText.length), 0);

if (compositionRef.current) {
if (isComposing) {
updateTextColor(divRef.current, parsedText);
updateSelection(e, {
start: newCursorPosition,
Expand Down Expand Up @@ -657,13 +659,8 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
[insertText],
);

const startComposition = useCallback(() => {
compositionRef.current = true;
}, []);

const endComposition = useCallback(
(e: React.CompositionEvent<HTMLDivElement>) => {
compositionRef.current = false;
handleOnChangeText(e);
},
[handleOnChangeText],
Expand Down Expand Up @@ -788,7 +785,6 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
autoCapitalize={autoCapitalize}
className={className}
onKeyDown={handleKeyPress}
onCompositionStart={startComposition}
onCompositionEnd={endComposition}
onInput={handleOnChangeText}
onClick={handleClick}
Expand Down Expand Up @@ -829,4 +825,4 @@ const styles = StyleSheet.create({

export default MarkdownTextInput;

export type {MarkdownTextInputProps, MarkdownTextInputElement, HTMLMarkdownElement};
export type {MarkdownNativeEvent, MarkdownTextInputProps, MarkdownTextInputElement, HTMLMarkdownElement};
4 changes: 2 additions & 2 deletions src/web/utils/inputUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {CSSProperties} from 'react';
import type {MarkdownTextInputElement} from '../../MarkdownTextInput.web';
import type {MarkdownNativeEvent, MarkdownTextInputElement} from '../../MarkdownTextInput.web';

const ZERO_WIDTH_SPACE = '\u200B';

// If an Input Method Editor is processing key input, the 'keyCode' is 229.
// https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
function isEventComposing(nativeEvent: globalThis.KeyboardEvent) {
function isEventComposing(nativeEvent: globalThis.KeyboardEvent | MarkdownNativeEvent) {
return nativeEvent.isComposing || nativeEvent.keyCode === 229;
}

Expand Down
Loading