From 0963cafedaa9bde7fc68a9335b448c8efcd06c79 Mon Sep 17 00:00:00 2001 From: delfino Date: Thu, 13 Feb 2025 21:59:29 +0000 Subject: [PATCH] adding support for force appendix --- ci/release/changelogs/next.md | 2 +- d2js/d2wasm/functions.go | 9 +++++++++ d2js/d2wasm/types.go | 15 ++++++++------- d2js/js/README.md | 1 + d2js/js/examples/customizable.html | 10 ++++++++++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 4a1a65c84..908542d89 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -2,6 +2,6 @@ #### Improvements 🧹 -d2js: Support additional render options (`themeID`, `darkThemeID`, `center`, `pad` and `scale`) [#2343](https://github.com/terrastruct/d2/pull/2343) +d2js: Support additional render options (`themeID`, `darkThemeID`, `center`, `pad`, `scale` and `forceAppendix`) [#2343](https://github.com/terrastruct/d2/pull/2343) #### Bugfixes ⛑️ diff --git a/d2js/d2wasm/functions.go b/d2js/d2wasm/functions.go index 1a9e55727..0823f04fa 100644 --- a/d2js/d2wasm/functions.go +++ b/d2js/d2wasm/functions.go @@ -21,6 +21,7 @@ import ( "oss.terrastruct.com/d2/d2parser" "oss.terrastruct.com/d2/d2renderers/d2fonts" "oss.terrastruct.com/d2/d2renderers/d2svg" + "oss.terrastruct.com/d2/d2renderers/d2svg/appendix" "oss.terrastruct.com/d2/lib/log" "oss.terrastruct.com/d2/lib/memfs" "oss.terrastruct.com/d2/lib/textmeasure" @@ -253,6 +254,11 @@ func Render(args []js.Value) (interface{}, error) { return nil, &WASMError{Message: "missing 'diagram' field in input JSON", Code: 400} } + ruler, err := textmeasure.NewRuler() + if err != nil { + return nil, &WASMError{Message: fmt.Sprintf("text ruler cannot be initialized: %s", err.Error()), Code: 500} + } + renderOpts := &d2svg.RenderOpts{} if input.Opts != nil && input.Opts.Sketch != nil { renderOpts.Sketch = input.Opts.Sketch @@ -276,6 +282,9 @@ func Render(args []js.Value) (interface{}, error) { if err != nil { return nil, &WASMError{Message: fmt.Sprintf("render failed: %s", err.Error()), Code: 500} } + if input.Opts != nil && *input.Opts.ForceAppendix { + out = appendix.Append(input.Diagram, renderOpts, ruler, out) + } return out, nil } diff --git a/d2js/d2wasm/types.go b/d2js/d2wasm/types.go index 34098c60b..615db1104 100644 --- a/d2js/d2wasm/types.go +++ b/d2js/d2wasm/types.go @@ -37,13 +37,14 @@ type CompileRequest struct { } type RenderOptions struct { - Layout *string `json:"layout"` - Pad *int64 `json:"pad"` - Sketch *bool `json:"sketch"` - Center *bool `json:"center"` - ThemeID *int64 `json:"themeID"` - DarkThemeID *int64 `json:"darkThemeID"` - Scale *float64 `json:"scale"` + Layout *string `json:"layout"` + Pad *int64 `json:"pad"` + Sketch *bool `json:"sketch"` + Center *bool `json:"center"` + ThemeID *int64 `json:"themeID"` + DarkThemeID *int64 `json:"darkThemeID"` + Scale *float64 `json:"scale"` + ForceAppendix *bool `json:"forceAppendix"` } type CompileResponse struct { diff --git a/d2js/js/README.md b/d2js/js/README.md index cdd77133f..4e3c80d23 100644 --- a/d2js/js/README.md +++ b/d2js/js/README.md @@ -61,6 +61,7 @@ Options: - `center`: Center the SVG in the containing viewbox [default: false] - `pad`: Pixels padded around the rendered diagram [default: 100] - `scale`: Scale the output. E.g., 0.5 to halve the default size. The default will render SVG's that will fit to screen. Setting to 1 turns off SVG fitting to screen. +- `forceAppendix`: Adds an appendix for tooltips and links [default: false] ### `render(diagram: Diagram, options?: RenderOptions): Promise` Renders a compiled diagram to SVG. diff --git a/d2js/js/examples/customizable.html b/d2js/js/examples/customizable.html index 561ea59e2..fa9fcbf7f 100644 --- a/d2js/js/examples/customizable.html +++ b/d2js/js/examples/customizable.html @@ -35,6 +35,7 @@ .layout-toggle, .sketch-toggle, .center-toggle, + .appendix-toggle, .theme-select, .dark-theme-select, .padding-input, @@ -112,6 +113,12 @@ Centered +
+ +