Keep g.Objects in order
This commit is contained in:
parent
8d79f28412
commit
2841b8b9fa
1 changed files with 14 additions and 13 deletions
|
|
@ -21,8 +21,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
||||||
oldObjects := g.Objects
|
oldObjects := g.Objects
|
||||||
oldEdges := g.Edges
|
oldEdges := g.Edges
|
||||||
|
|
||||||
// new graph objects
|
// flag objects to keep to avoid having to flag all descendants of sequence diagram to be removed
|
||||||
g.Objects = make([]*d2graph.Object, 0, len(g.Objects))
|
objectsToKeep := make(map[*d2graph.Object]struct{})
|
||||||
// edges flagged to be removed (these are internal edges of the sequence diagrams)
|
// edges flagged to be removed (these are internal edges of the sequence diagrams)
|
||||||
edgesToRemove := make(map[*d2graph.Edge]struct{})
|
edgesToRemove := make(map[*d2graph.Edge]struct{})
|
||||||
// store the sequence diagram related to a given node
|
// store the sequence diagram related to a given node
|
||||||
|
|
@ -38,9 +38,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
||||||
queue = queue[1:]
|
queue = queue[1:]
|
||||||
|
|
||||||
// root is not part of g.Objects, so we can't add it here
|
// root is not part of g.Objects, so we can't add it here
|
||||||
if obj != g.Root {
|
objectsToKeep[obj] = struct{}{}
|
||||||
g.Objects = append(g.Objects, obj)
|
|
||||||
}
|
|
||||||
if obj.Attributes.Shape.Value == d2target.ShapeSequenceDiagram {
|
if obj.Attributes.Shape.Value == d2target.ShapeSequenceDiagram {
|
||||||
// TODO: should update obj.References too?
|
// TODO: should update obj.References too?
|
||||||
|
|
||||||
|
|
@ -52,6 +50,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
||||||
sdMock := obj.EnsureChild([]string{"sequence_diagram"})
|
sdMock := obj.EnsureChild([]string{"sequence_diagram"})
|
||||||
sdMock.Attributes.Shape.Value = d2target.ShapeRectangle
|
sdMock.Attributes.Shape.Value = d2target.ShapeRectangle
|
||||||
sdMock.Attributes.Label.Value = ""
|
sdMock.Attributes.Label.Value = ""
|
||||||
|
objectsToKeep[sdMock] = struct{}{}
|
||||||
|
|
||||||
// find the edges that belong to this sequence diagra
|
// find the edges that belong to this sequence diagra
|
||||||
var edges []*d2graph.Edge
|
var edges []*d2graph.Edge
|
||||||
|
|
@ -81,15 +80,17 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// objToIndex := make(map[*d2graph.Object]int)
|
// done this way (by flagging objects) instead of appending to `queue`
|
||||||
// for i, obj := range oldObjects {
|
// because appending in that order would change the order of g.Objects which
|
||||||
// objToIndex[obj] = i
|
// could lead to layout changes (as the order of the objects might be important for the underlying engine)
|
||||||
// }
|
newObjects := make([]*d2graph.Object, 0, len(objectsToKeep))
|
||||||
|
for _, obj := range g.Objects {
|
||||||
// sort.Slice(g.Objects, func(i, j int) bool {
|
if _, exists := objectsToKeep[obj]; exists {
|
||||||
// return objToIndex[g.Objects[i]] < objToIndex[g.Objects[j]]
|
newObjects = append(newObjects, obj)
|
||||||
// })
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g.Objects = newObjects
|
||||||
g.Edges = newEdges
|
g.Edges = newEdges
|
||||||
if err := layout(ctx, g); err != nil {
|
if err := layout(ctx, g); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue