Skip to content

[lexical][lexical-website] Feature: Document and export common update tags#7441

Merged
etrepum merged 7 commits intofacebook:mainfrom
bgwebagency:feat/5992-document-export-update-tags
Apr 9, 2025
Merged

[lexical][lexical-website] Feature: Document and export common update tags#7441
etrepum merged 7 commits intofacebook:mainfrom
bgwebagency:feat/5992-document-export-update-tags

Conversation

@kirandash
Copy link
Copy Markdown
Contributor

Description

Current behavior:

  • Update tags are used internally but not exported or documented
  • No validation for typos in tag names
  • No comprehensive documentation about tag behavior and lifecycle
  • No test coverage for update tag functionality

Changes being added:

  • Create LexicalUpdateTags.ts to export common update tags as constants
  • Add tag validation to $addUpdateTag to catch typos
  • Add comprehensive documentation in updates.md about:
    • Tag usage and examples
    • Tag lifecycle (clearing after updates)
    • Common update tags and their purposes
    • Tag validation
  • Add test suite covering all update tag functionality

Closes #5992

Test plan

Before

  • No validation for incorrect tag names
  • No clear documentation about tag behavior
  • Tags were internal implementation details
  • No dedicated test file for update tags

After

Added comprehensive test suite in LexicalUpdateTags.test.ts that verifies:

  • All exported tags are in KNOWN_UPDATE_TAGS
  • $addUpdateTag and $hasUpdateTag functionality
  • Multiple tag support
  • Tag validation warnings
  • Tag lifecycle (clearing after updates)

All tests are passing and validate the documented behavior.

- Create LexicalUpdateTags.ts to export common update tags
- Add tag validation to  in LexicalUtils.ts
- Export update tags in main index.ts
- Add comprehensive test suite in LexicalUpdateTags.test.ts
- Create updates.md with detailed documentation

The changes improve developer experience by:
- Providing type-safe exports for common update tags
- Adding validation to catch typos in tag names
- Adding clear documentation and examples
- Adding tests to verify behavior

Fixes facebook#5992
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 7, 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 Apr 9, 2025 2:42am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 9, 2025 2:42am

@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 Apr 7, 2025
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 7, 2025
@@ -0,0 +1,86 @@
# Updates

Updates in Lexical are the way you can mutate the editor state. All updates are run asynchronously and batched by default. This means that if you have multiple updates back-to-back, they'll be batched together and run in a single update. This is done for performance reasons, as it means we can avoid unnecessary re-renders and DOM updates.
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.

This isn't accurate, updates are synchronous (except in nested update scenarios which should be deprecated) but the reconciliation (DOM updates) is batched for performance

});
```

Note: Update tags are cleared after each update. If you need to check for tags, make sure to do it within the same update callback where they were added.
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.

Typically update tags are checked in update and mutation listeners with the tags and updateTags properties in their respective payloads (as demonstrated below), it's less common to read update tags from inside the same update batch

```js
editor.update(() => {
// Will warn if "unknown-tag" is not a known tag
$addUpdateTag('unknown-tag', true);
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.

I think a better approach here would be to recommend that people use the exported constants rather than string literals to avoid typos (e.g. HISTORIC_TAG instead of 'historic').

Comment on lines +55 to +67
/**
* A set of all known update tags for validation purposes
*/
export const KNOWN_UPDATE_TAGS = new Set([
HISTORIC_TAG,
HISTORY_PUSH_TAG,
HISTORY_MERGE_TAG,
PASTE_TAG,
COLLABORATION_TAG,
SKIP_COLLAB_TAG,
SKIP_SCROLL_INTO_VIEW_TAG,
SKIP_DOM_SELECTION_TAG,
]);
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.

I don't think we need this

Comment on lines +302 to +310
COLLABORATION_TAG,
HISTORIC_TAG,
HISTORY_MERGE_TAG,
HISTORY_PUSH_TAG,
KNOWN_UPDATE_TAGS,
PASTE_TAG,
SKIP_COLLAB_TAG,
SKIP_DOM_SELECTION_TAG,
SKIP_SCROLL_INTO_VIEW_TAG,
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.

This is the useful part for preventing typos

kirandash and others added 4 commits April 8, 2025 12:58
Updates in Lexical are synchronous operations (except in nested update
scenarios which should be deprecated). Only the DOM reconciliation
process is batched for performance optimization.

Co-authored-by: Bob Ippolito <bob@ippoli.to>
Update the documentation to clarify that update tags are typically
accessed through listener payloads (tags and updateTags properties)
rather than within the same update batch. Add examples demonstrating
the recommended usage patterns in update and mutation listeners.

Co-authored-by: Bob Ippolito <bob@ippoli.to>
Replace string literals with exported constants (e.g. HISTORIC_TAG
instead of 'historic') in all documentation examples to promote type
safety and prevent typos. Add imports from lexical package and update
guidance to recommend using constants for both built-in and custom tags.

Co-authored-by: Bob Ippolito <bob@ippoli.to>
… constants

- Remove KNOWN_UPDATE_TAGS Set and runtime validation from LexicalUpdateTags.ts
- Simplify  by removing validateTag parameter from LexicalUtils.ts
- Update documentation in updates.md to emphasize using exported constants
- Remove validation-related exports from index.ts
- Update tests to use exported tag constants instead of validation

This change moves away from runtime validation of update tags in favor of
TypeScript's type system and exported constants, providing better type safety
and developer experience.

Co-authored-by: Bob Ippolito <bob@ippoli.to>
Restore the errorOnReadOnly() check that was accidentally removed while
removing the runtime tag validation. This check is important to prevent
update tag modifications in read-only mode.
@kirandash
Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @etrepum! I apologize for the confusion in the documentation. I had misunderstood how updates worked, incorrectly assuming they were asynchronous. Thanks for the clarification!

I've addressed all your feedback:

  1. Fixed the documentation about updates being synchronous, with only DOM reconciliation being batched
  2. Improved documentation for update tag usage patterns
  3. Use exported constants for update tag examples
  4. Removed the KNOWN_UPDATE_TAGS Set and runtime validation as suggested

Please take another look when you have a chance. Let me know if anything needs further adjustment!

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.

I think this looks great other than the missing sidebar reference. Perhaps we should create a follow-up issue to have the existing code use these new constants to match the recommendation from the new docs?

@@ -0,0 +1,132 @@
# Updates
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.

This new file needs to be referenced in packages/lexical-website/sidebars.js so you can find it in the documentation. I think this is also why the preview isn't rendered with a sidebar

new page:
https://lexical-git-fork-bgwebagency-feat-5992-docu-7750e0-fbopensource.vercel.app/docs/concepts/updates

existing page in that section:
https://lexical-git-fork-bgwebagency-feat-5992-docu-7750e0-fbopensource.vercel.app/docs/concepts/nodes

Add 'concepts/updates' to the Concepts section in sidebars.js to make
the updates documentation discoverable in the navigation.

Co-authored-by: Bob Ippolito <bob@ippoli.to>
@kirandash
Copy link
Copy Markdown
Contributor Author

I think this looks great other than the missing sidebar reference. Perhaps we should create a follow-up issue to have the existing code use these new constants to match the recommendation from the new docs?

Thanks for the review! I've fixed the missing sidebar reference and created a follow-up issue to migrate the existing code to use the new constants at #7446.

Let me know if there's anything else you'd like me to change or improve in this PR!

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 great!

@etrepum etrepum added this pull request to the merge queue Apr 9, 2025
Merged via the queue into facebook:main with commit cb74b8c Apr 9, 2025
39 checks passed
fantactuka pushed a commit that referenced this pull request Aug 11, 2025
… tags (#7441)

Co-authored-by: Bob Ippolito <bob@ippoli.to>
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.

Feature: document and export common/known update tags

3 participants