import { FindContainerById, MakeDFSIterator } from './itertools'; import { IEditorState } from '../Interfaces/IEditorState'; import { IHistoryState } from '../Interfaces/IHistoryState'; /** * 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 ReviveHistory(history); } export function ReviveHistory(history: IHistoryState[]): void { for (const state of history) { ReviveState(state); } } export function ReviveState(state: IHistoryState): void { if (state.mainContainer === null || state.mainContainer === undefined) { return; } state.symbols = new Map(state.symbols); for (const symbol of state.symbols.values()) { symbol.linkedContainers = new Set(symbol.linkedContainers); } const it = MakeDFSIterator(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; } } export function GetCircularReplacer(): (key: any, value: object | Map | null) => object | null | undefined { return (key: any, value: object | null) => { if (key === 'parent') { return; } if (key === 'symbols') { return Array.from((value as Map).entries()); } if (key === 'linkedContainers') { return Array.from(value as Set); } return value; }; } export function GetCircularReplacerKeepDataStructure(): (key: any, value: object | Map | null) => object | null | undefined { return (key: any, value: object | null) => { if (key === 'parent') { return; } return value; }; }