diff --git a/src/MarkdownTextInput.web.tsx b/src/MarkdownTextInput.web.tsx index 99efd9f3..e6380822 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],