Fix json selected container by saving its id
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Siklos 2022-08-04 19:30:48 +02:00
parent 7b704fa33d
commit f524e8f7b9
2 changed files with 42 additions and 25 deletions

View file

@ -9,6 +9,7 @@ import { Configuration } from './Interfaces/Configuration';
export interface IHistoryState { export interface IHistoryState {
MainContainer: IContainerModel | null, MainContainer: IContainerModel | null,
SelectedContainer: IContainerModel | null, SelectedContainer: IContainerModel | null,
SelectedContainerId: string,
TypeCounters: Record<string, number> TypeCounters: Record<string, number>
} }
@ -157,12 +158,11 @@ export async function fetchConfiguration(): Promise<Configuration> {
function Revive(editorState: IEditorState): void { function Revive(editorState: IEditorState): void {
const history = editorState.history; const history = editorState.history;
for (const state of history) { for (const state of history) {
if (state.MainContainer === null) { if (state.MainContainer === null || state.MainContainer === undefined) {
continue; continue;
} }
const it = MakeIterator(state.MainContainer); const it = MakeIterator(state.MainContainer);
state.SelectedContainer = state.MainContainer;
for (const container of it) { for (const container of it) {
const parentId = container.properties.parentId; const parentId = container.properties.parentId;
if (parentId === null) { if (parentId === null) {
@ -175,5 +175,12 @@ function Revive(editorState: IEditorState): void {
} }
container.parent = parent; container.parent = parent;
} }
const selected = findContainerById(state.MainContainer, state.SelectedContainerId);
if (selected === undefined) {
state.SelectedContainer = null;
continue;
}
state.SelectedContainer = selected;
} }
} }

View file

@ -6,7 +6,7 @@ import { ElementsSidebar } from './Components/ElementsSidebar/ElementsSidebar';
import { Configuration } from './Interfaces/Configuration'; import { Configuration } from './Interfaces/Configuration';
import { SVG } from './Components/SVG/SVG'; import { SVG } from './Components/SVG/SVG';
import { History } from './Components/History/History'; import { History } from './Components/History/History';
import { ContainerModel, IContainerModel, MakeIterator } from './Components/SVG/Elements/ContainerModel'; import { ContainerModel, findContainerById, IContainerModel, MakeIterator } from './Components/SVG/Elements/ContainerModel';
import Properties from './Interfaces/Properties'; import Properties from './Interfaces/Properties';
import { IHistoryState } from './App'; import { IHistoryState } from './App';
@ -79,11 +79,24 @@ class Editor extends React.Component<IEditorProps> {
public SelectContainer(container: ContainerModel) { public SelectContainer(container: ContainerModel) {
const history = this.getCurrentHistory(); const history = this.getCurrentHistory();
const current = history[history.length - 1]; const current = history[history.length - 1];
if (current.MainContainer === null) {
throw new Error('[SelectContainer] Tried to select a container while there is no main container!');
}
const mainContainerClone = structuredClone(current.MainContainer);
const SelectedContainer = findContainerById(mainContainerClone, container.properties.id);
if (SelectedContainer === undefined) {
throw new Error('[SelectContainer] Cannot find container among children of main container!');
}
this.setState({ this.setState({
history: history.concat([{ history: history.concat([{
MainContainer: current.MainContainer, MainContainer: mainContainerClone,
TypeCounters: current.TypeCounters, TypeCounters: Object.assign({}, current.TypeCounters),
SelectedContainer: container SelectedContainer,
SelectedContainerId: SelectedContainer.properties.id
}]), }]),
historyCurrentStep: history.length historyCurrentStep: history.length
} as IEditorState); } as IEditorState);
@ -110,30 +123,25 @@ class Editor extends React.Component<IEditorProps> {
} }
if (parent === null) { if (parent === null) {
const clone: IContainerModel = structuredClone(current.SelectedContainer); const selectedContainerClone: IContainerModel = structuredClone(current.SelectedContainer);
(clone.properties as any)[key] = value; (selectedContainerClone.properties as any)[key] = value;
this.setState({ this.setState({
history: history.concat([{ history: history.concat([{
SelectedContainer: clone, SelectedContainer: selectedContainerClone,
MainContainer: clone, SelectedContainerId: selectedContainerClone.properties.id,
TypeCounters: current.TypeCounters MainContainer: selectedContainerClone,
TypeCounters: Object.assign({}, current.TypeCounters)
}]), }]),
historyCurrentStep: history.length historyCurrentStep: history.length
} as IEditorState); } as IEditorState);
return; return;
} }
const clone: IContainerModel = structuredClone(current.MainContainer); const mainContainerClone: IContainerModel = structuredClone(current.MainContainer);
const it = MakeIterator(clone); const it = MakeIterator(mainContainerClone);
let container: ContainerModel | null = null; const container: ContainerModel | undefined = findContainerById(current.MainContainer, current.SelectedContainer.properties.id);
for (const child of it) {
if (child.properties.id === current.SelectedContainer.properties.id) {
container = child as ContainerModel;
break;
}
}
if (container === null) { if (container === null || container === undefined) {
throw new Error('[OnPropertyChange] Container model was not found among children of the main container!'); throw new Error('[OnPropertyChange] Container model was not found among children of the main container!');
} }
@ -143,8 +151,9 @@ class Editor extends React.Component<IEditorProps> {
{ {
history: history.concat([{ history: history.concat([{
SelectedContainer: container, SelectedContainer: container,
MainContainer: clone, SelectedContainerId: container.properties.id,
TypeCounters: current.TypeCounters MainContainer: mainContainerClone,
TypeCounters: Object.assign({}, current.TypeCounters)
}]), }]),
historyCurrentStep: history.length historyCurrentStep: history.length
} as IEditorState); } as IEditorState);
@ -230,7 +239,8 @@ class Editor extends React.Component<IEditorProps> {
history: history.concat([{ history: history.concat([{
MainContainer: clone, MainContainer: clone,
TypeCounters: newCounters, TypeCounters: newCounters,
SelectedContainer: parent SelectedContainer: parent,
SelectedContainerId: parent.properties.id
}]), }]),
historyCurrentStep: history.length historyCurrentStep: history.length
} as IEditorState); } as IEditorState);