Merged PR 216: Deprecate parent from IContainerModel for FindContainerById

This commit is contained in:
Eric Nguyen 2022-10-17 16:01:06 +00:00
parent d40cd8cf8e
commit b4eba6bb9b
19 changed files with 97 additions and 109 deletions

View file

@ -121,7 +121,6 @@ export function GetDefaultEditorState(configuration: IConfiguration): IEditorSta
);
}
const mainContainer = new ContainerModel(
null,
mainContainerConfig
);
const containers = new Map<string, IContainerModel>();

View file

@ -123,16 +123,16 @@ export function * MakeRecursionDFSIterator(
/**
* Returns the depth of the container
* @returns The depth of the container
* @deprecated Please avoid using this function inside an iterationl,
* @deprecated Please avoid using this function inside an iteration,
* use recursive DFS or iterative BFS to get the depth
*/
export function GetDepth(parent: IContainerModel): number {
export function GetDepth(containers: Map<string, IContainerModel>, parent: IContainerModel): number {
let depth = 0;
let current: IContainerModel | null = parent;
while (current != null) {
depth++;
current = current.parent;
current = FindContainerById(containers, current.properties.parentId) ?? null;
}
return depth;
@ -142,22 +142,31 @@ export function GetDepth(parent: IContainerModel): number {
* Returns the absolute position by iterating to the parent
* @returns The absolute position of the container
*/
export function GetAbsolutePosition(container: IContainerModel): [number, number] {
export function GetAbsolutePosition(containers: Map<string, IContainerModel>, container: IContainerModel): [number, number] {
const x = container.properties.x;
const y = container.properties.y;
return CancelParentTransform(container.parent, x, y);
const parent = FindContainerById(containers, container.properties.parentId) ?? null;
return CancelParentTransform(containers, parent, x, y);
}
export function GetContainerLinkedList(container: IContainerModel, stop?: IContainerModel): IContainerModel[] {
const it = MakeContainerLinkedListIterator(container, stop);
export function GetContainerLinkedList(
containers: Map<string, IContainerModel>,
container: IContainerModel,
stop?: IContainerModel
): IContainerModel[] {
const it = MakeContainerLinkedListIterator(containers, container, stop);
return [...it];
}
export function * MakeContainerLinkedListIterator(container: IContainerModel, stop?: IContainerModel): Generator<IContainerModel, void, unknown> {
export function * MakeContainerLinkedListIterator(
containers: Map<string, IContainerModel>,
container: IContainerModel,
stop?: IContainerModel
): Generator<IContainerModel, void, unknown> {
let current: IContainerModel | null = container;
while (current !== stop && current != null) {
yield current;
current = current.parent;
current = FindContainerById(containers, current.properties.parentId) ?? null;
}
}
@ -169,6 +178,7 @@ export function * MakeContainerLinkedListIterator(container: IContainerModel, st
* @returns x and y such that the transformations of the parent are cancelled
*/
export function CancelParentTransform(
containers: Map<string, IContainerModel>,
parent: IContainerModel | null,
x: number,
y: number,
@ -178,7 +188,7 @@ export function CancelParentTransform(
return [x, y];
}
const it = MakeContainerLinkedListIterator(parent, stop);
const it = MakeContainerLinkedListIterator(containers, parent, stop);
for (const current of it) {
x += current.properties.x;
y += current.properties.y;
@ -195,6 +205,7 @@ export function CancelParentTransform(
* @returns x and y such that the transformations of the parent are applied
*/
export function ApplyParentTransform(
containers: Map<string, IContainerModel>,
parent: IContainerModel | null,
x: number,
y: number,
@ -204,7 +215,7 @@ export function ApplyParentTransform(
return [x, y];
}
const it = MakeContainerLinkedListIterator(parent, stop);
const it = MakeContainerLinkedListIterator(containers, parent, stop);
for (const current of it) {
x -= current.properties.x;
y -= current.properties.y;

View file

@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { FindContainerById, MakeDFSIterator } from './itertools';
import { IEditorState } from '../Interfaces/IEditorState';
import { IHistoryState } from '../Interfaces/IHistoryState';
import { IContainerModel } from '../Interfaces/IContainerModel';
@ -37,36 +36,10 @@ export function ReviveState(state: IHistoryState): void {
const containers: Array<{ Key: string, Value: IContainerModel }> = (state.containers) as any;
state.containers = new Map(containers.map(({ Key, Value }: {Key: string, Value: IContainerModel}) => [Key, Value]));
const root = FindContainerById(state.containers, state.mainContainer);
if (root === undefined) {
return;
}
// TODO: remove parent and remove this bloc of code
// TODO: See IContainerModel.ts for more detail
const it = MakeDFSIterator(root, state.containers);
for (const container of it) {
const parentId = container.properties.parentId;
if (parentId === null) {
container.parent = null;
continue;
}
const parent = FindContainerById(state.containers, parentId);
if (parent === undefined) {
continue;
}
container.parent = parent;
}
}
export function GetCircularReplacer(): (key: any, value: object | Map<string, any> | null) => object | null | undefined {
return (key: any, value: object | null) => {
if (key === 'parent') {
return;
}
if (key === 'containers') {
return [...(value as Map<string, any>).entries()]
.map(([Key, Value]: [string, any]) => ({ Key, Value }));