Skip to content

Commit 79780ee

Browse files
committed
feat: Replace example links with dropdown populated from index.json
1 parent 2ff61c4 commit 79780ee

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

public/index.html

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,52 @@
1616
<div class="panel panel-editor">
1717
<div id="editor"></div>
1818
<p>
19-
Load an Example:
20-
<a href="?load=pinouts/esp32-c3-super-mini.yaml">ESP32-C3 Super Mini</a>,
21-
<a href="?load=pinouts/raspberry-pi.yaml">Raspberry Pi</a>,
22-
<a href="?load=pinouts/a4988.yaml">A4988 Motor Driver</a>
19+
<label for="example-select">Load an Example:</label>
20+
<select name="examples" id="example-select">
21+
<option value="">--Please choose an example--</option>
22+
</select>
2323
</p>
24+
<script>
25+
document.addEventListener('DOMContentLoaded', () => {
26+
const selectElement = document.getElementById('example-select');
27+
28+
fetch('index.json')
29+
.then(response => {
30+
if (!response.ok) {
31+
throw new Error(`HTTP error! status: ${response.status}`);
32+
}
33+
return response.json();
34+
})
35+
.then(data => {
36+
// Sort examples alphabetically by title
37+
const sortedExamples = Object.entries(data).sort(([, titleA], [, titleB]) =>
38+
titleA.localeCompare(titleB)
39+
);
40+
41+
sortedExamples.forEach(([path, title]) => {
42+
const option = document.createElement('option');
43+
option.value = path;
44+
option.textContent = title;
45+
selectElement.appendChild(option);
46+
});
47+
})
48+
.catch(error => {
49+
console.error('Error loading or parsing index.json:', error);
50+
// Optionally disable the select or show an error message
51+
selectElement.disabled = true;
52+
const errorOption = document.createElement('option');
53+
errorOption.textContent = 'Error loading examples';
54+
selectElement.appendChild(errorOption);
55+
});
56+
57+
selectElement.addEventListener('change', (event) => {
58+
const selectedPath = event.target.value;
59+
if (selectedPath) {
60+
window.location.search = `?load=${encodeURIComponent(selectedPath)}`;
61+
}
62+
});
63+
});
64+
</script>
2465

2566
<p>
2667
<b>Learn more</b> about the tool and available config options in the

0 commit comments

Comments
 (0)