bool DeleteNodeRecursively()

in tools/FigmaSharp.Designer/FigmaDesignerSession.cs [141:199]


        bool DeleteNodeRecursively(FigmaNode current, FigmaNode parent, FigmaNode toDelete)
        {
            if (current == toDelete)
            {
                if (parent is FigmaDocument parentDocument)
                {
                    var index = Array.FindIndex(parentDocument.children, row => row == current);
                    parentDocument.children = RemoveAt<FigmaCanvas>(parentDocument.children, index);
                    return true;
                }
                else if (parent is IFigmaNodeContainer parentNodeContainer)
                {
                    var index = Array.FindIndex(parentNodeContainer.children, row => row == current);
                    parentNodeContainer.children = RemoveAt<FigmaNode>(parentNodeContainer.children, index);
                    return true;
                }
            }

            if (current is FigmaDocument document)
            {
                if (document.children != null)
                {
                    foreach (var item in document.children)
                    {
                        try
                        {
                            if (DeleteNodeRecursively(item, current, toDelete))
                            {
                                return true;
                            };
                        }
                        catch (Exception ex)
                        {
                            LoggingService.LogError("FigmaDesignerSession Error", ex);
                        }
                    }
                }
                return false;
            }

            if (current is IFigmaNodeContainer container)
            {
                foreach (var item in container.children)
                {
                    try
                    {
                        if (DeleteNodeRecursively(item, current, toDelete))
                        {
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggingService.LogError("FigmaDesignerSession Error", ex);
                    }
                }
            }
            return false;
        }