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 Co-authored-by: Eric NGUYEN <enguyen@techform.fr> Reviewed-on: https://git.siklos-chaneru.duckdns.org/Siklos/svg-layout-designer-react/pulls/26
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import { IContainerModel } from '../Interfaces/IContainerModel';
|
|
|
|
/**
|
|
* Returns a Generator iterating of over the children depth-first
|
|
*/
|
|
export function * MakeIterator(root: IContainerModel): Generator<IContainerModel, void, unknown> {
|
|
const queue: IContainerModel[] = [root];
|
|
const visited = new Set<IContainerModel>(queue);
|
|
while (queue.length > 0) {
|
|
const container = queue.pop() as IContainerModel;
|
|
|
|
yield container;
|
|
|
|
for (let i = container.children.length - 1; i >= 0; i--) {
|
|
const child = container.children[i];
|
|
if (visited.has(child)) {
|
|
return;
|
|
}
|
|
visited.add(child);
|
|
queue.push(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the depth of the container
|
|
* @returns The depth of the container
|
|
*/
|
|
export function getDepth(parent: IContainerModel): number {
|
|
let depth = 0;
|
|
|
|
let current: IContainerModel | null = parent;
|
|
while (current != null) {
|
|
depth++;
|
|
current = current.parent;
|
|
}
|
|
|
|
return depth;
|
|
}
|
|
|
|
/**
|
|
* Returns the absolute position by iterating to the parent
|
|
* @returns The absolute position of the container
|
|
*/
|
|
export function getAbsolutePosition(container: IContainerModel): [number, number] {
|
|
let x = Number(container.properties.x);
|
|
let y = Number(container.properties.y);
|
|
let current = container.parent;
|
|
while (current != null) {
|
|
x += Number(current.properties.x);
|
|
y += Number(current.properties.y);
|
|
current = current.parent;
|
|
}
|
|
return [x, y];
|
|
}
|
|
|
|
export function findContainerById(root: IContainerModel, id: string): IContainerModel | undefined {
|
|
const it = MakeIterator(root);
|
|
for (const container of it) {
|
|
if (container.properties.id === id) {
|
|
return container;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|