Repository navigation
[lexical-website] Documentation Update: Document NodeState in $config JSON serialization - #8467
Conversation
… JSON serialization Since meta#7258 nodes can declare a `StateConfig` in `$config` with `flat: true` to lift NodeState values to the top level of the serialized node, but the NodeState and Serialization concept docs only described the `'$'`-nested layout. Users had to read the `nodes.mdx` `$config` example to learn that flat state preserves the legacy JSON shape, which is also the migration path away from a hand-written `__property` + `exportJSON`/`importJSON` node. This PR: - Adds a "Flat serialization with `$config`" subsection to `node-state.md` explaining when state appears at the top level vs. under `'$'`, with concrete JSON shapes for both, and notes the superclass-key-collision constraint. - Adds an "Upgrading a legacy JSON property to NodeState" recipe with before/after `ColoredNode` snippets, calling out that the on-the-wire JSON is unchanged when the state key name and default value match the old property. - Adds a parallel pointer at the top of the JSON section in `serialization.md` linking to both new subsections so users landing on the generic serialization page can find the `$config`-specific behavior. - Drops the now-stale "in the future ... lift those values to the top-level" sentence in the Serialization intro, which referenced meta#7260 (the very feature this PR documents as shipped). - Fixes an invalid trailing comma in the existing `'$'` JSON example. `pnpm -C packages/lexical-website run build` succeeds; the new anchors (`#flat-serialization-with-config`, `#upgrading-a-legacy-json-property-to-nodestate`) resolve cleanly under the existing `onBrokenAnchors: 'throw'` config. Closes meta#7781
- Replace inaccurate "throws at editor construction" claim for flat-key collisions with the actual silent-shadowing behavior (no runtime check; superclass field wins on serialize, value lifted into NodeState on parse). - Fix "second argument" -> "first argument" when describing the serialized key passed to createState (key is the first arg of createState(key, valueConfig)).
…n note The previous note asserted that the superclass field wins on serialize when a flat NodeState key collides with a superclass-serialized property. That's not generally true — TextNode's exportJSON spreads super last, so NodeState would actually overwrite the superclass field there. Reframe the note to describe the ambiguity without making a directional claim either way, since the precedence depends on the superclass's spread order.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| :::tip | ||
|
|
||
| To stage the migration without breaking existing payloads, keep the | ||
| serialized key name the same as the old property name (the first | ||
| argument to `createState`) and keep the default value identical to the | ||
| one your old `parse`/`updateFromJSON` produced for missing or invalid | ||
| input. Existing JSON will then deserialize unchanged. | ||
|
|
||
| ::: |
There was a problem hiding this comment.
The wording here is confusing, all that really matters is that the name used in the JSON representation is the same.
It's probably also worth mentioning that migrating from a not-flat NodeState to a flat NodeState also works. State that appears in the $ (NODE_STATE_KEY) will still be parsed even if the state is configured to be flat (but the flattened value takes precedence if they are both present).
There was a problem hiding this comment.
Tightened the wording in 250b903 and added the non-flat → flat migration note (with the flat-takes-precedence detail). Thanks for the pointer.
| on `TextNode`). There's no runtime check, but the resulting JSON shape is | ||
| ambiguous and not round-trip stable: which value wins on serialize depends | ||
| on the order the superclass's `exportJSON` spreads its own fields and | ||
| `super.exportJSON()`, and on parse the top-level value is lifted into | ||
| NodeState while the superclass's `updateFromJSON` may also reassign it to | ||
| the original property — leaving both representations populated and prone | ||
| to drift. |
There was a problem hiding this comment.
I don't think the documentation needs to go into this much detail about what happens when you break the rules, will be easier for people to understand "don't do this" rather than "don't do this [with lots of extra text to gloss over]"
There was a problem hiding this comment.
Trimmed the note in 250b903 — kept just the rule, dropped the failure-mode explanation.
…g docs Two changes per inline review on meta#8467: - Trim the flat-key collision note to the actionable rule ("don't do this") and drop the multi-clause explanation of the failure mode. - Tighten the migration tip to focus on what actually matters (keeping the JSON key name the same). Add the non-flat → flat migration note: state under `$` (NODE_STATE_KEY) still parses when configured flat, and the flat value takes precedence when both are present.
The previous claim that exportJSON/importJSON/updateFromJSON/clone/afterCloneFrom are "all generated from $config" was loose. Per LexicalUtils.ts the $config machinery only installs clone and importJSON; the others are inherited from LexicalNode/TextNode and already understand NodeState. Reframe as "no override needed" so the doc doesn't make a generation claim it can't back.
Summary
Addresses #7781:
### Flat serialization with $configsubsection innode-state.mdcovering how flat keys appear at the top level of a node's JSON and when each key is emitted vs. omitted (default-equality check).### Upgrading a legacy JSON property to NodeStaterecipe with aBefore/AfterColoredNode example, copied/adapted from the existingExtending TextNode with $configexample as suggested in the issue.serialization.mdJSON section linking readers to the NodeState content before they roll their ownexportJSON/importJSON.The
In the future ... see #7260paragraph is replaced — that issue shipped (it's what this PR documents). A trailing comma in an existing JSON code fence ("question": ...,) was removed in passing.Test plan
pnpm -C packages/lexical-website run buildsucceeds withonBrokenAnchors: 'throw'andonBrokenMarkdownLinks: 'throw'prettiercleanCloses #7781