@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 6.39 kB
Source Map (JSON)
{"version":3,"file":"prepare-bricks-and-fields.mjs","names":[],"sources":["../../../../src/services/documents-bricks/helpers/prepare-bricks-and-fields.ts"],"sourcesContent":["import type CollectionBuilder from \"../../../libs/collection/builders/collection-builder/index.js\";\nimport type CustomField from \"../../../libs/collection/custom-fields/custom-field.js\";\nimport registeredFields from \"../../../libs/collection/custom-fields/registered-fields.js\";\nimport { isStorageMode } from \"../../../libs/collection/custom-fields/storage/index.js\";\nimport type { BrickInputSchema } from \"../../../schemas/collection-bricks.js\";\nimport type { Config, FieldInputSchema, FieldTypes } from \"../../../types.js\";\n\n/**\n * - Processes fields to remove any that don't exist in the custom fields.\n * - Processes recursively for tree-table fields with nested groups.\n * - Based on collection and field translation support, sort out the fields translations/value props\n * - Normalizes the input value of the field using the custom field instance's normalizeInputValue method\n */\nconst processFields = (props: {\n\tcollection: CollectionBuilder;\n\tfields: Array<FieldInputSchema>;\n\tcustomFields: Map<string, CustomField<FieldTypes>>;\n\tlocalization: Config[\"localization\"];\n}): Array<FieldInputSchema> => {\n\treturn props.fields\n\t\t.filter((field) => props.customFields.has(field.key))\n\t\t.map((field) => {\n\t\t\t// biome-ignore lint/style/noNonNullAssertion: explanation\n\t\t\tconst cfInstance = props.customFields.get(field.key)!;\n\t\t\tconst processedField = { ...field };\n\t\t\tconst databaseConfig = registeredFields[cfInstance.type].config.database;\n\n\t\t\tif (processedField.value !== undefined) {\n\t\t\t\tprocessedField.value = cfInstance.normalizeInputValue(\n\t\t\t\t\tprocessedField.value,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (processedField.translations) {\n\t\t\t\tprocessedField.translations = Object.fromEntries(\n\t\t\t\t\tObject.entries(processedField.translations).map(\n\t\t\t\t\t\t([localeCode, value]) => [\n\t\t\t\t\t\t\tlocaleCode,\n\t\t\t\t\t\t\tcfInstance.normalizeInputValue(value),\n\t\t\t\t\t\t],\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (isStorageMode(databaseConfig, \"tree-table\") && field.groups) {\n\t\t\t\tprocessedField.groups = field.groups.map((group) => ({\n\t\t\t\t\t...group,\n\t\t\t\t\tfields: processFields({\n\t\t\t\t\t\tcollection: props.collection,\n\t\t\t\t\t\tfields: group.fields,\n\t\t\t\t\t\tcustomFields: props.customFields,\n\t\t\t\t\t\tlocalization: props.localization,\n\t\t\t\t\t}),\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\t// if collection uses translations and the field supports translations\n\t\t\tif (props.collection.getData.localized && cfInstance?.localizedEnabled) {\n\t\t\t\t// if processField.value is given only and no translations key - add the value to the translations object with the locale object key being the default locale\n\t\t\t\tif (\n\t\t\t\t\tprocessedField.value !== undefined &&\n\t\t\t\t\t!processedField.translations\n\t\t\t\t) {\n\t\t\t\t\tprocessedField.translations = {\n\t\t\t\t\t\t[props.localization.defaultLocale]: processedField.value,\n\t\t\t\t\t};\n\t\t\t\t\tprocessedField.value = undefined;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// if processField.translations is given, take the default locale translation value and set it as the processField.value\n\t\t\t\tif (processedField.translations && processedField.value === undefined) {\n\t\t\t\t\tconst translationValue =\n\t\t\t\t\t\tprocessedField.translations[props.localization.defaultLocale];\n\t\t\t\t\tprocessedField.value = translationValue;\n\t\t\t\t\tprocessedField.translations = undefined;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn processedField;\n\t\t});\n};\n\n/**\n * Prepares bricks and fields by removing invalid fields that don't exist in custom fields.\n */\nconst prepareBricksAndFields = (props: {\n\tcollection: CollectionBuilder;\n\tbricks?: Array<BrickInputSchema>;\n\tfields?: Array<FieldInputSchema>;\n\tlocalization: Config[\"localization\"];\n}) => {\n\t// Process collection fields\n\tconst preparedFields = props.fields\n\t\t? processFields({\n\t\t\t\tcollection: props.collection,\n\t\t\t\tfields: props.fields,\n\t\t\t\tcustomFields: props.collection.fields,\n\t\t\t\tlocalization: props.localization,\n\t\t\t})\n\t\t: undefined;\n\n\t// Process brick fields\n\tconst preparedBricks = props.bricks\n\t\t? props.bricks.map((brick) => {\n\t\t\t\tconst brickDefinition = props.collection.brickInstances.find(\n\t\t\t\t\t(b) => b.key === brick.key,\n\t\t\t\t);\n\t\t\t\tif (!brickDefinition || !brick.fields) return brick;\n\n\t\t\t\t// Process fields for this brick\n\t\t\t\tconst processedFields = processFields({\n\t\t\t\t\tcollection: props.collection,\n\t\t\t\t\tfields: brick.fields,\n\t\t\t\t\tcustomFields: brickDefinition.fields,\n\t\t\t\t\tlocalization: props.localization,\n\t\t\t\t});\n\n\t\t\t\treturn {\n\t\t\t\t\t...brick,\n\t\t\t\t\tfields: processedFields,\n\t\t\t\t};\n\t\t\t})\n\t\t: undefined;\n\n\treturn {\n\t\tpreparedBricks,\n\t\tpreparedFields,\n\t};\n};\n\nexport default prepareBricksAndFields;\n"],"mappings":"qKAaA,MAAM,EAAiB,GAMf,EAAM,OACX,OAAQ,GAAU,EAAM,aAAa,IAAI,EAAM,GAAG,CAAC,CAAC,CACpD,IAAK,GAAU,CAEf,IAAM,EAAa,EAAM,aAAa,IAAI,EAAM,GAAG,EAC7C,EAAiB,CAAE,GAAG,CAAM,EAC5B,EAAiB,EAAiB,EAAW,KAAK,CAAC,OAAO,SAoDhE,OAlDI,EAAe,QAAU,IAAA,KAC5B,EAAe,MAAQ,EAAW,oBACjC,EAAe,KAChB,GAED,AACC,EAAe,eAAe,OAAO,YACpC,OAAO,QAAQ,EAAe,YAAY,CAAC,CAAC,KAC1C,CAAC,EAAY,KAAW,CACxB,EACA,EAAW,oBAAoB,CAAK,CACrC,CACD,CACD,EAGG,EAAc,EAAgB,YAAY,GAAK,EAAM,SACxD,EAAe,OAAS,EAAM,OAAO,IAAK,IAAW,CACpD,GAAG,EACH,OAAQ,EAAc,CACrB,WAAY,EAAM,WAClB,OAAQ,EAAM,OACd,aAAc,EAAM,aACpB,aAAc,EAAM,YACr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