add --sketch_bg option

This commit is contained in:
Vojtěch Fošnár 2023-01-07 10:46:37 +01:00
parent 2d775c4d9c
commit 8f903d72e1
No known key found for this signature in database
GPG key ID: 657727E71C40859A
3 changed files with 22 additions and 16 deletions

View file

@ -62,9 +62,10 @@ var sketchStyleCSS string
var mdCSS string
type RenderOpts struct {
Pad int
Sketch bool
ThemeID int64
Pad int
Sketch bool
SketchBg bool
ThemeID int64
}
func setViewbox(writer io.Writer, diagram *d2target.Diagram, pad int, bgColor string) (width int, height int) {
@ -1106,8 +1107,10 @@ var fitToScreenScript string
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
var sketchRunner *d2sketch.Runner
pad := DEFAULT_PADDING
sketchBg := true
if opts != nil {
pad = opts.Pad
sketchBg = opts.SketchBg
if opts.Sketch {
var err error
sketchRunner, err = d2sketch.InitSketchVM()
@ -1128,11 +1131,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
if sketchRunner != nil {
styleCSS2 = "\n" + sketchStyleCSS
}
buf.WriteString(fmt.Sprintf(`<style type="text/css">
<![CDATA[
%s%s
]]>
</style>`, styleCSS, styleCSS2))
buf.WriteString(fmt.Sprintf(`<style type="text/css"><![CDATA[%s%s]]></style>`, styleCSS, styleCSS2))
// this script won't run in --watch mode because script tags are ignored when added via el.innerHTML = element
// https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
@ -1148,8 +1147,8 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
if hasMarkdown {
fmt.Fprintf(buf, `<style type="text/css">%s</style>`, mdCSS)
}
if sketchRunner != nil {
fmt.Fprintf(buf, d2sketch.DefineFillPattern())
if sketchRunner != nil && sketchBg {
fmt.Fprint(buf, d2sketch.DefineFillPattern())
}
// only define shadow filter if a shape uses it

16
main.go
View file

@ -75,6 +75,11 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
return err
}
sketchBgFlag, err := ms.Opts.Bool("D2_SKT_BG", "sketch_bg", "", true, "make the background look like it was sketched too")
if err != nil {
return err
}
ps, err := d2plugin.ListPlugins(ctx)
if err != nil {
return err
@ -209,7 +214,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, inputPath, outputPath, *bundleFlag, pw.Page)
_, written, err := compile(ctx, ms, plugin, *sketchFlag, *sketchBgFlag, *padFlag, *themeFlag, inputPath, outputPath, *bundleFlag, pw.Page)
if err != nil {
if written {
return fmt.Errorf("failed to fully compile (partial render written): %w", err)
@ -220,7 +225,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 int64, inputPath, outputPath string, bundle bool, page playwright.Page) (_ []byte, written bool, _ error) {
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, sketchBg bool, pad, themeID int64, inputPath, outputPath string, bundle bool, page playwright.Page) (_ []byte, written bool, _ error) {
input, err := ms.ReadPath(inputPath)
if err != nil {
return nil, false, err
@ -246,9 +251,10 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
}
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
Pad: int(pad),
Sketch: sketch,
ThemeID: themeID,
Pad: int(pad),
Sketch: sketch,
SketchBg: sketchBg,
ThemeID: themeID,
})
if err != nil {
return nil, false, err

View file

@ -43,6 +43,7 @@ type watcherOpts struct {
themeID int64
pad int64
sketch bool
sketchBg bool
host string
port string
inputPath string
@ -356,7 +357,7 @@ func (w *watcher) compileLoop(ctx context.Context) error {
w.pw = newPW
}
svg, _, err := compile(ctx, w.ms, w.layoutPlugin, w.sketch, w.pad, w.themeID, w.inputPath, w.outputPath, w.bundle, w.pw.Page)
svg, _, err := compile(ctx, w.ms, w.layoutPlugin, w.sketch, w.sketchBg, w.pad, w.themeID, w.inputPath, w.outputPath, w.bundle, w.pw.Page)
errs := ""
if err != nil {
if len(svg) > 0 {