From 583fa46cc5f68634efb2addb2f9e557cdaadfbe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Mon, 17 Apr 2023 14:46:48 -0300 Subject: [PATCH] refactor boardType --- d2cli/main.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/d2cli/main.go b/d2cli/main.go index 29819c579..653557838 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -353,7 +353,10 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende switch filepath.Ext(outputPath) { case ".pdf": pageMap := buildBoardIDToIndex(diagram, nil, nil) - pdf, err := renderPDF(ctx, ms, plugin, renderOpts, outputPath, page, ruler, diagram, nil, nil, "", pageMap) + path := []pdf.BoardTitle{ + {Name: "root", BoardID: "root"}, + } + pdf, err := renderPDF(ctx, ms, plugin, renderOpts, outputPath, page, ruler, diagram, nil, path, pageMap) if err != nil { return pdf, false, err } @@ -684,28 +687,13 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts return svg, nil } -func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts d2svg.RenderOpts, outputPath string, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram, doc *pdf.GoFPDF, boardPath []pdf.BoardTitle, boardType string, pageMap map[string]int) (svg []byte, err error) { +func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts d2svg.RenderOpts, outputPath string, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram, doc *pdf.GoFPDF, boardPath []pdf.BoardTitle, pageMap map[string]int) (svg []byte, err error) { var isRoot bool if doc == nil { doc = pdf.Init() isRoot = true } - var currBoardPath []pdf.BoardTitle - // Root board doesn't have a name, so we use the output filename - if diagram.Name == "" { - currBoardPath = append(boardPath, pdf.BoardTitle{ - Name: "root", - BoardID: "root", - }) - } else { - prev := boardPath[len(boardPath)-1] - currBoardPath = append(boardPath, pdf.BoardTitle{ - Name: diagram.Name, - BoardID: strings.Join([]string{prev.BoardID, boardType, diagram.Name}, "."), - }) - } - if !diagram.IsFolderOnly { rootFill := diagram.Root.Fill // gofpdf will print the png img with a slight filter @@ -749,26 +737,38 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opt if err != nil { return svg, err } - err = doc.AddPDFPage(pngImg, currBoardPath, opts.ThemeID, rootFill, diagram.Shapes, int64(opts.Pad), viewboxX, viewboxY, pageMap) + err = doc.AddPDFPage(pngImg, boardPath, opts.ThemeID, rootFill, diagram.Shapes, int64(opts.Pad), viewboxX, viewboxY, pageMap) if err != nil { return svg, err } } for _, dl := range diagram.Layers { - _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, currBoardPath, LAYERS, pageMap) + path := append(boardPath, pdf.BoardTitle{ + Name: dl.Name, + BoardID: strings.Join([]string{boardPath[len(boardPath)-1].BoardID, LAYERS, dl.Name}, "."), + }) + _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, path, pageMap) if err != nil { return nil, err } } for _, dl := range diagram.Scenarios { - _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, currBoardPath, SCENARIOS, pageMap) + path := append(boardPath, pdf.BoardTitle{ + Name: dl.Name, + BoardID: strings.Join([]string{boardPath[len(boardPath)-1].BoardID, SCENARIOS, dl.Name}, "."), + }) + _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, path, pageMap) if err != nil { return nil, err } } for _, dl := range diagram.Steps { - _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, currBoardPath, STEPS, pageMap) + path := append(boardPath, pdf.BoardTitle{ + Name: dl.Name, + BoardID: strings.Join([]string{boardPath[len(boardPath)-1].BoardID, STEPS, dl.Name}, "."), + }) + _, err := renderPDF(ctx, ms, plugin, opts, "", page, ruler, dl, doc, path, pageMap) if err != nil { return nil, err }