UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 6.92 kB
{"version":3,"file":"custom-field.mjs","names":[],"sources":["../../../../../../src/libs/collection/custom-fields/fields/rich-text/custom-field.ts"],"sourcesContent":["import z from \"zod\";\nimport type { ServiceResponse } from \"../../../../../types.js\";\nimport {\n\tgetObject,\n\trichTextHasContent,\n} from \"../../../../../utils/helpers/index.js\";\nimport { copy } from \"../../../../i18n/index.js\";\nimport { defaultTextFieldAiGuidance } from \"../../ai-guidance.js\";\nimport CustomField from \"../../custom-field.js\";\nimport type {\n\tCFConfig,\n\tCFProps,\n\tCFResponse,\n\tCustomFieldAiFormatResponse,\n\tCustomFieldErrorItem,\n\tGetSchemaDefinitionProps,\n\tSchemaDefinition,\n} from \"../../types.js\";\nimport keyToTitle from \"../../utils/key-to-title.js\";\nimport zodSafeParse from \"../../utils/zod-safe-parse.js\";\nimport { richTextFieldConfig } from \"./config.js\";\n\nclass RichTextCustomField extends CustomField<\"rich-text\"> {\n\ttype = richTextFieldConfig.type;\n\tconfig;\n\tkey;\n\tprops;\n\tconstructor(key: string, props?: CFProps<\"rich-text\">) {\n\t\tsuper();\n\t\tthis.key = key;\n\t\tthis.props = props;\n\t\tthis.config = {\n\t\t\tkey: this.key,\n\t\t\ttype: this.type,\n\t\t\tdetails: {\n\t\t\t\tlabel:\n\t\t\t\t\tthis.props?.details?.label ??\n\t\t\t\t\tcopy(`admin:fields.${this.type}.${this.key}.label`, {\n\t\t\t\t\t\tdefaultMessage: keyToTitle(this.key),\n\t\t\t\t\t}),\n\t\t\t\tsummary: this.props?.details?.summary,\n\t\t\t\tplaceholder: this.props?.details?.placeholder,\n\t\t\t},\n\t\t\tai: this.props?.ai,\n\t\t\tlocalized: this.props?.localized ?? true,\n\t\t\tdefault: this.props?.default ?? {\n\t\t\t\ttype: \"doc\",\n\t\t\t\tcontent: [{ type: \"paragraph\" }],\n\t\t\t},\n\t\t\tindex: this.props?.index,\n\t\t\tui: {\n\t\t\t\thidden: this.props?.ui?.hidden,\n\t\t\t\tdisabled: this.props?.ui?.disabled,\n\t\t\t\tcondition: this.props?.ui?.condition,\n\t\t\t\twidth: this.props?.ui?.width,\n\t\t\t},\n\t\t\tvalidation: this.props?.validation,\n\t\t} satisfies CFConfig<\"rich-text\">;\n\t}\n\toverride get supportsAi() {\n\t\treturn true;\n\t}\n\tprotected override get defaultAiGuidance() {\n\t\treturn defaultTextFieldAiGuidance;\n\t}\n\toverride get errors(): {\n\t\tfieldType: CustomFieldErrorItem;\n\t\trequired: CustomFieldErrorItem;\n\t\tzod: CustomFieldErrorItem;\n\t} {\n\t\tconst errors = super.errors;\n\n\t\treturn {\n\t\t\t...errors,\n\t\t\trequired: {\n\t\t\t\tcondition: (value: unknown) =>\n\t\t\t\t\terrors.required.condition?.(value) === true ||\n\t\t\t\t\t(getObject(value) !== null && !richTextHasContent(value)),\n\t\t\t\tmessage: errors.required.message,\n\t\t\t},\n\t\t};\n\t}\n\toverride get jsonSchema() {\n\t\tconst textNodeSchema = {\n\t\t\ttype: \"object\",\n\t\t\tadditionalProperties: false,\n\t\t\trequired: [\"type\", \"text\"],\n\t\t\tproperties: {\n\t\t\t\ttype: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\tconst: \"text\",\n\t\t\t\t},\n\t\t\t\ttext: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t\tconst paragraphNodeSchema = {\n\t\t\ttype: \"object\",\n\t\t\tadditionalProperties: false,\n\t\t\trequired: [\"type\", \"content\"],\n\t\t\tproperties: {\n\t\t\t\ttype: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\tconst: \"paragraph\",\n\t\t\t\t},\n\t\t\t\tcontent: {\n\t\t\t\t\ttype: \"array\",\n\t\t\t\t\titems: textNodeSchema,\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\n\t\treturn {\n\t\t\ttype: \"object\",\n\t\t\tadditionalProperties: false,\n\t\t\trequired: [\"type\", \"content\"],\n\t\t\tproperties: {\n\t\t\t\ttype: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\tconst: \"doc\",\n\t\t\t\t},\n\t\t\t\tcontent: {\n\t\t\t\t\ttype: \"array\",\n\t\t\t\t\titems: paragraphNodeSchema,\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\tgetSchemaDefinition(\n\t\tprops: GetSchemaDefinitionProps,\n\t): Awaited<ServiceResponse<SchemaDefinition>> {\n\t\treturn {\n\t\t\tdata: {\n\t\t\t\tcolumns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: this.key,\n\t\t\t\t\t\ttype: props.db.getDataType(\"json\"),\n\t\t\t\t\t\tnullable: true,\n\t\t\t\t\t\tdefault: this.config.default,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\terror: undefined,\n\t\t};\n\t}\n\tformatResponseValue(value?: Record<string, unknown> | null) {\n\t\treturn (value ??\n\t\t\tthis.config.default ??\n\t\t\tnull) satisfies CFResponse<\"rich-text\">[\"value\"];\n\t}\n\toverride formatAiGeneratedValue(value: unknown): CustomFieldAiFormatResponse {\n\t\tif (value && typeof value === \"object\" && !Array.isArray(value)) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tvalue,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tmessage: copy(\"server:core.routes.ai.generate.error.message\"),\n\t\t};\n\t}\n\tuniqueValidation(value: unknown) {\n\t\tconst valueSchema = z.record(\n\t\t\tz.union([z.string(), z.number(), z.symbol()]),\n\t\t\tz.unknown(),\n\t\t);\n\n\t\tconst valueValidate = zodSafeParse(value, valueSchema);\n\t\tif (!valueValidate.valid) return valueValidate;\n\n\t\treturn { valid: true };\n\t}\n}\n\nexport default RichTextCustomField;\n"],"mappings":"6cAsBA,IAAM,EAAN,cAAkC,CAAyB,CAC1D,KAAO,EAAoB,KAC3B,OACA,IACA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