Единый интерфейс элементов
This commit is contained in:
91
src/entitiy/element/model/interface.ts
Normal file
91
src/entitiy/element/model/interface.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { textDefinition } from '@/component/BasicElements'
|
||||
import { calibrationConditionsDefinition } from '@/component/BasicElements/CalibrationConditionsElement'
|
||||
import { checkboxDefinition } from '@/component/BasicElements/CheckboxElement'
|
||||
import { dateDefinition } from '@/component/BasicElements/DateElement'
|
||||
import { numberDefinition } from '@/component/BasicElements/NumberElement'
|
||||
import { radioDefinition } from '@/component/BasicElements/RadioElement'
|
||||
import { selectDefinition } from '@/component/BasicElements/SelectElement'
|
||||
import { textareaDefinition } from '@/component/BasicElements/TextareaElement'
|
||||
import { standardsDefinition } from '@/component/StandardsElement/definition'
|
||||
import { buttonGroupDefinition } from '@/entitiy/element/model/implementations/ButtonGroup'
|
||||
import { CellTarget } from '@/type/template'
|
||||
import { ReactNode } from 'react'
|
||||
|
||||
export interface ElementDefinition<Config = any, Value = any> {
|
||||
type: ElementType
|
||||
label: string
|
||||
icon: ReactNode
|
||||
|
||||
/* React-компоненты */
|
||||
Editor: React.FC<{ config: Config; onChange(c: Config): void }>
|
||||
Preview: React.FC<{ config: Config }>
|
||||
Render: React.FC<{ config: Config; value: Value; onChange?(v: Value): void }>
|
||||
|
||||
/* бизнес-логика */
|
||||
defaultConfig: Config
|
||||
mapToCells(config: Config): CellTarget[] // куда пишем данные
|
||||
|
||||
/* мета */
|
||||
version: number
|
||||
}
|
||||
|
||||
export const elementRegistry: Record<ElementType, ElementDefinition> = {} as any
|
||||
|
||||
// Функция для регистрации элемента
|
||||
export function registerElement<T extends ElementType>(
|
||||
type: T,
|
||||
definition: ElementDefinition
|
||||
) {
|
||||
elementRegistry[type] = definition
|
||||
}
|
||||
|
||||
// Получить все доступные типы элементов
|
||||
export function getAvailableElementTypes(): Array<{
|
||||
type: ElementType
|
||||
label: string
|
||||
icon: ReactNode
|
||||
}> {
|
||||
return Object.values(elementRegistry).map(({ type, label, icon }) => ({
|
||||
type,
|
||||
label,
|
||||
icon,
|
||||
}))
|
||||
}
|
||||
|
||||
// Получить определение элемента по типу
|
||||
export function getElementDefinition(
|
||||
type: ElementType
|
||||
): ElementDefinition | undefined {
|
||||
return elementRegistry[type]
|
||||
}
|
||||
|
||||
export const elementDefinitions = {
|
||||
text: textDefinition,
|
||||
select: selectDefinition,
|
||||
radio: radioDefinition,
|
||||
checkbox: checkboxDefinition,
|
||||
number: numberDefinition,
|
||||
textarea: textareaDefinition,
|
||||
date: dateDefinition,
|
||||
standards: standardsDefinition,
|
||||
calibration_conditions: calibrationConditionsDefinition,
|
||||
button_group: buttonGroupDefinition,
|
||||
} as const
|
||||
|
||||
export type ElementType = keyof typeof elementDefinitions
|
||||
|
||||
export function initializeElementRegistry() {
|
||||
// Базовые элементы
|
||||
registerElement('text', textDefinition)
|
||||
registerElement('select', selectDefinition)
|
||||
registerElement('number', numberDefinition)
|
||||
registerElement('date', dateDefinition)
|
||||
registerElement('textarea', textareaDefinition)
|
||||
registerElement('checkbox', checkboxDefinition)
|
||||
registerElement('radio', radioDefinition)
|
||||
registerElement('button_group', buttonGroupDefinition)
|
||||
registerElement('calibration_conditions', calibrationConditionsDefinition)
|
||||
|
||||
// Специальные элементы
|
||||
registerElement('standards', standardsDefinition)
|
||||
}
|
||||
Reference in New Issue
Block a user