Skip to content

Fix text selection on Web doesn't work when passing markdownStyle without useMemo#547

Closed
SzymczakJ wants to merge 8 commits intoExpensify:mainfrom
SzymczakJ:@szymczak/fix-text-selection
Closed

Fix text selection on Web doesn't work when passing markdownStyle without useMemo#547
SzymczakJ wants to merge 8 commits intoExpensify:mainfrom
SzymczakJ:@szymczak/fix-text-selection

Conversation

@SzymczakJ
Copy link
Copy Markdown
Contributor

@SzymczakJ SzymczakJ commented Nov 18, 2024

Details

markdownStyle object was compared by reference and was causing extra useMemo rerenders, which removed text selection. This PR makes MarkdownTextInput.web component compares markdownStyle object deeply and fixes this bug.

Related Issues

GH_LINK

#544

Manual Tests

On web example, try selecting text and verify it works.

Linked PRs

Copy link
Copy Markdown
Collaborator

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, does the selection work correctly when changing markdownStyle?

Comment on lines +92 to +98
function deepEqualMarkdownStyles(markdownStyle1: MarkdownStyle, markdownStyle2: PartialMarkdownStyle) {
const keys1 = Object.keys(markdownStyle1) as Array<keyof MarkdownStyle>;
const keys2 = Object.keys(markdownStyle2) as Array<keyof MarkdownStyle>;

if (keys1.length !== keys2.length) {
return false;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all values of markdownStyle are serializable, why can't we just compare JSON.stringify(markdownStyle1) === JSON.stringify(markdownStyle2)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to treat objects like {color: '#FF0000', backgroundColor: '#FF0000'} and {backgroundColor: '#FF0000', color: '#FF0000'} as equal? That's the only reason I didn't use JSON.stringify, because it maintains order. We could use JSON.stringify, but then some logically equal objects(but with changed order of props) would come out as not equal. WDYT?

export type {PartialMarkdownStyle};

export {mergeMarkdownStyleWithDefault, parseStringWithUnitToNumber};
export {mergeMarkdownStyleWithDefault, parseStringWithUnitToNumber, deepEqualMarkdownStyles as deepCompareMarkdownStyles};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we export deepEqualMarkdownStyles with a different name deepCompareMarkdownStyles?

@SzymczakJ SzymczakJ marked this pull request as ready for review November 21, 2024 09:54
@BartoszGrajdek
Copy link
Copy Markdown
Contributor

Maybe we should write some tests that would cover this case? @tomekzaw

@tomekzaw
Copy link
Copy Markdown
Collaborator

Definitely yes

@SzymczakJ SzymczakJ force-pushed the @szymczak/fix-text-selection branch from 1302bf2 to c89eeea Compare November 25, 2024 18:13
@jmusial
Copy link
Copy Markdown
Collaborator

jmusial commented Jun 11, 2025

Can be closed, has been fixed in #684

@tomekzaw tomekzaw closed this Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants