Skip to content

Commit

Permalink
fix: optimize implementation
Browse files Browse the repository at this point in the history
Signed-off-by: dominictb <[email protected]>
  • Loading branch information
dominictb committed May 27, 2024
1 parent 6403559 commit f89949b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 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 : '';

const currentCursorPostion = contentSelection?.current && contentSelection?.current.end <= text.length ? contentSelection.current.end : text.length;
parseText(divRef.current, text, processedMarkdownStyle, currentCursorPostion);
parseText(divRef.current, text, processedMarkdownStyle, contentSelection.current?.end);
updateTextColor(divRef.current, value);
},
[multiline, processedMarkdownStyle, processedValue],
Expand Down
7 changes: 4 additions & 3 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ function moveCursor(isFocused: boolean, alwaysMoveCursorToTheEnd: boolean, curso
}
}

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

let cursorPosition: number | null = curosrPositionIndex;
// 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;
const isFocused = document.activeElement === target;
if (isFocused && curosrPositionIndex === null) {
if (isFocused && cursorPositionIndex === null) {
const selection = CursorUtils.getCurrentCursorPosition(target);
cursorPosition = selection ? selection.end : null;
}
Expand Down

0 comments on commit f89949b

Please sign in to comment.