diff --git a/core/src/interface.ts b/core/src/interface.ts index 4326171e..5ed6712f 100644 --- a/core/src/interface.ts +++ b/core/src/interface.ts @@ -19,7 +19,7 @@ export interface Pages { [collection: string]: { isFetching?: boolean; page?: number; ids: string[] }; } -export type SortableField = +export type SortableField = | { key: string; name: string; @@ -27,7 +27,7 @@ export type SortableField = } | ({ key: string; - } & Field); + } & Field); export interface SortObject { key: string; @@ -131,11 +131,11 @@ export interface EditorConfig { frame?: boolean; } -export interface CollectionFile { +export interface CollectionFile { name: string; label: string; file: string; - fields: Field[]; + fields: Field[]; label_singular?: string; description?: string; media_folder?: string; @@ -157,17 +157,18 @@ export interface I18nSettings { export type Format = keyof typeof formatExtensions; -export interface i18nCollection extends Omit { - i18n: Required['i18n']; +export interface i18nCollection + extends Omit, 'i18n'> { + i18n: Required>['i18n']; } -export interface Collection { +export interface Collection { name: string; description?: string; icon?: string; folder?: string; - files?: CollectionFile[]; - fields: Field[]; + files?: CollectionFile[]; + fields: Field[]; isFetching?: boolean; media_folder?: string; public_folder?: string; @@ -193,7 +194,7 @@ export interface Collection { editor?: EditorConfig; } -export type Collections = Record; +export type Collections = Record>; export interface MediaLibraryInstance { show: (args: { @@ -221,9 +222,9 @@ export type TranslatedProps = T & ReactPolyglotTranslateProps; export type GetAssetFunction = (path: string, field?: Field) => Promise; -export interface WidgetControlProps { - collection: Collection; - config: Config; +export interface WidgetControlProps { + collection: Collection; + config: Config; entry: Entry; field: F; fieldsErrors: FieldsErrors; @@ -249,19 +250,27 @@ export interface WidgetControlProps { value: T | undefined | null; } -export interface WidgetPreviewProps { - config: Config; - collection: Collection; +export interface WidgetPreviewProps< + T = unknown, + F extends Field = Field, + EF extends BaseField = Field, +> { + config: Config; + collection: Collection; entry: Entry; field: RenderedField; getAsset: GetAssetFunction; value: T | undefined | null; } -export type WidgetPreviewComponent = +export type WidgetPreviewComponent< + T = unknown, + F extends Field = Field, + EF extends BaseField = Field, +> = // eslint-disable-next-line @typescript-eslint/no-explicit-any | React.ReactElement> - | ComponentType>; + | ComponentType>; export type WidgetsFor

= ( name: K, @@ -275,9 +284,9 @@ export type WidgetsFor

= ( widgets: Record; }; -export interface TemplatePreviewProps { - collection: Collection; - fields: Field[]; +export interface TemplatePreviewProps { + collection: Collection; + fields: Field[]; entry: Entry; document: Document | undefined | null; window: Window | undefined | null; @@ -286,7 +295,9 @@ export interface TemplatePreviewProps { widgetsFor: WidgetsFor; } -export type TemplatePreviewComponent = ComponentType>; +export type TemplatePreviewComponent = ComponentType< + TemplatePreviewProps +>; export interface WidgetOptions { validator?: Widget['validator']; @@ -294,18 +305,18 @@ export interface WidgetOptions { schema?: Widget['schema']; } -export interface Widget { - control: ComponentType>; - preview?: WidgetPreviewComponent; +export interface Widget { + control: ComponentType>; + preview?: WidgetPreviewComponent; validator: FieldValidationMethod; getValidValue: (value: T | undefined | null) => T | undefined | null; schema?: PropertiesSchema; } -export interface WidgetParam { +export interface WidgetParam { name: string; - controlComponent: Widget['control']; - previewComponent?: Widget['preview']; + controlComponent: Widget['control']; + previewComponent?: Widget['preview']; options?: WidgetOptions; } @@ -529,15 +540,15 @@ export interface FileOrImageField extends BaseField { public_folder?: string; } -export interface ObjectField extends BaseField { +export interface ObjectField extends BaseField { widget: 'object'; collapsed?: boolean; summary?: string; - fields: Field[]; + fields: Field[]; } -export interface ListField extends BaseField { +export interface ListField extends BaseField { widget: 'list'; default?: ObjectValue[]; @@ -545,7 +556,7 @@ export interface ListField extends BaseField { collapsed?: boolean; summary?: string; label_singular?: string; - fields?: Field[]; + fields?: Field[]; max?: number; min?: number; add_to_top?: boolean; @@ -618,7 +629,11 @@ export interface StringOrTextField extends BaseField { default?: string; } -export type Field = +export interface UnknownField extends BaseField { + widget: 'unknown'; +} + +export type Field = | BooleanField | CodeField | ColorField @@ -632,7 +647,8 @@ export type Field = | RelationField | SelectField | HiddenField - | StringOrTextField; + | StringOrTextField + | EF; export interface ViewFilter { id: string; @@ -701,9 +717,9 @@ export interface LocalBackend { allowed_hosts?: string[]; } -export interface Config { +export interface Config { backend: Backend; - collections: Collection[]; + collections: Collection[]; locale?: string; site_id?: string; site_url?: string; @@ -723,8 +739,8 @@ export interface Config { search?: boolean; } -export interface InitOptions { - config: Config; +export interface InitOptions { + config: Config; } export interface BackendInitializerOptions { diff --git a/core/src/lib/registry.ts b/core/src/lib/registry.ts index 147ac64d..7e0a26c0 100644 --- a/core/src/lib/registry.ts +++ b/core/src/lib/registry.ts @@ -121,8 +121,7 @@ export function getPreviewTemplate(name: string): TemplatePreviewComponent[]): void; +export function registerWidget(widgets: WidgetParam[]): void; export function registerWidget(widget: WidgetParam): void; export function registerWidget( name: string,