diff --git a/src/Components/ElementsSidebar/ElementsSidebar.tsx b/src/Components/ElementsSidebar/ElementsSidebar.tsx index 9f8bcc9..eefa336 100644 --- a/src/Components/ElementsSidebar/ElementsSidebar.tsx +++ b/src/Components/ElementsSidebar/ElementsSidebar.tsx @@ -14,26 +14,13 @@ interface IElementsSidebarProps { export class ElementsSidebar extends React.Component { public iterateChilds(handleContainer: (container: Container) => void): React.ReactNode { - const root = this.props.MainContainer; - if (!root) { + if (!this.props.MainContainer) { return null; } - const queue = [root]; - const visited = new Set([root]); - while (queue.length > 0) { - const container = queue.pop() as Container; - + const it = this.props.MainContainer.MakeIterator(); + for (const container of it) { handleContainer(container); - - // if this reverse() gets costly, replace it by a simple for - container.props.children.reverse().forEach((child) => { - if (visited.has(child)) { - return; - } - visited.add(child); - queue.push(child); - }); } } diff --git a/src/Components/SVG/Elements/Container.tsx b/src/Components/SVG/Elements/Container.tsx index ab23a7d..67d3d32 100644 --- a/src/Components/SVG/Elements/Container.tsx +++ b/src/Components/SVG/Elements/Container.tsx @@ -21,6 +21,25 @@ export class Container extends React.Component { return properties; } + public * MakeIterator() { + const queue: Container[] = [this]; + const visited = new Set(queue); + while (queue.length > 0) { + const container = queue.pop() as Container; + + yield container; + + // if this reverse() gets costly, replace it by a simple for + container.props.children.reverse().forEach((child) => { + if (visited.has(child)) { + return; + } + visited.add(child); + queue.push(child); + }); + } + } + public render(): React.ReactNode { const containersElements = this.props.children.map(child => child.render()); const defaultStyle = {