Skip to content

Commit

Permalink
fix elk self loop spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Apr 19, 2023
1 parent 9df4705 commit 9bcbdd9
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 310 deletions.
3 changes: 3 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#### Improvements 🧹

- ELK self loops get distributed around the object instead of stacking [#1232](https://github.com/terrastruct/d2/pull/1232)

#### Bugfixes ⛑️

- Fixes an issue with markdown labels that are empty when rendered [#1223](https://github.com/terrastruct/d2/issues/1223)
- ELK self loops always have enough space for long labels [#1232](https://github.com/terrastruct/d2/pull/1232)
28 changes: 27 additions & 1 deletion d2layouts/d2elklayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type elkOpts struct {
ForceNodeModelOrder bool `json:"elk.layered.crossingMinimization.forceNodeModelOrder,omitempty"`
ConsiderModelOrder string `json:"elk.layered.considerModelOrder.strategy,omitempty"`

SelfLoopDistribution string `json:"elk.layered.edgeRouting.selfLoopDistribution,omitempty"`

NodeSizeConstraints string `json:"elk.nodeSize.constraints,omitempty"`
ContentAlignment string `json:"elk.contentAlignment,omitempty"`
NodeSizeMinimum string `json:"elk.nodeSize.minimum,omitempty"`
Expand Down Expand Up @@ -159,6 +161,10 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
},
},
}
if elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing == DefaultOpts.SelfLoopSpacing {
// +5 for a tiny bit of padding
elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing = go2.Max(elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing, childrenMaxSelfLoop(g.Root, g.Root.Direction.Value == "down" || g.Root.Direction.Value == "" || g.Root.Direction.Value == "up")/2+5)
}
switch g.Root.Direction.Value {
case "down":
elkGraph.LayoutOptions.Direction = "DOWN"
Expand Down Expand Up @@ -239,6 +245,9 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
Padding: opts.Padding,
},
}
if n.LayoutOptions.ConfigurableOpts.SelfLoopSpacing == DefaultOpts.SelfLoopSpacing {
n.LayoutOptions.ConfigurableOpts.SelfLoopSpacing = go2.Max(n.LayoutOptions.ConfigurableOpts.SelfLoopSpacing, childrenMaxSelfLoop(obj, g.Root.Direction.Value == "down" || g.Root.Direction.Value == "" || g.Root.Direction.Value == "up")/2+5)
}

switch elkGraph.LayoutOptions.Direction {
case "DOWN", "UP":
Expand Down Expand Up @@ -277,7 +286,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}
} else {
n.LayoutOptions = &elkOpts{
// Margins: "[top=100,left=100,bottom=100,right=100]",
SelfLoopDistribution: "EQUALLY",
}
}

Expand Down Expand Up @@ -725,3 +734,20 @@ func countEdgeIntersects(g *d2graph.Graph, sEdge *d2graph.Edge, s geo.Segment) (
}
return crossingsCount, overlapsCount, closeOverlapsCount, touchingCount
}

func childrenMaxSelfLoop(parent *d2graph.Object, isWidth bool) int {
max := 0
for _, ch := range parent.Children {
for _, e := range parent.Graph.Edges {
if e.Src == e.Dst && e.Src == ch && e.Label.Value != "" {
if isWidth {
max = go2.Max(max, e.LabelDimensions.Width)
} else {
max = go2.Max(max, e.LabelDimensions.Height)
}
}
}
}

return max
}
Binary file modified e2etests-cli/testdata/TestCLI_E2E/internal_linked_pdf.exp.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion e2etests/testdata/files/ent2d2_basic.d2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ User <-> User: "spouse" {
shape: cf-one
}
}
User <-> User: "children/parent" {
User <-> User: "children/parent/ancestor" {
# o2m optional
source-arrowhead: {
shape: cf-one-required
Expand Down
34 changes: 17 additions & 17 deletions e2etests/testdata/stable/ent2d2_basic/dagre/board.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

160 changes: 80 additions & 80 deletions e2etests/testdata/stable/ent2d2_basic/dagre/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 18 additions & 18 deletions e2etests/testdata/stable/ent2d2_basic/elk/board.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9bcbdd9

Please sign in to comment.