Skip to content

Commit 75a3040

Browse files
authored
feat(richtext-lexical): export SerializedHeadingNode, add default node types (#6978)
1 parent 2daefb2 commit 75a3040

File tree

5 files changed

+94
-14
lines changed

5 files changed

+94
-14
lines changed

docs/lexical/overview.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ import type {
199199
SerializedTextNode,
200200
SerializedUploadNode,
201201
TypedEditorState,
202+
SerializedHeadingNode,
202203
} from '@payloadcms/richtext-lexical'
203204

204205
const editorState: TypedEditorState<
@@ -213,6 +214,7 @@ const editorState: TypedEditorState<
213214
| SerializedRelationshipNode
214215
| SerializedTextNode
215216
| SerializedUploadNode
217+
| SerializedHeadingNode
216218
> = {
217219
root: {
218220
type: 'root',
@@ -245,6 +247,47 @@ const editorState: TypedEditorState<
245247
}
246248
```
247249

250+
Alternatively, you can use the `DefaultTypedEditorState` type, which includes all types for all nodes included in the `defaultFeatures`:
251+
252+
```ts
253+
import type {
254+
DefaultTypedEditorState
255+
} from '@payloadcms/richtext-lexical'
256+
257+
const editorState: DefaultTypedEditorState = {
258+
root: {
259+
type: 'root',
260+
direction: 'ltr',
261+
format: '',
262+
indent: 0,
263+
version: 1,
264+
children: [
265+
{
266+
children: [
267+
{
268+
detail: 0,
269+
format: 0,
270+
mode: 'normal',
271+
style: '',
272+
text: 'Some text. Every property here is fully-typed',
273+
type: 'text',
274+
version: 1,
275+
},
276+
],
277+
direction: 'ltr',
278+
format: '',
279+
indent: 0,
280+
type: 'paragraph',
281+
textFormat: 0,
282+
version: 1,
283+
},
284+
],
285+
},
286+
}
287+
```
288+
289+
Just like `TypedEditorState`, the `DefaultTypedEditorState` also accepts an optional node type union as a generic. Here, this would **add** the specified node types to the default ones. Example: `DefaultTypedEditorState<SerializedBlockNode | YourCustomSerializedNode>`.
290+
248291
This is a type-safe representation of the editor state. Looking at the auto-suggestions of `type` it will show you all the possible node types you can use.
249292

250293
Make sure to only use types exported from `@payloadcms/richtext-lexical`, not from the lexical core packages. We only have control over types we export and can guarantee that those are correct, even though lexical core may export types with identical names.

packages/richtext-lexical/src/features/heading/feature.server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { HeadingTagType } from '@lexical/rich-text'
1+
import type {
2+
SerializedHeadingNode as _SerializedHeadingNode,
3+
HeadingTagType,
4+
} from '@lexical/rich-text'
5+
import type { Spread } from 'lexical'
26

37
import { HeadingNode } from '@lexical/rich-text'
48

@@ -10,6 +14,13 @@ import { createNode } from '../typeUtilities.js'
1014
import { i18n } from './i18n.js'
1115
import { MarkdownTransformer } from './markdownTransformer.js'
1216

17+
export type SerializedHeadingNode = Spread<
18+
{
19+
type: 'heading'
20+
},
21+
_SerializedHeadingNode
22+
>
23+
1324
export type HeadingFeatureProps = {
1425
enabledHeadingSizes?: HeadingTagType[]
1526
}

packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/converter/converters/heading/converter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SerializedHeadingNode } from '@lexical/rich-text'
2-
1+
import type { SerializedHeadingNode } from '../../../../../heading/feature.server.js'
32
import type { LexicalPluginNodeConverter } from '../../types.js'
43

54
import { convertLexicalPluginNodesToLexical } from '../../index.js'

packages/richtext-lexical/src/features/migrations/slateToLexical/converter/converters/heading/converter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SerializedHeadingNode } from '@lexical/rich-text'
2-
1+
import type { SerializedHeadingNode } from '../../../../../heading/feature.server.js'
32
import type { SlateNodeConverter } from '../../types.js'
43

54
import { convertSlateNodesToLexical } from '../../index.js'

packages/richtext-lexical/src/nodeTypes.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@ import type {
55
Spread,
66
TextModeType,
77
} from 'lexical'
8-
export type { SerializedQuoteNode } from './features/blockquote/feature.server.js'
9-
export type { SerializedBlockNode } from './features/blocks/nodes/BlocksNode.js'
10-
export type { SerializedHorizontalRuleNode } from './features/horizontalRule/nodes/HorizontalRuleNode.js'
118

12-
export type { SerializedAutoLinkNode, SerializedLinkNode } from './features/link/nodes/types.js'
9+
import type { SerializedQuoteNode } from './features/blockquote/feature.server.js'
10+
import type { SerializedBlockNode } from './features/blocks/nodes/BlocksNode.js'
11+
import type { SerializedHeadingNode } from './features/heading/feature.server.js'
12+
import type { SerializedHorizontalRuleNode } from './features/horizontalRule/nodes/HorizontalRuleNode.js'
13+
import type { SerializedAutoLinkNode, SerializedLinkNode } from './features/link/nodes/types.js'
14+
import type { SerializedListItemNode, SerializedListNode } from './features/lists/plugin/index.js'
15+
import type { SerializedRelationshipNode } from './features/relationship/nodes/RelationshipNode.js'
16+
import type { SerializedUploadNode } from './features/upload/nodes/UploadNode.js'
1317

14-
export type { SerializedListItemNode, SerializedListNode } from './features/lists/plugin/index.js'
15-
16-
export type { SerializedRelationshipNode } from './features/relationship/nodes/RelationshipNode.js'
17-
18-
export type { SerializedUploadNode } from './features/upload/nodes/UploadNode.js'
18+
export {
19+
SerializedAutoLinkNode,
20+
SerializedBlockNode,
21+
SerializedHeadingNode,
22+
SerializedHorizontalRuleNode,
23+
SerializedLinkNode,
24+
SerializedListItemNode,
25+
SerializedListNode,
26+
SerializedQuoteNode,
27+
SerializedRelationshipNode,
28+
SerializedUploadNode,
29+
}
1930

2031
export type SerializedParagraphNode<T extends SerializedLexicalNode = SerializedLexicalNode> =
2132
Spread<
@@ -46,3 +57,20 @@ type DecrementDepth<N extends number> = [0, 0, 1, 2, 3, 4][N]
4657

4758
export type TypedEditorState<T extends SerializedLexicalNode = SerializedLexicalNode> =
4859
SerializedEditorState<RecursiveNodes<T>>
60+
61+
export type DefaultNodeTypes =
62+
| SerializedAutoLinkNode
63+
| SerializedBlockNode
64+
| SerializedHeadingNode
65+
| SerializedHorizontalRuleNode
66+
| SerializedLinkNode
67+
| SerializedListItemNode
68+
| SerializedListNode
69+
| SerializedParagraphNode
70+
| SerializedQuoteNode
71+
| SerializedRelationshipNode
72+
| SerializedTextNode
73+
| SerializedUploadNode
74+
75+
export type DefaultTypedEditorState<T extends SerializedLexicalNode = SerializedLexicalNode> =
76+
TypedEditorState<DefaultNodeTypes | T>

0 commit comments

Comments
 (0)