Skip to content

Commit

Permalink
Add selectionStart and selectionEnd variables to markdown input ref (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid authored Feb 14, 2024
1 parent 5375a10 commit 575d6fd
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
// Empty placeholder would collapse the div, so we need to use zero-width space to prevent it
const heightSafePlaceholder = useMemo(() => getPlaceholderValue(placeholder), [placeholder]);

const updateSelection = useCallback(() => {
if (!divRef.current) {
return;
}
const selection = CursorUtils.getCurrentCursorPosition(divRef.current);
const markdownHTMLInput = divRef.current as HTMLInputElement;
markdownHTMLInput.selectionStart = selection.start;
markdownHTMLInput.selectionEnd = selection.end;
contentSelection.current = selection;
}, []);

const parseText = useCallback(
(target: HTMLDivElement, text: string | null, customMarkdownStyles: MarkdownStyle, cursorPosition: number | null = null, shouldAddToHistory = true) => {
if (text === null) {
Expand All @@ -177,12 +188,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
history.current.debouncedAdd(parsedText.text, parsedText.cursorPosition);
}

if (parsedText.cursorPosition !== null) {
contentSelection.current = {
start: parsedText.cursorPosition,
end: parsedText.cursorPosition,
};
}
updateSelection();

return parsedText;
},
Expand Down Expand Up @@ -354,10 +360,9 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
(event) => {
const e = event as unknown as NativeSyntheticEvent<TextInputSelectionChangeEventData>;
setEventProps(e);
const selection = CursorUtils.getCurrentCursorPosition(e.target as unknown as HTMLElement);
contentSelection.current = selection;
if (onSelectionChange) {
e.nativeEvent.selection = selection;
updateSelection();
if (onSelectionChange && contentSelection.current) {
e.nativeEvent.selection = contentSelection.current;
onSelectionChange(e);
}
},
Expand Down Expand Up @@ -414,6 +419,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(

const handleClick = useCallback(
(e: MouseEvent<HTMLDivElement, globalThis.MouseEvent>) => {
updateSelection();
if (!onClick || !divRef.current) {
return;
}
Expand Down Expand Up @@ -509,6 +515,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
autoCapitalize={autoCapitalize}
className={className}
onKeyDown={handleKeyPress}
onKeyUp={updateSelection}
onInput={handleOnChangeText}
onSelect={handleSelectionChange}
onClick={handleClick}
Expand Down

0 comments on commit 575d6fd

Please sign in to comment.