This commit is contained in:
Gavin Nishizawa 2023-09-15 16:50:27 -07:00
parent 9074b29f11
commit e83ac4c3ac
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 46 additions and 1 deletions

View file

@ -95,7 +95,7 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
} }
case SequenceDiagram: case SequenceDiagram:
err = d2sequence.Layout(ctx, g, coreLayout) err = d2sequence.Layout2(ctx, g, coreLayout)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -197,6 +197,7 @@ func ExtractSelf(container *d2graph.Object) *d2graph.Graph {
func ExtractDescendants(container *d2graph.Object) *d2graph.Graph { func ExtractDescendants(container *d2graph.Object) *d2graph.Graph {
nestedGraph := d2graph.NewGraph() nestedGraph := d2graph.NewGraph()
nestedGraph.RootLevel = int(container.Level()) nestedGraph.RootLevel = int(container.Level())
nestedGraph.Root.Box = &geo.Box{}
// separate out nested edges // separate out nested edges
g := container.Graph g := container.Graph

View file

@ -40,6 +40,50 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
return nil return nil
} }
func Layout2(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
// used in layout code
g.Root.Shape.Value = d2target.ShapeSequenceDiagram
sd, err := layoutSequenceDiagram(g, g.Root)
if err != nil {
return err
}
g.Root.Box = geo.NewBox(nil, sd.getWidth()+GROUP_CONTAINER_PADDING*2, sd.getHeight()+GROUP_CONTAINER_PADDING*2)
// the sequence diagram is the only layout engine if the whole diagram is
// shape: sequence_diagram
g.Root.TopLeft = geo.NewPoint(0, 0)
obj := g.Root
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
// shift the sequence diagrams as they are always placed at (0, 0) with some padding
sd.shift(
geo.NewPoint(
obj.TopLeft.X+GROUP_CONTAINER_PADDING,
obj.TopLeft.Y+GROUP_CONTAINER_PADDING,
),
)
obj.Children = make(map[string]*d2graph.Object)
obj.ChildrenArray = make([]*d2graph.Object, 0)
for _, child := range sd.actors {
obj.Children[strings.ToLower(child.ID)] = child
obj.ChildrenArray = append(obj.ChildrenArray, child)
}
for _, child := range sd.groups {
if child.Parent.AbsID() == obj.AbsID() {
obj.Children[strings.ToLower(child.ID)] = child
obj.ChildrenArray = append(obj.ChildrenArray, child)
}
}
g.Edges = append(g.Edges, sd.lifelines...)
return nil
}
func WithoutSequenceDiagrams(ctx context.Context, g *d2graph.Graph) (map[string]*sequenceDiagram, map[string]int, map[string]int, error) { func WithoutSequenceDiagrams(ctx context.Context, g *d2graph.Graph) (map[string]*sequenceDiagram, map[string]int, map[string]int, error) {
objectsToRemove := make(map[*d2graph.Object]struct{}) objectsToRemove := make(map[*d2graph.Object]struct{})
edgesToRemove := make(map[*d2graph.Edge]struct{}) edgesToRemove := make(map[*d2graph.Edge]struct{})