Implement DefaultChildType
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Siklos 2022-08-18 15:51:45 +02:00
parent f268011315
commit 9f9ec8dc65
4 changed files with 78 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import { findContainerById } from '../../utils/itertools';
import { getCurrentHistory } from './Editor'; import { getCurrentHistory } from './Editor';
import { AddMethod } from '../../Enums/AddMethod'; import { AddMethod } from '../../Enums/AddMethod';
import { IAvailableContainer } from '../../Interfaces/IAvailableContainer'; import { IAvailableContainer } from '../../Interfaces/IAvailableContainer';
import { GetDefaultContainerProps } from '../../utils/default'; import { GetDefaultContainerProps, DEFAULTCHILDTYPE_ALLOW_CYCLIC, DEFAULTCHILDTYPE_MAX_DEPTH } from '../../utils/default';
import { ApplyBehaviors } from './Behaviors/Behaviors'; import { ApplyBehaviors } from './Behaviors/Behaviors';
/** /**
@ -181,12 +181,7 @@ export function AddContainer(
// Set the counter of the object type in order to assign an unique id // Set the counter of the object type in order to assign an unique id
const newCounters = Object.assign({}, current.TypeCounters); const newCounters = Object.assign({}, current.TypeCounters);
if (newCounters[type] === null || UpdateCounters(newCounters, type);
newCounters[type] === undefined) {
newCounters[type] = 0;
} else {
newCounters[type]++;
}
const count = newCounters[type]; const count = newCounters[type];
// Create maincontainer model // Create maincontainer model
@ -234,6 +229,8 @@ export function AddContainer(
parentClone.children.splice(index, 0, newContainer); parentClone.children.splice(index, 0, newContainer);
} }
InitializeDefaultChild(configuration, containerConfig, newContainer, newCounters);
// Update the state // Update the state
history.push({ history.push({
LastAction: `Add ${newContainer.properties.id} in ${parentClone.properties.id}`, LastAction: `Add ${newContainer.properties.id} in ${parentClone.properties.id}`,
@ -246,6 +243,74 @@ export function AddContainer(
setHistoryCurrentStep(history.length - 1); setHistoryCurrentStep(history.length - 1);
} }
function UpdateCounters(counters: Record<string, number>, type: string): void {
if (counters[type] === null ||
counters[type] === undefined) {
counters[type] = 0;
} else {
counters[type]++;
}
}
function InitializeDefaultChild(
configuration: IConfiguration,
containerConfig: IAvailableContainer,
newContainer: ContainerModel,
newCounters: Record<string, number>
): void {
if (containerConfig.DefaultChildType === undefined) {
return;
}
let currentConfig = configuration.AvailableContainers
.find(option => option.Type === containerConfig.DefaultChildType);
let parent = newContainer;
let depth = 0;
const seen = new Set<string>([containerConfig.Type]);
while (currentConfig !== undefined &&
depth <= DEFAULTCHILDTYPE_MAX_DEPTH
) {
if (!DEFAULTCHILDTYPE_ALLOW_CYCLIC && seen.has(currentConfig.Type)) {
return;
}
seen.add(currentConfig.Type);
const x = currentConfig.DefaultX ?? 0;
const y = currentConfig.DefaultY ?? 0;
UpdateCounters(newCounters, currentConfig.Type);
const count = newCounters[currentConfig.Type];
const defaultChildProperties = GetDefaultContainerProps(
currentConfig.Type,
count,
parent,
x,
y,
currentConfig
);
// Create the container
const newChildContainer = new ContainerModel(
parent,
defaultChildProperties,
[],
{
type: currentConfig.Type
}
);
// And push it the the parent children
parent.children.push(newChildContainer);
// iterate
depth++;
parent = newChildContainer;
currentConfig = configuration.AvailableContainers
.find(option => option.Type === (currentConfig as IAvailableContainer).DefaultChildType);
}
}
/** /**
* Returns a new offset by applying an Add method (append, insert etc.) * Returns a new offset by applying an Add method (append, insert etc.)
* See AddMethod * See AddMethod

View file

@ -13,6 +13,7 @@ export interface IAvailableContainer {
AddMethod?: AddMethod AddMethod?: AddMethod
XPositionReference?: XPositionReference XPositionReference?: XPositionReference
CustomSVG?: string CustomSVG?: string
DefaultChildType?: string
Style?: React.CSSProperties Style?: React.CSSProperties
UserData?: object UserData?: object
} }

View file

@ -7,6 +7,8 @@ import IProperties from '../Interfaces/IProperties';
/// CONTAINRE DEFAULTS /// /// CONTAINRE DEFAULTS ///
export const SHOW_TEXT = true; export const SHOW_TEXT = true;
export const DEFAULTCHILDTYPE_ALLOW_CYCLIC = false;
export const DEFAULTCHILDTYPE_MAX_DEPTH = 10;
/// DIMENSIONS DEFAULTS /// /// DIMENSIONS DEFAULTS ///

View file

@ -55,6 +55,7 @@ const GetSVGLayoutConfiguration = () => {
Type: 'Chassis', Type: 'Chassis',
Width: 500, Width: 500,
MinWidth: 200, MinWidth: 200,
DefaultChildType: 'Trou',
Style: { Style: {
fillOpacity: 1, fillOpacity: 1,
strokeWidth: 2, strokeWidth: 2,
@ -68,6 +69,7 @@ const GetSVGLayoutConfiguration = () => {
DefaultY: 10, DefaultY: 10,
Width: 480, Width: 480,
Height: 180, Height: 180,
DefaultChildType: 'Remplissage',
Style: { Style: {
fillOpacity: 1, fillOpacity: 1,
strokeWidth: 2, strokeWidth: 2,
@ -77,6 +79,7 @@ const GetSVGLayoutConfiguration = () => {
}, },
{ {
Type: 'Remplissage', Type: 'Remplissage',
DefaultChildType: 'Trou',
CustomSVG: ` CustomSVG: `
<rect width="{width}" height="{height}" style="{style}"></rect> <rect width="{width}" height="{height}" style="{style}"></rect>
<rect width="{width}" height="{height}" stroke="black" fill-opacity="0"></rect> <rect width="{width}" height="{height}" stroke="black" fill-opacity="0"></rect>