This commit is contained in:
Alexander Wang 2023-03-13 20:07:13 -07:00
parent e656cf75d1
commit 366a36df67
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
5 changed files with 226 additions and 0 deletions

View file

@ -392,6 +392,8 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Style.Stroke = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "fill":
attrs.Style.Fill = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "fill-pattern":
attrs.Style.FillPattern = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "stroke-width":
attrs.Style.StrokeWidth = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "stroke-dash":

View file

@ -242,6 +242,25 @@ containers: {
}
},
},
{
name: "fill-pattern",
text: `x: {
style: {
fill-pattern: dots
}
}
`,
},
{
name: "invalid-fill-pattern",
text: `x: {
style: {
fill-pattern: ddots
}
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected "fill-pattern" to be one of: dots`,
},
{
name: "shape_unquoted_hex",

View file

@ -150,6 +150,7 @@ type Style struct {
Opacity *Scalar `json:"opacity,omitempty"`
Stroke *Scalar `json:"stroke,omitempty"`
Fill *Scalar `json:"fill,omitempty"`
FillPattern *Scalar `json:"fillPattern,omitempty"`
StrokeWidth *Scalar `json:"strokeWidth,omitempty"`
StrokeDash *Scalar `json:"strokeDash,omitempty"`
BorderRadius *Scalar `json:"borderRadius,omitempty"`
@ -194,6 +195,14 @@ func (s *Style) Apply(key, value string) error {
return errors.New(`expected "fill" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
}
s.Fill.Value = value
case "fill-pattern":
if s.FillPattern == nil {
break
}
if !go2.Contains(FillPatterns, strings.ToLower(value)) {
return fmt.Errorf(`expected "fill-pattern" to be one of: %s`, strings.Join(FillPatterns, ","))
}
s.FillPattern.Value = value
case "stroke-width":
if s.StrokeWidth == nil {
break
@ -1499,6 +1508,7 @@ var StyleKeywords = map[string]struct{}{
"opacity": {},
"stroke": {},
"fill": {},
"fill-pattern": {},
"stroke-width": {},
"stroke-dash": {},
"border-radius": {},
@ -1540,6 +1550,11 @@ var NearConstantsArray = []string{
}
var NearConstants map[string]struct{}
var FillPatterns = []string{
// TODO add lined later
"dots",
}
// BoardKeywords contains the keywords that create new boards.
var BoardKeywords = map[string]struct{}{
"layers": {},

178
testdata/d2compiler/TestCompile/fill-pattern.exp.json generated vendored Normal file
View file

@ -0,0 +1,178 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-5:0:44",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-4:1:43",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:3:3-4:0:42",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-3:3:41",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:8:13-3:2:40",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:22:37",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31",
"value": [
{
"string": "fill-pattern",
"raw_string": "fill-pattern"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:18:33-2:22:37",
"value": [
{
"string": "dots",
"raw_string": "dots"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"label_dimensions": {
"width": 0,
"height": 0
},
"attributes": {
"label": {
"value": ""
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": {
"value": ""
}
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "x",
"id_val": "x",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "x"
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": {
"value": ""
}
},
"zIndex": 0
}
]
},
"err": null
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2,2:18:33-2:23:38",
"errmsg": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected \"fill-pattern\" to be one of: dots"
}
]
}
}