add --sketch_bg option
This commit is contained in:
parent
2d775c4d9c
commit
8f903d72e1
3 changed files with 22 additions and 16 deletions
|
|
@ -62,9 +62,10 @@ var sketchStyleCSS string
|
||||||
var mdCSS string
|
var mdCSS string
|
||||||
|
|
||||||
type RenderOpts struct {
|
type RenderOpts struct {
|
||||||
Pad int
|
Pad int
|
||||||
Sketch bool
|
Sketch bool
|
||||||
ThemeID int64
|
SketchBg bool
|
||||||
|
ThemeID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func setViewbox(writer io.Writer, diagram *d2target.Diagram, pad int, bgColor string) (width int, height int) {
|
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) {
|
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||||
var sketchRunner *d2sketch.Runner
|
var sketchRunner *d2sketch.Runner
|
||||||
pad := DEFAULT_PADDING
|
pad := DEFAULT_PADDING
|
||||||
|
sketchBg := true
|
||||||
if opts != nil {
|
if opts != nil {
|
||||||
pad = opts.Pad
|
pad = opts.Pad
|
||||||
|
sketchBg = opts.SketchBg
|
||||||
if opts.Sketch {
|
if opts.Sketch {
|
||||||
var err error
|
var err error
|
||||||
sketchRunner, err = d2sketch.InitSketchVM()
|
sketchRunner, err = d2sketch.InitSketchVM()
|
||||||
|
|
@ -1128,11 +1131,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||||
if sketchRunner != nil {
|
if sketchRunner != nil {
|
||||||
styleCSS2 = "\n" + sketchStyleCSS
|
styleCSS2 = "\n" + sketchStyleCSS
|
||||||
}
|
}
|
||||||
buf.WriteString(fmt.Sprintf(`<style type="text/css">
|
buf.WriteString(fmt.Sprintf(`<style type="text/css"><![CDATA[%s%s]]></style>`, styleCSS, styleCSS2))
|
||||||
<![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
|
// 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
|
// 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 {
|
if hasMarkdown {
|
||||||
fmt.Fprintf(buf, `<style type="text/css">%s</style>`, mdCSS)
|
fmt.Fprintf(buf, `<style type="text/css">%s</style>`, mdCSS)
|
||||||
}
|
}
|
||||||
if sketchRunner != nil {
|
if sketchRunner != nil && sketchBg {
|
||||||
fmt.Fprintf(buf, d2sketch.DefineFillPattern())
|
fmt.Fprint(buf, d2sketch.DefineFillPattern())
|
||||||
}
|
}
|
||||||
|
|
||||||
// only define shadow filter if a shape uses it
|
// only define shadow filter if a shape uses it
|
||||||
|
|
|
||||||
16
main.go
16
main.go
|
|
@ -75,6 +75,11 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
return err
|
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)
|
ps, err := d2plugin.ListPlugins(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -209,7 +214,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Minute*2)
|
ctx, cancel := context.WithTimeout(ctx, time.Minute*2)
|
||||||
defer cancel()
|
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 err != nil {
|
||||||
if written {
|
if written {
|
||||||
return fmt.Errorf("failed to fully compile (partial render written): %w", err)
|
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
|
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)
|
input, err := ms.ReadPath(inputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
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{
|
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||||
Pad: int(pad),
|
Pad: int(pad),
|
||||||
Sketch: sketch,
|
Sketch: sketch,
|
||||||
ThemeID: themeID,
|
SketchBg: sketchBg,
|
||||||
|
ThemeID: themeID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
|
|
||||||
3
watch.go
3
watch.go
|
|
@ -43,6 +43,7 @@ type watcherOpts struct {
|
||||||
themeID int64
|
themeID int64
|
||||||
pad int64
|
pad int64
|
||||||
sketch bool
|
sketch bool
|
||||||
|
sketchBg bool
|
||||||
host string
|
host string
|
||||||
port string
|
port string
|
||||||
inputPath string
|
inputPath string
|
||||||
|
|
@ -356,7 +357,7 @@ func (w *watcher) compileLoop(ctx context.Context) error {
|
||||||
w.pw = newPW
|
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 := ""
|
errs := ""
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(svg) > 0 {
|
if len(svg) > 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue