adding support for d2-config in vars

This commit is contained in:
delfino 2025-02-14 03:03:17 +00:00
parent 06dda87d79
commit 68b36c4fc1
No known key found for this signature in database
GPG key ID: CFE0DD6A770BF48C
5 changed files with 296 additions and 228 deletions

View file

@ -232,13 +232,23 @@ func Compile(args []js.Value) (interface{}, error) {
return nil, &WASMError{Message: err.Error(), Code: 500} 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) input.FS["index"] = d2format.Format(g.AST)
return CompileResponse{ return CompileResponse{
FS: input.FS, FS: input.FS,
Diagram: *diagram, Diagram: *diagram,
Graph: *g, Graph: *g,
}, nil RenderOpts: mergedRenderOpts,
}, nil
} }
func Render(args []js.Value) (interface{}, error) { func Render(args []js.Value) (interface{}, error) {

View file

@ -33,11 +33,10 @@ type BoardPositionResponse struct {
type CompileRequest struct { type CompileRequest struct {
FS map[string]string `json:"fs"` FS map[string]string `json:"fs"`
Opts *RenderOptions `json:"options"` Opts *CompileOptions `json:"options"`
} }
type RenderOptions struct { type RenderOptions struct {
Layout *string `json:"layout"`
Pad *int64 `json:"pad"` Pad *int64 `json:"pad"`
Sketch *bool `json:"sketch"` Sketch *bool `json:"sketch"`
Center *bool `json:"center"` Center *bool `json:"center"`
@ -47,10 +46,16 @@ type RenderOptions struct {
ForceAppendix *bool `json:"forceAppendix"` ForceAppendix *bool `json:"forceAppendix"`
} }
type CompileOptions struct {
RenderOptions
Layout *string `json:"layout"`
}
type CompileResponse struct { type CompileResponse struct {
FS map[string]string `json:"fs"` FS map[string]string `json:"fs"`
Diagram d2target.Diagram `json:"diagram"` Diagram d2target.Diagram `json:"diagram"`
Graph d2graph.Graph `json:"graph"` Graph d2graph.Graph `json:"graph"`
RenderOpts RenderOptions `json:"renderOpts"`
} }
type CompletionResponse struct { type CompletionResponse struct {

View file

@ -1,241 +1,301 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head>
<style> <head>
body { <style>
display: flex; body {
gap: 20px; display: flex;
padding: 20px; gap: 20px;
height: 100vh; padding: 20px;
margin: 0; height: 100vh;
font-family: system-ui, -apple-system, sans-serif; margin: 0;
} font-family: system-ui, -apple-system, sans-serif;
.controls { }
display: flex;
flex-direction: column; .controls {
gap: 12px; display: flex;
width: 400px; flex-direction: column;
} gap: 12px;
textarea { width: 400px;
width: 100%; }
height: 300px;
padding: 8px; textarea {
border: 1px solid #ccc; width: 100%;
border-radius: 4px; height: 300px;
font-family: monospace; padding: 8px;
} border: 1px solid #ccc;
.options-group { border-radius: 4px;
display: flex; font-family: monospace;
flex-direction: column; }
gap: 12px;
padding: 12px; .options-group {
border: 1px solid #eee; display: flex;
border-radius: 4px; flex-direction: column;
} gap: 12px;
.layout-toggle, padding: 12px;
.sketch-toggle, border: 1px solid #eee;
.center-toggle, border-radius: 4px;
.appendix-toggle, }
.theme-select,
.dark-theme-select, .option:not(:has(.option-toggle-box:checked)) .option-select {
.padding-input, opacity: 0.5;
.scale-input { pointer-events: none;
display: flex; }
gap: 16px;
align-items: center; .option {
} display: flex;
.radio-group { gap: 16px;
display: flex; align-items: center;
gap: 12px; }
}
.input-label, .input-label,
.select-label, .checkbox-label,
.radio-label, .select-label {
.checkbox-label { display: flex;
display: flex; gap: 4px;
gap: 4px; align-items: center;
align-items: center; cursor: pointer;
cursor: pointer; }
}
.number-input { .number-input {
width: 3rem; width: 3rem;
} }
button {
padding: 8px 16px; button {
background: #0066cc; padding: 8px 16px;
color: white; background: #0066cc;
border: none; color: white;
border-radius: 4px; border: none;
cursor: pointer; border-radius: 4px;
} cursor: pointer;
button:hover { }
background: #0052a3;
} button:hover {
#output { background: #0052a3;
flex: 1; }
overflow: auto;
border: 1px solid #eee; #output {
border-radius: 4px; flex: 1;
padding: 16px; overflow: auto;
} border: 1px solid #eee;
#output svg { border-radius: 4px;
max-width: 100%; padding: 16px;
max-height: 90vh; }
}
</style> #output svg {
</head> max-width: 100%;
<body> max-height: 90vh;
<div class="controls"> }
<textarea id="input">x -> y</textarea> </style>
<div class="options-group"> </head>
<div class="layout-toggle">
<span>Layout:</span> <body>
<div class="controls">
<textarea id="input">x -> y</textarea>
<div class="options-group">
<div class="option">
<div class="option-toggle">
<label class="checkbox-label">
<input type="checkbox" id="layout-toggle" class="option-toggle-box" />
<span>Layout</span>
</label>
</div>
<div class="option-select">
<div class="radio-group"> <div class="radio-group">
<label class="radio-label"> <label class="radio-label">
<input type="radio" name="layout" value="dagre" checked /> <input type="radio" name="layout-select" value="dagre" checked />
Dagre Dagre
</label> </label>
<label class="radio-label"> <label class="radio-label">
<input type="radio" name="layout" value="elk" /> <input type="radio" name="layout-select" value="elk" />
ELK ELK
</label> </label>
</div> </div>
</div> </div>
<div class="sketch-toggle"> </div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label"> <label class="checkbox-label">
<input type="checkbox" id="sketch" /> <input type="checkbox" id="sketch-toggle" class="option-toggle-box" />
Sketch mode <span>Sketch Mode</span>
</label> </label>
</div> </div>
<div class="center-toggle"> <div class="option-select">
<div class="radio-group">
<label class="radio-label">
<input type="radio" name="sketch-select" value="true" checked />
Enabled
</label>
<label class="radio-label">
<input type="radio" name="sketch-select" value="false" />
Disabled
</label>
</div>
</div>
</div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label"> <label class="checkbox-label">
<input type="checkbox" id="center" /> <input type="checkbox" id="center-toggle" class="option-toggle-box" />
Centered <span>Centered</span>
</label> </label>
</div> </div>
<div class="appendix-toggle"> <div class="option-select">
<div class="radio-group">
<label class="radio-label">
<input type="radio" name="center-select" value="true" checked />
Enabled
</label>
<label class="radio-label">
<input type="radio" name="center-select" value="false" />
Disabled
</label>
</div>
</div>
</div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label"> <label class="checkbox-label">
<input type="checkbox" id="appendix" /> <input type="checkbox" id="appendix-toggle" class="option-toggle-box" />
Force Appendix <span>Force Appendix</span>
</label> </label>
</div> </div>
<div class="theme-select"> <div class="option-select">
<label class="select-label"> <div class="radio-group">
<select id="theme" name="theme"> <label class="radio-label">
<option value="-1"></option> <input type="radio" name="appendix-select" value="true" checked />
<option selected value="0">Default</option> Enabled
<option value="1">Neutral grey</option> </label>
<option value="3">Flagship Terrastruct</option> <label class="radio-label">
<option value="4">Cool classics</option> <input type="radio" name="appendix-select" value="false" />
<option value="5">Mixed berry blue</option> Disabled
<option value="6">Grape soda</option> </label>
<option value="7">Aubergine</option> </div>
<option value="8">Colorblind clear</option> </div>
<option value="100">Vanilla nitro cola</option> </div>
<option value="101">Orange creamsicle</option> <div class="option">
<option value="102">Shirley temple</option> <div class="option-toggle">
<option value="103">Earth tones</option> <label class="checkbox-label">
<option value="104">Everglade green</option> <input type="checkbox" id="theme-toggle" class="option-toggle-box" />
<option value="105">Buttered toast</option> <span>Theme</span>
<option value="200">Dark mauve</option>
<option value="300">Terminal</option>
<option value="301">Terminal grayscale</option>
</select>
Theme ID
</label> </label>
</div> </div>
<div class="dark-theme-select"> <div class="option-select">
<label class="select-label"> <select id="theme-select">
<select id="dark-theme" name="dark-theme"> <option selected value="0">Default</option>
<option value="-1"></option> <option value="1">Neutral grey</option>
<option value="0">Default</option> <option value="3">Flagship Terrastruct</option>
<option value="1">Neutral grey</option> <option value="4">Cool classics</option>
<option value="3">Flagship Terrastruct</option> <option value="5">Mixed berry blue</option>
<option value="4">Cool classics</option> <option value="6">Grape soda</option>
<option value="5">Mixed berry blue</option> <option value="7">Aubergine</option>
<option value="6">Grape soda</option> <option value="8">Colorblind clear</option>
<option value="7">Aubergine</option> <option value="100">Vanilla nitro cola</option>
<option value="8">Colorblind clear</option> <option value="101">Orange creamsicle</option>
<option value="100">Vanilla nitro cola</option> <option value="102">Shirley temple</option>
<option value="101">Orange creamsicle</option> <option value="103">Earth tones</option>
<option value="102">Shirley temple</option> <option value="104">Everglade green</option>
<option value="103">Earth tones</option> <option value="105">Buttered toast</option>
<option value="104">Everglade green</option> <option value="200">Dark mauve</option>
<option value="105">Buttered toast</option> <option value="300">Terminal</option>
<option value="200">Dark mauve</option> <option value="301">Terminal grayscale</option>
<option value="300">Terminal</option> </select>
<option value="301">Terminal grayscale</option> </div>
</select> </div>
Dark Theme ID <div class="option">
<div class="option-toggle">
<label class="checkbox-label">
<input type="checkbox" id="dark-theme-toggle" class="option-toggle-box" />
<span>Dark Theme</span>
</label> </label>
</div> </div>
<div class="padding-input"> <div class="option-select">
<select id="dark-theme-select">
<option selected value="0">Default</option>
<option value="1">Neutral grey</option>
<option value="3">Flagship Terrastruct</option>
<option value="4">Cool classics</option>
<option value="5">Mixed berry blue</option>
<option value="6">Grape soda</option>
<option value="7">Aubergine</option>
<option value="8">Colorblind clear</option>
<option value="100">Vanilla nitro cola</option>
<option value="101">Orange creamsicle</option>
<option value="102">Shirley temple</option>
<option value="103">Earth tones</option>
<option value="104">Everglade green</option>
<option value="105">Buttered toast</option>
<option value="200">Dark mauve</option>
<option value="300">Terminal</option>
<option value="301">Terminal grayscale</option>
</select>
</div>
</div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label">
<input type="checkbox" id="pad-toggle" class="option-toggle-box" />
<span>Padding</span>
</label>
</div>
<div class="option-select">
<label class="input-label"> <label class="input-label">
<input type="number" id="padding" value="20" step="10" class="number-input" /> <input type="number" id="pad-input" value="20" step="10" class="number-input" />
Padding </label>
</label> </div>
</div> </div>
<div class="scale-input"> <div class="option">
<label class="input-label"> <div class="option-toggle">
<input type="number" id="scale" value="-1" step="1" class="number-input" /> <label class="checkbox-label">
Scale <input type="checkbox" id="scale-toggle" class="option-toggle-box" />
<span>Scale</span>
</label>
</div>
<div class="option-select">
<label class="input-label">
<input type="number" id="scale-input" value="1" step="0.1" min="0" class="number-input" />
</label> </label>
</div> </div>
</div> </div>
<button onclick="compile()">Compile</button>
</div> </div>
<div id="output"></div> <button onclick="compile()">Compile</button>
<script type="module"> </div>
import { D2 } from "../dist/browser/index.js"; <div id="output"></div>
const d2 = new D2(); <script type="module">
window.compile = async () => { import {D2} from "../dist/browser/index.js";
const notNegative = (value) => { const d2 = new D2();
if (value < 0) { window.compile = async () => {
return null; const input = document.getElementById("input").value;
} else return value; const layout = document.getElementById("layout-toggle").checked ? document.querySelector('input[name="layout-select"]:checked').value : null
}; const sketch = document.getElementById("sketch-toggle").checked ? document.querySelector('input[name="sketch-select"]:checked').value == "true" : null
const input = document.getElementById("input").value; const center = document.getElementById("center-toggle").checked ? document.querySelector('input[name="center-select"]:checked').value == "true" : null
const layout = document.querySelector('input[name="layout"]:checked').value; const forceAppendix = document.getElementById("appendix-toggle").checked ? document.querySelector('input[name="appendix-select"]:checked').value == "true" : null
const sketch = document.getElementById("sketch").checked; const themeSelector = document.getElementById("theme-select")
const center = document.getElementById("center").checked; const themeId = document.getElementById("theme-toggle").checked ? Number(themeSelector.options[themeSelector.selectedIndex].value) : null
const themeSelector = document.getElementById("theme"); const darkThemeSelector = document.getElementById("dark-theme-select")
const themeId = notNegative( const darkThemeId = document.getElementById("dark-theme-toggle").checked ? Number(darkThemeSelector.options[darkThemeSelector.selectedIndex].value) : null
Number(themeSelector.options[themeSelector.selectedIndex].value) const pad = document.getElementById("pad-toggle").checked ? Number(document.getElementById("pad-input").value) : null
); const scale = document.getElementById("scale-toggle").checked ? Number(document.getElementById("scale-input").value) : null
const darkThemeSelector = document.getElementById("dark-theme"); try {
const darkThemeId = notNegative( const result = await d2.compile(input, {
Number(darkThemeSelector.options[darkThemeSelector.selectedIndex].value) layout,
); sketch,
const scale = notNegative(Number(document.getElementById("scale").value)); themeId,
const pad = Number(document.getElementById("padding").value); darkThemeId,
const forceAppendix = document.getElementById("appendix").checked; scale,
try { pad,
const result = await d2.compile(input, { center,
layout, forceAppendix,
sketch, });
themeId, const svg = await d2.render(result.diagram, result.renderOpts);
darkThemeId, document.getElementById("output").innerHTML = svg;
scale, } catch (err) {
pad, console.error(err);
center, document.getElementById("output").textContent = err.message;
forceAppendix, }
}); };
const svg = await d2.render(result.diagram, { compile();
sketch, </script>
themeId, </body>
darkThemeId,
scale,
pad,
center,
forceAppendix,
});
document.getElementById("output").innerHTML = svg;
} catch (err) {
console.error(err);
document.getElementById("output").textContent = err.message;
}
};
compile();
</script>
</body>
</html> </html>

View file

@ -1,10 +1,5 @@
import { createWorker, loadFile } from "./platform.js"; import { createWorker, loadFile } from "./platform.js";
const DEFAULT_OPTIONS = {
layout: "dagre",
sketch: false,
};
export class D2 { export class D2 {
constructor() { constructor() {
this.ready = this.init(); this.ready = this.init();
@ -86,17 +81,15 @@ export class D2 {
} }
async compile(input, options = {}) { async compile(input, options = {}) {
const opts = { ...DEFAULT_OPTIONS, ...options };
const request = const request =
typeof input === "string" typeof input === "string"
? { fs: { index: input }, options: opts } ? { fs: { index: input }, options }
: { ...input, options: { ...opts, ...input.options } }; : { ...input, options: { ...options, ...input.options } };
return this.sendMessage("compile", request); return this.sendMessage("compile", request);
} }
async render(diagram, options = {}) { async render(diagram, options = {}) {
const opts = { ...DEFAULT_OPTIONS, ...options }; return this.sendMessage("render", { diagram, options });
return this.sendMessage("render", { diagram, options: opts });
} }
async encode(script) { async encode(script) {