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

View File

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

View File

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

View File

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

View File

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