This commit is contained in:
donglixiaoche 2023-04-06 05:00:12 +08:00
parent 7a395ecf71
commit e61629e692
No known key found for this signature in database
GPG key ID: 3190E65EBAD6D6E2
6 changed files with 13 additions and 23 deletions

View file

@ -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 🧹

View file

@ -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]]
}

View file

@ -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",

View file

@ -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

View file

@ -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)

View file

@ -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"
}
]
}