handle lifeline edges

This commit is contained in:
Gavin Nishizawa 2023-09-28 17:54:01 -07:00
parent 2fe494f678
commit 2f71d575df
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 29 additions and 0 deletions

View file

@ -318,6 +318,15 @@ func ExtractSubgraph(container *d2graph.Object, includeSelf bool) (nestedGraph *
remainingEdges := make([]*d2graph.Edge, 0, len(g.Edges))
for _, edge := range g.Edges {
srcIsNested := isNestedObject(edge.Src)
if d2sequence.IsLifelineEnd(edge.Dst) {
// special handling for lifelines since their edge.Dst is a special Object
if srcIsNested {
nestedGraph.Edges = append(nestedGraph.Edges, edge)
} else {
remainingEdges = append(remainingEdges, edge)
}
continue
}
dstIsNested := isNestedObject(edge.Dst)
if srcIsNested && dstIsNested {
nestedGraph.Edges = append(nestedGraph.Edges, edge)

View file

@ -5,6 +5,7 @@ import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"oss.terrastruct.com/util-go/go2"
@ -411,6 +412,25 @@ func (sd *sequenceDiagram) addLifelineEdges() {
}
}
func IsLifelineEnd(obj *d2graph.Object) bool {
// lifeline ends only have ID and no graph parent or box set
if obj.Graph != nil || obj.Parent != nil || obj.Box != nil {
return false
}
if !strings.Contains(obj.ID, "-lifeline-end-") {
return false
}
parts := strings.Split(obj.ID, "-lifeline-end-")
if len(parts) > 1 {
hash := parts[len(parts)-1]
actorID := strings.Join(parts[:len(parts)-1], "-lifeline-end-")
if strconv.Itoa(go2.StringToIntHash(actorID+"-lifeline-end")) == hash {
return true
}
}
return false
}
func (sd *sequenceDiagram) placeNotes() {
rankToX := make(map[int]float64)
for _, actor := range sd.actors {