Skip to content

Commit 368dd2c

Browse files
authored
feat(richtext-lexical): simplify schemaMap handling (#6980)
1 parent 8f346df commit 368dd2c

File tree

6 files changed

+17
-57
lines changed

6 files changed

+17
-57
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Block, BlockField, Config, Field } from 'payload'
22

3-
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
43
import { baseBlockFields, fieldsToJSONSchema, formatLabels, sanitizeFields } from 'payload'
54

65
import type { BlocksFeatureClientProps } from './feature.client.js'
@@ -57,9 +56,7 @@ export const BlocksFeature = createServerFeature<
5756
return {
5857
ClientFeature: BlocksFeatureClient,
5958
clientFeatureProps: clientProps,
60-
generateSchemaMap: ({ config, i18n, props }) => {
61-
const validRelationships = config.collections.map((c) => c.slug) || []
62-
59+
generateSchemaMap: ({ props }) => {
6360
/**
6461
* Add sub-fields to the schemaMap. E.g. if you have an array field as part of the block, and it runs addRow, it will request these
6562
* sub-fields from the component map. Thus, we need to put them in the component map here.
@@ -68,15 +65,6 @@ export const BlocksFeature = createServerFeature<
6865

6966
for (const block of props.blocks) {
7067
schemaMap.set(block.slug, block.fields || [])
71-
72-
traverseFields({
73-
config,
74-
fields: block.fields,
75-
i18n,
76-
schemaMap,
77-
schemaPath: block.slug,
78-
validRelationships,
79-
})
8068
}
8169

8270
return schemaMap

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { CollectionSlug, Config, Field, FieldAffectingData, SanitizedConfig } from 'payload'
22

3-
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
43
import { sanitizeFields } from 'payload'
54
import { deepCopyObject } from 'payload/shared'
65

@@ -101,26 +100,14 @@ export const LinkFeature = createServerFeature<
101100
disabledCollections: props.disabledCollections,
102101
enabledCollections: props.enabledCollections,
103102
} as ExclusiveLinkCollectionsProps,
104-
generateSchemaMap: ({ config, i18n }) => {
103+
generateSchemaMap: () => {
105104
if (!sanitizedFields || !Array.isArray(sanitizedFields) || sanitizedFields.length === 0) {
106105
return null
107106
}
108107

109108
const schemaMap = new Map<string, Field[]>()
110-
111-
const validRelationships = config.collections.map((c) => c.slug) || []
112-
113109
schemaMap.set('fields', sanitizedFields)
114110

115-
traverseFields({
116-
config,
117-
fields: sanitizedFields,
118-
i18n,
119-
schemaMap,
120-
schemaPath: 'fields',
121-
validRelationships,
122-
})
123-
124111
return schemaMap
125112
},
126113
i18n,

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
TypeWithID,
99
} from 'payload'
1010

11-
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
1211
import { sanitizeFields } from 'payload'
1312

1413
import type { UploadFeaturePropsClient } from './feature.client.js'
@@ -82,23 +81,14 @@ export const UploadFeature = createServerFeature<
8281
return {
8382
ClientFeature: UploadFeatureClient,
8483
clientFeatureProps: clientProps,
85-
generateSchemaMap: ({ config, i18n, props }) => {
84+
generateSchemaMap: ({ props }) => {
8685
if (!props?.collections) return null
8786

8887
const schemaMap = new Map<string, Field[]>()
89-
const validRelationships = config.collections.map((c) => c.slug) || []
9088

9189
for (const collection in props.collections) {
9290
if (props.collections[collection].fields?.length) {
9391
schemaMap.set(collection, props.collections[collection].fields)
94-
traverseFields({
95-
config,
96-
fields: props.collections[collection].fields,
97-
i18n,
98-
schemaMap,
99-
schemaPath: collection,
100-
validRelationships,
101-
})
10292
}
10393
}
10494

packages/richtext-lexical/src/utilities/generateSchemaMap.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { RichTextAdapter } from 'payload'
22

3+
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
4+
35
import type { ResolvedServerFeatureMap } from '../features/typesServer.js'
46

57
export const getGenerateSchemaMap =
@@ -22,6 +24,15 @@ export const getGenerateSchemaMap =
2224

2325
if (schemas) {
2426
for (const [schemaKey, fields] of schemas.entries()) {
27+
// generate schema map entries for sub-fields using traverseFields
28+
traverseFields({
29+
config,
30+
fields,
31+
i18n,
32+
schemaMap: schemas,
33+
schemaPath: schemaKey,
34+
})
35+
2536
schemaMap.set(`${schemaPath}.feature.${featureKey}.${schemaKey}`, fields)
2637
}
2738
}

packages/ui/src/utilities/buildFieldSchemaMap/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ export const buildFieldSchemaMap = (args: {
1313

1414
const result: FieldSchemaMap = new Map()
1515

16-
const validRelationships = config.collections.map((c) => c.slug) || []
17-
1816
config.collections.forEach((collection) => {
1917
traverseFields({
2018
config,
2119
fields: collection.fields,
2220
i18n,
2321
schemaMap: result,
2422
schemaPath: collection.slug,
25-
validRelationships,
2623
})
2724
})
2825

@@ -33,7 +30,6 @@ export const buildFieldSchemaMap = (args: {
3330
i18n,
3431
schemaMap: result,
3532
schemaPath: global.slug,
36-
validRelationships,
3733
})
3834
})
3935

packages/ui/src/utilities/buildFieldSchemaMap/traverseFields.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,10 @@ type Args = {
1212
i18n: I18n<any, any>
1313
schemaMap: FieldSchemaMap
1414
schemaPath: string
15-
validRelationships: string[]
1615
}
1716

18-
export const traverseFields = ({
19-
config,
20-
fields,
21-
i18n,
22-
schemaMap,
23-
schemaPath,
24-
validRelationships,
25-
}: Args) => {
26-
fields.map((field) => {
17+
export const traverseFields = ({ config, fields, i18n, schemaMap, schemaPath }: Args) => {
18+
for (const field of fields) {
2719
switch (field.type) {
2820
case 'group':
2921
case 'array':
@@ -35,7 +27,6 @@ export const traverseFields = ({
3527
i18n,
3628
schemaMap,
3729
schemaPath: `${schemaPath}.${field.name}`,
38-
validRelationships,
3930
})
4031
break
4132

@@ -47,7 +38,6 @@ export const traverseFields = ({
4738
i18n,
4839
schemaMap,
4940
schemaPath,
50-
validRelationships,
5141
})
5242
break
5343

@@ -63,7 +53,6 @@ export const traverseFields = ({
6353
i18n,
6454
schemaMap,
6555
schemaPath: blockSchemaPath,
66-
validRelationships,
6756
})
6857
})
6958
break
@@ -101,10 +90,9 @@ export const traverseFields = ({
10190
i18n,
10291
schemaMap,
10392
schemaPath: tabSchemaPath,
104-
validRelationships,
10593
})
10694
})
10795
break
10896
}
109-
})
97+
}
11098
}

0 commit comments

Comments
 (0)