Commit 891543d5 authored by H. Fischer's avatar H. Fischer
Browse files

Merge branch...

Merge branch '50-bug-ensure-all-input-key-events-go-to-terminal-even-after-clicking-on-completing-a-task' into 'master'

Resolve "Bug: Ensure all input key events go to terminal, even after clicking on completing a task"

Closes #50

See merge request !15
parents 7a9de14c 90e9977c
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -157,3 +157,19 @@ export function runCommand(command) {
    // https://thewebdev.info/2021/05/02/how-to-programmatically-trigger-a-change-event-on-an-input-with-javascript/
    textarea.dispatchEvent(new Event("input"))
}

/**
 * Enables auto-focus to the terminal whenever a key is pressed.
 * This allows the user to click on an exercise completion button and then seamlessly continue
 * typing into the terminal, without having to click it first.
 * 
 * Note: This also prevents any shortcuts from working, so use with care
 */
export function enableAutoFocus() {
    window.onkeydown = function () {
        const textareaElement = document.querySelector("textarea.term_textarea")
        if (document.activeElement !== textareaElement) {
            textareaElement.focus()
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import { State } from "./state.mjs"
import * as tests from "./exercises.mjs"
import { createExerciseCard, displayAsSolved, displayAsNonCurrent, displayAsCurrent } from "./exercises.cards.mjs"
import { Exercise } from "./exercises.api.mjs"
import { enableAutoFocus } from "./jslinux.api.mjs"

const testId = new URLSearchParams(location.search).get("id")
const currentTest = tests[testId]
@@ -132,6 +133,7 @@ export function main() {
        document.querySelector("section.options").style.display = "none"
    }

    enableAutoFocus()
    initExerciseList()
    initActionLinks()