Refactor usage of EditorState + Add version to editorState + increment editorState to "v2.0.0"

This commit is contained in:
Eric NGUYEN 2023-03-01 11:46:22 +01:00
parent 460669987d
commit afd303f8cb
13 changed files with 136 additions and 179 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "svg-layout-designer-react", "name": "svg-layout-designer-react",
"private": true, "private": true,
"version": "v1.0.0", "version": "v2.0.0",
"type": "module", "type": "module",
"postinstall": "npx patch-package", "postinstall": "npx patch-package",
"scripts": { "scripts": {

View file

@ -63,7 +63,8 @@ export function App(props: IAppProps): JSX.Element {
typeCounters: {}, typeCounters: {},
symbols: new Map() symbols: new Map()
}], }],
historyCurrentStep: 0 historyCurrentStep: 0,
version: APP_VERSION
}); });
UseCustomEvents( UseCustomEvents(
@ -86,9 +87,7 @@ export function App(props: IAppProps): JSX.Element {
> >
<Editor <Editor
root={props.root} root={props.root}
configuration={editorState.configuration} editorState={editorState}
history={editorState.history}
historyCurrentStep={editorState.historyCurrentStep}
/> />
</div> </div>
); );

View file

@ -15,6 +15,7 @@ import { FindContainerById } from '../../../utils/itertools';
import { ApplyMargin, RestoreX, RestoreY } from '../../../utils/svg'; import { ApplyMargin, RestoreX, RestoreY } from '../../../utils/svg';
import { ApplyBehaviors, ApplyBehaviorsOnSiblingsChildren } from '../Behaviors/Behaviors'; import { ApplyBehaviors, ApplyBehaviorsOnSiblingsChildren } from '../Behaviors/Behaviors';
import { GetCurrentHistory, UpdateCounters } from '../Editor'; import { GetCurrentHistory, UpdateCounters } from '../Editor';
import { type IEditorState } from '../../../Interfaces/IEditorState';
import { SortChildren } from './ContainerOperations'; import { SortChildren } from './ContainerOperations';
/** /**
@ -28,17 +29,13 @@ import { SortChildren } from './ContainerOperations';
export function AddContainerToSelectedContainer( export function AddContainerToSelectedContainer(
type: string, type: string,
selected: IContainerModel, selected: IContainerModel,
configuration: IConfiguration, editorState: IEditorState
fullHistory: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] { ): IHistoryState[] {
return AddContainer( return AddContainer(
selected.children.length, selected.children.length,
type, type,
selected.properties.id, selected.properties.id,
configuration, editorState
fullHistory,
historyCurrentStep
); );
} }
@ -56,9 +53,11 @@ export function AddContainers(
index: number, index: number,
availableContainers: IAvailableContainer[], availableContainers: IAvailableContainer[],
parentId: string, parentId: string,
configuration: IConfiguration, {
fullHistory: IHistoryState[], configuration,
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): { ): {
history: IHistoryState[] history: IHistoryState[]
newContainers: IContainerModel[] newContainers: IContainerModel[]
@ -240,9 +239,7 @@ export function AddContainer(
index: number, index: number,
type: string, type: string,
parentId: string, parentId: string,
configuration: IConfiguration, editorState: IEditorState
fullHistory: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] { ): IHistoryState[] {
// just call AddContainers with an array on a single element // just call AddContainers with an array on a single element
const { history } = AddContainers( const { history } = AddContainers(
@ -250,9 +247,7 @@ export function AddContainer(
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
[{ Type: type }], [{ Type: type }],
parentId, parentId,
configuration, editorState
fullHistory,
historyCurrentStep
); );
return history; return history;
} }

View file

@ -8,7 +8,7 @@ import { type ISymbolModel } from '../../../Interfaces/ISymbolModel';
import { PropertyType } from '../../../Enums/PropertyType'; import { PropertyType } from '../../../Enums/PropertyType';
import { TransformX, TransformY } from '../../../utils/svg'; import { TransformX, TransformY } from '../../../utils/svg';
import { Orientation } from '../../../Enums/Orientation'; import { Orientation } from '../../../Enums/Orientation';
import { type IConfiguration } from '../../../Interfaces/IConfiguration'; import { type IEditorState } from '../../../Interfaces/IEditorState';
import { AddContainers } from './AddContainer'; import { AddContainers } from './AddContainer';
/** /**
@ -17,8 +17,10 @@ import { AddContainers } from './AddContainer';
*/ */
export function DeselectContainer( export function DeselectContainer(
containerId: string, containerId: string,
fullHistory: IHistoryState[], {
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];
@ -42,8 +44,10 @@ export function DeselectContainer(
*/ */
export function DeleteContainer( export function DeleteContainer(
containerId: string, containerId: string,
fullHistory: IHistoryState[], {
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];
@ -109,10 +113,10 @@ export function DeleteContainer(
export function ReplaceByContainer( export function ReplaceByContainer(
containerId: string, containerId: string,
newContainerId: string, newContainerId: string,
configuration: IConfiguration, editorState: IEditorState
fullHistory: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] { ): IHistoryState[] {
const fullHistory = editorState.history;
const historyCurrentStep = editorState.historyCurrentStep;
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];
@ -130,12 +134,18 @@ export function ReplaceByContainer(
containerParent.children.indexOf(containerId), containerParent.children.indexOf(containerId),
[{ Type: newContainerId }], [{ Type: newContainerId }],
containerParent.properties.id, containerParent.properties.id,
configuration, editorState
fullHistory,
historyCurrentStep
); );
const historyDelete = DeleteContainer(containerId, historyAdd.history, historyCurrentStep + 1); const historyDelete = DeleteContainer(
containerId,
{
configuration: editorState.configuration,
history: historyAdd.history,
historyCurrentStep: historyCurrentStep + 1,
version: editorState.version
}
);
const currentDelete = historyDelete[historyDelete.length - 1]; const currentDelete = historyDelete[historyDelete.length - 1];
fullHistory.push({ fullHistory.push({
@ -186,8 +196,10 @@ export function OnPropertyChange(
value: string | number | boolean | number[], value: string | number | boolean | number[],
type: PropertyType = PropertyType.Simple, type: PropertyType = PropertyType.Simple,
selected: IContainerModel | undefined, selected: IContainerModel | undefined,
fullHistory: IHistoryState[], {
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];

View file

@ -2,7 +2,6 @@ import Swal from 'sweetalert2';
import { type Dispatch, type SetStateAction } from 'react'; import { type Dispatch, type SetStateAction } from 'react';
import { AddMethod } from '../../../Enums/AddMethod'; import { AddMethod } from '../../../Enums/AddMethod';
import { type IAction } from '../../../Interfaces/IAction'; import { type IAction } from '../../../Interfaces/IAction';
import { type IConfiguration } from '../../../Interfaces/IConfiguration';
import { type IContainerModel } from '../../../Interfaces/IContainerModel'; import { type IContainerModel } from '../../../Interfaces/IContainerModel';
import { type IHistoryState } from '../../../Interfaces/IHistoryState'; import { type IHistoryState } from '../../../Interfaces/IHistoryState';
import { type ISetContainerListRequest } from '../../../Interfaces/ISetContainerListRequest'; import { type ISetContainerListRequest } from '../../../Interfaces/ISetContainerListRequest';
@ -14,19 +13,21 @@ import { type IMenuAction } from '../../Menu/Menu';
import { GetCurrentHistoryState } from '../Editor'; import { GetCurrentHistoryState } from '../Editor';
import { Text } from '../../Text/Text'; import { Text } from '../../Text/Text';
import { type IReplaceContainer } from '../../../Interfaces/IReplaceContainer'; import { type IReplaceContainer } from '../../../Interfaces/IReplaceContainer';
import { type IEditorState } from '../../../Interfaces/IEditorState';
import { AddContainers } from './AddContainer'; import { AddContainers } from './AddContainer';
import { DeleteContainer } from './ContainerOperations'; import { DeleteContainer } from './ContainerOperations';
import { DeleteSymbol } from './SymbolOperations'; import { DeleteSymbol } from './SymbolOperations';
export function InitActions( export function InitActions(
menuActions: Map<string, IMenuAction[]>, menuActions: Map<string, IMenuAction[]>,
configuration: IConfiguration, editorState: IEditorState,
history: IHistoryState[],
historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void, setNewHistory: (newHistory: IHistoryState[]) => void,
setHistoryCurrentStep: Dispatch<SetStateAction<number>>, setHistoryCurrentStep: Dispatch<SetStateAction<number>>,
setIsReplacingContainer: Dispatch<SetStateAction<IReplaceContainer>> setIsReplacingContainer: Dispatch<SetStateAction<IReplaceContainer>>
): void { ): void {
const configuration = editorState.configuration;
const history = editorState.history;
const historyCurrentStep = editorState.historyCurrentStep;
menuActions.set( menuActions.set(
'', '',
[ [
@ -82,8 +83,7 @@ export function InitActions(
const id = target.id; const id = target.id;
const newHistory = DeleteContainer( const newHistory = DeleteContainer(
id, id,
history, editorState
historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
} }
@ -100,8 +100,7 @@ export function InitActions(
const id = target.id; const id = target.id;
const newHistory = DeleteSymbol( const newHistory = DeleteSymbol(
id, id,
history, editorState
historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
} }
@ -130,9 +129,7 @@ export function InitActions(
action: GetAction( action: GetAction(
action, action,
currentState, currentState,
configuration, editorState,
history,
historyCurrentStep,
setNewHistory setNewHistory
) )
}; };
@ -145,9 +142,7 @@ export function InitActions(
function GetAction( function GetAction(
action: IAction, action: IAction,
currentState: IHistoryState, currentState: IHistoryState,
configuration: IConfiguration, editorState: IEditorState,
history: IHistoryState[],
historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void setNewHistory: (newHistory: IHistoryState[]) => void
): (target: HTMLElement) => void { ): (target: HTMLElement) => void {
return (target: HTMLElement) => { return (target: HTMLElement) => {
@ -175,15 +170,16 @@ function GetAction(
}; };
/* eslint-enable */ /* eslint-enable */
SetContainerList(request, configuration.APIConfiguration?.apiSetContainerListUrl) SetContainerList(
request,
editorState.configuration.APIConfiguration?.apiSetContainerListUrl
)
.then((response: ISetContainerListResponse) => { .then((response: ISetContainerListResponse) => {
HandleSetContainerList( HandleSetContainerList(
action, action,
container, container,
response, response,
configuration, editorState,
history,
historyCurrentStep,
setNewHistory setNewHistory
); );
}); });
@ -215,13 +211,14 @@ function HandleSetContainerList(
action: IAction, action: IAction,
selectedContainer: IContainerModel, selectedContainer: IContainerModel,
response: ISetContainerListResponse, response: ISetContainerListResponse,
configuration: IConfiguration, editorState: IEditorState,
history: IHistoryState[],
historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void setNewHistory: (newHistory: IHistoryState[]) => void
): void { ): void {
const addingBehavior = response.AddingBehavior ?? action.AddingBehavior; const addingBehavior = response.AddingBehavior ?? action.AddingBehavior;
const current = GetCurrentHistoryState(history, historyCurrentStep); const current = GetCurrentHistoryState(
editorState.history,
editorState.historyCurrentStep
);
const containers = current.containers; const containers = current.containers;
switch (addingBehavior) { switch (addingBehavior) {
case AddMethod.Insert: case AddMethod.Insert:
@ -233,9 +230,7 @@ function HandleSetContainerList(
selectedContainer.children.length, selectedContainer.children.length,
response.Containers, response.Containers,
selectedContainer.properties.id, selectedContainer.properties.id,
configuration, editorState
history,
historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
@ -246,9 +241,7 @@ function HandleSetContainerList(
containers, containers,
selectedContainer, selectedContainer,
response, response,
configuration, editorState
history,
historyCurrentStep
)); ));
break; break;
case AddMethod.ReplaceParent: { case AddMethod.ReplaceParent: {
@ -265,9 +258,7 @@ function HandleSetContainerList(
containers, containers,
parent, parent,
response, response,
configuration, editorState
history,
historyCurrentStep
)); ));
break; break;
} }
@ -278,9 +269,7 @@ function HandleReplace(
containers: Map<string, IContainerModel>, containers: Map<string, IContainerModel>,
selectedContainer: IContainerModel, selectedContainer: IContainerModel,
response: ISetContainerListResponse, response: ISetContainerListResponse,
configuration: IConfiguration, editorState: IEditorState
history: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] { ): IHistoryState[] {
const parent = FindContainerById(containers, selectedContainer.properties.parentId); const parent = FindContainerById(containers, selectedContainer.properties.parentId);
if (parent === undefined || parent === null) { if (parent === undefined || parent === null) {
@ -291,17 +280,21 @@ function HandleReplace(
const newHistoryAfterDelete = DeleteContainer( const newHistoryAfterDelete = DeleteContainer(
selectedContainer.properties.id, selectedContainer.properties.id,
history, editorState
historyCurrentStep
); );
const newEditorStateAfterDelete: IEditorState = {
configuration: editorState.configuration,
history: newHistoryAfterDelete,
historyCurrentStep: newHistoryAfterDelete.length - 1,
version: editorState.version
};
const { history: newHistoryBeforeDelete } = AddContainers( const { history: newHistoryBeforeDelete } = AddContainers(
index, index,
response.Containers, response.Containers,
selectedContainer.properties.parentId, selectedContainer.properties.parentId,
configuration, newEditorStateAfterDelete
newHistoryAfterDelete,
newHistoryAfterDelete.length - 1
); );
// Remove AddContainers from history // Remove AddContainers from history

View file

@ -1,24 +1,15 @@
import { type IHistoryState } from '../../../Interfaces/IHistoryState';
import { type IConfiguration } from '../../../Interfaces/IConfiguration';
import { GetCircularReplacer } from '../../../utils/saveload'; import { GetCircularReplacer } from '../../../utils/saveload';
import { ID } from '../../SVG/SVG'; import { ID } from '../../SVG/SVG';
import { type IEditorState } from '../../../Interfaces/IEditorState'; import { type IEditorState } from '../../../Interfaces/IEditorState';
import { SHOW_SELECTOR_TEXT } from '../../../utils/default'; import { SHOW_SELECTOR_TEXT } from '../../../utils/default';
export function SaveEditorAsJSON( export function SaveEditorAsJSON(
history: IHistoryState[], editorState: IEditorState
historyCurrentStep: number,
configuration: IConfiguration
): void { ): void {
const exportName = 'state.json'; const exportName = 'state.json';
const spaces = import.meta.env.DEV const spaces = import.meta.env.DEV
? 4 ? 4
: 0; : 0;
const editorState: IEditorState = {
history,
historyCurrentStep,
configuration
};
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (window.Worker) { if (window.Worker) {

View file

@ -1,5 +1,5 @@
import { type IConfiguration } from '../../../Interfaces/IConfiguration';
import { type IContainerModel } from '../../../Interfaces/IContainerModel'; import { type IContainerModel } from '../../../Interfaces/IContainerModel';
import { type IEditorState } from '../../../Interfaces/IEditorState';
import { type IHistoryState } from '../../../Interfaces/IHistoryState'; import { type IHistoryState } from '../../../Interfaces/IHistoryState';
import { type ISymbolModel } from '../../../Interfaces/ISymbolModel'; import { type ISymbolModel } from '../../../Interfaces/ISymbolModel';
import { GetDefaultSymbolModel } from '../../../utils/default'; import { GetDefaultSymbolModel } from '../../../utils/default';
@ -12,9 +12,12 @@ import { LinkSymbol } from './ContainerOperations';
export function AddSymbol( export function AddSymbol(
name: string, name: string,
configuration: IConfiguration, {
fullHistory: IHistoryState[], configuration,
historyCurrentStep: number history: fullHistory,
historyCurrentStep,
version
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
let history = GetCurrentHistory(fullHistory, historyCurrentStep); let history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];
@ -57,9 +60,12 @@ export function AddSymbol(
0, 0,
[symbolConfig.AssociatedContainer], [symbolConfig.AssociatedContainer],
current.mainContainer, current.mainContainer,
configuration, {
history, configuration,
historyCurrentStep + 1 history,
historyCurrentStep: historyCurrentStep + 1,
version
}
); );
history = newHistory; history = newHistory;
@ -76,8 +82,10 @@ export function AddSymbol(
export function DeleteSymbol( export function DeleteSymbol(
symbolId: string, symbolId: string,
fullHistory: IHistoryState[], {
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];
@ -131,8 +139,10 @@ export function OnPropertyChange(
key: string, key: string,
value: string | number | boolean, value: string | number | boolean,
selectedSymbolId: string, selectedSymbolId: string,
fullHistory: IHistoryState[], {
historyCurrentStep: number history: fullHistory,
historyCurrentStep
}: IEditorState
): IHistoryState[] { ): IHistoryState[] {
const history = GetCurrentHistory(fullHistory, historyCurrentStep); const history = GetCurrentHistory(fullHistory, historyCurrentStep);
const current = history[history.length - 1]; const current = history[history.length - 1];

View file

@ -1,6 +1,5 @@
import React, { type Dispatch, type SetStateAction, useEffect, useRef } from 'react'; import React, { type Dispatch, type SetStateAction, useEffect, useRef } from 'react';
import './Editor.scss'; import './Editor.scss';
import { type IConfiguration } from '../../Interfaces/IConfiguration';
import { type IHistoryState } from '../../Interfaces/IHistoryState'; import { type IHistoryState } from '../../Interfaces/IHistoryState';
import { UI } from '../UI/UI'; import { UI } from '../UI/UI';
import { UseCustomEvents, UseEditorListener } from '../../Events/EditorEvents'; import { UseCustomEvents, UseEditorListener } from '../../Events/EditorEvents';
@ -8,6 +7,7 @@ import { MAX_HISTORY } from '../../utils/default';
import { FindContainerById } from '../../utils/itertools'; import { FindContainerById } from '../../utils/itertools';
import { Menu } from '../Menu/Menu'; import { Menu } from '../Menu/Menu';
import { type IReplaceContainer } from '../../Interfaces/IReplaceContainer'; import { type IReplaceContainer } from '../../Interfaces/IReplaceContainer';
import { type IEditorState } from '../../Interfaces/IEditorState';
import { DeleteContainer, OnPropertyChange, ReplaceByContainer } from './Actions/ContainerOperations'; import { DeleteContainer, OnPropertyChange, ReplaceByContainer } from './Actions/ContainerOperations';
import { SaveEditorAsJSON, SaveEditorAsSVG } from './Actions/Save'; import { SaveEditorAsJSON, SaveEditorAsSVG } from './Actions/Save';
import { OnKey } from './Actions/Shortcuts'; import { OnKey } from './Actions/Shortcuts';
@ -21,9 +21,7 @@ import { AddContainerToSelectedContainer, AddContainer } from './Actions/AddCont
interface IEditorProps { interface IEditorProps {
root: Element | Document root: Element | Document
configuration: IConfiguration editorState: IEditorState
history: IHistoryState[]
historyCurrentStep: number
} }
function UseShortcuts( function UseShortcuts(
@ -73,8 +71,8 @@ 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.editorState.history));
const [historyCurrentStep, setHistoryCurrentStep] = React.useState<number>(props.historyCurrentStep); const [historyCurrentStep, setHistoryCurrentStep] = React.useState<number>(props.editorState.historyCurrentStep);
const [ const [
replaceContainer, replaceContainer,
setReplaceContainer setReplaceContainer
@ -86,6 +84,13 @@ export function Editor(props: IEditorProps): JSX.Element {
const eventCallbackQueue = useRef<CustomEvent[]>([]); const eventCallbackQueue = useRef<CustomEvent[]>([]);
const setNewHistory = UseNewHistoryState(setHistory, setHistoryCurrentStep); const setNewHistory = UseNewHistoryState(setHistory, setHistoryCurrentStep);
const editorState: IEditorState = {
configuration: props.editorState.configuration,
history,
historyCurrentStep,
version: props.editorState.version
};
function ResetState(): void { function ResetState(): void {
setReplaceContainer({ isReplacing: false, id: undefined, category: undefined }); setReplaceContainer({ isReplacing: false, id: undefined, category: undefined });
} }
@ -96,51 +101,40 @@ export function Editor(props: IEditorProps): JSX.Element {
historyCurrentStep, historyCurrentStep,
setHistoryCurrentStep, setHistoryCurrentStep,
() => { () => {
setNewHistory(DeleteContainer(selectedContainerId, history, historyCurrentStep)); setNewHistory(DeleteContainer(selectedContainerId, editorState));
}, },
ResetState ResetState
); );
UseCustomEvents( UseCustomEvents(
eventCallbackQueue, eventCallbackQueue,
props.root, props.root,
history, editorState,
historyCurrentStep,
props.configuration,
editorRef, editorRef,
setNewHistory setNewHistory
); );
UseEditorListener( UseEditorListener(
props.root, props.root,
history, editorState
historyCurrentStep,
props.configuration
); );
// Context Menu // Context Menu
const menuActions = new Map(); const menuActions = new Map();
InitActions( InitActions(
menuActions, menuActions,
props.configuration, editorState,
history,
historyCurrentStep,
setNewHistory, setNewHistory,
setHistoryCurrentStep, setHistoryCurrentStep,
setReplaceContainer setReplaceContainer
); );
// Render // Render
const configuration = props.configuration;
const current = GetCurrentHistoryState(history, historyCurrentStep); const current = GetCurrentHistoryState(history, historyCurrentStep);
const selected = FindContainerById(current.containers, selectedContainerId); const selected = FindContainerById(current.containers, selectedContainerId);
return ( return (
<div ref={editorRef} className="Editor font-sans h-full "> <div ref={editorRef} className="Editor font-sans h-full ">
<UI <UI
editorState={{ editorState={editorState}
configuration: props.configuration,
history,
historyCurrentStep
}}
replaceContainer={replaceContainer} replaceContainer={replaceContainer}
selectedContainerId={selectedContainerId} selectedContainerId={selectedContainerId}
selectedSymbolId={selectedSymbolId} selectedSymbolId={selectedSymbolId}
@ -150,8 +144,7 @@ export function Editor(props: IEditorProps): JSX.Element {
deleteContainer={(containerId: string) => { deleteContainer={(containerId: string) => {
setNewHistory(DeleteContainer( setNewHistory(DeleteContainer(
containerId, containerId,
history, editorState
historyCurrentStep
)); ));
}} }}
onPropertyChange={(key, value, type) => { onPropertyChange={(key, value, type) => {
@ -160,8 +153,7 @@ export function Editor(props: IEditorProps): JSX.Element {
value, value,
type, type,
selected, selected,
history, editorState
historyCurrentStep
)); ));
}} }}
addOrReplaceContainer={(type) => { addOrReplaceContainer={(type) => {
@ -172,9 +164,7 @@ export function Editor(props: IEditorProps): JSX.Element {
const newHistory = ReplaceByContainer( const newHistory = ReplaceByContainer(
replaceContainer.id, replaceContainer.id,
type, type,
configuration, editorState
history,
historyCurrentStep
); );
setReplaceContainer({ isReplacing: false, id: undefined, category: undefined }); setReplaceContainer({ isReplacing: false, id: undefined, category: undefined });
setNewHistory(newHistory); setNewHistory(newHistory);
@ -182,9 +172,7 @@ export function Editor(props: IEditorProps): JSX.Element {
setNewHistory(AddContainerToSelectedContainer( setNewHistory(AddContainerToSelectedContainer(
type, type,
selected, selected,
configuration, editorState
history,
historyCurrentStep
)); ));
} }
}} }}
@ -193,17 +181,13 @@ export function Editor(props: IEditorProps): JSX.Element {
index, index,
type, type,
parent, parent,
configuration, editorState
history,
historyCurrentStep
)); ));
}} }}
addSymbol={(type) => { addSymbol={(type) => {
setNewHistory(AddSymbol( setNewHistory(AddSymbol(
type, type,
configuration, editorState
history,
historyCurrentStep
)); ));
}} }}
onSymbolPropertyChange={(key, value) => { onSymbolPropertyChange={(key, value) => {
@ -211,8 +195,7 @@ export function Editor(props: IEditorProps): JSX.Element {
key, key,
value, value,
selectedSymbolId, selectedSymbolId,
history, editorState
historyCurrentStep
)); ));
}} }}
selectSymbol={(symbolId) => { selectSymbol={(symbolId) => {
@ -221,16 +204,11 @@ export function Editor(props: IEditorProps): JSX.Element {
deleteSymbol={(symbolId) => { deleteSymbol={(symbolId) => {
setNewHistory(DeleteSymbol( setNewHistory(DeleteSymbol(
symbolId, symbolId,
history, editorState
historyCurrentStep
)); ));
}} }}
saveEditorAsJSON={() => { saveEditorAsJSON={() => {
SaveEditorAsJSON( SaveEditorAsJSON(editorState);
history,
historyCurrentStep,
configuration
);
}} }}
saveEditorAsSVG={() => { SaveEditorAsSVG(); }} saveEditorAsSVG={() => { SaveEditorAsSVG(); }}
loadState={(move) => { setHistoryCurrentStep(move); }} loadState={(move) => { setHistoryCurrentStep(move); }}

View file

@ -43,18 +43,11 @@ export const events: IEditorEvent[] = [
export function UseCustomEvents( export function UseCustomEvents(
callbackQueue: React.RefObject<CustomEvent[]>, callbackQueue: React.RefObject<CustomEvent[]>,
root: Element | Document, root: Element | Document,
history: IHistoryState[], editorState: IEditorState,
historyCurrentStep: number,
configuration: IConfiguration,
editorRef: React.RefObject<HTMLDivElement>, editorRef: React.RefObject<HTMLDivElement>,
setNewHistory: (newHistory: IHistoryState[], historyCurrentStep?: number) => void setNewHistory: (newHistory: IHistoryState[], historyCurrentStep?: number) => void
): void { ): void {
useEffect(() => { useEffect(() => {
const editorState: IEditorState = {
history,
historyCurrentStep,
configuration
};
const current = editorRef.current; const current = editorRef.current;
if (current === null) { if (current === null) {
@ -110,16 +103,9 @@ export function UseCustomEvents(
export function UseEditorListener( export function UseEditorListener(
root: Element | Document, root: Element | Document,
history: IHistoryState[], editorState: IEditorState
historyCurrentStep: number,
configuration: IConfiguration
): void { ): void {
useEffect(() => { useEffect(() => {
const editorState: IEditorState = {
history,
historyCurrentStep,
configuration
};
const event = new CustomEvent('editorListener', { detail: editorState }); const event = new CustomEvent('editorListener', { detail: editorState });
root.dispatchEvent(event); root.dispatchEvent(event);
}); });
@ -209,14 +195,11 @@ function AddContainer({
parentId parentId
} = eventInitDict?.detail; } = eventInitDict?.detail;
const history = GetCurrentHistory(editorState.history, editorState.historyCurrentStep);
const newHistory = AddContainerAction( const newHistory = AddContainerAction(
index, index,
type, type,
parentId, parentId,
editorState.configuration, editorState
history,
editorState.historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
@ -249,9 +232,7 @@ function AppendContainer({
parent?.children.length ?? 0, parent?.children.length ?? 0,
type, type,
parentId, parentId,
editorState.configuration, editorState
history,
editorState.historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
@ -273,13 +254,9 @@ function DeleteContainer({
const { const {
containerId containerId
} = eventInitDict?.detail; } = eventInitDict?.detail;
const history = GetCurrentHistory(editorState.history, editorState.historyCurrentStep);
const newHistory = DeleteContainerAction( const newHistory = DeleteContainerAction(
containerId, containerId,
history, editorState
editorState.historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
@ -302,13 +279,9 @@ function AddSymbol({
name name
} = eventInitDict?.detail; } = eventInitDict?.detail;
const history = GetCurrentHistory(editorState.history, editorState.historyCurrentStep);
const newHistory = AddSymbolAction( const newHistory = AddSymbolAction(
name, name,
editorState.configuration, editorState
history,
editorState.historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);
@ -331,11 +304,9 @@ function DeleteSymbol({
symbolId symbolId
} = eventInitDict?.detail; } = eventInitDict?.detail;
const history = GetCurrentHistory(editorState.history, editorState.historyCurrentStep);
const newHistory = DeleteSymbolAction( const newHistory = DeleteSymbolAction(
symbolId, symbolId,
history, editorState
editorState.historyCurrentStep
); );
setNewHistory(newHistory); setNewHistory(newHistory);

View file

@ -5,4 +5,5 @@ export interface IEditorState {
history: IHistoryState[] history: IHistoryState[]
historyCurrentStep: number historyCurrentStep: number
configuration: IConfiguration configuration: IConfiguration
version: string
} }

View file

@ -10,6 +10,7 @@ import { Orientation } from '../Enums/Orientation';
import { AppState } from '../Enums/AppState'; import { AppState } from '../Enums/AppState';
import { type IDimensionOptions } from '../Interfaces/IDimensionOptions'; import { type IDimensionOptions } from '../Interfaces/IDimensionOptions';
import { type IDimensionStyle } from '../Components/SVG/Elements/Dimension'; import { type IDimensionStyle } from '../Components/SVG/Elements/Dimension';
import { version } from 'react';
/// EDITOR DEFAULTS /// /// EDITOR DEFAULTS ///
@ -151,7 +152,8 @@ export function GetDefaultEditorState(configuration: IConfiguration): IEditorSta
symbols: new Map() symbols: new Map()
} }
], ],
historyCurrentStep: 0 historyCurrentStep: 0,
version: APP_VERSION
}; };
} }

2
src/vite-env.d.ts vendored
View file

@ -11,3 +11,5 @@ interface ImportMetaEnv {
interface ImportMeta { interface ImportMeta {
readonly env: ImportMetaEnv readonly env: ImportMetaEnv
} }
declare const APP_VERSION: string;

View file

@ -6,5 +6,8 @@ export default defineConfig({
plugins: [ plugins: [
react() react()
], ],
base: './' base: './',
define: {
APP_VERSION: JSON.stringify(process.env.npm_package_version)
}
}); });