UNPKG

@dodona/papyros

Version:

Scratchpad for multiple programming languages in the browser.

131 lines 4.13 kB
import { InputMode } from "../InputManager"; import { UserInputHandler } from "./UserInputHandler"; import { t } from "../util/Util"; import { BatchInputEditor } from "../editor/BatchInputEditor"; import { BackendManager } from "../BackendManager"; import { BackendEventType } from "../BackendEvent"; export class BatchInputHandler extends UserInputHandler { /** * Construct a new BatchInputHandler * @param {function()} inputCallback Callback for when the user has entered a value */ constructor(inputCallback) { super(inputCallback); this.debugMode = false; this.debugLine = 0; this.lineNr = 0; this.previousInput = ""; this.running = false; this.prompts = []; this.batchEditor = new BatchInputEditor(); this.batchEditor.onChange({ onChange: this.handleInputChanged.bind(this), delay: 0 }); BackendManager.subscribe(BackendEventType.FrameChange, e => { this.debugLine = e.data.inputs; this.highlight(this.running); }); } /** * Handle new input, potentially sending it to the awaiting receiver * @param {string} newInput The new user input */ handleInputChanged(newInput) { const newLines = newInput ? newInput.split("\n") : []; if (newLines.length < this.lineNr) { this.lineNr = newLines.length; } if (this.waiting && newLines.length > this.lineNr + 1) { // Require explicitly pressing enter this.inputCallback(this.next()); } this.highlight(this.running); this.previousInput = newInput; } toggle(active) { if (active) { this.batchEditor.setText(this.previousInput); } else { this.previousInput = this.batchEditor.getText(); } } getInputMode() { return InputMode.Batch; } /** * Retrieve the lines of input that the user has given so far * @return {Array<string>} The entered lines */ get lines() { return this.batchEditor.getLines(); } hasNext() { return this.lineNr < this.lines.length; } highlight(running) { const whichLines = (index) => { if (this.debugMode) { return index < this.debugLine; } return index < this.lineNr; }; this.batchEditor.highlight({ running, getInfo: (lineNr) => { let message = t("Papyros.used_input"); const index = lineNr - 1; const shouldShow = whichLines(index); if (index < this.prompts.length && this.prompts[index]) { message = t("Papyros.used_input_with_prompt", { prompt: this.prompts[index] }); } return { lineNr, on: shouldShow, title: message }; } }); } next() { const nextLine = this.lines[this.lineNr]; this.lineNr += 1; this.highlight(true); return nextLine; } reset() { super.reset(); this.lineNr = 0; this.debugLine = 0; this.prompts = []; this.highlight(this.running); } onRunStart() { this.running = true; this.reset(); } onRunEnd() { this.running = false; this.highlight(false); } waitWithPrompt(waiting, prompt) { super.waitWithPrompt(waiting, prompt); if (this.waiting) { this.prompts.push(prompt || ""); if (this.hasNext()) { this.inputCallback(this.next()); } } } setPlaceholder(placeholderValue) { this.batchEditor.setPlaceholder(placeholderValue); } focus() { this.batchEditor.focus(); } _render(options) { this.batchEditor.render(options); if (options.inputStyling) { this.batchEditor.setStyling(options.inputStyling); } this.highlight(this.running); } } //# sourceMappingURL=BatchInputHandler.js.map