feat(richtext-lexical): simplify schemaMap handling (#6980)

This commit is contained in:
Alessio Gravili
2024-06-28 16:35:51 -04:00
committed by GitHub
parent 8f346dfb62
commit 368dd2c167
6 changed files with 17 additions and 57 deletions

View File

@@ -1,6 +1,5 @@
import type { Block, BlockField, Config, Field } from 'payload'
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
import { baseBlockFields, fieldsToJSONSchema, formatLabels, sanitizeFields } from 'payload'
import type { BlocksFeatureClientProps } from './feature.client.js'
@@ -57,9 +56,7 @@ export const BlocksFeature = createServerFeature<
return {
ClientFeature: BlocksFeatureClient,
clientFeatureProps: clientProps,
generateSchemaMap: ({ config, i18n, props }) => {
const validRelationships = config.collections.map((c) => c.slug) || []
generateSchemaMap: ({ props }) => {
/**
* 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
* 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<
for (const block of props.blocks) {
schemaMap.set(block.slug, block.fields || [])
traverseFields({
config,
fields: block.fields,
i18n,
schemaMap,
schemaPath: block.slug,
validRelationships,
})
}
return schemaMap

View File

@@ -1,6 +1,5 @@
import type { CollectionSlug, Config, Field, FieldAffectingData, SanitizedConfig } from 'payload'
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
import { sanitizeFields } from 'payload'
import { deepCopyObject } from 'payload/shared'
@@ -101,26 +100,14 @@ export const LinkFeature = createServerFeature<
disabledCollections: props.disabledCollections,
enabledCollections: props.enabledCollections,
} as ExclusiveLinkCollectionsProps,
generateSchemaMap: ({ config, i18n }) => {
generateSchemaMap: () => {
if (!sanitizedFields || !Array.isArray(sanitizedFields) || sanitizedFields.length === 0) {
return null
}
const schemaMap = new Map<string, Field[]>()
const validRelationships = config.collections.map((c) => c.slug) || []
schemaMap.set('fields', sanitizedFields)
traverseFields({
config,
fields: sanitizedFields,
i18n,
schemaMap,
schemaPath: 'fields',
validRelationships,
})
return schemaMap
},
i18n,

View File

@@ -8,7 +8,6 @@ import type {
TypeWithID,
} from 'payload'
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
import { sanitizeFields } from 'payload'
import type { UploadFeaturePropsClient } from './feature.client.js'
@@ -82,23 +81,14 @@ export const UploadFeature = createServerFeature<
return {
ClientFeature: UploadFeatureClient,
clientFeatureProps: clientProps,
generateSchemaMap: ({ config, i18n, props }) => {
generateSchemaMap: ({ props }) => {
if (!props?.collections) return null
const schemaMap = new Map<string, Field[]>()
const validRelationships = config.collections.map((c) => c.slug) || []
for (const collection in props.collections) {
if (props.collections[collection].fields?.length) {
schemaMap.set(collection, props.collections[collection].fields)
traverseFields({
config,
fields: props.collections[collection].fields,
i18n,
schemaMap,
schemaPath: collection,
validRelationships,
})
}
}

View File

@@ -1,5 +1,7 @@
import type { RichTextAdapter } from 'payload'
import { traverseFields } from '@payloadcms/ui/utilities/buildFieldSchemaMap/traverseFields'
import type { ResolvedServerFeatureMap } from '../features/typesServer.js'
export const getGenerateSchemaMap =
@@ -22,6 +24,15 @@ export const getGenerateSchemaMap =
if (schemas) {
for (const [schemaKey, fields] of schemas.entries()) {
// generate schema map entries for sub-fields using traverseFields
traverseFields({
config,
fields,
i18n,
schemaMap: schemas,
schemaPath: schemaKey,
})
schemaMap.set(`${schemaPath}.feature.${featureKey}.${schemaKey}`, fields)
}
}

View File

@@ -13,8 +13,6 @@ export const buildFieldSchemaMap = (args: {
const result: FieldSchemaMap = new Map()
const validRelationships = config.collections.map((c) => c.slug) || []
config.collections.forEach((collection) => {
traverseFields({
config,
@@ -22,7 +20,6 @@ export const buildFieldSchemaMap = (args: {
i18n,
schemaMap: result,
schemaPath: collection.slug,
validRelationships,
})
})
@@ -33,7 +30,6 @@ export const buildFieldSchemaMap = (args: {
i18n,
schemaMap: result,
schemaPath: global.slug,
validRelationships,
})
})

View File

@@ -12,18 +12,10 @@ type Args = {
i18n: I18n<any, any>
schemaMap: FieldSchemaMap
schemaPath: string
validRelationships: string[]
}
export const traverseFields = ({
config,
fields,
i18n,
schemaMap,
schemaPath,
validRelationships,
}: Args) => {
fields.map((field) => {
export const traverseFields = ({ config, fields, i18n, schemaMap, schemaPath }: Args) => {
for (const field of fields) {
switch (field.type) {
case 'group':
case 'array':
@@ -35,7 +27,6 @@ export const traverseFields = ({
i18n,
schemaMap,
schemaPath: `${schemaPath}.${field.name}`,
validRelationships,
})
break
@@ -47,7 +38,6 @@ export const traverseFields = ({
i18n,
schemaMap,
schemaPath,
validRelationships,
})
break
@@ -63,7 +53,6 @@ export const traverseFields = ({
i18n,
schemaMap,
schemaPath: blockSchemaPath,
validRelationships,
})
})
break
@@ -101,10 +90,9 @@ export const traverseFields = ({
i18n,
schemaMap,
schemaPath: tabSchemaPath,
validRelationships,
})
})
break
}
})
}
}