diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 6acfa4bcc..041b8e68b 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,7 +1,6 @@ #### Features ๐Ÿš€ - Container with constant key near attribute now can have descendant objects and connections [#1071](https://github.com/terrastruct/d2/pull/1071) - - Multi-board SVG outputs with internal links go to their output paths [#1116](https://github.com/terrastruct/d2/pull/1116) #### Improvements ๐Ÿงน diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 0af1f26b6..e75d791f7 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -734,22 +734,10 @@ func (c *compiler) validateNear(g *d2graph.Graph) { } } } else if isConst { - is := false - for _, e := range g.Edges { - if e.Src == obj || e.Dst == obj { - is = true - break - } - } - if is { - c.errorf(obj.Attributes.NearKey, "constant near keys cannot be set on connected shapes") - continue - } if obj.Parent != g.Root { c.errorf(obj.Attributes.NearKey, "constant near keys can only be set on root level shapes") continue } - } else { c.errorf(obj.Attributes.NearKey, "near key %#v must be the absolute path to a shape or one of the following constants: %s", d2format.Format(obj.Attributes.NearKey), strings.Join(d2graph.NearConstantsArray, ", ")) continue @@ -763,10 +751,10 @@ func (c *compiler) validateNear(g *d2graph.Graph) { var isSrcNearConst, isDstNearConst bool - if srcNearContainer != nil && srcNearContainer != edge.Src { + if srcNearContainer != nil { _, isSrcNearConst = d2graph.NearConstants[d2graph.Key(srcNearContainer.Attributes.NearKey)[0]] } - if dstNearContainer != nil && dstNearContainer != edge.Dst { + if dstNearContainer != nil { _, isDstNearConst = d2graph.NearConstants[d2graph.Key(dstNearContainer.Attributes.NearKey)[0]] } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 586da1f7b..4a66b8e85 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1567,7 +1567,7 @@ d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set } x -> y `, - expErr: `d2/testdata/d2compiler/TestCompile/near_bad_connected.d2:3:12: constant near keys cannot be set on connected shapes`, + expErr: `d2/testdata/d2compiler/TestCompile/near_bad_connected.d2:5:5: cannot connect objects from within a container, that has near constant set, to objects outside that container`, }, { name: "near_descendant_connect_to_outside", diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 6f3a177be..88b8382d8 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -20,6 +20,13 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph return nil } + for _, tempGraph := range constantNearGraphs { + tempGraph.Root.ChildrenArray[0].Parent = g.Root + for _, obj := range tempGraph.Objects { + obj.Graph = g + } + } + // Imagine the graph has two long texts, one at top center and one at top left. // Top left should go left enough to not collide with center. // So place the center ones first, then the later ones will consider them for bounding box @@ -151,7 +158,7 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGr tempGraph := d2graph.NewGraph() tempGraph.Root.ChildrenArray = []*d2graph.Object{obj} - tempGraph.Root.Children[obj.ID] = obj + tempGraph.Root.Children[strings.ToLower(obj.ID)] = obj for _, descendantObj := range descendantObjects { descendantObj.Graph = tempGraph diff --git a/d2lib/d2.go b/d2lib/d2.go index 160e73158..8f94d86c2 100644 --- a/d2lib/d2.go +++ b/d2lib/d2.go @@ -75,10 +75,6 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta if err = coreLayout(ctx, tempGraph); err != nil { return nil, err } - tempGraph.Root.ChildrenArray[0].Parent = g.Root - for _, obj := range tempGraph.Objects { - obj.Graph = g - } } err = d2sequence.Layout(ctx, g, coreLayout) diff --git a/testdata/d2compiler/TestCompile/near_bad_connected.exp.json b/testdata/d2compiler/TestCompile/near_bad_connected.exp.json index 03c55a204..b5ac2eb4a 100644 --- a/testdata/d2compiler/TestCompile/near_bad_connected.exp.json +++ b/testdata/d2compiler/TestCompile/near_bad_connected.exp.json @@ -4,8 +4,8 @@ "ioerr": null, "errs": [ { - "range": "d2/testdata/d2compiler/TestCompile/near_bad_connected.d2,2:11:21-2:21:31", - "errmsg": "d2/testdata/d2compiler/TestCompile/near_bad_connected.d2:3:12: constant near keys cannot be set on connected shapes" + "range": "d2/testdata/d2compiler/TestCompile/near_bad_connected.d2,4:4:42-4:10:48", + "errmsg": "d2/testdata/d2compiler/TestCompile/near_bad_connected.d2:5:5: cannot connect objects from within a container, that has near constant set, to objects outside that container" } ] }