commit
0cee21ecba
2 changed files with 47 additions and 11 deletions
|
|
@ -12,16 +12,7 @@ import (
|
|||
"oss.terrastruct.com/util-go/go2"
|
||||
)
|
||||
|
||||
// Layout runs the sequence diagram layout engine on objects of shape sequence_diagram
|
||||
//
|
||||
// 1. Traverse graph from root, skip objects with shape not `sequence_diagram`
|
||||
// 2. Construct a sequence diagram from all descendant objects and edges
|
||||
// 3. Remove those objects and edges from the main graph
|
||||
// 4. Run layout on sequence diagrams
|
||||
// 5. Set the resulting dimensions to the main graph shape
|
||||
// 6. Run core layouts (still without sequence diagram innards)
|
||||
// 7. Put back sequence diagram innards in correct location
|
||||
func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) 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{})
|
||||
edgesToRemove := make(map[*d2graph.Edge]struct{})
|
||||
sequenceDiagrams := make(map[string]*sequenceDiagram)
|
||||
|
|
@ -41,7 +32,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
|||
|
||||
sd, err := layoutSequenceDiagram(g, obj)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
obj.Children = make(map[string]*d2graph.Object)
|
||||
obj.ChildrenArray = nil
|
||||
|
|
@ -72,6 +63,24 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
|
|||
// TODO this isn't a proper deletion because the objects still appear as children of the object
|
||||
g.Objects = layoutObjects
|
||||
|
||||
return sequenceDiagrams, objectOrder, edgeOrder, nil
|
||||
}
|
||||
|
||||
// Layout runs the sequence diagram layout engine on objects of shape sequence_diagram
|
||||
//
|
||||
// 1. Traverse graph from root, skip objects with shape not `sequence_diagram`
|
||||
// 2. Construct a sequence diagram from all descendant objects and edges
|
||||
// 3. Remove those objects and edges from the main graph
|
||||
// 4. Run layout on sequence diagrams
|
||||
// 5. Set the resulting dimensions to the main graph shape
|
||||
// 6. Run core layouts (still without sequence diagram innards)
|
||||
// 7. Put back sequence diagram innards in correct location
|
||||
func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error {
|
||||
sequenceDiagrams, objectOrder, edgeOrder, err := WithoutSequenceDiagrams(ctx, g)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if g.Root.IsSequenceDiagram() {
|
||||
// the sequence diagram is the only layout engine if the whole diagram is
|
||||
// shape: sequence_diagram
|
||||
|
|
|
|||
|
|
@ -17,9 +17,12 @@ import (
|
|||
"oss.terrastruct.com/util-go/assert"
|
||||
"oss.terrastruct.com/util-go/diff"
|
||||
|
||||
"oss.terrastruct.com/d2/d2compiler"
|
||||
"oss.terrastruct.com/d2/d2graph"
|
||||
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
|
||||
"oss.terrastruct.com/d2/d2layouts/d2elklayout"
|
||||
"oss.terrastruct.com/d2/d2layouts/d2near"
|
||||
"oss.terrastruct.com/d2/d2layouts/d2sequence"
|
||||
"oss.terrastruct.com/d2/d2lib"
|
||||
"oss.terrastruct.com/d2/d2renderers/d2svg"
|
||||
"oss.terrastruct.com/d2/d2target"
|
||||
|
|
@ -88,6 +91,28 @@ func runa(t *testing.T, tcs []testCase) {
|
|||
}
|
||||
}
|
||||
|
||||
// serde exercises serializing and deserializing the graph
|
||||
// We want to run all the steps leading up to serialization in the course of regular layout
|
||||
func serde(t *testing.T, tc testCase, ruler *textmeasure.Ruler) {
|
||||
ctx := context.Background()
|
||||
ctx = log.WithTB(ctx, t, nil)
|
||||
g, err := d2compiler.Compile("", strings.NewReader(tc.script), &d2compiler.CompileOptions{
|
||||
UTF16: false,
|
||||
})
|
||||
tassert.Nil(t, err)
|
||||
if len(g.Objects) > 0 {
|
||||
err = g.SetDimensions(nil, ruler, nil)
|
||||
tassert.Nil(t, err)
|
||||
d2near.WithoutConstantNears(ctx, g)
|
||||
d2sequence.WithoutSequenceDiagrams(ctx, g)
|
||||
}
|
||||
b, err := d2graph.SerializeGraph(g)
|
||||
tassert.Nil(t, err)
|
||||
var newG d2graph.Graph
|
||||
err = d2graph.DeserializeGraph(b, &newG)
|
||||
tassert.Nil(t, err)
|
||||
}
|
||||
|
||||
func run(t *testing.T, tc testCase) {
|
||||
ctx := context.Background()
|
||||
ctx = log.WithTB(ctx, t, nil)
|
||||
|
|
@ -98,6 +123,8 @@ func run(t *testing.T, tc testCase) {
|
|||
return
|
||||
}
|
||||
|
||||
serde(t, tc, ruler)
|
||||
|
||||
layoutsTested := []string{"dagre", "elk"}
|
||||
|
||||
for _, layoutName := range layoutsTested {
|
||||
|
|
|
|||
Loading…
Reference in a new issue