Skip to content

Commit d894ac7

Browse files
committed
fix(richtext-lexical): migrate scripts not working due to migration hooks running during migrate script
1 parent af0105c commit d894ac7

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

docs/lexical/migration.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ IMPORTANT: This will overwrite all slate data. We recommend doing the following
1818
1. Take a backup of your entire database. If anything goes wrong and you do not have a backup, you are on your own and will not receive any support.
1919
2. Make every richText field a lexical editor. This script will only convert lexical richText fields with old Slate data
2020
3. Add the SlateToLexicalFeature (as seen below) first, and test it out by loading up the Admin Panel, to see if the migrator works as expected. You might have to build some custom converters for some fields first in order to convert custom Slate nodes. The SlateToLexicalFeature is where the converters are stored. Only fields with this feature added will be migrated.
21+
4. If this works as expected, add the `disableHooks: true` prop everywhere you're initializing `SlateToLexicalFeature`. Example: `SlateToLexicalFeature({ disableHooks: true })`. Once you did that, you're ready to run the migration script.
2122

2223
```ts
2324
import { migrateSlateToLexical } from '@payloadcms/richtext-lexical/migrate'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function convertLexicalPluginNodesToLexical({
4747
parentNodeType: string
4848
quiet?: boolean
4949
}): SerializedLexicalNode[] {
50-
if (!lexicalPluginNodes?.length) {
50+
if (!lexicalPluginNodes?.length || !converters?.length) {
5151
return []
5252
}
5353
const unknownConverter = converters.find((converter) => converter.nodeTypes.includes('unknown'))

packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/feature.server.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type LexicalPluginToLexicalFeatureProps = {
1313
defaultConverters: LexicalPluginNodeConverter[]
1414
}) => LexicalPluginNodeConverter[])
1515
| LexicalPluginNodeConverter[]
16+
disableHooks?: boolean
1617
quiet?: boolean
1718
}
1819

@@ -37,23 +38,25 @@ export const LexicalPluginToLexicalFeature =
3738

3839
return {
3940
ClientFeature: '@payloadcms/richtext-lexical/client#LexicalPluginToLexicalFeatureClient',
40-
hooks: {
41-
afterRead: [
42-
({ value }) => {
43-
if (!value || !('jsonContent' in value)) {
44-
// incomingEditorState null or not from Lexical Plugin
45-
return value
46-
}
41+
hooks: props.disableHooks
42+
? undefined
43+
: {
44+
afterRead: [
45+
({ value }) => {
46+
if (!value || !('jsonContent' in value)) {
47+
// incomingEditorState null or not from Lexical Plugin
48+
return value
49+
}
4750

48-
// Lexical Plugin => convert to lexical
49-
return convertLexicalPluginToLexical({
50-
converters: props.converters as LexicalPluginNodeConverter[],
51-
lexicalPluginData: value as PayloadPluginLexicalData,
52-
quiet: props?.quiet,
53-
})
51+
// Lexical Plugin => convert to lexical
52+
return convertLexicalPluginToLexical({
53+
converters: props.converters as LexicalPluginNodeConverter[],
54+
lexicalPluginData: value as PayloadPluginLexicalData,
55+
quiet: props?.quiet,
56+
})
57+
},
58+
],
5459
},
55-
],
56-
},
5760
nodes: [
5861
{
5962
node: UnknownConvertedNode,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function convertSlateNodesToLexical({
4747
parentNodeType: string
4848
slateNodes: SlateNode[]
4949
}): SerializedLexicalNode[] {
50-
if (!converters?.length) {
50+
if (!converters?.length || !slateNodes?.length) {
5151
return []
5252
}
5353
const unknownConverter = converters.find((converter) => converter.nodeTypes.includes('unknown'))

packages/richtext-lexical/src/features/migrations/slateToLexical/feature.server.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type SlateToLexicalFeatureProps = {
99
converters?:
1010
| (({ defaultConverters }: { defaultConverters: SlateNodeConverter[] }) => SlateNodeConverter[])
1111
| SlateNodeConverter[]
12+
disableHooks?: boolean
1213
}
1314

1415
export const SlateToLexicalFeature = createServerFeature<
@@ -35,22 +36,24 @@ export const SlateToLexicalFeature = createServerFeature<
3536

3637
return {
3738
ClientFeature: '@payloadcms/richtext-lexical/client#SlateToLexicalFeatureClient',
38-
hooks: {
39-
afterRead: [
40-
({ value }) => {
41-
if (!value || !Array.isArray(value) || 'root' in value) {
42-
// incomingEditorState null or not from Slate
43-
return value
44-
}
39+
hooks: props.disableHooks
40+
? undefined
41+
: {
42+
afterRead: [
43+
({ value }) => {
44+
if (!value || !Array.isArray(value) || 'root' in value) {
45+
// incomingEditorState null or not from Slate
46+
return value
47+
}
4548

46-
// Slate => convert to lexical
47-
return convertSlateToLexical({
48-
converters: props.converters as SlateNodeConverter[],
49-
slateData: value,
50-
})
49+
// Slate => convert to lexical
50+
return convertSlateToLexical({
51+
converters: props.converters as SlateNodeConverter[],
52+
slateData: value,
53+
})
54+
},
55+
],
5156
},
52-
],
53-
},
5457
nodes: [
5558
{
5659
node: UnknownConvertedNode,

0 commit comments

Comments
 (0)