Fix dfs and Element's id

This commit is contained in:
Siklos 2022-08-01 12:19:11 +02:00
parent b2a020730c
commit 5121259e83
2 changed files with 27 additions and 23 deletions

View file

@ -10,8 +10,7 @@ interface IElementsSidebarProps {
}
export class ElementsSidebar extends React.Component<IElementsSidebarProps> {
public iterateChilds(handleContainer: (container: Container, depth: number) => void): React.ReactNode {
// TODO: fix this by using dfs or sort
public iterateChilds(handleContainer: (container: Container) => void): React.ReactNode {
const root = this.props.MainContainer;
if (!root) {
return null;
@ -19,24 +18,18 @@ export class ElementsSidebar extends React.Component<IElementsSidebarProps> {
const queue = [root];
const visited = new Set([root]);
let depth = 0;
while (queue.length > 0) {
let size = queue.length;
const container = queue.pop() as Container;
while (size-- > 0) {
const container = queue.shift() as Container;
handleContainer(container);
handleContainer(container, depth);
container.props.children.forEach((child) => {
if (visited.has(child)) {
return;
}
visited.add(child);
queue.push(child);
});
}
depth++;
container.props.children.forEach((child) => {
if (visited.has(child)) {
return;
}
visited.add(child);
queue.push(child);
});
}
}
@ -44,7 +37,8 @@ export class ElementsSidebar extends React.Component<IElementsSidebarProps> {
const isOpenClasses = this.props.isOpen ? 'right-0' : '-right-64';
const containerRows: React.ReactNode[] = [];
this.iterateChilds((container: Container, depth: number) => {
this.iterateChilds((container: Container) => {
const depth: number = Container.getDepth(container);
const key = container.props.id.toString();
const text = '\t\t'.repeat(depth) + key;
const selectedClass: string = this.props.SelectedContainer !== null &&