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 toggling styles on collapsed selections with $patchStyleText #3922

Merged
merged 1 commit into from
Feb 17, 2023

Conversation

birtles
Copy link
Contributor

@birtles birtles commented Feb 16, 2023

Fixes #3921

@vercel
Copy link

vercel bot commented Feb 16, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
lexical ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 17, 2023 at 4:43AM (UTC)
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 17, 2023 at 4:43AM (UTC)

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 16, 2023
@@ -281,13 +281,16 @@ export function $patchStyleText(
if (selection.isCollapsed()) {
const styles = getStyleObjectFromCSS(selection.style);
Object.entries(patch).forEach(([key, value]) => {
if (value !== null) {
if (value === null) {
delete styles[key];
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be best to avoid the delete keyword here, as there are some cases where it causes V8 to switch away from optimized paths. Could we just set it to undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know that. I think it might be better to be consistent with $patchNodeStyle though? Also the type of styles is Record<string, string> so we can't use undefined here without changing the type. (And I don't suppose a helper like $patchStyleText is likely to be on the critical path anyway.)

Copy link
Contributor

@acywatson acywatson Feb 17, 2023

Choose a reason for hiding this comment

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

I didn't know that. I think it might be better to be consistent with $patchNodeStyle though? Also the type of styles is Record<string, string> so we can't use undefined here without changing the type. (And I don't suppose a helper like $patchStyleText is likely to be on the critical path anyway.)

Yea, I think it's fine in this context, given the other considerations. We generally try to avoid it where possible because the manifestations of the performance impact aren't always obvious. IIRC, delete sometimes causes V8 to switch to a less-optimal read structure for that object, which makes future lookups on the same object slower. Admittedly, I'm just using a mental heuristic here - I don't have perfect recall of all the ways the delete keyword can impact performance.

@trueadm am I wrong here?

@acywatson
Copy link
Contributor

Thank you for this! If you pull the latest from main and rebase, the e2e tests should pass.

@birtles
Copy link
Contributor Author

birtles commented Feb 17, 2023

Thank you for this! If you pull the latest from main and rebase, the e2e tests should pass.

Thanks! Rebased.

Copy link
Contributor

@thegreatercurve thegreatercurve left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @birtles

@thegreatercurve thegreatercurve merged commit ee32d77 into facebook:main Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: $patchStyleText does not clear styles or update the style cache on collapsed selections
4 participants