|
|
|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
title: Lexical Rich Text
|
|
|
|
|
label: Lexical
|
|
|
|
|
order: 30
|
|
|
|
|
desc: Built by Meta, Lexical is an incredibly powerful rich text editor and it works beautifully within Payload.
|
|
|
|
|
desc: Built by Meta, Lexical is an incredibly powerful rich text editor, and it works beautifully within Payload.
|
|
|
|
|
keywords: lexical, rich text, editor, headless cms
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
@@ -45,7 +45,39 @@ export default buildConfig({
|
|
|
|
|
You can also override Lexical settings on a field-by-field basis as follows:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { CollectionConfig } from 'payload/types'
|
|
|
|
|
import type { CollectionConfig } from 'payload/types'
|
|
|
|
|
import {
|
|
|
|
|
lexicalEditor
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
export const Pages: CollectionConfig = {
|
|
|
|
|
slug: 'pages',
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'content',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
// Pass the Lexical editor here and override base settings as necessary
|
|
|
|
|
editor: lexicalEditor({})
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Extending the lexical editor with Features
|
|
|
|
|
|
|
|
|
|
Lexical has been designed with extensibility in mind. Whether you're aiming to introduce new functionalities or tweak the existing ones, Lexical makes it seamless for you to bring those changes to life.
|
|
|
|
|
|
|
|
|
|
### Features: The Building Blocks
|
|
|
|
|
|
|
|
|
|
At the heart of Lexical's customization potential are "features". While Lexical ships with a set of default features we believe are essential for most use cases, the true power lies in your ability to redefine, expand, or prune these as needed.
|
|
|
|
|
|
|
|
|
|
If you remove all the default features, you're left with a blank editor. You can then add in only the features you need, or you can build your own custom features from scratch.
|
|
|
|
|
|
|
|
|
|
### Integrating New Features
|
|
|
|
|
|
|
|
|
|
To weave in your custom features, utilize the `features` prop when initializing the Lexical Editor. Here's a basic example of how this is done:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import {
|
|
|
|
|
BlocksFeature,
|
|
|
|
|
LinkFeature,
|
|
|
|
|
@@ -55,62 +87,508 @@ import {
|
|
|
|
|
import { Banner } from '../blocks/Banner'
|
|
|
|
|
import { CallToAction } from '../blocks/CallToAction'
|
|
|
|
|
|
|
|
|
|
export const Pages: CollectionConfig = {
|
|
|
|
|
{
|
|
|
|
|
editor: lexicalEditor({
|
|
|
|
|
features: ({ defaultFeatures }) => [
|
|
|
|
|
...defaultFeatures,
|
|
|
|
|
LinkFeature({
|
|
|
|
|
// Example showing how to customize the built-in fields
|
|
|
|
|
// of the Link feature
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'rel',
|
|
|
|
|
label: 'Rel Attribute',
|
|
|
|
|
type: 'select',
|
|
|
|
|
hasMany: true,
|
|
|
|
|
options: ['noopener', 'noreferrer', 'nofollow'],
|
|
|
|
|
admin: {
|
|
|
|
|
description:
|
|
|
|
|
'The rel attribute defines the relationship between a linked resource and the current document. This is a custom link field.',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
UploadFeature({
|
|
|
|
|
collections: {
|
|
|
|
|
uploads: {
|
|
|
|
|
// Example showing how to customize the built-in fields
|
|
|
|
|
// of the Upload feature
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'caption',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
editor: lexicalEditor(),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
// This is incredibly powerful. You can re-use your Payload blocks
|
|
|
|
|
// directly in the Lexical editor as follows:
|
|
|
|
|
BlocksFeature({
|
|
|
|
|
blocks: [
|
|
|
|
|
Banner,
|
|
|
|
|
CallToAction,
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Features overview
|
|
|
|
|
|
|
|
|
|
Here's an overview of all the included features:
|
|
|
|
|
|
|
|
|
|
| Feature Name | Included by default | Description |
|
|
|
|
|
|--------------------------------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
|
|
|
| **`BoldTextFeature`** | Yes | Handles the bold text format |
|
|
|
|
|
| **`ItalicTextFeature`** | Yes | Handles the italic text format |
|
|
|
|
|
| **`UnderlineTextFeature`** | Yes | Handles the underline text format |
|
|
|
|
|
| **`StrikethroughTextFeature`** | Yes | Handles the strikethrough text format |
|
|
|
|
|
| **`SubscriptTextFeature`** | Yes | Handles the subscript text format |
|
|
|
|
|
| **`SuperscriptTextFeature`** | Yes | Handles the superscript text format |
|
|
|
|
|
| **`InlineCodeTextFeature`** | Yes | Handles the inline-code text format |
|
|
|
|
|
| **`ParagraphFeature`** | Yes | Handles paragraphs. Since they are already a key feature of lexical itself, this Feature mainly handles the Slash and Add-Block menu entries for paragraphs |
|
|
|
|
|
| **`HeadingFeature`** | Yes | Adds Heading Nodes (by default, H1 - H6, but that can be customized) |
|
|
|
|
|
| **`AlignFeature`** | Yes | Allows you to align text left, centered and right |
|
|
|
|
|
| **`IndentFeature`** | Yes | Allows you to indent text with the tab key |
|
|
|
|
|
| **`UnoderedListFeature`** | Yes | Adds unordered lists (ol) |
|
|
|
|
|
| **`OrderedListFeature`** | Yes | Adds ordered lists (ul) |
|
|
|
|
|
| **`CheckListFeature`** | Yes | Adds checklists |
|
|
|
|
|
| **`LinkFeature`** | Yes | Allows you to create internal and external links |
|
|
|
|
|
| **`RelationshipFeature`** | Yes | Allows you to create block-level (not inline) relationships to other documents |
|
|
|
|
|
| **`BlockQuoteFeature`** | Yes | Allows you to create block-level quotes |
|
|
|
|
|
| **`UploadFeature`** | Yes | Allows you to create block-level upload nodes - this supports all kinds of uploads, not just images |
|
|
|
|
|
| **`BlocksFeature`** | No | Allows you to use Payload's [Blocks Field](/docs/fields/blocks) directly inside your editor. In the feature props, you can specify the allowed blocks - just like in the Blocks field. |
|
|
|
|
|
| **`TreeViewFeature`** | No | Adds a debug box under the editor, which allows you to see the current editor state live, the dom, as well as time travel. Very useful for debugging |
|
|
|
|
|
|
|
|
|
|
## Creating your own, custom Feature
|
|
|
|
|
|
|
|
|
|
Creating your own custom feature requires deep knowledge of the Lexical editor. We recommend you take a look at the [Lexical documentation](https://lexical.dev/docs/intro) first - especially the "concepts" section.
|
|
|
|
|
|
|
|
|
|
Next, take a look at the [features we've already built](https://github.com/payloadcms/payload/tree/main/packages/richtext-lexical/src/field/features) - understanding how they work will help you understand how to create your own. There is no difference between the features included by default and the ones you create yourself - since those features are all isolated from the "core", you have access to the same APIs, whether the feature is part of payload or not!
|
|
|
|
|
|
|
|
|
|
## Converters
|
|
|
|
|
|
|
|
|
|
### Lexical => HTML
|
|
|
|
|
|
|
|
|
|
Lexical saves data in JSON, but can also generate its HTML representation via two main methods:
|
|
|
|
|
|
|
|
|
|
1. **Outputting HTML from the Collection:** Create a new field in your collection to convert saved JSON content to HTML. Payload generates and outputs the HTML for use in your frontend.
|
|
|
|
|
2. **Generating HTML on the Frontend:** Convert JSON to HTML on-demand, either in your frontend or elsewhere.
|
|
|
|
|
|
|
|
|
|
The editor comes with built-in HTML serializers, simplifying the process of converting JSON to HTML.
|
|
|
|
|
|
|
|
|
|
#### Outputting HTML from the Collection
|
|
|
|
|
|
|
|
|
|
To add HTML generation directly within the collection, follow the example below:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { CollectionConfig } from 'payload/types'
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
HTMLConverterFeature,
|
|
|
|
|
lexicalEditor,
|
|
|
|
|
lexicalHTML
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
const Pages: CollectionConfig = {
|
|
|
|
|
slug: 'pages',
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'content',
|
|
|
|
|
name: 'nameOfYourRichTextField',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
// Pass the Lexical editor here and override base settings as necessary
|
|
|
|
|
editor: lexicalEditor({
|
|
|
|
|
features: ({ defaultFeatures }) => [
|
|
|
|
|
...defaultFeatures,
|
|
|
|
|
LinkFeature({
|
|
|
|
|
// Example showing how to customize the built-in fields
|
|
|
|
|
// of the Link feature
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'rel',
|
|
|
|
|
label: 'Rel Attribute',
|
|
|
|
|
type: 'select',
|
|
|
|
|
hasMany: true,
|
|
|
|
|
options: ['noopener', 'noreferrer', 'nofollow'],
|
|
|
|
|
admin: {
|
|
|
|
|
description:
|
|
|
|
|
'The rel attribute defines the relationship between a linked resource and the current document. This is a custom link field.',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
UploadFeature({
|
|
|
|
|
collections: {
|
|
|
|
|
uploads: {
|
|
|
|
|
// Example showing how to customize the built-in fields
|
|
|
|
|
// of the Upload feature
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'caption',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
editor: lexicalEditor(),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
// This is incredibly powerful. You can re-use your Payload blocks
|
|
|
|
|
// directly in the Lexical editor as follows:
|
|
|
|
|
BlocksFeature({
|
|
|
|
|
blocks: [
|
|
|
|
|
Banner,
|
|
|
|
|
CallToAction,
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
// The HTMLConverter Feature is the feature which manages the HTML serializers. If you do not pass any arguments to it, it will use the default serializers.
|
|
|
|
|
HTMLConverterFeature({}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
lexicalHTML('nameOfYourRichTextField', { name: 'nameOfYourRichTextField_html' }),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
The `lexicalHTML()` function creates a new field that automatically converts the referenced lexical richText field into HTML through an afterRead hook.
|
|
|
|
|
|
|
|
|
|
#### Generating HTML in the Frontend:
|
|
|
|
|
|
|
|
|
|
If you wish to convert JSON to HTML ad-hoc, use this code snippet:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { SerializedEditorState } from 'lexical'
|
|
|
|
|
import {
|
|
|
|
|
type SanitizedEditorConfig,
|
|
|
|
|
convertLexicalToHTML,
|
|
|
|
|
consolidateHTMLConverters,
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
async function lexicalToHTML(editorData: SerializedEditorState, editorConfig: SanitizedEditorConfig) {
|
|
|
|
|
return await convertLexicalToHTML({
|
|
|
|
|
converters: consolidateHTMLConverters({ editorConfig }),
|
|
|
|
|
data: editorData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
This method employs `convertLexicalToHTML` from `@payloadcms/richtext-lexical`, which converts the serialized editor state into HTML.
|
|
|
|
|
|
|
|
|
|
Because every `Feature` is able to provide html converters, and because the `htmlFeature` can modify those or provide their own, we need to consolidate them with the default html Converters using the `consolidateHTMLConverters` function.
|
|
|
|
|
|
|
|
|
|
#### Creating your own HTML Converter
|
|
|
|
|
|
|
|
|
|
HTML Converters are typed as `HTMLConverter`, which contains the node type it should handle, and a function that accepts the serialized node from the lexical editor, and outputs the HTML string. Here's the HTML Converter of the Upload node as an example:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { HTMLConverter } from '@payloadcms/richtext-lexical'
|
|
|
|
|
import payload from 'payload'
|
|
|
|
|
|
|
|
|
|
const UploadHTMLConverter: HTMLConverter<SerializedUploadNode> = {
|
|
|
|
|
converter: async ({ node }) => {
|
|
|
|
|
const uploadDocument = await payload.findByID({
|
|
|
|
|
id: node.value.id,
|
|
|
|
|
collection: node.relationTo,
|
|
|
|
|
})
|
|
|
|
|
const url = (payload?.config?.serverURL || '') + uploadDocument?.url
|
|
|
|
|
|
|
|
|
|
if (!(uploadDocument?.mimeType as string)?.startsWith('image')) {
|
|
|
|
|
// Only images can be serialized as HTML
|
|
|
|
|
return ``
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `<img src="${url}" alt="${uploadDocument?.filename}" width="${uploadDocument?.width}" height="${uploadDocument?.height}"/>`
|
|
|
|
|
},
|
|
|
|
|
nodeTypes: [UploadNode.getType()], // This is the type of the lexical node that this converter can handle. Instead of hardcoding 'upload' we can get the node type directly from the UploadNode, since it's static.
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
As you can see, we have access to all the information saved in the node (for the Upload node, this is `value`and `relationTo`) and we can use that to generate the HTML.
|
|
|
|
|
|
|
|
|
|
The `convertLexicalToHTML` is part of `@payloadcms/richtext-lexical` automatically handles traversing the editor state and calling the correct converter for each node.
|
|
|
|
|
|
|
|
|
|
#### Embedding the HTML Converter in your Feature
|
|
|
|
|
|
|
|
|
|
You can embed your HTML Converter directly within your custom `Feature`, allowing it to be handled automatically by the `consolidateHTMLConverters` function. Here is an example:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
export const UploadFeature = (props?: UploadFeatureProps): FeatureProvider => {
|
|
|
|
|
return {
|
|
|
|
|
feature: () => {
|
|
|
|
|
return {
|
|
|
|
|
nodes: [
|
|
|
|
|
{
|
|
|
|
|
converters: {
|
|
|
|
|
html: yourHTMLConverter, // <= This is where you define your HTML Converter
|
|
|
|
|
},
|
|
|
|
|
node: UploadNode,
|
|
|
|
|
type: UploadNode.getType(),
|
|
|
|
|
//...
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
plugins: [/*...*/],
|
|
|
|
|
props: props,
|
|
|
|
|
slashMenu: {/*...*/},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
key: 'upload',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Headless Editor
|
|
|
|
|
|
|
|
|
|
Lexical provides a seamless way to perform conversions between various other formats:
|
|
|
|
|
- HTML to Lexical (or, importing HTML into the lexical editor)
|
|
|
|
|
- Markdown to Lexical (or, importing Markdown into the lexical editor)
|
|
|
|
|
- Lexical to Markdown
|
|
|
|
|
|
|
|
|
|
A headless editor can perform such conversions outside of the main editor instance. Follow this method to initiate a headless editor:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { createHeadlessEditor } from '@lexical/headless' // <= make sure this package is installed
|
|
|
|
|
import {
|
|
|
|
|
getEnabledNodes,
|
|
|
|
|
sanitizeEditorConfig,
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
const yourEditorConfig; // <= your editor config here
|
|
|
|
|
|
|
|
|
|
const headlessEditor = await createHeadlessEditor({
|
|
|
|
|
nodes: getEnabledNodes({
|
|
|
|
|
editorConfig: sanitizeEditorConfig(yourEditorConfig),
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### HTML => Lexical
|
|
|
|
|
|
|
|
|
|
Once you have your headless editor instance, you can use it to convert HTML to Lexical:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { $generateNodesFromDOM } from '@lexical/html'
|
|
|
|
|
import { $getRoot,$getSelection } from 'lexical'
|
|
|
|
|
|
|
|
|
|
headlessEditor.update(() => {
|
|
|
|
|
// In a headless environment you can use a package such as JSDom to parse the HTML string.
|
|
|
|
|
const dom = new JSDOM(htmlString)
|
|
|
|
|
|
|
|
|
|
// Once you have the DOM instance it's easy to generate LexicalNodes.
|
|
|
|
|
const nodes = $generateNodesFromDOM(editor, dom.window.document)
|
|
|
|
|
|
|
|
|
|
// Select the root
|
|
|
|
|
$getRoot().select()
|
|
|
|
|
|
|
|
|
|
// Insert them at a selection.
|
|
|
|
|
const selection = $getSelection()
|
|
|
|
|
selection.insertNodes(nodes)
|
|
|
|
|
}, { discrete: true })
|
|
|
|
|
|
|
|
|
|
// Do this if you then want to get the editor JSON
|
|
|
|
|
const editorJSON = headlessEditor.getEditorState().toJSON()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This has been taken from the [lexical serialization & deserialization docs](https://lexical.dev/docs/concepts/serialization#html---lexical).
|
|
|
|
|
|
|
|
|
|
<Banner type="success">
|
|
|
|
|
<strong>Note:</strong>
|
|
|
|
|
<br />
|
|
|
|
|
Using the <code>discrete: true</code> flag ensures instant updates to the editor state. If immediate reading of the updated state isn't necessary, you can omit the flag.
|
|
|
|
|
</Banner>
|
|
|
|
|
|
|
|
|
|
### Markdown => Lexical
|
|
|
|
|
|
|
|
|
|
Convert markdown content to the Lexical editor format with the following:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { $convertFromMarkdownString } from '@lexical/markdown'
|
|
|
|
|
import { sanitizeEditorConfig } from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
const yourSanitizedEditorConfig = sanitizeEditorConfig(yourEditorConfig) // <= your editor config here
|
|
|
|
|
const markdown = `# Hello World`
|
|
|
|
|
|
|
|
|
|
headlessEditor.update(() => { $convertFromMarkdownString(markdown, yourSanitizedEditorConfig.features.markdownTransformers) }, { discrete: true })
|
|
|
|
|
|
|
|
|
|
// Do this if you then want to get the editor JSON
|
|
|
|
|
const editorJSON = headlessEditor.getEditorState().toJSON()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Lexical => Markdown
|
|
|
|
|
|
|
|
|
|
Export content from the Lexical editor into Markdown format using these steps:
|
|
|
|
|
|
|
|
|
|
1. Import your current editor state into the headless editor.
|
|
|
|
|
2. Convert and fetch the resulting markdown string.
|
|
|
|
|
|
|
|
|
|
Here's the code for it:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { $convertToMarkdownString } from '@lexical/markdown'
|
|
|
|
|
import { sanitizeEditorConfig } from '@payloadcms/richtext-lexical'
|
|
|
|
|
import type { SerializedEditorState } from "lexical"
|
|
|
|
|
|
|
|
|
|
const yourSanitizedEditorConfig = sanitizeEditorConfig(yourEditorConfig) // <= your editor config here
|
|
|
|
|
const yourEditorState: SerializedEditorState // <= your current editor state here
|
|
|
|
|
|
|
|
|
|
// Import editor state into your headless editor
|
|
|
|
|
try {
|
|
|
|
|
headlessEditor.setEditorState(headlessEditor.parseEditorState(yourEditorState)) // This should commit the editor state immediately
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error({ err: e }, 'ERROR parsing editor state')
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export to markdown
|
|
|
|
|
let markdown: string
|
|
|
|
|
headlessEditor.getEditorState().read(() => {
|
|
|
|
|
markdown = $convertToMarkdownString(yourSanitizedEditorConfig?.features?.markdownTransformers)
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `.setEditorState()` function immediately updates your editor state. Thus, there's no need for the `discrete: true` flag when reading the state afterward.
|
|
|
|
|
|
|
|
|
|
## Migrating from Slate
|
|
|
|
|
|
|
|
|
|
While both Slate and Lexical save the editor state in JSON, the structure of the JSON is different.
|
|
|
|
|
|
|
|
|
|
### Migration via SlateToLexicalFeature
|
|
|
|
|
|
|
|
|
|
One way to handle this is to just give your lexical editor the ability to read the slate JSON.
|
|
|
|
|
|
|
|
|
|
Simply add the `SlateToLexicalFeature` to your editor:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { CollectionConfig } from 'payload/types'
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
SlateToLexicalFeature,
|
|
|
|
|
lexicalEditor,
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
const Pages: CollectionConfig = {
|
|
|
|
|
slug: 'pages',
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'nameOfYourRichTextField',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
editor: lexicalEditor({
|
|
|
|
|
features: ({ defaultFeatures }) => [
|
|
|
|
|
...defaultFeatures,
|
|
|
|
|
SlateToLexicalFeature({})
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
and done! Now, everytime this lexical editor is initialized, it converts the slate date to lexical on-the-fly. If the data is already in lexical format, it will just pass it through.
|
|
|
|
|
|
|
|
|
|
This is by far the easiest way to migrate from Slate to Lexical, although it does come with a few caveats:
|
|
|
|
|
- There is a performance hit when initializing the lexical editor
|
|
|
|
|
- The editor will still output the Slate data in the output JSON, as the on-the-fly converter only runs for the admin panel
|
|
|
|
|
|
|
|
|
|
The easy way to solve this: Just save the document! This overrides the slate data with the lexical data, and the next time the document is loaded, the lexical data will be used. This solves both the performance and the output issue for that specific document.
|
|
|
|
|
|
|
|
|
|
### Migration via migration script
|
|
|
|
|
|
|
|
|
|
The method described above does not solve the issue for all documents, though. If you want to convert all your documents to lexical, you can use a migration script. Here's a simple example:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
|
import type { YourDocumentType } from 'payload/generated-types'
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
cloneDeep,
|
|
|
|
|
convertSlateToLexical,
|
|
|
|
|
defaultSlateConverters,
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
import { AnotherCustomConverter } from './lexicalFeatures/converters/AnotherCustomConverter'
|
|
|
|
|
|
|
|
|
|
export async function convertAll(payload: Payload, collectionName: string, fieldName: string) {
|
|
|
|
|
const docs: YourDocumentType[] = await payload.db.collections[collectionName].find({}).exec() // Use MongoDB models directly to query all documents at once
|
|
|
|
|
console.log(`Found ${docs.length} ${collectionName} docs`)
|
|
|
|
|
|
|
|
|
|
const converters = cloneDeep([...defaultSlateConverters, AnotherCustomConverter])
|
|
|
|
|
|
|
|
|
|
// Split docs into batches of 20.
|
|
|
|
|
const batchSize = 20
|
|
|
|
|
const batches = []
|
|
|
|
|
for (let i = 0; i < docs.length; i += batchSize) {
|
|
|
|
|
batches.push(docs.slice(i, i + batchSize))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let processed = 0 // Number of processed docs
|
|
|
|
|
|
|
|
|
|
for (const batch of batches) {
|
|
|
|
|
// Process each batch asynchronously
|
|
|
|
|
const promises = batch.map(async (doc: YourDocumentType) => {
|
|
|
|
|
const richText = doc[fieldName]
|
|
|
|
|
|
|
|
|
|
if (richText && Array.isArray(richText) && !('root' in richText)) { // It's Slate data - skip already-converted data
|
|
|
|
|
const converted = convertSlateToLexical({
|
|
|
|
|
converters: converters,
|
|
|
|
|
slateData: richText,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await payload.update({
|
|
|
|
|
id: doc.id,
|
|
|
|
|
collection: collectionName as any,
|
|
|
|
|
data: {
|
|
|
|
|
[fieldName]: converted,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Wait for all promises in the batch to complete. Resolving batches of 20 asynchronously is faster than waiting for each doc to update individually
|
|
|
|
|
await Promise.all(promises)
|
|
|
|
|
|
|
|
|
|
// Update the count of processed docs
|
|
|
|
|
processed += batch.length
|
|
|
|
|
console.log(`Converted ${processed} of ${docs.length}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `convertSlateToLexical` is the same method used in the `SlateToLexicalFeature` - it handles traversing the Slate JSON for you.
|
|
|
|
|
|
|
|
|
|
Do note that this script might require adjustment depending on your document structure, especially if you have nested richText fields or localization enabled.
|
|
|
|
|
|
|
|
|
|
### Converting custom Slate nodes
|
|
|
|
|
|
|
|
|
|
If you have custom Slate nodes, create a custom converter for them. Here's the Upload converter as an example:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { SerializedUploadNode } from '../uploadNode.'
|
|
|
|
|
import type { SlateNodeConverter } from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
export const SlateUploadConverter: SlateNodeConverter = {
|
|
|
|
|
converter({ slateNode }) {
|
|
|
|
|
return {
|
|
|
|
|
fields: {
|
|
|
|
|
...slateNode.fields,
|
|
|
|
|
},
|
|
|
|
|
format: '',
|
|
|
|
|
relationTo: slateNode.relationTo,
|
|
|
|
|
type: 'upload',
|
|
|
|
|
value: {
|
|
|
|
|
id: slateNode.value?.id || '',
|
|
|
|
|
},
|
|
|
|
|
version: 1,
|
|
|
|
|
} as const as SerializedUploadNode
|
|
|
|
|
},
|
|
|
|
|
nodeTypes: ['upload'],
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
It's pretty simple: You get a Slate node as input, and you return the lexical node. The `nodeTypes` array is used to determine which Slate nodes this converter can handle.
|
|
|
|
|
|
|
|
|
|
When using a migration script, you can add your custom converters to the `converters` property of the `convertSlateToLexical` props, as seen in the example above
|
|
|
|
|
|
|
|
|
|
When using the `SlateToLexicalFeature`, you can add your custom converters to the `converters` property of the `SlateToLexicalFeature` props:
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import type { CollectionConfig } from 'payload/types'
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
SlateToLexicalFeature,
|
|
|
|
|
lexicalEditor,
|
|
|
|
|
defaultSlateConverters
|
|
|
|
|
} from '@payloadcms/richtext-lexical'
|
|
|
|
|
|
|
|
|
|
import { YourCustomConverter } from '../converters/YourCustomConverter'
|
|
|
|
|
|
|
|
|
|
const Pages: CollectionConfig = {
|
|
|
|
|
slug: 'pages',
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'nameOfYourRichTextField',
|
|
|
|
|
type: 'richText',
|
|
|
|
|
editor: lexicalEditor({
|
|
|
|
|
features: ({ defaultFeatures }) => [
|
|
|
|
|
...defaultFeatures,
|
|
|
|
|
SlateToLexicalFeature({
|
|
|
|
|
converters: [
|
|
|
|
|
...defaultSlateConverters,
|
|
|
|
|
YourCustomConverter
|
|
|
|
|
]
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Migrating from payload-plugin-lexical
|
|
|
|
|
|
|
|
|
|
Migrating from [payload-plugin-lexical](https://github.com/AlessioGr/payload-plugin-lexical) works similar to migrating from Slate.
|
|
|
|
|
|
|
|
|
|
Instead of a `SlateToLexicalFeature` there is a `LexicalPluginToLexicalFeature` you can use. And instead of `convertSlateToLexical` you can use `convertLexicalPluginToLexical`.
|
|
|
|
|
|
|
|
|
|
## Coming Soon
|
|
|
|
|
|
|
|
|
|
Lots more documentation will be coming soon, which will show in detail how to create your own custom features within Lexical.
|
|
|
|
|
|
|
|
|
|
|