diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index 0162820cb..a380d6f10 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -30,6 +30,7 @@ var setupJS string var dagreJS string type DagreNode struct { + ID string `json:"id"` X float64 `json:"x"` Y float64 `json:"y"` Width float64 `json:"width"` @@ -49,7 +50,7 @@ type dagreGraphAttrs struct { rankdir string } -func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { +func Layout(ctx context.Context, g *d2graph.Graph) (err error) { defer xdefer.Errorf(&err, "failed to dagre layout") debugJS := false @@ -66,7 +67,7 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { edgesep: 40, nodesep: 60, } - switch d2graph.Root.Attributes.Direction.Value { + switch g.Root.Attributes.Direction.Value { case "down": rootAttrs.rankdir = "TB" case "right": @@ -84,14 +85,16 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { } loadScript := "" - for _, obj := range d2graph.Objects { + idToObj := make(map[string]*d2graph.Object) + for _, obj := range g.Objects { id := obj.AbsID() + idToObj[id] = obj loadScript += generateAddNodeLine(id, int(obj.Width), int(obj.Height)) - if obj.Parent != d2graph.Root { + if obj.Parent != g.Root { loadScript += generateAddParentLine(id, obj.Parent.AbsID()) } } - for _, edge := range d2graph.Edges { + for _, edge := range g.Edges { // dagre doesn't work with edges to containers so we connect container edges to their first child instead (going all the way down) // we will chop the edge where it intersects the container border so it only shows the edge from the container src := edge.Src @@ -124,13 +127,7 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { return err } - // val, err := v8ctx.RunScript("JSON.stringify(dagre.graphlib.json.write(g))", "q.js") - // if err != nil { - // return err - // } - // log.Debug(ctx, "graph", slog.F("json", val.String())) - - for i, obj := range d2graph.Objects { + for i := range g.Objects { val, err := v8ctx.RunScript(fmt.Sprintf("JSON.stringify(g.node(g.nodes()[%d]))", i), "value.js") if err != nil { return err @@ -139,6 +136,11 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { if err := json.Unmarshal([]byte(val.String()), &dn); err != nil { return err } + if debugJS { + log.Debug(ctx, "graph", slog.F("json", dn)) + } + + obj := idToObj[dn.ID] // dagre gives center of node obj.TopLeft = geo.NewPoint(math.Round(dn.X-dn.Width/2), math.Round(dn.Y-dn.Height/2)) @@ -159,7 +161,7 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { } } - for i, edge := range d2graph.Edges { + for i, edge := range g.Edges { val, err := v8ctx.RunScript(fmt.Sprintf("JSON.stringify(g.edge(g.edges()[%d]))", i), "value.js") if err != nil { return err @@ -168,6 +170,9 @@ func Layout(ctx context.Context, d2graph *d2graph.Graph) (err error) { if err := json.Unmarshal([]byte(val.String()), &de); err != nil { return err } + if debugJS { + log.Debug(ctx, "graph", slog.F("json", de)) + } points := make([]*geo.Point, len(de.Points)) for i := range de.Points { @@ -250,7 +255,7 @@ func setGraphAttrs(attrs dagreGraphAttrs) string { } func generateAddNodeLine(id string, width, height int) string { - return fmt.Sprintf("g.setNode(`%s`, { width: %d, height: %d });\n", id, width, height) + return fmt.Sprintf("g.setNode(`%s`, { id: `%s`, width: %d, height: %d });\n", id, id, width, height) } func generateAddParentLine(childID, parentID string) string { diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go index d52d2cdc8..6bc25dcf1 100644 --- a/e2etests/stable_test.go +++ b/e2etests/stable_test.go @@ -1213,7 +1213,6 @@ finally.sequence.scorer -> finally.sequence.itemResponse.c`, { name: "number_connections", script: `1 -> 2 - foo baz: Foo Baz foo baz -> hello diff --git a/e2etests/testdata/stable/number_connections/dagre/board.exp.json b/e2etests/testdata/stable/number_connections/dagre/board.exp.json index 0dffaa1b3..8ab82e6c4 100644 --- a/e2etests/testdata/stable/number_connections/dagre/board.exp.json +++ b/e2etests/testdata/stable/number_connections/dagre/board.exp.json @@ -5,10 +5,10 @@ "id": "foo baz", "type": "", "pos": { - "x": 1, + "x": 173, "y": 0 }, - "width": 112, + "width": 159, "height": 126, "opacity": 1, "strokeDash": 0, @@ -44,10 +44,10 @@ "id": "1", "type": "", "pos": { - "x": 0, - "y": 226 + "x": 1, + "y": 0 }, - "width": 113, + "width": 112, "height": 126, "opacity": 1, "strokeDash": 0, @@ -83,10 +83,10 @@ "id": "2", "type": "", "pos": { - "x": 173, - "y": 0 + "x": 0, + "y": 226 }, - "width": 159, + "width": 113, "height": 126, "opacity": 1, "strokeDash": 0, @@ -186,15 +186,15 @@ "route": [ { "x": 56.5, - "y": 226 + "y": 126 }, { "x": 56.5, - "y": 226 + "y": 166 }, { "x": 56.5, - "y": 226 + "y": 186 }, { "x": 56.5, diff --git a/e2etests/testdata/stable/number_connections/dagre/sketch.exp.svg b/e2etests/testdata/stable/number_connections/dagre/sketch.exp.svg index e2feda03c..6ef443e1e 100644 --- a/e2etests/testdata/stable/number_connections/dagre/sketch.exp.svg +++ b/e2etests/testdata/stable/number_connections/dagre/sketch.exp.svg @@ -14,7 +14,7 @@ width="532" height="552" viewBox="-100 -100 532 552">Foo Baz12hello Foo Baz12hello