import type { CountryCode } from 'libphonenumber-js/min'; export type EponymeFieldType = 'string' | 'slug' | 'email' | 'phone' | 'url' | 'mediaPlayer' | 'textarea' | 'richText' | 'number' | 'boolean' | 'file' | 'image' | 'select' | 'radio' | 'checkboxGroup' | 'tags' | 'date' | 'datetime' | 'duration' | 'color' | 'custom' | 'array' | 'section' | 'tabs'; export type FieldVisibilityCondition = { field: string; equals: unknown; notEquals?: never; } | { field: string; notEquals: unknown; equals?: never; }; export type FieldValidationResult = true | string | string[] | null | undefined; export type FieldValidator = { bivarianceHack(value: T, data: Readonly>): FieldValidationResult; }['bivarianceHack']; export interface DefaultFieldOptions { label?: string; description?: string; required?: boolean; defaultValue?: T; visibleWhen?: FieldVisibilityCondition | FieldVisibilityCondition[]; validate?: FieldValidator; } export interface EponymeCustomFieldValues { } export type EponymeCustomFieldOptions = DefaultFieldOptions & Options; export interface EponymeCustomFieldTypeDefinition> { defaultValue: Value; normalize?: (value: unknown, options: Readonly>) => unknown; validate: (value: unknown, options: Readonly>, data: Readonly>) => FieldValidationResult; } export type InferEponymeCustomFieldValue = Definition extends EponymeCustomFieldTypeDefinition ? Value : unknown; export type InferEponymeCustomFieldOptions = Definition extends EponymeCustomFieldTypeDefinition ? Options : Record; export interface EponymeCustomFieldDefinition> { type: 'custom'; name: Name; options: EponymeCustomFieldOptions; } export interface EponymeCustomFieldComponentProps> { id: string; modelValue?: Value; label: string; description?: string; required?: boolean; errors?: string[]; disabled?: boolean; options: Readonly>; } export interface TextFieldOptions extends DefaultFieldOptions { minLength?: number; maxLength?: number; showCounter?: boolean; placeholder?: string; trim?: boolean; regex?: RegExp; mask?: string; } export type StringFieldOptions = TextFieldOptions; export type TextareaFieldOptions = TextFieldOptions; export interface StringFieldDefinition { type: 'string'; options: StringFieldOptions; } export interface SlugFieldOptions extends DefaultFieldOptions { minLength?: number; maxLength?: number; showCounter?: boolean; placeholder?: string; } export interface SlugFieldDefinition { type: 'slug'; options: SlugFieldOptions; } export interface TextareaFieldDefinition { type: 'textarea'; options: TextareaFieldOptions; } export interface RichTextFieldOptions extends DefaultFieldOptions { minLength?: number; maxLength?: number; showCounter?: boolean; placeholder?: string; } export interface RichTextFieldDefinition { type: 'richText'; options: RichTextFieldOptions; } export type EmailFieldOptions = TextFieldOptions; export interface EmailFieldDefinition { type: 'email'; options: EmailFieldOptions; } /** Countries a phone field accepts, as ISO 3166-1 alpha-2 codes. */ export type PhoneCountry = CountryCode; export interface PhoneFieldOptions extends DefaultFieldOptions { placeholder?: string; countries?: PhoneCountry[]; /** Country a number typed without an international prefix belongs to. */ defaultCountry?: PhoneCountry; /** * Whether a number may be written in national form and resolved against `defaultCountry`. * * @default true */ detectCountry?: boolean; /** Value of the input's `autocomplete` attribute. */ autocomplete?: 'tel' | 'tel-national' | 'tel-country-code'; } export interface PhoneFieldDefinition { type: 'phone'; options: PhoneFieldOptions; } export type UrlType = 'internal' | 'external'; export interface UrlValue { href: string; type: UrlType; openInNewTab: boolean; download?: boolean; } /** Scheme of an external link, without the trailing colon. */ export type UrlProtocol = 'http' | 'https' | 'mailto' | 'tel' | 'sms' | 'ftp' | (string & {}); export interface UrlFieldOptions extends DefaultFieldOptions { placeholder?: string; /** * Schemes an external link may use. * * @default ['http', 'https'] */ protocols?: UrlProtocol[]; } export interface UrlFieldDefinition { type: 'url'; options: UrlFieldOptions; } export type MediaPlayerProvider = 'youtube' | 'vimeo' | 'vkvideo' | 'url'; export interface MediaPlayerValue { /** Detected from `url`. Empty while nothing has been entered, or when nothing recognized it. */ provider: MediaPlayerProvider | ''; /** Address as it was entered. */ url: string; /** Video id at its provider. Empty for `url`, which has none. */ id: string; } export interface MediaPlayerFieldOptions extends DefaultFieldOptions { placeholder?: string; /** * Sources this field accepts. * * @default ['youtube', 'vimeo', 'vkvideo', 'url'] */ providers?: readonly MediaPlayerProvider[]; } export interface MediaPlayerFieldDefinition { type: 'mediaPlayer'; options: MediaPlayerFieldOptions; } export interface NumberFieldOptions extends DefaultFieldOptions { min?: number; max?: number; step?: number; slider?: boolean; /** Displayed inside the input. The stored value stays the bare number. */ prefix?: string; suffix?: string; } export interface NumberFieldDefinition { type: 'number'; options: NumberFieldOptions; } export type BooleanFieldOptions = DefaultFieldOptions; export interface BooleanFieldDefinition { type: 'boolean'; options: BooleanFieldOptions; } export interface FileFieldOptions extends DefaultFieldOptions { label?: string; description?: string; placeholder?: string; /** * Media types this field accepts, narrowing the module-wide `storage.accept`. * * @example ['application/pdf'] */ accept?: string[]; /** Largest accepted upload for this field, in bytes. */ maxSize?: number; } export interface FileFieldDefinition { type: 'file'; options: FileFieldOptions; } /** Where an image may come from: another site, a path on this one, or the media library. */ export type ImageSource = 'absolute' | 'relative' | 'upload'; /** * The point of a picture that has to stay in view whatever it is cropped to, as fractions of its width and * height from the top left corner: `{ x: 0.5, y: 0.5 }` is the centre. */ export interface EponymeImageFocal { x: number; y: number; } /** What a `field.image()` stores and returns. An empty field is `null`. */ export interface EponymeImageValue { /** Address the picture is served from. */ src: string; /** Text alternative. Empty for a decorative picture. */ alt: string; /** Intrinsic size in pixels, when it is known: read on upload, absent for a value saved as a bare address. */ width?: number; height?: number; focal: EponymeImageFocal; caption?: string; /** Media type, such as `image/webp`. */ mime?: string; /** Size in bytes. */ size?: number; } /** * An aspect ratio, as `width:height` (`'16:9'`, `'1200:630'`) or as a number (`1.5`, `16 / 9`). */ export type EponymeImageRatio = `${number}:${number}` | `${number}/${number}` | number; /** A crop the editor previews, and that a page can ask `` for. */ export interface EponymeImageCrop { label?: string; ratio: EponymeImageRatio; } /** What `defaultValue` takes: an address on its own is enough. */ export type EponymeImageInput = string | (Partial & { src: string; }); export interface ImageFieldOptions extends Omit, Omit, 'defaultValue'> { defaultValue?: EponymeImageInput | null; /** * Origins this image may use. * * @default ['absolute', 'relative', 'upload'] */ sources?: readonly ImageSource[]; /** * Refuses to publish the image without a text alternative. A decorative picture should leave this off, * and its alternative empty. * * @default false */ requireAlt?: boolean; /** Offers a caption under the text alternative. */ caption?: boolean; /** * Lets the editor pick the point a crop keeps in view. * * @default true */ focal?: boolean; /** * Crops previewed in the editor, so the author sees what each page will cut. Turn them off with `[]`. * * @default ['16:9', '1:1', '4:5'] */ crops?: ReadonlyArray; /** Smallest accepted width, in pixels. Checked whenever the width is known. */ minWidth?: number; /** Smallest accepted height, in pixels. Checked whenever the height is known. */ minHeight?: number; } export interface ImageFieldDefinition { type: 'image'; options: ImageFieldOptions; } export interface SelectOption { label: string; value: T; } export interface SelectFieldOptions extends DefaultFieldOptions { options: ReadonlyArray>; placeholder?: string; } export interface SelectFieldDefinition { type: 'select'; options: SelectFieldOptions; } export interface RadioFieldOptions extends DefaultFieldOptions { options: ReadonlyArray>; } export interface RadioFieldDefinition { type: 'radio'; options: RadioFieldOptions; } export interface CheckboxGroupFieldOptions extends DefaultFieldOptions { options: ReadonlyArray>; minItems?: number; maxItems?: number; } export interface CheckboxGroupFieldDefinition { type: 'checkboxGroup'; options: CheckboxGroupFieldOptions; } export interface TagsFieldOptions extends DefaultFieldOptions { /** Offered while typing. With `allowCustom` off, they are also the only accepted values. */ suggestions?: readonly T[]; /** * Whether a tag outside `suggestions` may be entered. * * @default false */ allowCustom?: boolean; minItems?: number; maxItems?: number; placeholder?: string; } /** * Values a tags field may hold, read from the options as they were written: the suggestions themselves * while the list is closed, any string once `allowCustom` opens it. */ export type TagsValueOf = O extends { allowCustom: true; } ? string : O extends { suggestions: readonly (infer S extends string)[]; } ? S : string; export interface TagsFieldDefinition { type: 'tags'; options: O; } export interface TodayDateDefault { readonly __eponymeDefault: 'today'; } export interface DateFieldOptions extends Omit, 'defaultValue'> { defaultValue?: string | TodayDateDefault; min?: string; max?: string; } export interface DateFieldDefinition { type: 'date'; options: DateFieldOptions; } export interface DateTimeFieldOptions extends DefaultFieldOptions { /** Inclusive lower bound, as an ISO 8601 instant with an explicit time zone. */ min?: string; /** Inclusive upper bound, as an ISO 8601 instant with an explicit time zone. */ max?: string; } export interface DateTimeFieldDefinition { type: 'datetime'; options: DateTimeFieldOptions; } export type DurationInput = number | string; export interface DurationFieldOptions extends Omit, 'defaultValue'> { /** Milliseconds, or a readable duration such as `1h 10min`. */ defaultValue?: DurationInput; /** Inclusive lower bound, in milliseconds or as a readable duration. */ min?: DurationInput; /** Inclusive upper bound, in milliseconds or as a readable duration. */ max?: DurationInput; } export interface NormalizedDurationFieldOptions extends Omit { defaultValue?: number; min?: number; max?: number; } export interface DurationFieldDefinition { type: 'duration'; options: NormalizedDurationFieldOptions; } export interface ColorPreset { label: string; value: string; } export interface ColorFieldOptions extends DefaultFieldOptions { /** Clickable palette for this field; falls back to the module-wide `colorPresets`. */ presets?: ReadonlyArray; /** Keeps the native color picker next to the palette. Turn it off to allow presets only. */ allowCustom?: boolean; } export interface ColorFieldDefinition { type: 'color'; options: ColorFieldOptions; } export interface RelationFieldOptions extends DefaultFieldOptions { /** * Name of the collection pointed at, as it is declared in the config - or of a folder of singletons, in * which case the stored value is the singleton's own name inside that folder. */ to: To; /** Holds a list of slugs rather than one. */ multiple?: Multiple; /** Upper bound on a `multiple` relation. */ maxItems?: number; placeholder?: string; } /** Points at an entry of a collection, by slug, or at a singleton of a folder, by name. */ export interface RelationFieldDefinition { type: 'relation'; options: RelationFieldOptions; } export type ArrayItemFieldDefinition = StringFieldDefinition | SlugFieldDefinition | EmailFieldDefinition | PhoneFieldDefinition | UrlFieldDefinition | MediaPlayerFieldDefinition | TextareaFieldDefinition | RichTextFieldDefinition | NumberFieldDefinition | BooleanFieldDefinition | FileFieldDefinition | ImageFieldDefinition | SelectFieldDefinition | RadioFieldDefinition | CheckboxGroupFieldDefinition | TagsFieldDefinition | DateFieldDefinition | DateTimeFieldDefinition | DurationFieldDefinition | ColorFieldDefinition | RelationFieldDefinition | EponymeCustomFieldDefinition; export type ArrayItemSchema = Record; export type ArrayItemDefinition = ArrayItemFieldDefinition | ArrayItemSchema; export type ArrayItemValue = T extends NumberFieldDefinition | DurationFieldDefinition ? number : T extends BooleanFieldDefinition ? boolean : T extends UrlFieldDefinition ? UrlValue : T extends MediaPlayerFieldDefinition ? MediaPlayerValue : T extends CheckboxGroupFieldDefinition ? Value[] : T extends RelationFieldDefinition ? (Multiple extends true ? string[] : string) : T extends EponymeCustomFieldDefinition ? Value : T extends ImageFieldDefinition ? EponymeImageValue | null : T extends ArrayItemFieldDefinition ? string : T extends ArrayItemSchema ? { [K in keyof T]: ArrayItemValue; } : never; export interface ArrayFieldOptions extends DefaultFieldOptions>> { of: T; minItems?: number; maxItems?: number; addLabel?: string; /** * Heading of each item. * * @default 'Item $i' * @example * ```ts * itemLabel: '$question' * itemLabel: 'Step $i – $title' * ``` */ itemLabel?: string; /** Starts every item folded, for an array whose items are long. */ collapsed?: boolean; showCounter?: boolean; } export interface ArrayFieldDefinition { type: 'array'; options: ArrayFieldOptions; } /** * A section holds sections, which is what lets a `defineBlock()` sit inside a tab: a tab is a section under * the hood. */ export type SectionItemFieldDefinition = ArrayItemFieldDefinition | ArrayFieldDefinition | SectionFieldDefinition; export type SectionSchema = Record; export type SectionItemValue = T extends ArrayFieldDefinition ? Array> : T extends SectionFieldDefinition ? SectionValue
: T extends ArrayItemFieldDefinition ? ArrayItemValue : never; export type SectionValue = { [K in keyof T]: SectionItemValue; }; export interface SectionFieldOptions extends DefaultFieldOptions> { fields: T; } export interface SectionFieldDefinition { type: 'section'; options: SectionFieldOptions; } export interface TabDefinition { label?: string; fields: T; } export type TabsSchema = Record; export type TabsValue = { [K in keyof T]: SectionValue; }; export interface TabFieldOptions extends DefaultFieldOptions> { tabs: T; } export interface TabFieldDefinition { type: 'tabs'; options: TabFieldOptions; } export type FieldDefinition = SectionItemFieldDefinition | SectionFieldDefinition | TabFieldDefinition;