Skip to content

Commit

Permalink
Change what getCurrentCursorPosition returns when selection is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid committed Mar 5, 2024
1 parent 226da3f commit 4504afb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
}
const currentSelection = CursorUtils.getCurrentCursorPosition(divRef.current);

if (contentSelection.current.start !== currentSelection.start || contentSelection.current.end !== currentSelection.end) {
if (currentSelection && (contentSelection.current.start !== currentSelection.start || contentSelection.current.end !== currentSelection.end)) {
if (contentSelection.current.start >= 0 && contentSelection.current.end >= 0) {
updateRefSelectionVariables(contentSelection.current.start, contentSelection.current.end);
contentSelection.current = currentSelection;
Expand Down
2 changes: 1 addition & 1 deletion src/web/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function getCurrentCursorPosition(target: HTMLElement) {
const end = start + range.toString().length;
return {start, end};
}
return {start: -1, end: -1};
return null;
}

function removeSelection() {
Expand Down
5 changes: 3 additions & 2 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ function parseText(target: HTMLElement, text: string, curosrPositionIndex: numbe
let cursorPosition: number | null = curosrPositionIndex;
const isFocused = document.activeElement === target;
if (isFocused && curosrPositionIndex === null) {
cursorPosition = CursorUtils.getCurrentCursorPosition(target).start;
const selection = CursorUtils.getCurrentCursorPosition(target);
cursorPosition = selection ? selection.end : null;
}
const ranges = global.parseExpensiMarkToRanges(text);

Expand All @@ -182,7 +183,7 @@ function parseText(target: HTMLElement, text: string, curosrPositionIndex: numbe
}

if (isFocused) {
if (alwaysMoveCursorToTheEnd || (cursorPosition !== null && cursorPosition < 0)) {
if (alwaysMoveCursorToTheEnd || cursorPosition === null) {
CursorUtils.moveCursorToEnd(target);
} else if (cursorPosition !== null) {
CursorUtils.setCursorPosition(target, cursorPosition);
Expand Down

0 comments on commit 4504afb

Please sign in to comment.