@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 12.3 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":["#removeDuplicateBricks","#fieldCollectionHelper","#formatGroup","constants"],"sources":["../../../../../src/libs/collection/builders/collection-builder/index.ts"],"sourcesContent":["import constants from \"../../../../constants/constants.js\";\nimport { normalizeCopy } from \"../../../i18n/index.js\";\nimport type { CFProps } from \"../../custom-fields/types.js\";\nimport type BrickBuilder from \"../brick-builder/index.js\";\nimport FieldBuilder from \"../field-builder/index.js\";\nimport type {\n\tCollectionBrickConfig,\n\tCollectionConfigSchemaType,\n\tCollectionData,\n\tCollectionLabelFieldOptions,\n\tCollectionListFieldOptions,\n} from \"./types.js\";\n\nclass CollectionBuilder<\n\tconst TCollectionKey extends string = string,\n> extends FieldBuilder {\n\tkey: TCollectionKey;\n\tconfig: CollectionConfigSchemaType<TCollectionKey>;\n\tlisting: string[] = [];\n\tlabelFields: string[] = [];\n\tconstructor(\n\t\tkey: TCollectionKey,\n\t\tconfig: Omit<CollectionConfigSchemaType<TCollectionKey>, \"key\">,\n\t) {\n\t\tsuper();\n\t\tthis.key = key;\n\t\tthis.config = {\n\t\t\tkey: this.key,\n\t\t\t...config,\n\t\t};\n\n\t\tif (this.config.bricks?.fixed) {\n\t\t\tthis.config.bricks.fixed = this.#removeDuplicateBricks(\n\t\t\t\tconfig.bricks?.fixed,\n\t\t\t);\n\t\t}\n\t\tif (this.config.bricks?.builder) {\n\t\t\tthis.config.bricks.builder = this.#removeDuplicateBricks(\n\t\t\t\tconfig.bricks?.builder,\n\t\t\t);\n\t\t}\n\t}\n\t// ------------------------------------\n\t// Builder Methods\n\taddText(key: string, props?: CFProps<\"text\"> & CollectionLabelFieldOptions) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addText(key, props);\n\t\treturn this;\n\t}\n\taddNumber(\n\t\tkey: string,\n\t\tprops?: CFProps<\"number\"> & CollectionLabelFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addNumber(key, props);\n\t\treturn this;\n\t}\n\taddRange(key: string, props?: CFProps<\"range\"> & CollectionListFieldOptions) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addRange(key, props);\n\t\treturn this;\n\t}\n\taddCheckbox(\n\t\tkey: string,\n\t\tprops?: CFProps<\"checkbox\"> & CollectionListFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addCheckbox(key, props);\n\t\treturn this;\n\t}\n\taddSelect(\n\t\tkey: string,\n\t\tprops?: CFProps<\"select\"> & CollectionLabelFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addSelect(key, props);\n\t\treturn this;\n\t}\n\taddTextarea(\n\t\tkey: string,\n\t\tprops?: CFProps<\"textarea\"> & CollectionLabelFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addTextarea(key, props);\n\t\treturn this;\n\t}\n\taddDateTime(\n\t\tkey: string,\n\t\tprops?: CFProps<\"datetime\"> & CollectionLabelFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addDateTime(key, props);\n\t\treturn this;\n\t}\n\taddUser(key: string, props?: CFProps<\"user\"> & CollectionListFieldOptions) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addUser(key, props);\n\t\treturn this;\n\t}\n\taddMedia(key: string, props?: CFProps<\"media\"> & CollectionListFieldOptions) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addMedia(key, props);\n\t\treturn this;\n\t}\n\taddRelation(\n\t\tkey: string,\n\t\tprops: CFProps<\"relation\"> & CollectionListFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addRelation(key, props);\n\t\treturn this;\n\t}\n\taddColor(\n\t\tkey: string,\n\t\tprops?: CFProps<\"color\"> & CollectionLabelFieldOptions,\n\t) {\n\t\tthis.#fieldCollectionHelper(key, props);\n\t\tsuper.addColor(key, props);\n\t\treturn this;\n\t}\n\t// ------------------------------------\n\t// Private Methods\n\t#removeDuplicateBricks = (bricks?: Array<BrickBuilder>) => {\n\t\tif (!bricks) return undefined;\n\n\t\treturn bricks.filter(\n\t\t\t(brick, index) => bricks.findIndex((b) => b.key === brick.key) === index,\n\t\t);\n\t};\n\t/** Tracks collection-level field display config while fields are registered. */\n\t#fieldCollectionHelper = (\n\t\tkey: string,\n\t\toptions?: CollectionLabelFieldOptions,\n\t) => {\n\t\tif (options?.showInList) this.listing.push(key);\n\t\tif (options?.useAsLabel) this.labelFields.push(key);\n\t};\n\t#formatGroup = (): CollectionData[\"group\"] => {\n\t\tconst group = this.config.group;\n\t\tif (group === undefined) return null;\n\t\tif (typeof group === \"string\") {\n\t\t\treturn {\n\t\t\t\tkey: group,\n\t\t\t\tname: null,\n\t\t\t\torder: null,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tkey: group.key,\n\t\t\tname: normalizeCopy(group.name) ?? null,\n\t\t\torder: group.order ?? null,\n\t\t};\n\t};\n\n\t// ------------------------------------\n\t// Getters\n\tget getData(): CollectionData {\n\t\treturn {\n\t\t\tkey: this.key,\n\t\t\tmode: this.config.mode,\n\t\t\tgroup: this.#formatGroup(),\n\t\t\tdetails: {\n\t\t\t\tname: normalizeCopy(this.config.details.name),\n\t\t\t\tsingularName: normalizeCopy(this.config.details.singularName),\n\t\t\t\tsummary: normalizeCopy(this.config.details.summary) ?? null,\n\t\t\t},\n\t\t\tlocked: this.config.locked ?? constants.collectionBuilder.locked,\n\t\t\trevisions: this.config.revisions ?? constants.collectionBuilder.revisions,\n\t\t\tlocalized: this.config.localized ?? constants.collectionBuilder.localized,\n\t\t\tautoSave: this.config.autoSave ?? constants.collectionBuilder.autoSave,\n\t\t\tscheduling:\n\t\t\t\tthis.config.scheduling ?? constants.collectionBuilder.scheduling,\n\t\t\torderable: this.config.orderable ?? constants.collectionBuilder.orderable,\n\t\t\t...(this.config.review\n\t\t\t\t? {\n\t\t\t\t\t\treview: {\n\t\t\t\t\t\t\trequiredFor: this.config.review?.requiredFor ?? [],\n\t\t\t\t\t\t\tallowSelfApproval:\n\t\t\t\t\t\t\t\tthis.config.review?.allowSelfApproval ??\n\t\t\t\t\t\t\t\tconstants.collectionBuilder.publishing.allowSelfApproval,\n\t\t\t\t\t\t\tcomments: {\n\t\t\t\t\t\t\t\trequest:\n\t\t\t\t\t\t\t\t\tthis.config.review?.comments?.request ??\n\t\t\t\t\t\t\t\t\tconstants.collectionBuilder.publishing.comments.request,\n\t\t\t\t\t\t\t\tdecision:\n\t\t\t\t\t\t\t\t\tthis.config.review?.comments?.decision ??\n\t\t\t\t\t\t\t\t\tconstants.collectionBuilder.publishing.comments.decision,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t: {}),\n\t\t\t...(this.config.workflow\n\t\t\t\t? {\n\t\t\t\t\t\tworkflow: {\n\t\t\t\t\t\t\tinitial:\n\t\t\t\t\t\t\t\tthis.config.workflow.initial ??\n\t\t\t\t\t\t\t\tthis.config.workflow.stages[0]?.key ??\n\t\t\t\t\t\t\t\t\"\",\n\t\t\t\t\t\t\tstages: this.config.workflow.stages.map((stage) => ({\n\t\t\t\t\t\t\t\tkey: stage.key,\n\t\t\t\t\t\t\t\tname: normalizeCopy(stage.name),\n\t\t\t\t\t\t\t\tcolor:\n\t\t\t\t\t\t\t\t\tstage.color ??\n\t\t\t\t\t\t\t\t\tconstants.collectionBuilder.publishing.workflow.color,\n\t\t\t\t\t\t\t\tpublishTargets: stage.publishTargets ?? [],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t: {}),\n\t\t\tlisting: this.listing,\n\t\t\tlabelFields: this.labelFields,\n\t\t\tenvironments:\n\t\t\t\tthis.config.environments?.map((environment) => ({\n\t\t\t\t\t...environment,\n\t\t\t\t\tname: normalizeCopy(environment.name),\n\t\t\t\t\trequires: environment.requires ?? [],\n\t\t\t\t\tcollectionVersions: environment.collectionVersions ?? {},\n\t\t\t\t})) ?? [],\n\t\t\trevisionRetentionDays:\n\t\t\t\tthis.config.revisionRetentionDays ??\n\t\t\t\tconstants.collectionBuilder.revisionRetentionDays,\n\t\t\tpreview: this.config.preview\n\t\t\t\t? {\n\t\t\t\t\t\tbreakpoints:\n\t\t\t\t\t\t\tthis.config.preview.breakpoints?.map((breakpoint) => ({\n\t\t\t\t\t\t\t\t...breakpoint,\n\t\t\t\t\t\t\t\tlabel: normalizeCopy(breakpoint.label),\n\t\t\t\t\t\t\t})) ?? [],\n\t\t\t\t\t}\n\t\t\t\t: null,\n\t\t\ttenants: this.config.tenants ?? [],\n\t\t};\n\t}\n\tget fixedBricks(): CollectionBrickConfig[] {\n\t\treturn (\n\t\t\tthis.config.bricks?.fixed?.map((brick) => ({\n\t\t\t\tkey: brick.key,\n\t\t\t\tdetails: brick.config.details,\n\t\t\t\tpreview: brick.config.preview,\n\t\t\t\ttenants: brick.config.tenants,\n\t\t\t\tfields: brick.fieldTree,\n\t\t\t})) ?? []\n\t\t);\n\t}\n\tget builderBricks(): CollectionBrickConfig[] {\n\t\treturn (\n\t\t\tthis.config.bricks?.builder?.map((brick) => ({\n\t\t\t\tkey: brick.key,\n\t\t\t\tdetails: brick.config.details,\n\t\t\t\tpreview: brick.config.preview,\n\t\t\t\ttenants: brick.config.tenants,\n\t\t\t\tfields: brick.fieldTree,\n\t\t\t})) ?? []\n\t\t);\n\t}\n\tget brickInstances(): Array<BrickBuilder> {\n\t\treturn (this.config.bricks?.builder || []).concat(\n\t\t\tthis.config.bricks?.fixed || [],\n\t\t);\n\t}\n}\n\nexport default CollectionBuilder;\n"],"mappings":"oJAaA,IAAM,EAAN,cAEU,CAAa,CACtB,IACA,OACA,QAAoB,CAAC,EACrB,YAAwB,CAAC,EACzB,YACC,EACA,EACC,CACD,MAAM,EACN,KA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