use pointer for DarkThemeID
This commit is contained in:
parent
46766d0f04
commit
6e64949b9e
9 changed files with 32 additions and 34 deletions
|
|
@ -544,10 +544,9 @@ func run(t *testing.T, tc testCase) {
|
|||
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
|
||||
|
||||
svgBytes, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
Sketch: true,
|
||||
ThemeID: 0,
|
||||
DarkThemeID: -1,
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
Sketch: true,
|
||||
ThemeID: 0,
|
||||
})
|
||||
assert.Success(t, err)
|
||||
err = os.MkdirAll(dataPath, 0755)
|
||||
|
|
|
|||
|
|
@ -130,9 +130,8 @@ func run(t *testing.T, tc testCase) {
|
|||
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
|
||||
|
||||
svgBytes, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 0,
|
||||
DarkThemeID: -1,
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 0,
|
||||
})
|
||||
assert.Success(t, err)
|
||||
svgBytes = appendix.Append(diagram, ruler, svgBytes)
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ type RenderOpts struct {
|
|||
Pad int
|
||||
Sketch bool
|
||||
ThemeID int64
|
||||
DarkThemeID int64
|
||||
DarkThemeID *int64
|
||||
}
|
||||
|
||||
func dimensions(diagram *d2target.Diagram, pad int) (left, top, width, height int) {
|
||||
|
|
@ -1453,10 +1453,11 @@ const (
|
|||
BG_COLOR = color.N7
|
||||
FG_COLOR = color.N1
|
||||
|
||||
DEFAULT_THEME int64 = 0
|
||||
DEFAULT_DARK_THEME int64 = -1 // no theme selected
|
||||
DEFAULT_THEME int64 = 0
|
||||
)
|
||||
|
||||
var DEFAULT_DARK_THEME *int64 = nil // no theme selected
|
||||
|
||||
// TODO minify output at end
|
||||
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||
var sketchRunner *d2sketch.Runner
|
||||
|
|
@ -1587,14 +1588,14 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
|||
}
|
||||
|
||||
// TODO include only colors that are being used to reduce size
|
||||
func themeCSS(themeID, darkThemeID int64) (stylesheet string, err error) {
|
||||
func themeCSS(themeID int64, darkThemeID *int64) (stylesheet string, err error) {
|
||||
out, err := singleThemeRulesets(themeID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if darkThemeID != -1 {
|
||||
darkOut, err := singleThemeRulesets(darkThemeID)
|
||||
if darkThemeID != nil {
|
||||
darkOut, err := singleThemeRulesets(*darkThemeID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -422,9 +422,8 @@ func run(t *testing.T, tc testCase) {
|
|||
pathGotSVG := filepath.Join(dataPath, "dark_theme.got.svg")
|
||||
|
||||
svgBytes, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 200,
|
||||
DarkThemeID: -1,
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 200,
|
||||
})
|
||||
assert.Success(t, err)
|
||||
err = os.MkdirAll(dataPath, 0755)
|
||||
|
|
|
|||
|
|
@ -24,9 +24,8 @@ func main() {
|
|||
Ruler: ruler,
|
||||
})
|
||||
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: d2themescatalog.GrapeSoda.ID,
|
||||
DarkThemeID: -1,
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: d2themescatalog.GrapeSoda.ID,
|
||||
})
|
||||
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,8 @@ func main() {
|
|||
_ = d2dagrelayout.Layout(context.Background(), graph, nil)
|
||||
diagram, _ := d2exporter.Export(context.Background(), graph, nil)
|
||||
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: d2themescatalog.NeutralDefault.ID,
|
||||
DarkThemeID: -1, // Without dark theme
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: d2themescatalog.NeutralDefault.ID,
|
||||
})
|
||||
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -169,9 +169,8 @@ func run(t *testing.T, tc testCase) {
|
|||
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
|
||||
|
||||
svgBytes, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 0,
|
||||
DarkThemeID: -1,
|
||||
Pad: d2svg.DEFAULT_PADDING,
|
||||
ThemeID: 0,
|
||||
})
|
||||
assert.Success(t, err)
|
||||
err = os.MkdirAll(dataPath, 0755)
|
||||
|
|
|
|||
19
main.go
19
main.go
|
|
@ -161,7 +161,10 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
}
|
||||
ms.Log.Debug.Printf("using theme %s (ID: %d)", match.Name, *themeFlag)
|
||||
|
||||
if *darkThemeFlag != -1 {
|
||||
if *darkThemeFlag == -1 {
|
||||
darkThemeFlag = nil // TODO this is a temporary solution: https://github.com/terrastruct/util-go/issues/7
|
||||
}
|
||||
if darkThemeFlag != nil {
|
||||
match = d2themescatalog.Find(*darkThemeFlag)
|
||||
if match == (d2themes.Theme{}) {
|
||||
return xmain.UsageErrorf("--dark-theme could not be found. The available options are:\n%s\nYou provided: %d", d2themescatalog.CLIString(), *darkThemeFlag)
|
||||
|
|
@ -194,9 +197,9 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
|
||||
var pw png.Playwright
|
||||
if filepath.Ext(outputPath) == ".png" {
|
||||
if *darkThemeFlag != -1 {
|
||||
if darkThemeFlag != nil {
|
||||
ms.Log.Warn.Printf("--dark-theme cannot be used while exporting to another format other than .svg")
|
||||
*darkThemeFlag = -1
|
||||
darkThemeFlag = nil
|
||||
}
|
||||
pw, err = png.InitPlaywright()
|
||||
if err != nil {
|
||||
|
|
@ -218,7 +221,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
layoutPlugin: plugin,
|
||||
sketch: *sketchFlag,
|
||||
themeID: *themeFlag,
|
||||
darkThemeID: *darkThemeFlag,
|
||||
darkThemeID: darkThemeFlag,
|
||||
pad: *padFlag,
|
||||
host: *hostFlag,
|
||||
port: *portFlag,
|
||||
|
|
@ -237,7 +240,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
ctx, cancel := context.WithTimeout(ctx, time.Minute*2)
|
||||
defer cancel()
|
||||
|
||||
_, written, err := compile(ctx, ms, plugin, *sketchFlag, *padFlag, *themeFlag, *darkThemeFlag, inputPath, outputPath, *bundleFlag, *forceAppendixFlag, pw.Page)
|
||||
_, written, err := compile(ctx, ms, plugin, *sketchFlag, *padFlag, *themeFlag, darkThemeFlag, inputPath, outputPath, *bundleFlag, *forceAppendixFlag, pw.Page)
|
||||
if err != nil {
|
||||
if written {
|
||||
return fmt.Errorf("failed to fully compile (partial render written): %w", err)
|
||||
|
|
@ -247,7 +250,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad, themeID, darkThemeID int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page) (_ []byte, written bool, _ error) {
|
||||
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad, themeID int64, darkThemeID *int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page) (_ []byte, written bool, _ error) {
|
||||
start := time.Now()
|
||||
input, err := ms.ReadPath(inputPath)
|
||||
if err != nil {
|
||||
|
|
@ -280,7 +283,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
|
|||
return svg, true, nil
|
||||
}
|
||||
|
||||
func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plugin d2plugin.Plugin, sketch bool, pad int64, themeID, darkThemeID int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
|
||||
func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plugin d2plugin.Plugin, sketch bool, pad int64, themeID int64, darkThemeID *int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
|
||||
outputPath = layerOutputPath(outputPath, diagram)
|
||||
for _, dl := range diagram.Layers {
|
||||
_, err := render(ctx, ms, compileDur, plugin, sketch, pad, themeID, darkThemeID, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
|
||||
|
|
@ -310,7 +313,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
|
|||
return svg, nil
|
||||
}
|
||||
|
||||
func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad int64, themeID, darkThemeID int64, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
|
||||
func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad int64, themeID int64, darkThemeID *int64, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
|
||||
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: int(pad),
|
||||
Sketch: sketch,
|
||||
|
|
|
|||
2
watch.go
2
watch.go
|
|
@ -41,7 +41,7 @@ var staticFS embed.FS
|
|||
type watcherOpts struct {
|
||||
layoutPlugin d2plugin.Plugin
|
||||
themeID int64
|
||||
darkThemeID int64
|
||||
darkThemeID *int64
|
||||
pad int64
|
||||
sketch bool
|
||||
host string
|
||||
|
|
|
|||
Loading…
Reference in a new issue