diff --git a/package.json b/package.json index 1442fbd9b4d..64a949a3691 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,14 @@ "analyze-circular-dependencies-fields": "madge test/fields/config.ts --circular --warning", "analyze-circular-dependencies-lexical": "madge packages/richtext-lexical/src/index.ts --circular --warning", "analyze-circular-dependencies-lexical-client": "madge packages/richtext-lexical/src/exports/client/index.ts --circular --warning", + "analyze-circular-dependencies-lexical-migrate": "madge packages/richtext-lexical/src/exports/server/migrate.ts --circular --warning", "analyze-circular-dependencies-mongodb": "madge packages/db-mongodb/src/index.ts --circular --warning", "analyze-circular-dependencies-postgres": "madge packages/db-postgres/src/index.ts --circular --warning", "analyze-circular-dependencies-drizzle": "madge packages/drizzle/src/index.ts --circular --warning", "analyze-circular-dependencies-drizzle-postgres": "madge packages/drizzle/src/exports/postgres.ts --circular --warning", "analyze-circular-dependencies-graphql": "madge packages/grapqhl/src/index.ts --circular --warning", + "analyze-circular-dependencies-fieldSchemasToFormState": "madge packages/ui/src/forms/fieldSchemasToFormState/index.tsx --circular --warning", + "analyze-circular-dependencies-buildFieldSchemaMap": "madge packages/ui/src/utilities/buildFieldSchemaMap/index.ts --circular --warning", "bf": "pnpm run build:force", "build": "pnpm run build:core", "build:all": "turbo build", diff --git a/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/Component.tsx b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/Component.tsx index 42707c44d7e..e5f553e0643 100644 --- a/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/Component.tsx +++ b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/Component.tsx @@ -1,10 +1,10 @@ 'use client' import React from 'react' -import type { UnknownConvertedNodeData } from './index.js' - import './index.scss' +import type { UnknownConvertedNodeData } from './types.js' + type Props = { data: UnknownConvertedNodeData } diff --git a/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/index.tsx b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/index.tsx index b345d09e368..6bf583537b3 100644 --- a/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/index.tsx +++ b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/index.tsx @@ -5,10 +5,7 @@ import { addClassNamesToElement } from '@lexical/utils' import { DecoratorNode } from 'lexical' import * as React from 'react' -export type UnknownConvertedNodeData = { - nodeData: unknown - nodeType: string -} +import type { UnknownConvertedNodeData } from './types.js' export type SerializedUnknownConvertedNode = Spread< { diff --git a/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/types.ts b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/types.ts new file mode 100644 index 00000000000..2e335aca44d --- /dev/null +++ b/packages/richtext-lexical/src/features/migrations/lexicalPluginToLexical/nodes/unknownConvertedNode/types.ts @@ -0,0 +1,4 @@ +export type UnknownConvertedNodeData = { + nodeData: unknown + nodeType: string +} diff --git a/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/Component.tsx b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/Component.tsx index 43f65b99621..8f00916af87 100644 --- a/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/Component.tsx +++ b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/Component.tsx @@ -1,9 +1,8 @@ 'use client' import React from 'react' -import type { UnknownConvertedNodeData } from './index.js' - import './index.scss' +import type { UnknownConvertedNodeData } from './types.js' type Props = { data: UnknownConvertedNodeData diff --git a/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/index.tsx b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/index.tsx index b345d09e368..6bf583537b3 100644 --- a/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/index.tsx +++ b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/index.tsx @@ -5,10 +5,7 @@ import { addClassNamesToElement } from '@lexical/utils' import { DecoratorNode } from 'lexical' import * as React from 'react' -export type UnknownConvertedNodeData = { - nodeData: unknown - nodeType: string -} +import type { UnknownConvertedNodeData } from './types.js' export type SerializedUnknownConvertedNode = Spread< { diff --git a/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/types.ts b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/types.ts new file mode 100644 index 00000000000..2e335aca44d --- /dev/null +++ b/packages/richtext-lexical/src/features/migrations/slateToLexical/nodes/unknownConvertedNode/types.ts @@ -0,0 +1,4 @@ +export type UnknownConvertedNodeData = { + nodeData: unknown + nodeType: string +} diff --git a/packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts b/packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts index 2b8bd0fe6ed..6288bfa03dc 100644 --- a/packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts +++ b/packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts @@ -22,7 +22,6 @@ import { import type { RenderFieldMethod } from './types.js' import { getFilterOptionsQuery } from './getFilterOptionsQuery.js' -import { iterateFields } from './iterateFields.js' const ObjectId = (ObjectIdImport.default || ObjectIdImport) as unknown as typeof ObjectIdImport.default @@ -736,3 +735,137 @@ export const addFieldStatePromise = async (args: AddFieldStatePromiseArgs): Prom }) } } + +type IterateFieldsArgs = { + addErrorPathToParent: (fieldPath: string) => void + /** + * if any parents is localized, then the field is localized. @default false + */ + anyParentLocalized?: boolean + collectionSlug?: string + data: Data + fields: Field[] + fieldSchemaMap: FieldSchemaMap + filter?: (args: AddFieldStatePromiseArgs) => boolean + /** + * Force the value of fields like arrays or blocks to be the full value instead of the length @default false + */ + forceFullValue?: boolean + fullData: Data + id?: number | string + /** + * Whether the field schema should be included in the state. @default false + */ + includeSchema?: boolean + /** + * Whether to omit parent fields in the state. @default false + */ + omitParents?: boolean + /** + * operation is only needed for validation + */ + operation: 'create' | 'update' + parentIndexPath: string + parentPassesCondition?: boolean + parentPath: string + parentSchemaPath: string + permissions: + | { + [fieldName: string]: SanitizedFieldPermissions + } + | null + | SanitizedFieldPermissions + preferences?: DocumentPreferences + previousFormState: FormState + renderAllFields: boolean + renderFieldFn: RenderFieldMethod + req: PayloadRequest + /** + * Whether to skip checking the field's condition. @default false + */ + skipConditionChecks?: boolean + /** + * Whether to skip validating the field. @default false + */ + skipValidation?: boolean + state?: FormStateWithoutComponents +} + +/** + * Flattens the fields schema and fields data + */ +export const iterateFields = async ({ + id, + addErrorPathToParent: addErrorPathToParentArg, + anyParentLocalized = false, + collectionSlug, + data, + fields, + fieldSchemaMap, + filter, + forceFullValue = false, + fullData, + includeSchema = false, + omitParents = false, + operation, + parentIndexPath, + parentPassesCondition = true, + parentPath, + parentSchemaPath, + permissions, + preferences, + previousFormState, + renderAllFields, + renderFieldFn: renderFieldFn, + req, + skipConditionChecks = false, + skipValidation = false, + state = {}, +}: IterateFieldsArgs): Promise => { + const promises = [] + + fields.forEach((field, fieldIndex) => { + let passesCondition = true + if (!skipConditionChecks) { + passesCondition = Boolean( + (field?.admin?.condition + ? Boolean(field.admin.condition(fullData || {}, data || {}, { user: req.user })) + : true) && parentPassesCondition, + ) + } + + promises.push( + addFieldStatePromise({ + id, + addErrorPathToParent: addErrorPathToParentArg, + anyParentLocalized, + collectionSlug, + data, + field, + fieldIndex, + fieldSchemaMap, + filter, + forceFullValue, + fullData, + includeSchema, + omitParents, + operation, + parentIndexPath, + parentPath, + parentSchemaPath, + passesCondition, + permissions, + preferences, + previousFormState, + renderAllFields, + renderFieldFn, + req, + skipConditionChecks, + skipValidation, + state, + }), + ) + }) + + await Promise.all(promises) +} diff --git a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/index.ts b/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/index.ts index 2f4e67436ae..3116db02e43 100644 --- a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/index.ts +++ b/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/index.ts @@ -1,6 +1,6 @@ import type { Data, Field as FieldSchema, User } from 'payload' -import { iterateFields } from './iterateFields.js' +import { iterateFields } from './promise.js' type Args = { data: Data diff --git a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/iterateFields.ts b/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/iterateFields.ts deleted file mode 100644 index 7cb52a4f641..00000000000 --- a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/iterateFields.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { Data, Field, TabAsField, User } from 'payload' - -import { defaultValuePromise } from './promise.js' - -type Args = { - data: T - fields: (Field | TabAsField)[] - id?: number | string - locale: string | undefined - siblingData: Data - user: User -} - -export const iterateFields = async ({ - id, - data, - fields, - locale, - siblingData, - user, -}: Args): Promise => { - const promises = [] - - fields.forEach((field) => { - promises.push( - defaultValuePromise({ - id, - data, - field, - locale, - siblingData, - user, - }), - ) - }) - - await Promise.all(promises) -} diff --git a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/promise.ts b/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/promise.ts index e9637a5f10b..b28acbe0ca3 100644 --- a/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/promise.ts +++ b/packages/ui/src/forms/fieldSchemasToFormState/calculateDefaultValues/promise.ts @@ -3,8 +3,6 @@ import type { Data, Field, TabAsField, User } from 'payload' import { getDefaultValue } from 'payload' import { fieldAffectsData, tabHasName } from 'payload/shared' -import { iterateFields } from './iterateFields.js' - type Args = { data: T field: Field | TabAsField @@ -168,3 +166,38 @@ export const defaultValuePromise = async ({ } } } + +type IterateFieldsArgs = { + data: T + fields: (Field | TabAsField)[] + id?: number | string + locale: string | undefined + siblingData: Data + user: User +} + +export const iterateFields = async ({ + id, + data, + fields, + locale, + siblingData, + user, +}: IterateFieldsArgs): Promise => { + const promises = [] + + fields.forEach((field) => { + promises.push( + defaultValuePromise({ + id, + data, + field, + locale, + siblingData, + user, + }), + ) + }) + + await Promise.all(promises) +} diff --git a/packages/ui/src/forms/fieldSchemasToFormState/index.tsx b/packages/ui/src/forms/fieldSchemasToFormState/index.tsx index 7c5d983af49..1fb768ca84a 100644 --- a/packages/ui/src/forms/fieldSchemasToFormState/index.tsx +++ b/packages/ui/src/forms/fieldSchemasToFormState/index.tsx @@ -11,8 +11,8 @@ import type { import type { RenderFieldMethod } from './types.js' +import { iterateFields } from './addFieldStatePromise.js' import { calculateDefaultValues } from './calculateDefaultValues/index.js' -import { iterateFields } from './iterateFields.js' type Args = { collectionSlug?: string diff --git a/packages/ui/src/forms/fieldSchemasToFormState/iterateFields.ts b/packages/ui/src/forms/fieldSchemasToFormState/iterateFields.ts deleted file mode 100644 index 5a25d1f4f6f..00000000000 --- a/packages/ui/src/forms/fieldSchemasToFormState/iterateFields.ts +++ /dev/null @@ -1,149 +0,0 @@ -import type { - Data, - DocumentPreferences, - Field as FieldSchema, - FieldSchemaMap, - FormState, - FormStateWithoutComponents, - PayloadRequest, - SanitizedFieldPermissions, -} from 'payload' - -import type { AddFieldStatePromiseArgs } from './addFieldStatePromise.js' -import type { RenderFieldMethod } from './types.js' - -import { addFieldStatePromise } from './addFieldStatePromise.js' - -type Args = { - addErrorPathToParent: (fieldPath: string) => void - /** - * if any parents is localized, then the field is localized. @default false - */ - anyParentLocalized?: boolean - collectionSlug?: string - data: Data - fields: FieldSchema[] - fieldSchemaMap: FieldSchemaMap - filter?: (args: AddFieldStatePromiseArgs) => boolean - /** - * Force the value of fields like arrays or blocks to be the full value instead of the length @default false - */ - forceFullValue?: boolean - fullData: Data - id?: number | string - /** - * Whether the field schema should be included in the state. @default false - */ - includeSchema?: boolean - /** - * Whether to omit parent fields in the state. @default false - */ - omitParents?: boolean - /** - * operation is only needed for validation - */ - operation: 'create' | 'update' - parentIndexPath: string - parentPassesCondition?: boolean - parentPath: string - parentSchemaPath: string - permissions: - | { - [fieldName: string]: SanitizedFieldPermissions - } - | null - | SanitizedFieldPermissions - preferences?: DocumentPreferences - previousFormState: FormState - renderAllFields: boolean - renderFieldFn: RenderFieldMethod - req: PayloadRequest - /** - * Whether to skip checking the field's condition. @default false - */ - skipConditionChecks?: boolean - /** - * Whether to skip validating the field. @default false - */ - skipValidation?: boolean - state?: FormStateWithoutComponents -} - -/** - * Flattens the fields schema and fields data - */ -export const iterateFields = async ({ - id, - addErrorPathToParent: addErrorPathToParentArg, - anyParentLocalized = false, - collectionSlug, - data, - fields, - fieldSchemaMap, - filter, - forceFullValue = false, - fullData, - includeSchema = false, - omitParents = false, - operation, - parentIndexPath, - parentPassesCondition = true, - parentPath, - parentSchemaPath, - permissions, - preferences, - previousFormState, - renderAllFields, - renderFieldFn: renderFieldFn, - req, - skipConditionChecks = false, - skipValidation = false, - state = {}, -}: Args): Promise => { - const promises = [] - - fields.forEach((field, fieldIndex) => { - let passesCondition = true - if (!skipConditionChecks) { - passesCondition = Boolean( - (field?.admin?.condition - ? Boolean(field.admin.condition(fullData || {}, data || {}, { user: req.user })) - : true) && parentPassesCondition, - ) - } - - promises.push( - addFieldStatePromise({ - id, - addErrorPathToParent: addErrorPathToParentArg, - anyParentLocalized, - collectionSlug, - data, - field, - fieldIndex, - fieldSchemaMap, - filter, - forceFullValue, - fullData, - includeSchema, - omitParents, - operation, - parentIndexPath, - parentPath, - parentSchemaPath, - passesCondition, - permissions, - preferences, - previousFormState, - renderAllFields, - renderFieldFn, - req, - skipConditionChecks, - skipValidation, - state, - }), - ) - }) - - await Promise.all(promises) -} diff --git a/test/helpers/snapshot.ts b/test/helpers/snapshot.ts index eb5fb86ba38..84273b81798 100644 --- a/test/helpers/snapshot.ts +++ b/test/helpers/snapshot.ts @@ -4,7 +4,6 @@ import type { PgTable } from 'drizzle-orm/pg-core' import type { SQLiteTable } from 'drizzle-orm/sqlite-core' import type { Payload } from 'payload' -import { GenericTable } from '@payloadcms/drizzle/types' import { sql } from 'drizzle-orm' import { isMongoose } from './isMongoose.js'