Единый интерфейс элементов
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
getAvailableElementTypes,
|
||||
getElementDefinition,
|
||||
} from '@/lib/element-registry'
|
||||
} from '@/entitiy/element/model/interface'
|
||||
import {
|
||||
autoArrangeElements,
|
||||
findFreePosition,
|
||||
@@ -109,8 +109,6 @@ const CellTargetEditor: React.FC<{
|
||||
<SelectContent>
|
||||
<SelectItem value="L">L (Левый)</SelectItem>
|
||||
<SelectItem value="R">R (Правый)</SelectItem>
|
||||
<SelectItem value="Report">Report</SelectItem>
|
||||
<SelectItem value="Calculations">Calculations</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
@@ -254,7 +252,7 @@ const ElementPreview: React.FC<{ element: Partial<TemplateElement> }> = ({
|
||||
</div>
|
||||
)
|
||||
|
||||
case 'calibration-conditions':
|
||||
case 'calibration_conditions':
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-3 rounded-lg border bg-muted/50 px-3 py-2">
|
||||
@@ -287,7 +285,7 @@ const ElementPreview: React.FC<{ element: Partial<TemplateElement> }> = ({
|
||||
</div>
|
||||
)
|
||||
|
||||
case 'button-group':
|
||||
case 'button_group':
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
@@ -381,7 +379,7 @@ const ElementForm: React.FC<ElementFormProps> = ({ formData, setFormData }) => {
|
||||
const needsOptions =
|
||||
formData.type === 'select' ||
|
||||
formData.type === 'radio' ||
|
||||
formData.type === 'button-group'
|
||||
formData.type === 'button_group'
|
||||
|
||||
// Если есть определение элемента в реестре, используем его редактор
|
||||
if (elementDefinition && elementDefinition.Editor) {
|
||||
@@ -395,35 +393,21 @@ const ElementForm: React.FC<ElementFormProps> = ({ formData, setFormData }) => {
|
||||
value={formData.type}
|
||||
onValueChange={value => {
|
||||
const newType = value as ElementType
|
||||
// console.log('Element type changed to:', newType)
|
||||
const elementDefinition = getElementDefinition(newType)
|
||||
|
||||
setFormData(prev => {
|
||||
const updates: Partial<TemplateElement> = { type: newType }
|
||||
|
||||
// Если выбран тип "standards" или "calibration-conditions", устанавливаем значения по умолчанию
|
||||
if (
|
||||
newType === 'standards' ||
|
||||
newType === 'calibration-conditions'
|
||||
) {
|
||||
// console.log(
|
||||
// `${newType} type selected, getting element definition`
|
||||
// )
|
||||
const elementDefinition = getElementDefinition(newType)
|
||||
// console.log('Element definition:', elementDefinition)
|
||||
|
||||
if (elementDefinition && elementDefinition.defaultConfig) {
|
||||
const defaultConfig =
|
||||
elementDefinition.defaultConfig as any
|
||||
// console.log('Default config:', defaultConfig)
|
||||
updates.targetCells =
|
||||
elementDefinition.mapToCells?.(defaultConfig) || []
|
||||
// console.log('Generated targetCells:', updates.targetCells)
|
||||
}
|
||||
const updates: Partial<TemplateElement> = {
|
||||
type: newType,
|
||||
// Сбрасываем targetCells при смене типа
|
||||
targetCells: [],
|
||||
}
|
||||
|
||||
const newFormData = { ...prev, ...updates }
|
||||
// console.log('New form data:', newFormData)
|
||||
return newFormData
|
||||
// Применяем конфиг по умолчанию из определения элемента
|
||||
if (elementDefinition && elementDefinition.defaultConfig) {
|
||||
Object.assign(updates, elementDefinition.defaultConfig)
|
||||
}
|
||||
|
||||
return { ...prev, ...updates }
|
||||
})
|
||||
}}
|
||||
>
|
||||
@@ -520,40 +504,21 @@ const ElementForm: React.FC<ElementFormProps> = ({ formData, setFormData }) => {
|
||||
value={formData.type}
|
||||
onValueChange={value => {
|
||||
const newType = value as ElementType
|
||||
// console.log('Element type changed to (fallback):', newType)
|
||||
const elementDefinition = getElementDefinition(newType)
|
||||
|
||||
setFormData(prev => {
|
||||
const updates: Partial<TemplateElement> = { type: newType }
|
||||
|
||||
// Если выбран тип "standards" или "calibration-conditions", устанавливаем значения по умолчанию
|
||||
if (
|
||||
newType === 'standards' ||
|
||||
newType === 'calibration-conditions'
|
||||
) {
|
||||
// console.log(
|
||||
// `${newType} type selected (fallback), getting element definition`
|
||||
// )
|
||||
const elementDefinition = getElementDefinition(newType)
|
||||
// console.log(
|
||||
// 'Element definition (fallback):',
|
||||
// elementDefinition
|
||||
// )
|
||||
|
||||
if (elementDefinition && elementDefinition.defaultConfig) {
|
||||
const defaultConfig = elementDefinition.defaultConfig as any
|
||||
// console.log('Default config (fallback):', defaultConfig)
|
||||
updates.targetCells =
|
||||
elementDefinition.mapToCells?.(defaultConfig) || []
|
||||
// console.log(
|
||||
// 'Generated targetCells (fallback):',
|
||||
// updates.targetCells
|
||||
// )
|
||||
}
|
||||
const updates: Partial<TemplateElement> = {
|
||||
type: newType,
|
||||
// Сбрасываем targetCells при смене типа
|
||||
targetCells: [],
|
||||
}
|
||||
|
||||
const newFormData = { ...prev, ...updates }
|
||||
// console.log('New form data (fallback):', newFormData)
|
||||
return newFormData
|
||||
// Применяем конфиг по умолчанию из определения элемента
|
||||
if (elementDefinition && elementDefinition.defaultConfig) {
|
||||
Object.assign(updates, elementDefinition.defaultConfig)
|
||||
}
|
||||
|
||||
return { ...prev, ...updates }
|
||||
})
|
||||
}}
|
||||
>
|
||||
@@ -760,23 +725,14 @@ export const ElementConstructor: React.FC<ElementConstructorProps> = ({
|
||||
}
|
||||
|
||||
const handleAddElement = () => {
|
||||
// console.log('handleAddElement called with formData:', formData)
|
||||
|
||||
if (!formData.label) {
|
||||
// console.log('Validation failed: missing label')
|
||||
return
|
||||
}
|
||||
|
||||
// Для элементов типа "standards", "calibration-conditions" и "button-group" не требуем targetCells, так как они генерируются автоматически или не нужны
|
||||
if (
|
||||
formData.type !== 'standards' &&
|
||||
formData.type !== 'calibration-conditions' &&
|
||||
formData.type !== 'button-group' &&
|
||||
!formData.targetCells?.length
|
||||
) {
|
||||
// console.log(
|
||||
// 'Validation failed: missing targetCells for element that requires manual targetCells'
|
||||
// )
|
||||
// Получаем определение элемента
|
||||
const elementDefinition = getElementDefinition(formData.type as ElementType)
|
||||
if (!elementDefinition) {
|
||||
console.error(`Element definition for ${formData.type} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -790,25 +746,10 @@ export const ElementConstructor: React.FC<ElementConstructorProps> = ({
|
||||
layoutSettings.gridSize
|
||||
)
|
||||
|
||||
// Специальная обработка для элементов с автоматической генерацией targetCells
|
||||
// Используем targetCells из формы или генерируем через mapToCells
|
||||
let targetCells = formData.targetCells || []
|
||||
if (
|
||||
formData.type === 'standards' ||
|
||||
formData.type === 'calibration-conditions'
|
||||
) {
|
||||
// Генерируем targetCells автоматически, если их нет
|
||||
if (!targetCells.length) {
|
||||
const elementDefinition = getElementDefinition(formData.type)
|
||||
if (elementDefinition && elementDefinition.defaultConfig) {
|
||||
const defaultConfig = elementDefinition.defaultConfig as any
|
||||
targetCells = elementDefinition.mapToCells?.(defaultConfig) || []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Для button-group не нужны targetCells, так как это UI элемент
|
||||
if (formData.type === 'button-group') {
|
||||
targetCells = []
|
||||
if (!targetCells.length) {
|
||||
targetCells = elementDefinition.mapToCells(formData as any) || []
|
||||
}
|
||||
|
||||
const newElement: TemplateElement = {
|
||||
@@ -837,14 +778,20 @@ export const ElementConstructor: React.FC<ElementConstructorProps> = ({
|
||||
const handleUpdateElement = () => {
|
||||
if (!editingElement || !formData.label) return
|
||||
|
||||
// Для элементов типа "standards", "calibration-conditions" и "button-group" не требуем targetCells, так как они генерируются автоматически или не нужны
|
||||
if (
|
||||
formData.type !== 'standards' &&
|
||||
formData.type !== 'calibration-conditions' &&
|
||||
formData.type !== 'button-group' &&
|
||||
!formData.targetCells?.length
|
||||
// Получаем определение элемента
|
||||
const elementDefinition = getElementDefinition(
|
||||
editingElement.type as ElementType
|
||||
)
|
||||
if (!elementDefinition) {
|
||||
console.error(`Element definition for ${editingElement.type} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
// Используем targetCells из формы или генерируем через mapToCells
|
||||
let targetCells = formData.targetCells || []
|
||||
if (!targetCells.length) {
|
||||
targetCells = elementDefinition.mapToCells(formData as any) || []
|
||||
}
|
||||
|
||||
const updatedData = {
|
||||
...formData,
|
||||
@@ -852,6 +799,7 @@ export const ElementConstructor: React.FC<ElementConstructorProps> = ({
|
||||
formData.label?.toLowerCase().replace(/\s+/g, '_') ||
|
||||
formData.name ||
|
||||
'',
|
||||
targetCells,
|
||||
}
|
||||
|
||||
onElementUpdate(editingElement.id, updatedData)
|
||||
@@ -943,14 +891,7 @@ export const ElementConstructor: React.FC<ElementConstructorProps> = ({
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleAddElement}
|
||||
disabled={
|
||||
disabled ||
|
||||
!formData.label ||
|
||||
(formData.type !== 'standards' &&
|
||||
formData.type !== 'calibration-conditions' &&
|
||||
formData.type !== 'button-group' &&
|
||||
!formData.targetCells?.length)
|
||||
}
|
||||
disabled={disabled || !formData.label}
|
||||
>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Добавить элемент
|
||||
|
||||
Reference in New Issue
Block a user