export class ConsoleInput { constructor(inputElem, console, codeRunner) { this.inputElem = inputElem; this.console = console; this.codeRunner = codeRunner; this.history = []; this.historyIndex = 0; this.inputElem.onkeydown = (ev) => this.onKeyDown(ev); } onKeyDown(ev) { switch (ev.key) { case "Enter": this.submit(); break; case "ArrowUp": this.moveHistory(-1); ev.preventDefault(); break; case "ArrowDown": this.moveHistory(1); ev.preventDefault(); break; case "l": if (ev.ctrlKey) { this.console.clear(); ev.preventDefault(); } break; } } submit() { const code = this.inputElem.value; this.inputElem.value = ""; this.console.addInput(code); this.history.push(code); this.historyIndex = this.history.length; this.codeRunner.evaluateCode(code); } moveHistory(delta) { this.historyIndex += delta; if (this.historyIndex < 0) { this.historyIndex = 0; return; } if (this.historyIndex >= this.history.length) { this.historyIndex = this.history.length; this.inputElem.value = ""; return; } this.inputElem.value = this.history[this.historyIndex]; this.inputElem.setSelectionRange( this.inputElem.value.length, this.inputElem.value.length, ); } }