shift edges with grid

This commit is contained in:
Gavin Nishizawa 2023-09-13 15:02:26 -07:00
parent c0c66ad36e
commit c9a6e65c6b
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
4 changed files with 25 additions and 4 deletions

View file

@ -1169,6 +1169,13 @@ func (e *Edge) Text() *d2target.MText {
}
}
func (e *Edge) Move(dx, dy float64) {
for _, p := range e.Route {
p.X += dx
p.Y += dy
}
}
func (e *Edge) AbsID() string {
srcIDA := e.Src.AbsIDArray()
dstIDA := e.Dst.AbsIDArray()

View file

@ -81,7 +81,12 @@ func pluckObjAndEdges(g *Graph, obj *Object) (descendantsObjects []*Object, edge
func (g *Graph) InjectNestedGraph(tempGraph *Graph, parent *Object) {
obj := tempGraph.Root.ChildrenArray[0]
obj.MoveWithDescendantsTo(0, 0)
dx := 0 - obj.TopLeft.X
dy := 0 - obj.TopLeft.Y
obj.MoveWithDescendants(dx, dy)
for _, e := range tempGraph.Edges {
e.Move(dx, dy)
}
obj.Parent = parent
for _, obj := range tempGraph.Objects {
obj.Graph = g

View file

@ -108,6 +108,9 @@ func (gd *gridDiagram) shift(dx, dy float64) {
for _, obj := range gd.objects {
obj.MoveWithDescendants(dx, dy)
}
for _, e := range gd.edges {
e.Move(dx, dy)
}
}
func (gd *gridDiagram) cleanup(obj *d2graph.Object, graph *d2graph.Graph) {

View file

@ -212,16 +212,22 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph, layout d2graph.L
// simple straight line edge routing between grid objects
for i, e := range g.Edges {
edgeOrder[e.AbsID()] = i
if e.Dst.Parent.ClosestGridDiagram() != obj {
if !e.Src.Parent.IsDescendantOf(obj) && !e.Dst.Parent.IsDescendantOf(obj) {
continue
}
// if edge is within grid, remove it from outer layout
gd.edges = append(gd.edges, e)
edgeToRemove[e] = struct{}{}
if e.Src.Parent != obj || e.Dst.Parent != obj {
continue
}
// if edge is grid child, use simple routing
e.Route = []*geo.Point{e.Src.Center(), e.Dst.Center()}
e.TraceToShape(e.Route, 0, 1)
if e.Label.Value != "" {
e.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter))
}
gd.edges = append(gd.edges, e)
edgeToRemove[e] = struct{}{}
}
return nil