add check for nodes that need to move with edges even if they start behind the original node

This commit is contained in:
Gavin Nishizawa 2023-07-28 02:10:27 -07:00 committed by Alexander Wang
parent 120811e687
commit 189edf552a
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE

View file

@ -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{} { func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance float64, isHorizontal, isMargin bool) map[*d2graph.Object]struct{} {
q := []*d2graph.Object{obj} q := []*d2graph.Object{obj}
needsMove := make(map[*d2graph.Object]struct{})
seen := make(map[*d2graph.Object]struct{}) seen := make(map[*d2graph.Object]struct{})
shifted := make(map[*d2graph.Object]struct{}) shifted := make(map[*d2graph.Object]struct{})
shiftedEdges := make(map[*d2graph.Edge]struct{}) shiftedEdges := make(map[*d2graph.Edge]struct{})
@ -915,6 +916,7 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
} }
// skip other objects behind start // skip other objects behind start
if curr != obj { if curr != obj {
if _, in := needsMove[curr]; !in {
if isHorizontal { if isHorizontal {
if curr.TopLeft.X < start { if curr.TopLeft.X < start {
continue continue
@ -925,26 +927,31 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
} }
} }
} }
}
if isHorizontal { if isHorizontal {
shift := false _, shift := needsMove[curr]
if !shift {
if !isMargin { if !isMargin {
shift = start < curr.TopLeft.X shift = start < curr.TopLeft.X
} else { } else {
shift = start <= curr.TopLeft.X shift = start <= curr.TopLeft.X
} }
}
if shift { if shift {
curr.TopLeft.X += distance curr.TopLeft.X += distance
shifted[curr] = struct{}{} shifted[curr] = struct{}{}
} }
} else { } else {
shift := false _, shift := needsMove[curr]
if !shift {
if !isMargin { if !isMargin {
shift = start < curr.TopLeft.Y shift = start < curr.TopLeft.Y
} else { } else {
shift = start <= curr.TopLeft.Y shift = start <= curr.TopLeft.Y
} }
}
if shift { if shift {
curr.TopLeft.Y += distance curr.TopLeft.Y += distance
shifted[curr] = struct{}{} shifted[curr] = struct{}{}
@ -977,6 +984,18 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
shiftedEdges[e] = struct{}{} shiftedEdges[e] = struct{}{}
continue continue
} else if e.Src == curr { } 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) queue(e.Dst)
if isHorizontal { if isHorizontal {
for _, p := range e.Route { for _, p := range e.Route {
@ -993,6 +1012,18 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
} }
shiftedEdges[e] = struct{}{} shiftedEdges[e] = struct{}{}
} else if e.Dst == curr { } 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) queue(e.Src)
if isHorizontal { if isHorizontal {
for _, p := range e.Route { for _, p := range e.Route {