d2/d2js/js/examples/customizable.html

302 lines
9.9 KiB
HTML
Raw Normal View History

2025-01-15 18:37:04 +00:00
<!DOCTYPE html>
<html>
2025-02-14 03:03:17 +00:00
<head>
<style>
body {
display: flex;
gap: 20px;
padding: 20px;
height: 100vh;
margin: 0;
font-family: system-ui, -apple-system, sans-serif;
}
.controls {
display: flex;
flex-direction: column;
gap: 12px;
width: 400px;
}
textarea {
width: 100%;
height: 300px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: monospace;
}
.options-group {
display: flex;
flex-direction: column;
gap: 12px;
padding: 12px;
border: 1px solid #eee;
border-radius: 4px;
}
.option:not(:has(.option-toggle-box:checked)) .option-select {
opacity: 0.5;
pointer-events: none;
}
.option {
display: flex;
gap: 16px;
align-items: center;
}
.input-label,
.checkbox-label,
.select-label {
display: flex;
gap: 4px;
align-items: center;
cursor: pointer;
}
.number-input {
width: 3rem;
}
button {
padding: 8px 16px;
background: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #0052a3;
}
#output {
flex: 1;
overflow: auto;
border: 1px solid #eee;
border-radius: 4px;
padding: 16px;
}
#output svg {
max-width: 100%;
max-height: 90vh;
}
</style>
</head>
<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">
2025-01-15 23:30:17 +00:00
<div class="radio-group">
<label class="radio-label">
2025-02-14 03:03:17 +00:00
<input type="radio" name="layout-select" value="dagre" checked />
2025-01-15 23:30:17 +00:00
Dagre
</label>
<label class="radio-label">
2025-02-14 03:03:17 +00:00
<input type="radio" name="layout-select" value="elk" />
2025-01-15 23:30:17 +00:00
ELK
</label>
</div>
</div>
2025-02-14 03:03:17 +00:00
</div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label">
<input type="checkbox" id="sketch-toggle" class="option-toggle-box" />
<span>Sketch Mode</span>
</label>
</div>
<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">
2025-01-15 23:30:17 +00:00
<label class="checkbox-label">
2025-02-14 03:03:17 +00:00
<input type="checkbox" id="center-toggle" class="option-toggle-box" />
<span>Centered</span>
2025-01-15 18:37:04 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<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">
2025-02-11 04:10:27 +00:00
<label class="checkbox-label">
2025-02-14 03:03:17 +00:00
<input type="checkbox" id="appendix-toggle" class="option-toggle-box" />
<span>Force Appendix</span>
2025-02-11 04:10:27 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<div class="option-select">
<div class="radio-group">
<label class="radio-label">
<input type="radio" name="appendix-select" value="true" checked />
Enabled
</label>
<label class="radio-label">
<input type="radio" name="appendix-select" value="false" />
Disabled
</label>
</div>
</div>
</div>
<div class="option">
<div class="option-toggle">
2025-02-13 21:59:29 +00:00
<label class="checkbox-label">
2025-02-14 03:03:17 +00:00
<input type="checkbox" id="theme-toggle" class="option-toggle-box" />
<span>Theme</span>
2025-02-13 21:59:29 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<div class="option-select">
<select id="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="dark-theme-toggle" class="option-toggle-box" />
<span>Dark Theme</span>
2025-02-11 04:10:27 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<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>
2025-02-11 04:10:27 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<div class="option-select">
2025-02-11 04:10:27 +00:00
<label class="input-label">
2025-02-14 03:03:17 +00:00
<input type="number" id="pad-input" value="20" step="10" class="number-input" />
</label>
</div>
</div>
<div class="option">
<div class="option-toggle">
<label class="checkbox-label">
<input type="checkbox" id="scale-toggle" class="option-toggle-box" />
<span>Scale</span>
2025-02-11 04:10:27 +00:00
</label>
</div>
2025-02-14 03:03:17 +00:00
<div class="option-select">
2025-02-11 04:10:27 +00:00
<label class="input-label">
2025-02-14 03:03:17 +00:00
<input type="number" id="scale-input" value="1" step="0.1" min="0" class="number-input" />
2025-02-11 04:10:27 +00:00
</label>
</div>
2025-01-15 18:37:04 +00:00
</div>
</div>
2025-02-14 03:03:17 +00:00
<button onclick="compile()">Compile</button>
</div>
<div id="output"></div>
<script type="module">
import {D2} from "../dist/browser/index.js";
const d2 = new D2();
window.compile = async () => {
const input = document.getElementById("input").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 center = document.getElementById("center-toggle").checked ? document.querySelector('input[name="center-select"]:checked').value == "true" : null
const forceAppendix = document.getElementById("appendix-toggle").checked ? document.querySelector('input[name="appendix-select"]:checked').value == "true" : null
const themeSelector = document.getElementById("theme-select")
const themeId = document.getElementById("theme-toggle").checked ? Number(themeSelector.options[themeSelector.selectedIndex].value) : null
const darkThemeSelector = document.getElementById("dark-theme-select")
const darkThemeId = document.getElementById("dark-theme-toggle").checked ? Number(darkThemeSelector.options[darkThemeSelector.selectedIndex].value) : null
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
try {
const result = await d2.compile(input, {
layout,
sketch,
themeId,
darkThemeId,
scale,
pad,
center,
forceAppendix,
});
const svg = await d2.render(result.diagram, result.renderOpts);
document.getElementById("output").innerHTML = svg;
} catch (err) {
console.error(err);
document.getElementById("output").textContent = err.message;
}
};
compile();
</script>
</body>
2025-01-15 18:37:04 +00:00
</html>