Skip to content

Commit

Permalink
Revert "fix: use setBaseAndExtent instead of removeAllRanges (#357)"
Browse files Browse the repository at this point in the history
This reverts commit 591af40.
  • Loading branch information
BartoszGrajdek authored Jun 10, 2024
1 parent 1f8dc1d commit f3823f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
}

const text = processedValue !== undefined ? processedValue : '';

parseText(divRef.current, text, processedMarkdownStyle, contentSelection.current?.end);
parseText(divRef.current, text, processedMarkdownStyle, text.length);
updateTextColor(divRef.current, value);
},
[multiline, processedMarkdownStyle, processedValue],
Expand Down
6 changes: 4 additions & 2 deletions src/web/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function setCursorPosition(target: HTMLElement, start: number, end: number | nul

const selection = window.getSelection();
if (selection) {
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
selection.removeAllRanges();
selection.addRange(range);
}

scrollCursorIntoView(target as HTMLInputElement);
Expand All @@ -106,7 +107,8 @@ function moveCursorToEnd(target: HTMLElement) {
if (selection) {
range.setStart(target, target.childNodes.length);
range.collapse(true);
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
selection.removeAllRanges();
selection.addRange(range);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,12 @@ function moveCursor(isFocused: boolean, alwaysMoveCursorToTheEnd: boolean, curso
}
}

function parseText(target: HTMLElement, text: string, cursorPositionIndex: number | null, markdownStyle: PartialMarkdownStyle = {}, alwaysMoveCursorToTheEnd = false) {
function parseText(target: HTMLElement, text: string, curosrPositionIndex: number | null, markdownStyle: PartialMarkdownStyle = {}, alwaysMoveCursorToTheEnd = false) {
const targetElement = target;

// in case the cursorPositionIndex is larger than text length, cursorPosition will be null, i.e: move the caret to the end
let cursorPosition: number | null = cursorPositionIndex && cursorPositionIndex <= text.length ? cursorPositionIndex : null;
let cursorPosition: number | null = curosrPositionIndex;
const isFocused = document.activeElement === target;
if (isFocused && cursorPositionIndex === null) {
if (isFocused && curosrPositionIndex === null) {
const selection = CursorUtils.getCurrentCursorPosition(target);
cursorPosition = selection ? selection.end : null;
}
Expand Down

0 comments on commit f3823f2

Please sign in to comment.