From 6a3ddea763108cd577bdcd85255007789200e1e6 Mon Sep 17 00:00:00 2001 From: Siklos Date: Thu, 8 Sep 2022 14:25:00 +0200 Subject: [PATCH] Allow AddContainers to extends the default config --- .../Editor/Actions/ContainerOperations.ts | 17 +++++++++++------ .../Editor/Actions/ContextMenuActions.ts | 7 +++---- src/Interfaces/IConfiguration.ts | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Components/Editor/Actions/ContainerOperations.ts b/src/Components/Editor/Actions/ContainerOperations.ts index 4a05dfa..e702259 100644 --- a/src/Components/Editor/Actions/ContainerOperations.ts +++ b/src/Components/Editor/Actions/ContainerOperations.ts @@ -190,7 +190,7 @@ export function AddContainerToSelectedContainer( */ export function AddContainers( index: number, - types: string[], + availableContainers: IAvailableContainer[], parentId: string, configuration: IConfiguration, fullHistory: IHistoryState[], @@ -218,15 +218,19 @@ export function AddContainers( const containerIds: string[] = []; // Iterate over the containers - types.forEach((type, typeIndex) => { + availableContainers.forEach((availableContainer, typeIndex) => { // Get the preset properties from the API - const containerConfig = configuration.AvailableContainers + const type = availableContainer.Type; + + const defaultConfig = configuration.AvailableContainers .find(option => option.Type === type); - if (containerConfig === undefined) { - throw new Error(`[AddContainer] Object type not found. Found: ${type}`); + if (defaultConfig === undefined) { + throw new Error(`[AddContainer] Object type not found among default config. Found: ${type}`); } + const containerConfig = Object.assign(defaultConfig, availableContainer); + // Default margin const left: number = containerConfig.Margin?.left ?? 0; const bottom: number = containerConfig.Margin?.bottom ?? 0; @@ -330,7 +334,8 @@ export function AddContainer( // just call AddContainers with an array on a single element return AddContainers( index, - [type], + // eslint-disable-next-line @typescript-eslint/naming-convention + [{ Type: type }], parentId, configuration, fullHistory, diff --git a/src/Components/Editor/Actions/ContextMenuActions.ts b/src/Components/Editor/Actions/ContextMenuActions.ts index 72863b0..85ae2b3 100644 --- a/src/Components/Editor/Actions/ContextMenuActions.ts +++ b/src/Components/Editor/Actions/ContextMenuActions.ts @@ -85,7 +85,7 @@ function HandleSetContainerList( setNewHistory( AddContainers( selectedContainer.children.length, - response.Containers.map(container => container.Type), + response.Containers, selectedContainer.properties.id, configuration, history, @@ -121,8 +121,6 @@ function HandleReplace( const index = selectedContainer.parent.children.indexOf(selectedContainer); - const types = response.Containers.map(container => container.Type); - const newHistoryAfterDelete = DeleteContainer( selectedContainer.properties.id, history, @@ -131,7 +129,7 @@ function HandleReplace( const newHistoryBeforeDelete = AddContainers( index, - types, + response.Containers, selectedContainer.properties.parentId, configuration, newHistoryAfterDelete, @@ -144,6 +142,7 @@ function HandleReplace( } // Rename the last action by Replace + const types = response.Containers.map(container => container.Type); newHistoryBeforeDelete[newHistoryBeforeDelete.length - 1].lastAction = `Replace ${selectedContainer.properties.id} by [${types.join(', ')}]`; diff --git a/src/Interfaces/IConfiguration.ts b/src/Interfaces/IConfiguration.ts index c44f41c..7f1ffc0 100644 --- a/src/Interfaces/IConfiguration.ts +++ b/src/Interfaces/IConfiguration.ts @@ -4,7 +4,7 @@ import { IAvailableSymbol } from './IAvailableSymbol'; /** Model of configuration for the application to configure it */ export interface IConfiguration { - AvailableContainers: IAvailableContainer[] - AvailableSymbols: IAvailableSymbol[] + AvailableContainers: IAvailableContainer[] // TODO: Use a Map + AvailableSymbols: IAvailableSymbol[] // TODO: Use a Map MainContainer: IAvailableContainer }