56 lines
1.5 KiB
TypeScript
56 lines
1.5 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 => {
|
|
return (key: any, value: object | null) => {
|
|
if (key === 'parent') {
|
|
return;
|
|
}
|
|
|
|
if (key === 'SelectedContainer') {
|
|
return;
|
|
}
|
|
|
|
return value;
|
|
};
|
|
};
|