Skip to content

Commit c15d679

Browse files
committed
fix(richtext-lexical)!: html converters not respecting overrideAccess property when populating values, in local API
1 parent a422a0d commit c15d679

File tree

14 files changed

+156
-27
lines changed

14 files changed

+156
-27
lines changed

packages/payload/src/fields/config/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { CSSProperties } from 'react'
77
import monacoeditor from 'monaco-editor' // IMPORTANT - DO NOT REMOVE: This is required for pnpm's default isolated mode to work - even though the import is not used. This is due to a typescript bug: https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189. (tsbugisolatedmode)
88
import type { JSONSchema4 } from 'json-schema'
99
import type React from 'react'
10-
import type { DeepPartial } from 'ts-essentials'
1110

1211
import type { RichTextAdapter, RichTextAdapterProvider } from '../../admin/RichText.js'
1312
import type { ErrorComponent } from '../../admin/forms/Error.js'
@@ -33,6 +32,10 @@ export type FieldHookArgs<TData extends TypeWithID = any, TValue = any, TSibling
3332
context: RequestContext
3433
/** The data passed to update the document within create and update operations, and the full document itself in the afterRead hook. */
3534
data?: Partial<TData>
35+
/**
36+
* Only available in the `afterRead` hook.
37+
*/
38+
draft?: boolean
3639
/** The field which the hook is running against. */
3740
field: FieldAffectingData
3841
/** Boolean to denote if this hook is running against finding one, or finding many within the afterRead hook. */
@@ -60,6 +63,10 @@ export type FieldHookArgs<TData extends TypeWithID = any, TValue = any, TSibling
6063
* The schemaPath of the field, e.g. ["group", "myArray", "textField"]. The schemaPath is the path but without indexes and would be used in the context of field schemas, not field data.
6164
*/
6265
schemaPath: string[]
66+
/**
67+
* Only available in the `afterRead` hook.
68+
*/
69+
showHiddenFields?: boolean
6370
/** The sibling data passed to a field that the hook is running against. */
6471
siblingData: Partial<TSiblingData>
6572
/**

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export const promise = async ({
205205
collection,
206206
context,
207207
data: doc,
208+
draft,
208209
field,
209210
findMany,
210211
global,
@@ -214,6 +215,7 @@ export const promise = async ({
214215
path: fieldPath,
215216
req,
216217
schemaPath: fieldSchemaPath,
218+
showHiddenFields,
217219
siblingData: siblingDoc,
218220
value,
219221
})
@@ -230,6 +232,7 @@ export const promise = async ({
230232
collection,
231233
context,
232234
data: doc,
235+
draft,
233236
field,
234237
findMany,
235238
global,
@@ -239,6 +242,7 @@ export const promise = async ({
239242
path: fieldPath,
240243
req,
241244
schemaPath: fieldSchemaPath,
245+
showHiddenFields,
242246
siblingData: siblingDoc,
243247
value: siblingDoc[field.name],
244248
})

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,26 @@ export const BlockquoteFeature = createServerFeature({
2828
createNode({
2929
converters: {
3030
html: {
31-
converter: async ({ converters, node, parent, req }) => {
31+
converter: async ({
32+
converters,
33+
draft,
34+
node,
35+
overrideAccess,
36+
parent,
37+
req,
38+
showHiddenFields,
39+
}) => {
3240
const childrenText = await convertLexicalNodesToHTML({
3341
converters,
42+
draft,
3443
lexicalNodes: node.children,
44+
overrideAccess,
3545
parent: {
3646
...node,
3747
parent,
3848
},
3949
req,
50+
showHiddenFields,
4051
})
4152

4253
return `<blockquote>${childrenText}</blockquote>`

packages/richtext-lexical/src/features/converters/html/converter/converters/paragraph.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import type { HTMLConverter } from '../types.js'
55
import { convertLexicalNodesToHTML } from '../index.js'
66

77
export const ParagraphHTMLConverter: HTMLConverter<SerializedParagraphNode> = {
8-
async converter({ converters, node, parent, req }) {
8+
async converter({ converters, draft, node, overrideAccess, parent, req, showHiddenFields }) {
99
const childrenText = await convertLexicalNodesToHTML({
1010
converters,
11+
draft,
1112
lexicalNodes: node.children,
13+
overrideAccess,
1214
parent: {
1315
...node,
1416
parent,
1517
},
1618
req,
19+
showHiddenFields,
1720
})
1821
return `<p>${childrenText}</p>`
1922
},

packages/richtext-lexical/src/features/converters/html/converter/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import type { HTMLConverter, SerializedLexicalNodeWithParent } from './types.js'
88
export type ConvertLexicalToHTMLArgs = {
99
converters: HTMLConverter[]
1010
data: SerializedEditorState
11+
draft?: boolean // default false
12+
overrideAccess?: boolean // default false
13+
showHiddenFields?: boolean // default false
1114
} & (
1215
| {
1316
/**
@@ -40,8 +43,11 @@ export type ConvertLexicalToHTMLArgs = {
4043
export async function convertLexicalToHTML({
4144
converters,
4245
data,
46+
draft,
47+
overrideAccess,
4348
payload,
4449
req,
50+
showHiddenFields,
4551
}: ConvertLexicalToHTMLArgs): Promise<string> {
4652
if (data?.root?.children?.length) {
4753
if (req === undefined && payload) {
@@ -50,27 +56,36 @@ export async function convertLexicalToHTML({
5056

5157
return await convertLexicalNodesToHTML({
5258
converters,
59+
draft: draft === undefined ? false : draft,
5360
lexicalNodes: data?.root?.children,
61+
overrideAccess: overrideAccess === undefined ? false : overrideAccess,
5462
parent: data?.root,
5563
req,
64+
showHiddenFields: showHiddenFields === undefined ? false : showHiddenFields,
5665
})
5766
}
5867
return ''
5968
}
6069

6170
export async function convertLexicalNodesToHTML({
6271
converters,
72+
draft,
6373
lexicalNodes,
74+
overrideAccess,
6475
parent,
6576
req,
77+
showHiddenFields,
6678
}: {
6779
converters: HTMLConverter[]
80+
draft: boolean
6881
lexicalNodes: SerializedLexicalNode[]
82+
overrideAccess: boolean
6983
parent: SerializedLexicalNodeWithParent
7084
/**
7185
* When the converter is called, req CAN be passed in depending on where it's run.
7286
*/
7387
req: PayloadRequest | null
88+
showHiddenFields: boolean
7489
}): Promise<string> {
7590
const unknownConverter = converters.find((converter) => converter.nodeTypes.includes('unknown'))
7691

@@ -85,19 +100,25 @@ export async function convertLexicalNodesToHTML({
85100
return await unknownConverter.converter({
86101
childIndex: i,
87102
converters,
103+
draft,
88104
node,
105+
overrideAccess,
89106
parent,
90107
req,
108+
showHiddenFields,
91109
})
92110
}
93111
return '<span>unknown node</span>'
94112
}
95113
return await converterForNode.converter({
96114
childIndex: i,
97115
converters,
116+
draft,
98117
node,
118+
overrideAccess,
99119
parent,
100120
req,
121+
showHiddenFields,
101122
})
102123
} catch (error) {
103124
console.error('Error converting lexical node to HTML:', error, 'node:', node)

packages/richtext-lexical/src/features/converters/html/converter/types.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import type { SerializedLexicalNode } from 'lexical'
2-
import type { Payload, PayloadRequest } from 'payload'
2+
import type { PayloadRequest } from 'payload'
33

44
export type HTMLConverter<T extends SerializedLexicalNode = SerializedLexicalNode> = {
5-
converter: ({
6-
childIndex,
7-
converters,
8-
node,
9-
parent,
10-
req,
11-
}: {
5+
converter: (args: {
126
childIndex: number
13-
converters: HTMLConverter[]
7+
converters: HTMLConverter<any>[]
8+
draft: boolean
149
node: T
10+
overrideAccess: boolean
1511
parent: SerializedLexicalNodeWithParent
1612
/**
1713
* When the converter is called, req CAN be passed in depending on where it's run.
1814
*/
1915
req: PayloadRequest | null
16+
showHiddenFields: boolean
2017
}) => Promise<string> | string
2118
nodeTypes: string[]
2219
}

packages/richtext-lexical/src/features/converters/html/feature.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { createServerFeature } from '../../../utilities/createServerFeature.js'
44

55
export type HTMLConverterFeatureProps = {
66
converters?:
7-
| (({ defaultConverters }: { defaultConverters: HTMLConverter[] }) => HTMLConverter[])
8-
| HTMLConverter[]
7+
| (({ defaultConverters }: { defaultConverters: HTMLConverter<any>[] }) => HTMLConverter<any>[])
8+
| HTMLConverter<any>[]
99
}
1010

1111
// This is just used to save the props on the richText field

packages/richtext-lexical/src/features/converters/html/field/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,16 @@ export const lexicalHTML: (
160160
},
161161
hooks: {
162162
afterRead: [
163-
async ({ collection, field, global, req, siblingData }) => {
163+
async ({
164+
collection,
165+
draft,
166+
field,
167+
global,
168+
overrideAccess,
169+
req,
170+
showHiddenFields,
171+
siblingData,
172+
}) => {
164173
const fields = collection ? collection.fields : global.fields
165174

166175
const foundSiblingFields = findFieldPathAndSiblingFields(fields, [], field)
@@ -209,7 +218,10 @@ export const lexicalHTML: (
209218
return await convertLexicalToHTML({
210219
converters: finalConverters,
211220
data: lexicalFieldData,
221+
draft,
222+
overrideAccess,
212223
req,
224+
showHiddenFields,
213225
})
214226
},
215227
],

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,26 @@ export const EXPERIMENTAL_TableFeature = createServerFeature({
4646
createNode({
4747
converters: {
4848
html: {
49-
converter: async ({ converters, node, parent, req }) => {
49+
converter: async ({
50+
converters,
51+
draft,
52+
node,
53+
overrideAccess,
54+
parent,
55+
req,
56+
showHiddenFields,
57+
}) => {
5058
const childrenText = await convertLexicalNodesToHTML({
5159
converters,
60+
draft,
5261
lexicalNodes: node.children,
62+
overrideAccess,
5363
parent: {
5464
...node,
5565
parent,
5666
},
5767
req,
68+
showHiddenFields,
5869
})
5970
return `<table class="lexical-table" style="border-collapse: collapse;">${childrenText}</table>`
6071
},
@@ -66,15 +77,26 @@ export const EXPERIMENTAL_TableFeature = createServerFeature({
6677
createNode({
6778
converters: {
6879
html: {
69-
converter: async ({ converters, node, parent, req }) => {
80+
converter: async ({
81+
converters,
82+
draft,
83+
node,
84+
overrideAccess,
85+
parent,
86+
req,
87+
showHiddenFields,
88+
}) => {
7089
const childrenText = await convertLexicalNodesToHTML({
7190
converters,
91+
draft,
7292
lexicalNodes: node.children,
93+
overrideAccess,
7394
parent: {
7495
...node,
7596
parent,
7697
},
7798
req,
99+
showHiddenFields,
78100
})
79101

80102
const tagName = node.headerState > 0 ? 'th' : 'td'
@@ -95,15 +117,26 @@ export const EXPERIMENTAL_TableFeature = createServerFeature({
95117
createNode({
96118
converters: {
97119
html: {
98-
converter: async ({ converters, node, parent, req }) => {
120+
converter: async ({
121+
converters,
122+
draft,
123+
node,
124+
overrideAccess,
125+
parent,
126+
req,
127+
showHiddenFields,
128+
}) => {
99129
const childrenText = await convertLexicalNodesToHTML({
100130
converters,
131+
draft,
101132
lexicalNodes: node.children,
133+
overrideAccess,
102134
parent: {
103135
...node,
104136
parent,
105137
},
106138
req,
139+
showHiddenFields,
107140
})
108141
return `<tr class="lexical-table-row">${childrenText}</tr>`
109142
},

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,26 @@ export const HeadingFeature = createServerFeature<
4646
createNode({
4747
converters: {
4848
html: {
49-
converter: async ({ converters, node, parent, req }) => {
49+
converter: async ({
50+
converters,
51+
draft,
52+
node,
53+
overrideAccess,
54+
parent,
55+
req,
56+
showHiddenFields,
57+
}) => {
5058
const childrenText = await convertLexicalNodesToHTML({
5159
converters,
60+
draft,
5261
lexicalNodes: node.children,
62+
overrideAccess,
5363
parent: {
5464
...node,
5565
parent,
5666
},
5767
req,
68+
showHiddenFields,
5869
})
5970

6071
return '<' + node?.tag + '>' + childrenText + '</' + node?.tag + '>'

0 commit comments

Comments
 (0)