svg-layout-designer-react/src/utils/saveload.ts
Siklos c81a6fe44b
All checks were successful
continuous-integration/drone/push Build is passing
Implement events for external use + Rename interfaces with a I prefix + add some documentation (#26)
Implement events for external use
Rename interfaces with a I prefix
Add some documentation

Co-authored-by: Eric NGUYEN <enguyen@techform.fr>
Reviewed-on: https://git.siklos-chaneru.duckdns.org/Siklos/svg-layout-designer-react/pulls/26
2022-08-12 06:36:14 -04:00

63 lines
1.7 KiB
TypeScript

import { findContainerById, MakeIterator } from './itertools';
import { IEditorState } from '../Interfaces/IEditorState';
/**
* Revive the Editor state
* by setting the containers references to their parent
* @param editorState Editor state
*/
export function Revive(editorState: IEditorState): void {
const history = editorState.history;
// restore last step
editorState.historyCurrentStep = history.length - 1;
// restore the parents and the selected container
for (const state of history) {
if (state.MainContainer === null || state.MainContainer === undefined) {
continue;
}
const it = MakeIterator(state.MainContainer);
for (const container of it) {
const parentId = container.properties.parentId;
if (parentId === null) {
container.parent = null;
continue;
}
const parent = findContainerById(state.MainContainer, parentId);
if (parent === undefined) {
continue;
}
container.parent = parent;
}
const selected = findContainerById(state.MainContainer, state.SelectedContainerId);
if (selected === undefined) {
state.SelectedContainer = null;
continue;
}
state.SelectedContainer = selected;
}
}
export const getCircularReplacer = (): (key: any, value: object | null) => object | null | undefined => {
const seen = new WeakSet();
return (key: any, value: object | null) => {
if (key === 'parent') {
return;
}
if (key === 'SelectedContainer') {
return;
}
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};