-
Notifications
You must be signed in to change notification settings - Fork 8
/
InputManager.ts
140 lines (120 loc) · 4.69 KB
/
InputManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
SWITCH_INPUT_MODE_A_ID,
USER_INPUT_WRAPPER_ID
} from "./Constants";
import { BackendEvent, BackendEventType } from "./BackendEvent";
import {
addListener, getElement, t
} from "./util/Util";
import { InteractiveInputHandler } from "./input/InteractiveInputHandler";
import { UserInputHandler } from "./input/UserInputHandler";
import { BatchInputHandler } from "./input/BatchInputHandler";
import { BackendManager } from "./BackendManager";
import { Renderable, RenderOptions, renderWithOptions } from "./util/Rendering";
import { EditorStyling } from "./editor/CodeMirrorEditor";
export enum InputMode {
Interactive = "interactive",
Batch = "batch"
}
export const INPUT_MODES = [InputMode.Batch, InputMode.Interactive];
export interface InputManagerRenderOptions extends RenderOptions {
/**
* Option to allow styling the editor area of the input handler
*/
inputStyling?: Partial<EditorStyling>;
}
export class InputManager extends Renderable<InputManagerRenderOptions> {
private inputMode: InputMode;
private inputHandlers: Map<InputMode, UserInputHandler>;
private waiting: boolean;
private prompt: string;
private sendInput: (input: string) => void;
constructor(sendInput: (input: string) => void, inputMode: InputMode) {
super();
this.onUserInput = this.onUserInput.bind(this);
this.inputHandlers = this.buildInputHandlerMap();
this.inputMode = inputMode;
this.sendInput = sendInput;
this.waiting = false;
this.prompt = "";
BackendManager.subscribe(BackendEventType.Start, () => this.onRunStart());
BackendManager.subscribe(BackendEventType.End, () => this.onRunEnd());
BackendManager.subscribe(BackendEventType.Input, e => this.onInputRequest(e));
}
private buildInputHandlerMap(): Map<InputMode, UserInputHandler> {
const interactiveInputHandler: UserInputHandler =
new InteractiveInputHandler(this.onUserInput);
const batchInputHandler: UserInputHandler =
new BatchInputHandler(this.onUserInput);
return new Map([
[InputMode.Interactive, interactiveInputHandler],
[InputMode.Batch, batchInputHandler]
]);
}
public getInputMode(): InputMode {
return this.inputMode;
}
public setInputMode(inputMode: InputMode): void {
this.inputHandler.toggle(false);
this.inputMode = inputMode;
this.render();
this.inputHandler.toggle(true);
}
public getInputHandler(inputMode: InputMode): UserInputHandler {
return this.inputHandlers.get(inputMode)!;
}
public get inputHandler(): UserInputHandler {
return this.getInputHandler(this.inputMode)!;
}
public isWaiting(): boolean {
return this.waiting;
}
protected override _render(options: InputManagerRenderOptions): void {
let switchMode = "";
const otherMode = this.inputMode === InputMode.Interactive ?
InputMode.Batch : InputMode.Interactive;
switchMode = `<a id="${SWITCH_INPUT_MODE_A_ID}" data-value="${otherMode}"
class="_tw-flex _tw-flex-row-reverse hover:_tw-cursor-pointer _tw-text-blue-500">
${t(`Papyros.switch_input_mode_to.${otherMode}`)}
</a>`;
renderWithOptions(options, `
<div id="${USER_INPUT_WRAPPER_ID}" class="_tw-my-1">
</div>
${switchMode}`);
addListener<InputMode>(SWITCH_INPUT_MODE_A_ID, im => this.setInputMode(im),
"click", "data-value");
this.inputHandler.render({
parentElementId: USER_INPUT_WRAPPER_ID,
darkMode: options.darkMode,
inputStyling: options.inputStyling
});
this.inputHandler.waitWithPrompt(this.waiting, this.prompt);
}
private waitWithPrompt(waiting: boolean, prompt = ""): void {
this.waiting = waiting;
this.prompt = prompt;
this.inputHandler.waitWithPrompt(this.waiting, this.prompt);
}
private onUserInput(line: string): void {
this.sendInput(line);
this.waitWithPrompt(false);
}
/**
* Asynchronously handle an input request by prompting the user for input
* @param {BackendEvent} e Event containing the input data
*/
private onInputRequest(e: BackendEvent): void {
this.waitWithPrompt(true, e.data);
}
private onRunStart(): void {
// Prevent switching input mode during runs
getElement(SWITCH_INPUT_MODE_A_ID).hidden = true;
this.waitWithPrompt(false);
this.inputHandler.onRunStart();
}
private onRunEnd(): void {
getElement(SWITCH_INPUT_MODE_A_ID).hidden = false;
this.inputHandler.onRunEnd();
this.waitWithPrompt(false);
}
}