From 68b36c4fc17187c11902af48af1e7c71d83508c0 Mon Sep 17 00:00:00 2001 From: delfino Date: Fri, 14 Feb 2025 03:03:17 +0000 Subject: [PATCH] adding support for d2-config in vars --- d2js/d2wasm/functions.go | 12 +- d2js/d2wasm/types.go | 15 +- d2js/js/examples/customizable.html | 482 ++++++++++++++++------------- d2js/js/src/index.js | 13 +- d2js/js/src/platform.js | 2 +- 5 files changed, 296 insertions(+), 228 deletions(-) diff --git a/d2js/d2wasm/functions.go b/d2js/d2wasm/functions.go index 23e2f4c7e..d2aa115e9 100644 --- a/d2js/d2wasm/functions.go +++ b/d2js/d2wasm/functions.go @@ -232,13 +232,23 @@ func Compile(args []js.Value) (interface{}, error) { return nil, &WASMError{Message: err.Error(), Code: 500} } + mergedRenderOpts := RenderOptions{ + ThemeID: renderOpts.ThemeID, + DarkThemeID: renderOpts.DarkThemeID, + Sketch: renderOpts.Sketch, + Pad: renderOpts.Pad, + Center: renderOpts.Center, + ForceAppendix: input.Opts.ForceAppendix, + }; + input.FS["index"] = d2format.Format(g.AST) return CompileResponse{ FS: input.FS, Diagram: *diagram, Graph: *g, - }, nil + RenderOpts: mergedRenderOpts, + }, nil } func Render(args []js.Value) (interface{}, error) { diff --git a/d2js/d2wasm/types.go b/d2js/d2wasm/types.go index 615db1104..2437924d8 100644 --- a/d2js/d2wasm/types.go +++ b/d2js/d2wasm/types.go @@ -33,11 +33,10 @@ type BoardPositionResponse struct { type CompileRequest struct { FS map[string]string `json:"fs"` - Opts *RenderOptions `json:"options"` + Opts *CompileOptions `json:"options"` } type RenderOptions struct { - Layout *string `json:"layout"` Pad *int64 `json:"pad"` Sketch *bool `json:"sketch"` Center *bool `json:"center"` @@ -47,10 +46,16 @@ type RenderOptions struct { ForceAppendix *bool `json:"forceAppendix"` } +type CompileOptions struct { + RenderOptions + Layout *string `json:"layout"` +} + type CompileResponse struct { - FS map[string]string `json:"fs"` - Diagram d2target.Diagram `json:"diagram"` - Graph d2graph.Graph `json:"graph"` + FS map[string]string `json:"fs"` + Diagram d2target.Diagram `json:"diagram"` + Graph d2graph.Graph `json:"graph"` + RenderOpts RenderOptions `json:"renderOpts"` } type CompletionResponse struct { diff --git a/d2js/js/examples/customizable.html b/d2js/js/examples/customizable.html index fa9fcbf7f..e13fa60c1 100644 --- a/d2js/js/examples/customizable.html +++ b/d2js/js/examples/customizable.html @@ -1,241 +1,301 @@ - - - - -
- -
-
- Layout: + + + + + + +
+ +
+
+
+ +
+
-
+
+
+
-
+
+
+ + +
+
+
+
+
-
+
+
+ + +
+
+
+
+
-
-
+
+
+
-
-
+
+
+
-
+
+ +
+
+
+
+ +
+
-
-
- +
+
+
+
+ +
+
+
-
-
- - + +
+
+ + + diff --git a/d2js/js/src/index.js b/d2js/js/src/index.js index 8fd09fc7d..a2c28adbf 100644 --- a/d2js/js/src/index.js +++ b/d2js/js/src/index.js @@ -1,10 +1,5 @@ import { createWorker, loadFile } from "./platform.js"; -const DEFAULT_OPTIONS = { - layout: "dagre", - sketch: false, -}; - export class D2 { constructor() { this.ready = this.init(); @@ -86,17 +81,15 @@ export class D2 { } async compile(input, options = {}) { - const opts = { ...DEFAULT_OPTIONS, ...options }; const request = typeof input === "string" - ? { fs: { index: input }, options: opts } - : { ...input, options: { ...opts, ...input.options } }; + ? { fs: { index: input }, options } + : { ...input, options: { ...options, ...input.options } }; return this.sendMessage("compile", request); } async render(diagram, options = {}) { - const opts = { ...DEFAULT_OPTIONS, ...options }; - return this.sendMessage("render", { diagram, options: opts }); + return this.sendMessage("render", { diagram, options }); } async encode(script) { diff --git a/d2js/js/src/platform.js b/d2js/js/src/platform.js index 1a607e21d..fdcbaa051 100644 --- a/d2js/js/src/platform.js +++ b/d2js/js/src/platform.js @@ -1 +1 @@ -export * from "./platform.node.js"; +export * from "./platform.node.js"; \ No newline at end of file