Add back button

This commit is contained in:
2024-02-06 22:24:56 +01:00
parent e2022b409d
commit ead7f09aad
4 changed files with 41 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ const buttons = /** @type {HTMLDivElement} */ (document.getElementById('input-bu
const controlsButton = /** @type {HTMLButtonElement} */ (document.getElementById('controls-button'));
const clearButton = /** @type {HTMLButtonElement} */ (document.getElementById('clear-button'));
const light = /** @type {HTMLDivElement} */ (document.getElementById('light'));
const container = /** @type {HTMLDivElement} */ (document.getElementById('state-graph'));
/**
* @param {import('./examples.js').State[]} states
@@ -34,7 +35,9 @@ export function openAutomaton(states, editable = false) {
console.error(e);
}
});
const container = /** @type {HTMLDivElement} */ (document.getElementById('state-graph'));
while (container.firstChild) {
container.removeChild(container.firstChild);
}
container.appendChild(viewer);
viewer.graph = graph;
@@ -83,21 +86,30 @@ export function openAutomaton(states, editable = false) {
updateUIState();
}
// Dynamically create buttons for each letter in the alphabet
/**
* @type {string[]}
* Dynamically create buttons for each letter in the alphabet.
*/
const alphabet = Array.from(states.reduce((acc, current) => {
Object.keys(current.transitions).forEach(current => acc.add(current));
return acc;
}, new Set())).sort();
for (const letter of alphabet) {
const button = document.createElement('button');
button.innerText = letter;
button.addEventListener('click', () => step(letter));
buttons.appendChild(button);
function createLetters() {
/**
* @type {string[]}
*/
const alphabet = Array.from(states.reduce((acc, current) => {
Object.keys(current.transitions).forEach(current => acc.add(current));
return acc;
}, new Set())).sort();
while (buttons.firstChild) {
buttons.removeChild(buttons.firstChild);
}
for (const letter of alphabet) {
const button = document.createElement('button');
button.innerText = letter;
button.addEventListener('click', () => step(letter));
buttons.appendChild(button);
}
}
createLetters();
// Reacts to input in the text box
wordInput.addEventListener('input', () => type());