Added AddMethod and Default x,y,width,height on Add
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Siklos 2022-08-15 22:07:08 +02:00
parent b8bd179167
commit 522cd722c0
5 changed files with 63 additions and 22 deletions

View file

@ -1,10 +1,12 @@
import { Dispatch, SetStateAction } from 'react'; import React, { Dispatch, SetStateAction } from 'react';
import { IHistoryState } from '../../Interfaces/IHistoryState'; import { IHistoryState } from '../../Interfaces/IHistoryState';
import { IConfiguration } from '../../Interfaces/IConfiguration'; import { IConfiguration } from '../../Interfaces/IConfiguration';
import { ContainerModel, IContainerModel } from '../../Interfaces/IContainerModel'; import { ContainerModel, IContainerModel } from '../../Interfaces/IContainerModel';
import { findContainerById } from '../../utils/itertools'; import { findContainerById } from '../../utils/itertools';
import { getCurrentHistory } from './Editor'; import { getCurrentHistory } from './Editor';
import IProperties from '../../Interfaces/IProperties'; import IProperties from '../../Interfaces/IProperties';
import { AddMethod } from '../../Enums/AddMethod';
import { IAvailableContainer } from '../../Interfaces/IAvailableContainer';
/** /**
* Select a container * Select a container
@ -169,10 +171,10 @@ export function AddContainer(
} }
// Get the preset properties from the API // Get the preset properties from the API
const properties = configuration.AvailableContainers const containerConfig = configuration.AvailableContainers
.find(option => option.Type === type); .find(option => option.Type === type);
if (properties === undefined) { if (containerConfig === undefined) {
throw new Error(`[AddContainer] Object type not found. Found: ${type}`); throw new Error(`[AddContainer] Object type not found. Found: ${type}`);
} }
@ -198,27 +200,25 @@ export function AddContainer(
throw new Error('[AddContainer] Container model was not found among children of the main container!'); throw new Error('[AddContainer] Container model was not found among children of the main container!');
} }
let x = 0; let x = containerConfig.DefaultX ?? 0;
if (index > 0) { const y = containerConfig.DefaultY ?? 0;
const lastChild: IContainerModel | undefined = parentClone.children.at(index - 1); const width = containerConfig.Width ?? parentClone.properties.width;
if (lastChild !== undefined) { const height = containerConfig.Height ?? parentClone.properties.height;
x = lastChild.properties.x + Number(lastChild.properties.width);
} x = ApplyAddMethod(index, containerConfig, parentClone, x);
}
const defaultProperties: IProperties = { const defaultProperties: IProperties = {
id: `${type}-${count}`, id: `${type}-${count}`,
parentId: parentClone.properties.id, parentId: parentClone.properties.id,
x, x,
y: 0, y,
width: properties.Width, width,
height: parentClone.properties.height, height,
isRigidBody: false, isRigidBody: false,
isAnchor: false, isAnchor: false,
XPositionReference: properties.XPositionReference, XPositionReference: containerConfig.XPositionReference,
...properties.Style ...containerConfig.Style
}; };
// Create the container // Create the container
const newContainer = new ContainerModel( const newContainer = new ContainerModel(
parentClone, parentClone,
@ -247,3 +247,15 @@ export function AddContainer(
setHistory(history); setHistory(history);
setHistoryCurrentStep(history.length - 1); setHistoryCurrentStep(history.length - 1);
} }
function ApplyAddMethod(index: number, containerConfig: IAvailableContainer, parentClone: IContainerModel, x: number): number {
if (index > 0 && (
containerConfig.AddMethod === undefined ||
containerConfig.AddMethod === AddMethod.Append)) {
const lastChild: IContainerModel | undefined = parentClone.children.at(index - 1);
if (lastChild !== undefined) {
x += lastChild.properties.x + Number(lastChild.properties.width);
}
}
return x;
}

View file

@ -4,5 +4,6 @@ export const INPUT_TYPES: Record<string, string> = {
width: 'number', width: 'number',
height: 'number', height: 'number',
isRigidBody: 'checkbox', isRigidBody: 'checkbox',
isAnchor: 'checkbox' isAnchor: 'checkbox',
XPositionReference: 'number'
}; };

10
src/Enums/AddMethod.ts Normal file
View file

@ -0,0 +1,10 @@
/**
* Add method when creating a container
* - Append will append to the last children in list
* - Insert will always place it at the begining
* (default: Append)
*/
export enum AddMethod {
Append,
Insert
}

View file

@ -1,11 +1,15 @@
import React from 'react'; import React from 'react';
import { AddMethod } from '../Enums/AddMethod';
import { XPositionReference } from '../Enums/XPositionReference'; import { XPositionReference } from '../Enums/XPositionReference';
/** Model of available container used in application configuration */ /** Model of available container used in application configuration */
export interface IAvailableContainer { export interface IAvailableContainer {
Type: string Type: string
Width: number Width?: number
Height: number Height?: number
DefaultX?: number
DefaultY?: number
AddMethod?: AddMethod
XPositionReference?: XPositionReference XPositionReference?: XPositionReference
Style: React.CSSProperties Style: React.CSSProperties
} }

View file

@ -62,20 +62,34 @@ const GetSVGLayoutConfiguration = () => {
}, },
{ {
Type: 'Trou', Type: 'Trou',
Width: 300, DefaultX: 10,
DefaultY: 10,
Width: 480,
Height: 180,
Style: { Style: {
fillOpacity: 0, fillOpacity: 0,
borderWidth: 2, borderWidth: 2,
stroke: 'green' stroke: 'green'
} }
}, },
{
Type: 'Remplissage',
Style: {
fillOpacity: 1,
borderWidth: 2,
stroke: '#bfdbfe',
fill: '#bfdbfe'
}
},
{ {
Type: 'Montant', Type: 'Montant',
Width: 100, Width: 10,
XPositionReference: 1,
Style: { Style: {
fillOpacity: 0, fillOpacity: 0,
borderWidth: 2, borderWidth: 2,
stroke: 'blue', stroke: '#713f12',
fill: '#713f12'
} }
} }
], ],