Skip to content

[lexical-playground] Bug Fix: include font sizes in pt as well in parseAllowedFontSize#7719

Merged
etrepum merged 11 commits intofacebook:mainfrom
harshmetkel24:7718
Jul 26, 2025
Merged

[lexical-playground] Bug Fix: include font sizes in pt as well in parseAllowedFontSize#7719
etrepum merged 11 commits intofacebook:mainfrom
harshmetkel24:7718

Conversation

@harshmetkel24
Copy link
Copy Markdown
Contributor

@harshmetkel24 harshmetkel24 commented Jul 22, 2025

Description

Describe the changes in this pull request
Consider font sizes in pt as well along with px in parseAllowedFontSize as most of editors like Google docs and MS word export font-sizes in pt when copied content

Closes #7718
Closes #7726

Test plan

tests added for custom font-size in HTMLCopyAndPaste.test.ts

Before

Screen.Recording.2025-07-22.at.5.53.43.PM.mov

After

Screen.Recording.2025-07-22.at.5.54.26.PM.mov

@vercel
Copy link
Copy Markdown

vercel bot commented Jul 22, 2025

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

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 26, 2025 4:08pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 26, 2025 4:08pm

@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 Jul 22, 2025
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

Tests and lint are failing

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

Tests are still failing

@harshmetkel24
Copy link
Copy Markdown
Contributor Author

I’ve resolved the lint error, but the test is still failing. After investigating, I realized my fix only addresses the issue in the playground, which doesn’t require updating unit tests in core.

I’d appreciate your guidance on how to proceed. Here are the options I’m considering:

  1. Remove the test I added: This avoids misleading others but might allow regressions or encourage unsupported workarounds.
  2. Fix it in lexicalTextNode: This would involve applying styles during importDOM, as shown in the first screenshot. However, this causes unintended font-size styles to propagate broadly, resulting in more test failures (see second screenshot).

Let me know what you think is the best direction.
Thanks!

image image

@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Jul 22, 2025

You can test the behavior of the playground with e2e tests. I do not think the default behavior of TextNode should be changed

Test verifies font-size handling when copying content from Google
Docs/Word and pasting into Lexical.
@harshmetkel24
Copy link
Copy Markdown
Contributor Author

oh looks like some other tests related to copy paste are failing now 😢

@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Jul 22, 2025

Yes it looks like other tests are expecting the font-size in pt to be ignored on paste, so those expectations would have to change.

fix tests by style updates for HTML tables and text format in
CopyAndPaste specs.
etrepum
etrepum previously approved these changes Jul 23, 2025
@etrepum etrepum dismissed their stale review July 23, 2025 23:48

On closer inspection, the toolbar's size is not displayed correctly when the font-size is in pt

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

When inspecting the playground for this I noticed that the toolbar displays the raw number associated with the font-size, the number parsing should be used for this functionality as well.

          <FontSize
            selectionFontSize={toolbarState.fontSize.slice(0, -2)}
            editor={activeEditor}
            disabled={!isEditable}

Here's a playground link for this PR with some content that uses pt font sizes

In e2e tests and font size plugin, update font sizes to pixels for
better rendering.
@harshmetkel24
Copy link
Copy Markdown
Contributor Author

When inspecting the playground for this I noticed that the toolbar displays the raw number associated with the font-size, the number parsing should be used for this functionality as well.

          <FontSize
            selectionFontSize={toolbarState.fontSize.slice(0, -2)}
            editor={activeEditor}
            disabled={!isEditable}

Here's a playground link for this PR with some content that uses pt font sizes

yes

@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Jul 24, 2025

It looks like some tests are failing because the font-size is a floating point and the precision is different depending on the browser

@harshmetkel24
Copy link
Copy Markdown
Contributor Author

oh okay I got that. I am thinking to keep font-size in style in pt only. Just round off pt value to nearest px value in toolbar
This should work right?

@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Jul 25, 2025

Yes handling it in a different way in the toolbar will likely work, the parsing should be split into two functions so the toolbar code can just get the px value as a number

@harshmetkel24
Copy link
Copy Markdown
Contributor Author

Yes agree, I made changes for that

@etrepum
Copy link
Copy Markdown
Collaborator

etrepum commented Jul 25, 2025

The formatting check is failing npm run prettier:fix will typically resolve that

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

This isn't really how I would've split things up, there are three operations here:

  1. Parse a font-size as [fontSize, unit] for certain known units (pt, px)
  2. Normalize that [fontSize, unit] to a numeric px value (fontSizePx)
  3. Validate whether that fontSize falls in the range of allowed sizes

The parseFontSizeForToolbar function needs to do steps 1 and 2 and return the value from 2, but by calling parseAllowedFontSize it has to do all of the steps and then basically repeats steps 1 & 2.

The parseAllowedFontSize function needs to do all of the steps and return the input or an empty string if validation fails.

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

Looks good, nice work!

@etrepum etrepum added this pull request to the merge queue Jul 26, 2025
Merged via the queue into facebook:main with commit 914c239 Jul 26, 2025
39 checks passed
@etrepum etrepum mentioned this pull request Aug 7, 2025
GermanJablo added a commit to payloadcms/payload that referenced this pull request Sep 3, 2025
Fixes #13386

Below I write a clarification to copy and paste into the release note,
based on our latest upgrade of Lexical [in
v3.29.0](https://github.com/payloadcms/payload/releases/tag/v3.29.0).

## Important
This release upgrades the lexical dependency from 0.28.0 to 0.34.0.

If you installed lexical manually, update it to 0.34.0. Installing
lexical manually is not recommended, as it may break between updates,
and our re-exported versions should be used. See the [yellow banner
box](https://payloadcms.com/docs/rich-text/custom-features) for details.

If you still encounter richtext-lexical errors, do the following, in
this order:

- Delete node_modules
- Delete your lockfile (e.g. pnpm-lock.json)
- Reinstall your dependencies (e.g. pnpm install)

### Lexical Breaking Changes

The following Lexical releases describe breaking changes. We recommend
reading them if you're using Lexical APIs directly
(`@payloadcms/richtext-lexical/lexical/*`).

- [v.0.33.0](https://github.com/facebook/lexical/releases/tag/v0.33.0)
- [v.0.30.0](https://github.com/facebook/lexical/releases/tag/v0.30.0)
- [v.0.29.0](https://github.com/facebook/lexical/releases/tag/v0.29.0)

___

TODO:
- [x] facebook/lexical#7719
- [x] facebook/lexical#7362
- [x] facebook/lexical#7707
- [x] facebook/lexical#7388
- [x] facebook/lexical#7357
- [x] facebook/lexical#7352
- [x] facebook/lexical#7472
- [x] facebook/lexical#7556
- [x] facebook/lexical#7417
- [x] facebook/lexical#1036
- [x] facebook/lexical#7509
- [x] facebook/lexical#7693
- [x] facebook/lexical#7408
- [x] facebook/lexical#7450
- [x] facebook/lexical#7415
- [x] facebook/lexical#7368
- [x] facebook/lexical#7372
- [x] facebook/lexical#7572
- [x] facebook/lexical#7558
- [x] facebook/lexical#7613
- [x] facebook/lexical#7405
- [x] facebook/lexical#7420
- [x] facebook/lexical#7662

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1211202581885926
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: when content from google docs or microsoft word, font-size is lost

4 participants