From e831f95d2005f1819aaffa6bd871eb6dc64f6b46 Mon Sep 17 00:00:00 2001 From: Siklos Date: Thu, 18 Aug 2022 10:15:12 +0200 Subject: [PATCH 1/2] Refactor behaviors functions to a single function called applyBehaviors --- src/Components/Editor/Behaviors/Behaviors.ts | 21 +++++++++++++++++++ src/Components/Editor/PropertiesOperations.ts | 16 ++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 src/Components/Editor/Behaviors/Behaviors.ts diff --git a/src/Components/Editor/Behaviors/Behaviors.ts b/src/Components/Editor/Behaviors/Behaviors.ts new file mode 100644 index 0000000..b93a0bf --- /dev/null +++ b/src/Components/Editor/Behaviors/Behaviors.ts @@ -0,0 +1,21 @@ +import { IContainerModel } from '../../../Interfaces/IContainerModel'; +import { ImposePosition } from './AnchorBehaviors'; +import { RecalculatePhysics } from './RigidBodyBehaviors'; + +/** + * Recalculate the position of the container and its neighbors + * Mutate and returns the updated container + * @param container Container to recalculate its positions + * @returns Updated container + */ +export function applyBehaviors(container: IContainerModel): IContainerModel { + if (container.properties.isAnchor) { + ImposePosition(container); + } + + if (container.properties.isRigidBody) { + RecalculatePhysics(container); + } + + return container; +} diff --git a/src/Components/Editor/PropertiesOperations.ts b/src/Components/Editor/PropertiesOperations.ts index df84b85..570a668 100644 --- a/src/Components/Editor/PropertiesOperations.ts +++ b/src/Components/Editor/PropertiesOperations.ts @@ -3,9 +3,8 @@ import { IContainerModel, ContainerModel } from '../../Interfaces/IContainerMode import { IHistoryState } from '../../Interfaces/IHistoryState'; import { findContainerById } from '../../utils/itertools'; import { getCurrentHistory } from './Editor'; -import { RecalculatePhysics } from './Behaviors/RigidBodyBehaviors'; -import { ImposePosition } from './Behaviors/AnchorBehaviors'; import { restoreX } from '../SVG/Elements/Container'; +import { applyBehaviors } from './Behaviors/Behaviors'; /** * Handled the property change event in the properties form @@ -43,13 +42,7 @@ export function OnPropertyChange( (container.properties as any)[key] = value; } - if (container.properties.isAnchor) { - ImposePosition(container); - } - - if (container.properties.isRigidBody) { - RecalculatePhysics(container); - } + applyBehaviors(container); history.push({ LastAction: `Change ${key} of ${container.properties.id}`, @@ -116,9 +109,8 @@ export function OnPropertiesSubmit( submitCSSForm(form, styleProperty, container); } - if (container.properties.isRigidBody) { - RecalculatePhysics(container); - } + // Apply the behaviors + applyBehaviors(container); history.push({ LastAction: `Change properties of ${container.properties.id}`, -- 2.47.2 From c1139216c2141728e1e2fc25181ea11135102d4d Mon Sep 17 00:00:00 2001 From: Siklos Date: Thu, 18 Aug 2022 11:46:28 +0200 Subject: [PATCH 2/2] Rename applyBehaviors --- src/Components/Editor/Behaviors/Behaviors.ts | 2 +- src/Components/Editor/ContainerOperations.ts | 3 +++ src/Components/Editor/PropertiesOperations.ts | 6 +++--- src/utils/default.ts | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Components/Editor/Behaviors/Behaviors.ts b/src/Components/Editor/Behaviors/Behaviors.ts index b93a0bf..63018b4 100644 --- a/src/Components/Editor/Behaviors/Behaviors.ts +++ b/src/Components/Editor/Behaviors/Behaviors.ts @@ -8,7 +8,7 @@ import { RecalculatePhysics } from './RigidBodyBehaviors'; * @param container Container to recalculate its positions * @returns Updated container */ -export function applyBehaviors(container: IContainerModel): IContainerModel { +export function ApplyBehaviors(container: IContainerModel): IContainerModel { if (container.properties.isAnchor) { ImposePosition(container); } diff --git a/src/Components/Editor/ContainerOperations.ts b/src/Components/Editor/ContainerOperations.ts index a913d79..360530c 100644 --- a/src/Components/Editor/ContainerOperations.ts +++ b/src/Components/Editor/ContainerOperations.ts @@ -7,6 +7,7 @@ import { getCurrentHistory } from './Editor'; import { AddMethod } from '../../Enums/AddMethod'; import { IAvailableContainer } from '../../Interfaces/IAvailableContainer'; import { GetDefaultContainerProps } from '../../utils/default'; +import { ApplyBehaviors } from './Behaviors/Behaviors'; /** * Select a container @@ -224,6 +225,8 @@ export function AddContainer( } ); + ApplyBehaviors(newContainer); + // And push it the the parent children if (index === parentClone.children.length) { parentClone.children.push(newContainer); diff --git a/src/Components/Editor/PropertiesOperations.ts b/src/Components/Editor/PropertiesOperations.ts index 570a668..70984da 100644 --- a/src/Components/Editor/PropertiesOperations.ts +++ b/src/Components/Editor/PropertiesOperations.ts @@ -4,7 +4,7 @@ import { IHistoryState } from '../../Interfaces/IHistoryState'; import { findContainerById } from '../../utils/itertools'; import { getCurrentHistory } from './Editor'; import { restoreX } from '../SVG/Elements/Container'; -import { applyBehaviors } from './Behaviors/Behaviors'; +import { ApplyBehaviors } from './Behaviors/Behaviors'; /** * Handled the property change event in the properties form @@ -42,7 +42,7 @@ export function OnPropertyChange( (container.properties as any)[key] = value; } - applyBehaviors(container); + ApplyBehaviors(container); history.push({ LastAction: `Change ${key} of ${container.properties.id}`, @@ -110,7 +110,7 @@ export function OnPropertiesSubmit( } // Apply the behaviors - applyBehaviors(container); + ApplyBehaviors(container); history.push({ LastAction: `Change properties of ${container.properties.id}`, diff --git a/src/utils/default.ts b/src/utils/default.ts index cc7acf6..1b4a41c 100644 --- a/src/utils/default.ts +++ b/src/utils/default.ts @@ -59,7 +59,7 @@ export const GetDefaultContainerProps = ( y, width: containerConfig.Width ?? containerConfig.MinWidth ?? parent.properties.width, height: containerConfig.Height ?? parent.properties.height, - isRigidBody: false, + isRigidBody: false, // set this to true to replicate Florian's project isAnchor: false, XPositionReference: containerConfig.XPositionReference ?? XPositionReference.Left, minWidth: containerConfig.MinWidth ?? 0, -- 2.47.2