Skip to content

Commit

Permalink
fix selection when rerendering because of markdownStyle change
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymczakJ committed Nov 18, 2024
1 parent 824e698 commit 37fe064
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
shouldAddToHistory = true,
shouldForceDOMUpdate = false,
shouldScrollIntoView = false,
shouldPreserveSelection = false,
): ParseTextResult => {
if (!divRef.current) {
return {text: text || '', cursorPosition: null};
Expand All @@ -173,10 +174,21 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
if (text === null) {
return {text: divRef.current.value, cursorPosition: null};
}
const parsedText = updateInputStructure(target, text, cursorPosition, multiline, customMarkdownStyles, false, shouldForceDOMUpdate, shouldScrollIntoView, {
addAuthTokenToImageURLCallback,
imagePreviewAuthRequiredURLs,
});
const parsedText = updateInputStructure(
target,
text,
cursorPosition,
multiline,
customMarkdownStyles,
false,
shouldForceDOMUpdate,
shouldScrollIntoView,
{
addAuthTokenToImageURLCallback,
imagePreviewAuthRequiredURLs,
},
shouldPreserveSelection,
);
divRef.current.value = parsedText.text;

if (history.current && shouldAddToHistory) {
Expand All @@ -191,7 +203,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
const processedMarkdownStyle = useMemo(() => {
const newMarkdownStyle = processMarkdownStyle(memoizedMarkdownStyle);
if (divRef.current) {
parseText(divRef.current, divRef.current.value, newMarkdownStyle, null, false, false);
parseText(divRef.current, divRef.current.value, newMarkdownStyle, null, false, false, false, true);
}
return newMarkdownStyle;
}, [memoizedMarkdownStyle, parseText]);
Expand Down
17 changes: 14 additions & 3 deletions src/web/utils/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,22 @@ function parseRangesToHTMLNodes(
return {dom: rootElement, tree: rootNode};
}

function moveCursor(isFocused: boolean, alwaysMoveCursorToTheEnd: boolean, cursorPosition: number | null, target: MarkdownTextInputElement, shouldScrollIntoView = false) {
function moveCursor(
isFocused: boolean,
alwaysMoveCursorToTheEnd: boolean,
cursorPosition: number | null,
target: MarkdownTextInputElement,
selectionEnd: number | null = null,
shouldScrollIntoView = false,
) {
if (!isFocused) {
return;
}

if (alwaysMoveCursorToTheEnd || cursorPosition === null) {
moveCursorToEnd(target);
} else if (cursorPosition !== null) {
setCursorPosition(target, cursorPosition, null, shouldScrollIntoView);
setCursorPosition(target, cursorPosition, selectionEnd, shouldScrollIntoView);
}
}

Expand All @@ -281,15 +288,19 @@ function updateInputStructure(
shouldForceDOMUpdate = false,
shouldScrollIntoView = false,
inlineImagesProps: InlineImagesInputProps = {},
shouldPreserveSelection = 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 !== null && cursorPositionIndex <= text.length ? cursorPositionIndex : null;
let selectionEndPosition: number | null = null;
const isFocused = document.activeElement === target;
if (isFocused && cursorPositionIndex === null) {
const selection = getCurrentCursorPosition(target);
cursorPosition = selection ? selection.start : null;
// in some cases like rerendering because style was changed we want to preserve selection
selectionEndPosition = shouldPreserveSelection && selection ? selection.end : null;
}
const markdownRanges = global.parseExpensiMarkToRanges(text);
if (!text || targetElement.innerHTML === '<br>' || (targetElement && targetElement.innerHTML === '\n')) {
Expand All @@ -312,7 +323,7 @@ function updateInputStructure(
updateTreeElementRefs(tree, targetElement);
targetElement.tree = tree;

moveCursor(isFocused, alwaysMoveCursorToTheEnd, cursorPosition, targetElement, shouldScrollIntoView);
moveCursor(isFocused, alwaysMoveCursorToTheEnd, cursorPosition, targetElement, selectionEndPosition, shouldScrollIntoView);
} else {
targetElement.tree = createRootTreeNode(targetElement);
}
Expand Down

0 comments on commit 37fe064

Please sign in to comment.