Skip to content

Commit

Permalink
Fix selection prop (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid authored Apr 24, 2024
1 parent 732a426 commit 2220ea6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,12 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
if (!divRef.current || !selection || (contentSelection.current && selection.start === contentSelection.current.start && selection.end === contentSelection.current.end)) {
return;
}
CursorUtils.setCursorPosition(divRef.current, selection.start, selection.end);
updateSelection(null, {start: selection.start, end: selection.end || selection.start});
}, [selection, updateSelection]);

const newSelection: Selection = {start: selection.start, end: selection.end ?? selection.start};
contentSelection.current = newSelection;
updateRefSelectionVariables(newSelection);
CursorUtils.setCursorPosition(divRef.current, newSelection.start, newSelection.end);
}, [selection, updateRefSelectionVariables]);

useEffect(() => {
if (history.current?.history.length !== 0) {
Expand Down
9 changes: 3 additions & 6 deletions src/web/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,18 @@ function setCursorPosition(target: HTMLElement, start: number, end: number | nul
if (!startNode && start >= charCount && (start <= nextCharCount || (start === nextCharCount && i < n - 1))) {
startNode = textNode;

// There are 4(/5) cases to consider here:
// There are 4 cases to consider here:
// 1. Caret in front of a character, when pressing enter
// 2. Caret at the end of a line (not last one)
// 3a. Caret at the end of whole input, when pressing enter - On firefox
// 3b. Caret at the end of whole input, when pressing enter - On other browsers
// 3. Caret at the end of whole input, when pressing enter
// 4. All other placements
if (prevChar === '\n' && prevTextLength !== undefined && prevTextLength < textCharacters.length) {
if (nextChar !== '\n') {
range.setStart(textNodes[i + 1] as Node, 0);
} else if (i !== textNodes.length - 1) {
range.setStart(textNodes[i] as Node, 1);
} else if (BrowserUtils.isFirefox) {
range.setStart(textNode, start - charCount);
} else {
range.setStart(textNodes[i] as Node, 2);
range.setStart(textNode, start - charCount);
}
} else {
range.setStart(textNode, start - charCount);
Expand Down

0 comments on commit 2220ea6

Please sign in to comment.