From 140f3c77fca732a8933c551b1f3ea74fdc491d2e Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 14 Sep 2023 13:38:59 -0700 Subject: [PATCH] add nested layout design --- d2layouts/d2layouts.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 d2layouts/d2layouts.go diff --git a/d2layouts/d2layouts.go b/d2layouts/d2layouts.go new file mode 100644 index 000000000..bbddba0cd --- /dev/null +++ b/d2layouts/d2layouts.go @@ -0,0 +1,61 @@ +package d2layouts + +import "oss.terrastruct.com/d2/d2graph" + +func LayoutNested(g *d2graph.Graph, graphType string, coreLayout d2graph.LayoutGraph) { + // Before we can layout these nodes, we need to handle all nested diagrams first. + extracted := make(map[*d2graph.Object]*d2graph.Graph) + + // Iterate top-down from Root so all nested diagrams can process their own contents + queue := make([]*d2graph.Object, 0, len(g.Root.ChildrenArray)) + queue = append(queue, g.Root.ChildrenArray...) + + for _, child := range queue { + if graphType := NestedGraphType(child); graphType != nil { + // There is a nested diagram here, so extract its contents and process in the same way + nestedGraph := ExtractNested(child) + + // Layout of nestedGraph is completed + LayoutNested(nestedGraph, *graphType, coreLayout) + + // Fit child to size of nested layout + FitToGraph(child, nestedGraph) + + // We will restore the contents after running layout with child as the placeholder + extracted[child] = nestedGraph + } else if len(child.Children) > 0 { + queue = append(queue, child.ChildrenArray...) + } + } + + // We can now run layout with accurate sizes of nested layout containers + // Layout according to the type of diagram + LayoutDiagram(g, graphType, coreLayout) + + // With the layout set, inject all the extracted graphs + for n, nestedGraph := range extracted { + InjectNested(n, nestedGraph) + } +} + +func NestedGraphType(container *d2graph.Object) *string { + // TODO + return nil +} + +func ExtractNested(container *d2graph.Object) *d2graph.Graph { + // TODO + return nil +} + +func InjectNested(container *d2graph.Object, graph *d2graph.Graph) { + // TODO +} + +func FitToGraph(container *d2graph.Object, graph *d2graph.Graph) { + // TODO +} + +func LayoutDiagram(graph *d2graph.Graph, graphType string, coreLayout d2graph.LayoutGraph) { + // TODO +}