UNPKG

@lucidcms/core

Version:

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

2 lines 2.98 kB
import{copy as e}from"../../../../i18n/copy.mjs";import t from"../../../../../constants/constants.mjs";import{codeFieldConfig as n}from"./config.mjs";import{zodToJsonSchema as r}from"../../utils/helpers.mjs";import i from"../../utils/zod-safe-parse.mjs";import a from"../../custom-field.mjs";import o from"../../utils/key-to-title.mjs";import s from"zod";var c=class extends a{type=n.type;config;key;props;constructor(n,r){super(),this.key=n,this.props=r,this.config={key:this.key,type:this.type,details:{label:this.props?.details?.label??e(`admin:fields.${this.type}.${this.key}.label`,{defaultMessage:o(this.key)}),summary:this.props?.details?.summary,placeholder:this.props?.details?.placeholder},ai:this.props?.ai,localized:this.props?.localized??!1,default:this.props?.default??null,languages:this.props?.languages??[...t.customFields.code.languages],index:this.props?.index,ui:{hidden:this.props?.ui?.hidden,disabled:this.props?.ui?.disabled,condition:this.props?.ui?.condition,width:this.props?.ui?.width},validation:this.props?.validation}}get supportsAi(){return!0}get jsonSchema(){return r(this.config.validation?.zod,{type:`object`,description:`A code snippet with its language.`,properties:{language:{type:`string`,enum:this.config.languages,description:`The language of the code snippet. Infer it from the existing value or the instruction, and keep the existing language unless asked to change it.`},value:{type:`string`,description:`The raw code snippet content. Do not wrap it in markdown code fences.`}},required:[`language`,`value`],additionalProperties:!1})}getSchemaDefinition(e){return{data:{columns:[{name:this.key,type:e.db.getDataType(`json`),nullable:!0,default:this.config.default}]},error:void 0}}formatResponseValue(e){return e??this.config.default??null}formatAiGeneratedValue(e){if(typeof e==`string`){try{let t=JSON.parse(e),n=this.coerceAiCodeValue(t);if(n)return{success:!0,value:n}}catch{}return{success:!0,value:{language:this.fallbackAiLanguage(),value:e}}}return{success:!0,value:this.coerceAiCodeValue(e)??{language:this.fallbackAiLanguage(),value:String(e??``)}}}coerceAiCodeValue(e){if(!e||typeof e!=`object`||Array.isArray(e))return null;let t=e;return typeof t.value==`string`?{language:typeof t.language==`string`&&this.config.languages.includes(t.language)?t.language:this.fallbackAiLanguage(),value:t.value}:null}fallbackAiLanguage(){return this.config.languages[0]??`text`}normalizeInputValue(e){if(typeof e==`string`&&e.trim()===``)return null;if(e&&typeof e==`object`&&!Array.isArray(e)){let t=e;if(typeof t.value==`string`&&t.value.trim()===``)return null}return e}uniqueValidation(t){let n=i(t,s.object({language:s.string(),value:s.string()}));if(!n.valid)return n;let r=t;return this.config.languages.includes(r.language)?{valid:!0}:{valid:!1,message:e(`server:core.fields.code.validation.language.error.message`,{data:{valid:this.config.languages.join(`, `)}})}}};export{c as default}; //# sourceMappingURL=custom-field.mjs.map