Add Escape to shortcut to deselect replaceContainer

This commit is contained in:
Eric NGUYEN 2023-02-14 16:10:00 +01:00
parent b4c9c34403
commit 2d1e5c94d7
2 changed files with 98 additions and 69 deletions

View file

@ -7,7 +7,8 @@ export function OnKey(
history: IHistoryState[], history: IHistoryState[],
historyCurrentStep: number, historyCurrentStep: number,
setHistoryCurrentStep: Dispatch<SetStateAction<number>>, setHistoryCurrentStep: Dispatch<SetStateAction<number>>,
deleteAction: () => void deleteAction: () => void,
resetState: () => void
): void { ): void {
if (!ENABLE_SHORTCUTS) { if (!ENABLE_SHORTCUTS) {
return; return;
@ -27,5 +28,7 @@ export function OnKey(
setHistoryCurrentStep(historyCurrentStep + 1); setHistoryCurrentStep(historyCurrentStep + 1);
} else if (event.key === 'Delete') { } else if (event.key === 'Delete') {
deleteAction(); deleteAction();
} else if (event.key === 'Escape') {
resetState();
} }
} }

View file

@ -1,7 +1,7 @@
import React, { Dispatch, SetStateAction, useEffect, useRef } from 'react'; import React, { type Dispatch, type SetStateAction, useEffect, useRef } from 'react';
import './Editor.scss'; import './Editor.scss';
import { IConfiguration } from '../../Interfaces/IConfiguration'; import { type IConfiguration } from '../../Interfaces/IConfiguration';
import { IHistoryState } from '../../Interfaces/IHistoryState'; import { type IHistoryState } from '../../Interfaces/IHistoryState';
import { UI } from '../UI/UI'; import { UI } from '../UI/UI';
import { SelectContainer, DeleteContainer, OnPropertyChange, ReplaceByContainer } from './Actions/ContainerOperations'; import { SelectContainer, DeleteContainer, OnPropertyChange, ReplaceByContainer } from './Actions/ContainerOperations';
import { SaveEditorAsJSON, SaveEditorAsSVG } from './Actions/Save'; import { SaveEditorAsJSON, SaveEditorAsSVG } from './Actions/Save';
@ -13,7 +13,7 @@ import { FindContainerById } from '../../utils/itertools';
import { Menu } from '../Menu/Menu'; import { Menu } from '../Menu/Menu';
import { InitActions } from './Actions/ContextMenuActions'; import { InitActions } from './Actions/ContextMenuActions';
import { AddContainerToSelectedContainer, AddContainer } from './Actions/AddContainer'; import { AddContainerToSelectedContainer, AddContainer } from './Actions/AddContainer';
import { IReplaceContainer } from '../../Interfaces/IReplaceContainer'; import { type IReplaceContainer } from '../../Interfaces/IReplaceContainer';
interface IEditorProps { interface IEditorProps {
root: Element | Document root: Element | Document
@ -26,16 +26,18 @@ function UseShortcuts(
history: IHistoryState[], history: IHistoryState[],
historyCurrentStep: number, historyCurrentStep: number,
setHistoryCurrentStep: Dispatch<SetStateAction<number>>, setHistoryCurrentStep: Dispatch<SetStateAction<number>>,
deleteAction: () => void deleteAction: () => void,
resetState: () => void
): void { ): void {
useEffect(() => { useEffect(() => {
function OnKeyUp(event: KeyboardEvent): void { function OnKeyUp(event: KeyboardEvent): void {
return OnKey( OnKey(
event, event,
history, history,
historyCurrentStep, historyCurrentStep,
setHistoryCurrentStep, setHistoryCurrentStep,
deleteAction deleteAction,
resetState
); );
} }
@ -63,6 +65,7 @@ function UseNewHistoryState(
}; };
} }
export function Editor(props: IEditorProps): JSX.Element { export function Editor(props: IEditorProps): JSX.Element {
// States // States
const [history, setHistory] = React.useState<IHistoryState[]>(structuredClone(props.history)); const [history, setHistory] = React.useState<IHistoryState[]>(structuredClone(props.history));
@ -72,6 +75,10 @@ export function Editor(props: IEditorProps): JSX.Element {
const editorRef = useRef<HTMLDivElement>(null); const editorRef = useRef<HTMLDivElement>(null);
const setNewHistory = UseNewHistoryState(setHistory, setHistoryCurrentStep); const setNewHistory = UseNewHistoryState(setHistory, setHistoryCurrentStep);
function ResetState(): void {
setReplaceContainer({ isReplacing: false, id: undefined, category: undefined });
}
// Events // Events
UseShortcuts( UseShortcuts(
history, history,
@ -82,7 +89,8 @@ export function Editor(props: IEditorProps): JSX.Element {
setNewHistory( setNewHistory(
DeleteContainer(current.selectedContainerId, history, historyCurrentStep) DeleteContainer(current.selectedContainerId, history, historyCurrentStep)
); );
} },
ResetState
); );
UseCustomEvents( UseCustomEvents(
props.root, props.root,
@ -125,25 +133,31 @@ export function Editor(props: IEditorProps): JSX.Element {
historyCurrentStep historyCurrentStep
}} }}
replaceContainer={replaceContainer} replaceContainer={replaceContainer}
selectContainer={(container) => setNewHistory( selectContainer={(container) => {
setNewHistory(
SelectContainer( SelectContainer(
container, container,
history, history,
historyCurrentStep historyCurrentStep
))} ));
deleteContainer={(containerId: string) => setNewHistory( }}
deleteContainer={(containerId: string) => {
setNewHistory(
DeleteContainer( DeleteContainer(
containerId, containerId,
history, history,
historyCurrentStep historyCurrentStep
))} ));
onPropertyChange={(key, value, type) => setNewHistory( }}
onPropertyChange={(key, value, type) => {
setNewHistory(
OnPropertyChange( OnPropertyChange(
key, value, type, key, value, type,
selected, selected,
history, history,
historyCurrentStep historyCurrentStep
))} ));
}}
addOrReplaceContainer={(type) => { addOrReplaceContainer={(type) => {
if (selected === null || selected === undefined) { if (selected === null || selected === undefined) {
return; return;
@ -168,7 +182,8 @@ export function Editor(props: IEditorProps): JSX.Element {
)); ));
} }
}} }}
addContainerAt={(index, type, parent) => setNewHistory( addContainerAt={(index, type, parent) => {
setNewHistory(
AddContainer( AddContainer(
index, index,
type, type,
@ -177,39 +192,50 @@ export function Editor(props: IEditorProps): JSX.Element {
history, history,
historyCurrentStep historyCurrentStep
) )
)} );
addSymbol={(type) => setNewHistory( }}
addSymbol={(type) => {
setNewHistory(
AddSymbol( AddSymbol(
type, type,
configuration, configuration,
history, history,
historyCurrentStep historyCurrentStep
))} ));
onSymbolPropertyChange={(key, value) => setNewHistory( }}
onSymbolPropertyChange={(key, value) => {
setNewHistory(
OnSymbolPropertyChange( OnSymbolPropertyChange(
key, value, key, value,
history, history,
historyCurrentStep historyCurrentStep
))} ));
selectSymbol={(symbolId) => setNewHistory( }}
selectSymbol={(symbolId) => {
setNewHistory(
SelectSymbol( SelectSymbol(
symbolId, symbolId,
history, history,
historyCurrentStep historyCurrentStep
))} ));
deleteSymbol={(symbolId) => setNewHistory( }}
deleteSymbol={(symbolId) => {
setNewHistory(
DeleteSymbol( DeleteSymbol(
symbolId, symbolId,
history, history,
historyCurrentStep historyCurrentStep
))} ));
saveEditorAsJSON={() => SaveEditorAsJSON( }}
saveEditorAsJSON={() => {
SaveEditorAsJSON(
history, history,
historyCurrentStep, historyCurrentStep,
configuration configuration
)} );
saveEditorAsSVG={() => SaveEditorAsSVG()} }}
loadState={(move) => setHistoryCurrentStep(move)} saveEditorAsSVG={() => { SaveEditorAsSVG(); }}
loadState={(move) => { setHistoryCurrentStep(move); }}
setReplaceContainer={setReplaceContainer} setReplaceContainer={setReplaceContainer}
/> />
<Menu <Menu