From f3823f2fc8c731508fb1098244767f19e2c7e793 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Mon, 10 Jun 2024 22:43:08 +0200 Subject: [PATCH] Revert "fix: use setBaseAndExtent instead of removeAllRanges (#357)" This reverts commit 591af40d23d90b8a23fff665856372415f64d8ae. --- src/MarkdownTextInput.web.tsx | 3 +-- src/web/cursorUtils.ts | 6 ++++-- src/web/parserUtils.ts | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/MarkdownTextInput.web.tsx b/src/MarkdownTextInput.web.tsx index bd76f616..fae1fa40 100644 --- a/src/MarkdownTextInput.web.tsx +++ b/src/MarkdownTextInput.web.tsx @@ -560,8 +560,7 @@ const MarkdownTextInput = React.forwardRef( } 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], diff --git a/src/web/cursorUtils.ts b/src/web/cursorUtils.ts index 1cda6599..98c50ca0 100644 --- a/src/web/cursorUtils.ts +++ b/src/web/cursorUtils.ts @@ -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); @@ -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); } } diff --git a/src/web/parserUtils.ts b/src/web/parserUtils.ts index 3758bbaf..5e2ef243 100644 --- a/src/web/parserUtils.ts +++ b/src/web/parserUtils.ts @@ -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; }