Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,9 @@ func (c *compiler) validatePositionsCompatibility(g *d2graph.Graph) {
if o.Parent.GridColumns != nil || o.Parent.GridRows != nil {
c.errorf(pos.MapKey, `position keywords cannot be used with grids`)
}
if o.Parent.IsCycleDiagram() {
c.errorf(pos.MapKey, `position keywords cannot be used inside shape "cycle"`)
}
}
}
}
Expand Down Expand Up @@ -1282,6 +1285,22 @@ func (c *compiler) validateEdges(g *d2graph.Graph) {
c.errorf(edge.GetAstEdge(), "edge from grid cell %#v cannot enter itself", edge.Dst.AbsID())
continue
}
if edge.Src.IsCycleDiagram() && edge.Dst.IsDescendantOf(edge.Src) {
c.errorf(edge.GetAstEdge(), "edge from cycle diagram %#v cannot enter itself", edge.Src.AbsID())
continue
}
if edge.Dst.IsCycleDiagram() && edge.Src.IsDescendantOf(edge.Dst) {
c.errorf(edge.GetAstEdge(), "edge from cycle diagram %#v cannot enter itself", edge.Dst.AbsID())
continue
}
if edge.Src.Parent.IsCycleDiagram() && edge.Dst.IsDescendantOf(edge.Src) {
c.errorf(edge.GetAstEdge(), "edge from cycle node %#v cannot enter itself", edge.Src.AbsID())
continue
}
if edge.Dst.Parent.IsCycleDiagram() && edge.Src.IsDescendantOf(edge.Dst) {
c.errorf(edge.GetAstEdge(), "edge from cycle node %#v cannot enter itself", edge.Dst.AbsID())
continue
}
if edge.Src.IsSequenceDiagram() && edge.Dst.IsDescendantOf(edge.Src) {
c.errorf(edge.GetAstEdge(), "edge from sequence diagram %#v cannot enter itself", edge.Src.AbsID())
continue
Expand Down
22 changes: 22 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,28 @@ svc_1.t2 -> b: do with B
}
}

func TestCompileCycleShape(t *testing.T) {
g, _, err := d2compiler.Compile("d2/testdata/d2compiler/TestCompileCycleShape.d2", strings.NewReader(`
x: {
shape: cycle
a
b
}
`), nil)
if err != nil {
t.Fatal(err)
}
if len(g.Objects) != 3 {
t.Fatalf("expected 3 objects: %#v", g.Objects)
}
if g.Objects[0].Shape.Value != d2target.ShapeCycle {
t.Fatalf("expected g.Objects[0].Shape.Value to be cycle: %#v", g.Objects[0].Shape.Value)
}
if !g.Objects[0].IsCycleDiagram() {
t.Fatalf("expected x to be a cycle diagram")
}
}

func TestCompile2(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions d2graph/cycle_diagram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package d2graph

import "oss.terrastruct.com/d2/d2target"

func (obj *Object) IsCycleDiagram() bool {
return obj != nil && obj.Shape.Value == d2target.ShapeCycle
}
4 changes: 2 additions & 2 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func (obj *Object) GetFill() string {
return color.N7
}

if shape == "" || strings.EqualFold(shape, d2target.ShapeSquare) || strings.EqualFold(shape, d2target.ShapeCircle) || strings.EqualFold(shape, d2target.ShapeOval) || strings.EqualFold(shape, d2target.ShapeRectangle) || strings.EqualFold(shape, d2target.ShapeHierarchy) {
if shape == "" || strings.EqualFold(shape, d2target.ShapeSquare) || strings.EqualFold(shape, d2target.ShapeCircle) || strings.EqualFold(shape, d2target.ShapeOval) || strings.EqualFold(shape, d2target.ShapeRectangle) || strings.EqualFold(shape, d2target.ShapeCycle) || strings.EqualFold(shape, d2target.ShapeHierarchy) {
if level == 1 {
if !obj.IsContainer() {
return color.B6
Expand Down Expand Up @@ -675,7 +675,7 @@ func (obj *Object) Text() *d2target.MText {

if obj.OuterSequenceDiagram() == nil {
// Note: during grid layout when children are temporarily removed `IsContainer` is false
if (obj.IsContainer() || obj.IsGridDiagram()) && obj.Shape.Value != "text" {
if (obj.IsContainer() || obj.IsGridDiagram() || obj.IsCycleDiagram()) && obj.Shape.Value != "text" {
fontSize = obj.Level().LabelSize()
}
} else {
Expand Down
35 changes: 35 additions & 0 deletions d2layouts/d2cycle/cycle_diagram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package d2cycle

import (
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/geo"
)

type cycleDiagram struct {
objects []*d2graph.Object
edges []*d2graph.Edge

width float64
height float64
}

func newCycleDiagram(root *d2graph.Object) *cycleDiagram {
cd := &cycleDiagram{
objects: root.ChildrenArray,
}

for _, o := range cd.objects {
o.TopLeft = geo.NewPoint(0, 0)
}

return cd
}

func (cd *cycleDiagram) shift(dx, dy float64) {
for _, obj := range cd.objects {
obj.MoveWithDescendants(dx, dy)
}
for _, e := range cd.edges {
e.Move(dx, dy)
}
}
Loading