Skip to content

Commit

Permalink
only at root
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jul 20, 2023
1 parent 342a05c commit 917c8fe
Show file tree
Hide file tree
Showing 8 changed files with 975 additions and 6 deletions.
3 changes: 0 additions & 3 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,6 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la
LayoutResolver: LayoutResolver(ctx, ms, plugins),
Layout: layout,
}
if renderOpts.Sketch != nil && *renderOpts.Sketch {
opts.FontFamily = go2.Pointer(d2fonts.HandDrawn)
}

cancel := background.Repeat(func() {
ms.Log.Info.Printf("compiling & running layout algorithms...")
Expand Down
32 changes: 32 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3832,6 +3832,38 @@ x -> y
`, `d2/testdata/d2compiler/TestCompile2/vars/config/invalid.d2:4:5: expected a boolean for "sketch", got "lol"`)
},
},
{
name: "not-root",
run: func(t *testing.T) {
assertCompile(t, `
x: {
vars: {
d2-config: {
sketch: false
}
}
}
`, `d2/testdata/d2compiler/TestCompile2/vars/config/not-root.d2:4:4: "d2-config" can only appear at root vars`)
},
},
{
name: "not-root-2",
run: func(t *testing.T) {
assertCompile(t, `
x
layers: {
y: {
vars: {
d2-config: {
sketch: false
}
}
c
}
}
`, `d2/testdata/d2compiler/TestCompile2/vars/config/not-root-2.d2:6:6: "d2-config" can only appear at root vars`)
},
},
}

for _, tc := range tca {
Expand Down
5 changes: 5 additions & 0 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (c *compiler) validateConfigs(configs *Field) {
return
}

if !ParentMap(ParentMap(configs)).Root() {
c.errorf(configs.LastRef().AST(), `"%s" can only appear at root vars`, configs.Name)
return
}

for _, f := range configs.Map().Fields {
var val string
if f.Primary() == nil {
Expand Down
4 changes: 1 addition & 3 deletions d2lib/d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ func getLayout(opts *CompileOptions) (d2graph.LayoutGraph, error) {
}
}

// TODO test multi-board, test animated steps
// applyConfigs applies the configs read from D2 and applies it to passed in opts
// It will only write to opt fields that are nil, as passed-in opts have precedence
func applyConfigs(config *d2target.Config, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) {
if config == nil {
return
}

// TODO: Maybe add scale
if compileOpts.Layout == nil {
compileOpts.Layout = config.LayoutEngine
}
Expand Down Expand Up @@ -186,7 +184,7 @@ func applyDefaults(compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) {
if renderOpts.Sketch == nil {
renderOpts.Sketch = go2.Pointer(false)
}
if renderOpts.Sketch != nil && *renderOpts.Sketch {
if *renderOpts.Sketch {
compileOpts.FontFamily = go2.Pointer(d2fonts.HandDrawn)
}
if renderOpts.Pad == nil {
Expand Down
32 changes: 32 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ func TestCLI_E2E(t *testing.T) {
shape: text
}
steps: {
1: {
Approach road
}
2: {
Approach road -> Cross road
}
3: {
Cross road -> Make you wonder why
}
}
`)
err := runTestMain(t, ctx, dir, env, "--animate-interval=1400", "animation.d2")
assert.Success(t, err)
svg := readFile(t, dir, "animation.svg")
assert.Testdata(t, ".svg", svg)
},
},
{
name: "vars-animation",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "animation.d2", `vars: {
d2-config: {
theme-id: 300
}
}
Chicken's plan: {
style.font-size: 35
near: top-center
shape: text
}
steps: {
1: {
Approach road
Expand Down
Loading

0 comments on commit 917c8fe

Please sign in to comment.