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

@ -16,6 +16,7 @@ interface IAppState {
configuration: Configuration, configuration: Configuration,
MainContainer: Container | null, MainContainer: Container | null,
SelectedContainer: Container | null SelectedContainer: Container | null
Counters: Record<string, number>
} }
class App extends React.Component<IAppProps> { class App extends React.Component<IAppProps> {
@ -32,7 +33,8 @@ class App extends React.Component<IAppProps> {
MainContainer: {} as AvailableContainer MainContainer: {} as AvailableContainer
}, },
MainContainer: null, MainContainer: null,
SelectedContainer: null SelectedContainer: null,
Counters: {}
}; };
} }
@ -97,12 +99,19 @@ class App extends React.Component<IAppProps> {
throw new Error(`[AddContainer] Object type not found. Found: ${type}`); throw new Error(`[AddContainer] Object type not found. Found: ${type}`);
} }
const parent = this.state.SelectedContainer; const newCounters = Object.assign({}, this.state.Counters);
if (newCounters[type] === null ||
newCounters[type] === undefined) {
newCounters[type] = 0;
} else {
newCounters[type]++;
}
const depth: number = Container.getDepth(parent) + 1; const parent = this.state.SelectedContainer;
const count = newCounters[type];
const container = new Container({ const container = new Container({
parent, parent,
id: `${type}-${depth}-${parent.props.children.length.toString()}`, id: `${type}-${count}`,
x: 0, x: 0,
y: 0, y: 0,
width: properties?.Width, width: properties?.Width,
@ -119,7 +128,8 @@ class App extends React.Component<IAppProps> {
const newMainContainer = new Container(Object.assign({}, this.state.MainContainer.props)); const newMainContainer = new Container(Object.assign({}, this.state.MainContainer.props));
this.setState({ this.setState({
MainContainer: newMainContainer MainContainer: newMainContainer,
Counters: newCounters
}); });
} }

View file

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