Skip to content

Commit

Permalink
Merge pull request #1501 from gavin-ts/fix-dagre-edge-disconnect
Browse files Browse the repository at this point in the history
Fix dagre edge disconnect
  • Loading branch information
gavin-ts authored Jul 28, 2023
2 parents 93c7c64 + 92af7a7 commit 3b0939f
Show file tree
Hide file tree
Showing 7 changed files with 649 additions and 17 deletions.
65 changes: 48 additions & 17 deletions d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ func shiftUp(g *d2graph.Graph, start, distance float64, isHorizontal bool) {
func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance float64, isHorizontal, isMargin bool) map[*d2graph.Object]struct{} {
q := []*d2graph.Object{obj}

needsMove := make(map[*d2graph.Object]struct{})
seen := make(map[*d2graph.Object]struct{})
shifted := make(map[*d2graph.Object]struct{})
shiftedEdges := make(map[*d2graph.Edge]struct{})
Expand Down Expand Up @@ -915,35 +916,41 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
}
// skip other objects behind start
if curr != obj {
if isHorizontal {
if curr.TopLeft.X < start {
continue
}
} else {
if curr.TopLeft.Y < start {
continue
if _, in := needsMove[curr]; !in {
if isHorizontal {
if curr.TopLeft.X < start {
continue
}
} else {
if curr.TopLeft.Y < start {
continue
}
}
}
}

if isHorizontal {
shift := false
if !isMargin {
shift = start < curr.TopLeft.X
} else {
shift = start <= curr.TopLeft.X
_, shift := needsMove[curr]
if !shift {
if !isMargin {
shift = start < curr.TopLeft.X
} else {
shift = start <= curr.TopLeft.X
}
}

if shift {
curr.TopLeft.X += distance
shifted[curr] = struct{}{}
}
} else {
shift := false
if !isMargin {
shift = start < curr.TopLeft.Y
} else {
shift = start <= curr.TopLeft.Y
_, shift := needsMove[curr]
if !shift {
if !isMargin {
shift = start < curr.TopLeft.Y
} else {
shift = start <= curr.TopLeft.Y
}
}
if shift {
curr.TopLeft.Y += distance
Expand Down Expand Up @@ -977,6 +984,18 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
shiftedEdges[e] = struct{}{}
continue
} else if e.Src == curr {
last := e.Route[len(e.Route)-1]
if isHorizontal {
if start <= last.X &&
e.Dst.TopLeft.X+e.Dst.Width < last.X+distance {
needsMove[e.Dst] = struct{}{}
}
} else {
if start <= last.Y &&
e.Dst.TopLeft.Y+e.Dst.Height < last.Y+distance {
needsMove[e.Dst] = struct{}{}
}
}
queue(e.Dst)
if isHorizontal {
for _, p := range e.Route {
Expand All @@ -993,6 +1012,18 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
}
shiftedEdges[e] = struct{}{}
} else if e.Dst == curr {
first := e.Route[0]
if isHorizontal {
if start <= first.X &&
e.Src.TopLeft.X+e.Src.Width < first.X+distance {
needsMove[e.Src] = struct{}{}
}
} else {
if start <= first.Y &&
e.Src.TopLeft.Y+e.Src.Height < first.Y+distance {
needsMove[e.Src] = struct{}{}
}
}
queue(e.Src)
if isHorizontal {
for _, p := range e.Route {
Expand Down
1 change: 1 addition & 0 deletions e2etests/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ cf many required: {
loadFromFile(t, "cylinder_grid_label"),
loadFromFile(t, "grid_with_latex"),
loadFromFile(t, "icons_on_top"),
loadFromFile(t, "dagre_disconnected_edge"),
}

runa(t, tcs)
Expand Down
13 changes: 13 additions & 0 deletions e2etests/testdata/files/dagre_disconnected_edge.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
x -> y

x: program {
label.near: top-center
icon: https://icons.terrastruct.com/essentials%2F005-programmer.svg
icon.near: outside-top-right
}

y: profits {
label.near: bottom-right
icon: https://icons.terrastruct.com/essentials%2Fprofits.svg
icon.near: outside-left-center
}

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

Loading

0 comments on commit 3b0939f

Please sign in to comment.