From 81be965446ce6f60971aa89cd41681e32cceb8d9 Mon Sep 17 00:00:00 2001 From: dominictb Date: Mon, 10 Jun 2024 02:20:33 +0700 Subject: [PATCH] chore: create func to restore cursor imperatively - revert the use of contentSelection to restore cursor pos during text update to cursor jumping around funny since contentSelection ref value can get outdated. - create func to restore cursor in the ref object Signed-off-by: dominictb --- src/MarkdownTextInput.web.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/MarkdownTextInput.web.tsx b/src/MarkdownTextInput.web.tsx index 99efd9f34..e6380822f 100644 --- a/src/MarkdownTextInput.web.tsx +++ b/src/MarkdownTextInput.web.tsx @@ -364,11 +364,6 @@ const MarkdownTextInput = React.forwardRef( text = parseText(divRef.current, changedText, processedMarkdownStyle).text; } - const selectionAfterTextChange = CursorUtils.getCurrentCursorPosition(divRef.current); - if (selectionAfterTextChange) { - contentSelection.current = selectionAfterTextChange; - } - if (pasteRef?.current) { pasteRef.current = false; updateSelection(e); @@ -536,6 +531,15 @@ const MarkdownTextInput = React.forwardRef( updateTextColor(r, ''); }; + (r as any).restoreSelectionPosition = () => { + if (contentSelection.current) { + CursorUtils.setCursorPosition(r, contentSelection.current.start, contentSelection.current.end); + } else { + const valueLength = value ? value.length : r.innerText.length; + CursorUtils.setCursorPosition(r, valueLength, null); + } + }; + if (value === '' || value === undefined) { // update to placeholder color when value is empty updateTextColor(r, r.innerText); @@ -566,7 +570,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],