@dodona/papyros
Version:
Scratchpad for multiple programming languages in the browser.
39 lines • 1.47 kB
JavaScript
import { t } from "../util/Util";
import { Renderable } from "../util/Rendering";
/**
* Base class for components that handle input from the user
*/
export class UserInputHandler extends Renderable {
/**
* Construct a new UserInputHandler
* @param {function()} inputCallback Callback for when the user has entered a value
*/
constructor(inputCallback) {
super();
this.waiting = false;
this.inputCallback = inputCallback;
}
/**
* Wait for input of the user for a certain prompt
* @param {boolean} waiting Whether we are waiting for input
* @param {string} prompt Optional message to display if waiting
*/
waitWithPrompt(waiting, prompt = "") {
this.waiting = waiting;
this.setPlaceholder(prompt || t(`Papyros.input_placeholder.${this.getInputMode()}`));
if (this.waiting) {
// Focusing is a rendering operation
// Subclasses can execute code after this operation, skipping the rendering
// Using setTimeout ensures rendering will be done when the main thread has time
// More info here: https://stackoverflow.com/questions/1096436/document-getelementbyidid-focus-is-not-working-for-firefox-or-chrome
setTimeout(() => this.focus(), 0);
}
}
/**
* Helper method to reset internal state
*/
reset() {
this.waiting = false;
}
}
//# sourceMappingURL=UserInputHandler.js.map