add top, left keywords

This commit is contained in:
Alexander Wang 2023-02-18 14:54:10 -08:00
parent c3b6eeabd6
commit 641a6741ff
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 38 additions and 2 deletions

View file

@ -285,6 +285,24 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Height = &d2graph.Scalar{}
attrs.Height.Value = scalar.ScalarString()
attrs.Height.MapKey = f.LastPrimaryKey()
case "top":
_, err := strconv.Atoi(scalar.ScalarString())
if err != nil {
c.errorf(scalar, "non-integer top %#v: %s", scalar.ScalarString(), err)
return
}
attrs.Top = &d2graph.Scalar{}
attrs.Top.Value = scalar.ScalarString()
attrs.Top.MapKey = f.LastPrimaryKey()
case "left":
_, err := strconv.Atoi(scalar.ScalarString())
if err != nil {
c.errorf(scalar, "non-integer left %#v: %s", scalar.ScalarString(), err)
return
}
attrs.Left = &d2graph.Scalar{}
attrs.Left.Value = scalar.ScalarString()
attrs.Left.MapKey = f.LastPrimaryKey()
case "link":
attrs.Link = scalar.ScalarString()
case "direction":
@ -364,6 +382,10 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Width = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "height":
attrs.Height = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "top":
attrs.Top = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "left":
attrs.Left = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "double-border":
attrs.Style.DoubleBorder = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
}

View file

@ -86,7 +86,6 @@ x: {
}
},
},
{
name: "dimensions_on_nonimage",
@ -114,6 +113,17 @@ x: {
}
},
},
{
name: "positions",
text: `hey: {
top: 200
left: 230
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "200", g.Objects[0].Attributes.Top.Value)
},
},
{
name: "equal_dimensions_on_circle",

View file

@ -98,10 +98,12 @@ type Attributes struct {
Tooltip string `json:"tooltip,omitempty"`
Link string `json:"link,omitempty"`
// Only applicable for images right now
Width *Scalar `json:"width,omitempty"`
Height *Scalar `json:"height,omitempty"`
Top *Scalar `json:"top,omitempty"`
Left *Scalar `json:"left,omitempty"`
// TODO consider separate Attributes struct for shape-specific and edge-specific
// Shapes only
NearKey *d2ast.KeyPath `json:"near_key"`
@ -1442,6 +1444,8 @@ var SimpleReservedKeywords = map[string]struct{}{
"width": {},
"height": {},
"direction": {},
"top": {},
"left": {},
}
// ReservedKeywordHolders are reserved keywords that are meaningless on its own and exist solely to hold a set of reserved keywords