Commit 90e9977c authored by Jakob Moser's avatar Jakob Moser
Browse files

Move and shorten code

It might be less efficient now, but it works and is shorter :)
parent 36af7159
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 −15
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()

@@ -139,18 +141,3 @@ export function main() {
}

window.onload = main

let verifiedTextareaExistance = false
let textareaElement = null
window.onkeydown = function () {
    if (!verifiedTextareaExistance) {
        textareaElement = document.querySelector("textarea.term_textarea")
        if (textareaElement) {
            verifiedTextareaExistance = true
        }
    }
    if (document.activeElement !== textareaElement) {
        console.log("Triggering")
        textareaElement.focus()
    }
}
 No newline at end of file