Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

SzymczakJ
Copy link
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
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
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
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?

function parseStringWithUnitToNumber(value: string | null): number {
return value ? parseInt(value.replace('px', ''), 10) : 0;
}

export type {PartialMarkdownStyle};

export {mergeMarkdownStyleWithDefault, parseStringWithUnitToNumber};
export {mergeMarkdownStyleWithDefault, parseStringWithUnitToNumber, deepEqualMarkdownStyles as deepCompareMarkdownStyles};
Copy link
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
Collaborator

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

@tomekzaw
Copy link
Collaborator

Definitely yes

@SzymczakJ SzymczakJ force-pushed the @szymczak/fix-text-selection branch from 1302bf2 to c89eeea Compare November 25, 2024 18:13
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.

3 participants