Refactor AddContainerToSelectedContainer

This commit is contained in:
Eric NGUYEN 2022-10-17 19:32:27 +02:00
parent d05d0fb196
commit b1b30e8486
4 changed files with 19 additions and 28 deletions

View file

@ -1,5 +1,5 @@
import React, { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react'; import React, { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
import { events as EVENTS, UseCustomEvents } from '../../Events/AppEvents'; import { UseCustomEvents } from '../../Events/AppEvents';
import { MainMenu } from '../MainMenu/MainMenu'; import { MainMenu } from '../MainMenu/MainMenu';
import { ContainerModel, IContainerModel } from '../../Interfaces/IContainerModel'; import { ContainerModel, IContainerModel } from '../../Interfaces/IContainerModel';
import { Editor } from '../Editor/Editor'; import { Editor } from '../Editor/Editor';

View file

@ -27,21 +27,15 @@ import { SortChildren } from './ContainerOperations';
*/ */
export function AddContainerToSelectedContainer( export function AddContainerToSelectedContainer(
type: string, type: string,
selected: IContainerModel | undefined, selected: IContainerModel,
configuration: IConfiguration, configuration: IConfiguration,
fullHistory: IHistoryState[], fullHistory: IHistoryState[],
historyCurrentStep: number historyCurrentStep: number
): IHistoryState[] | null { ): IHistoryState[] {
if (selected === null ||
selected === undefined) {
return null;
}
const parent = selected;
return AddContainer( return AddContainer(
parent.children.length, selected.children.length,
type, type,
parent.properties.id, selected.properties.id,
configuration, configuration,
fullHistory, fullHistory,
historyCurrentStep historyCurrentStep

View file

@ -140,16 +140,17 @@ export function Editor(props: IEditorProps): JSX.Element {
historyCurrentStep historyCurrentStep
))} ))}
addContainer={(type) => { addContainer={(type) => {
const newHistory = AddContainerToSelectedContainer( if (selected === null || selected === undefined) {
return;
}
setNewHistory(AddContainerToSelectedContainer(
type, type,
selected, selected,
configuration, configuration,
history, history,
historyCurrentStep historyCurrentStep
); ));
if (newHistory !== null) {
setNewHistory(newHistory);
}
}} }}
addContainerAt={(index, type, parent) => setNewHistory( addContainerAt={(index, type, parent) => setNewHistory(
AddContainer( AddContainer(

View file

@ -240,20 +240,16 @@ function AppendContainerToSelectedContainer(root: Element | Document,
const selected = FindContainerById(currentState.containers, currentState.selectedContainerId); const selected = FindContainerById(currentState.containers, currentState.selectedContainerId);
const newHistory = AddContainerToSelectedContainerAction( if (selected !== null && selected !== undefined) {
setNewHistory(AddContainerToSelectedContainerAction(
type, type,
selected, selected,
editorState.configuration, editorState.configuration,
history, history,
editorState.historyCurrentStep editorState.historyCurrentStep
); ));
if (newHistory === null) {
return;
} }
setNewHistory(newHistory);
const customEvent = new CustomEvent<IHistoryState>( const customEvent = new CustomEvent<IHistoryState>(
'appendContainerToSelectedContainer', 'appendContainerToSelectedContainer',
{ detail: structuredClone(editorState.history[editorState.historyCurrentStep]) }); { detail: structuredClone(editorState.history[editorState.historyCurrentStep]) });