Skip to content

Commit 9883c9b

Browse files
committed
yeet input history
1 parent 36b5251 commit 9883c9b

12 files changed

Lines changed: 103 additions & 69 deletions

File tree

frontend/src/ts/commandline/lists/result-screen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {
55
showErrorNotification,
66
showSuccessNotification,
77
} from "../../states/notifications";
8-
import * as TestInput from "../../test/test-input";
98
import * as TestState from "../../test/test-state";
109
import * as TestWords from "../../test/test-words";
1110
import { Config } from "../../config/store";
1211
import * as PractiseWords from "../../test/practise-words";
1312
import { Command, CommandsSubgroup } from "../types";
1413
import * as TestScreenshot from "../../test/test-screenshot";
14+
import { getInputHistory } from "../../test/events/stats";
1515

1616
const practiceSubgroup: CommandsSubgroup = {
1717
title: "Practice words...",
@@ -141,8 +141,8 @@ const commands: Command[] = [
141141
exec: (): void => {
142142
const words = (
143143
Config.mode === "zen"
144-
? TestInput.input.getHistory()
145-
: TestWords.words.list.slice(0, TestInput.input.getHistory().length)
144+
? getInputHistory()
145+
: TestWords.words.list.slice(0, getInputHistory().length)
146146
).join(" ");
147147

148148
navigator.clipboard.writeText(words).then(

frontend/src/ts/input/handlers/before-delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Config } from "../../config/store";
2-
import * as TestInput from "../../test/test-input";
32
import * as TestState from "../../test/test-state";
43
import * as TestWords from "../../test/test-words";
54
import { getInputElementValue } from "../input-element";
65
import * as TestUI from "../../test/test-ui";
76
import { isAwaitingNextWord } from "../state";
7+
import { getInputForWord } from "../../test/events/stats";
88

99
export function onBeforeDelete(event: InputEvent): void {
1010
if (!TestState.isActive) {
@@ -45,7 +45,7 @@ export function onBeforeDelete(event: InputEvent): void {
4545

4646
const confidence = Config.confidenceMode;
4747
const previousWordCorrect =
48-
(TestInput.input.get(TestState.activeWordIndex - 1) ?? "") ===
48+
getInputForWord(TestState.activeWordIndex - 1) ===
4949
TestWords.words.getText(TestState.activeWordIndex - 1);
5050

5151
if (confidence === "on" && inputIsEmpty && !previousWordCorrect) {

frontend/src/ts/input/helpers/word-navigation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { showLoaderBar, hideLoaderBar } from "../../states/loader-bar";
1515
import { setInputElementValue } from "../input-element";
1616
import { setAwaitingNextWord } from "../state";
1717
import { DeleteInputType } from "./input-type";
18-
import { getWordBurst } from "../../test/events/stats";
18+
import { getInputForWord, getWordBurst } from "../../test/events/stats";
1919

2020
type GoToNextWordParams = {
2121
correctInsert: boolean;
@@ -65,7 +65,7 @@ export async function goToNextWord({
6565

6666
Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex + 1));
6767

68-
TestInput.input.pushHistory();
68+
// TestInput.input.pushHistory();
6969

7070
const lastWord = TestState.activeWordIndex >= TestWords.words.length - 1;
7171
if (lastWord) {
@@ -107,7 +107,7 @@ export function goToPreviousWord(
107107

108108
Replay.addReplayEvent("backWord");
109109

110-
const word = TestInput.input.popHistory();
110+
const word = getInputForWord(TestState.activeWordIndex);
111111
TestState.decreaseActiveWordIndex();
112112

113113
Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex));

frontend/src/ts/test/events/data.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,30 @@ export function getPressedKeys(): Map<
310310
return pressedKeys;
311311
}
312312

313+
export function getInputEventsForWord(wordIndex: number): InputEvent[] {
314+
const events = getAllTestEvents();
315+
const result: InputEvent[] = [];
316+
for (const event of events) {
317+
if (event.type !== "input") continue;
318+
319+
let eventWordIndex = event.data.wordIndex;
320+
321+
if (
322+
(event.data.inputType === "deleteWordBackward" ||
323+
event.data.inputType === "deleteContentBackward") &&
324+
event.data.charIndex === 0 &&
325+
eventWordIndex > 0
326+
) {
327+
eventWordIndex -= 1;
328+
}
329+
330+
if (eventWordIndex === wordIndex) {
331+
result.push(event);
332+
}
333+
}
334+
return result;
335+
}
336+
313337
export function getInputEventsPerWord(
314338
startMs?: number,
315339
testMsLimit?: number,

frontend/src/ts/test/events/stats.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
getAllTestEvents,
33
getInputEvents,
4+
getInputEventsForWord,
45
getInputEventsPerWord,
56
getPressedKeys,
67
logTestEvent,
@@ -373,7 +374,7 @@ function computeBurst(events: InputEvent[], now?: number): number {
373374
}
374375

375376
export function getWordBurst(wordIndex: number, now?: number): number {
376-
const events = getInputEventsPerWord().get(wordIndex) ?? [];
377+
const events = getInputEventsForWord(wordIndex);
377378
return computeBurst(events, now);
378379
}
379380

@@ -438,7 +439,14 @@ export function getChars(countPartialLastWord = false): CharCounts {
438439
};
439440
}
440441

442+
export function getInputForWord(wordIndex: number): string {
443+
const events = getInputEventsForWord(wordIndex);
444+
return getSimulatedInput(events).trimEnd();
445+
}
446+
441447
export function getInputHistory(): string[] {
448+
console.log("getting input history");
449+
console.trace("getting input");
442450
const eventsPerWordIndex = getInputEventsPerWord();
443451
const history: string[] = [];
444452

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { WordGenError } from "../../utils/word-gen-error";
2828
import { FunboxName, KeymapLayout, Layout } from "@monkeytype/schemas/configs";
2929
import { Language, LanguageObject } from "@monkeytype/schemas/languages";
3030
import { qs } from "../../utils/dom";
31+
import { getInputForWord } from "../events/stats";
3132

3233
export type FunboxFunctions = {
3334
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -62,7 +63,7 @@ async function readAheadHandleKeydown(event: KeyboardEvent): Promise<void> {
6263
event.key === "Backspace" &&
6364
!isCorrect &&
6465
(TestInput.input.current !== "" ||
65-
TestInput.input.getHistory(TestState.activeWordIndex - 1) !==
66+
getInputForWord(TestState.activeWordIndex - 1) !==
6667
TestWords.words.getText(TestState.activeWordIndex - 1) ||
6768
Config.freedomMode)
6869
) {
@@ -425,7 +426,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
425426
const outOf: number = TestWords.words.length;
426427
const wordsPerLayout = Math.floor(outOf / layouts.length);
427428
const index = Math.floor(
428-
(TestInput.input.getHistory().length + 1) / wordsPerLayout,
429+
(TestState.activeWordIndex + 1) / wordsPerLayout,
429430
);
430431
const mod =
431432
wordsPerLayout - ((TestState.activeWordIndex + 1) % wordsPerLayout);

frontend/src/ts/test/practise-words.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { showNoticeNotification } from "../states/notifications";
44
import { Config } from "../config/store";
55
import { setConfig } from "../config/setters";
66
import * as CustomText from "./custom-text";
7-
import * as TestInput from "./test-input";
87
import { configEvent } from "../events/config";
98
import { setCustomTextName } from "../legacy-states/custom-text-name";
109
import { Mode } from "@monkeytype/schemas/shared";
1110
import { CustomTextSettings } from "@monkeytype/schemas/results";
12-
import { getBurstHistory, getMissedWords } from "./events/stats";
11+
import {
12+
getBurstHistory,
13+
getInputHistory,
14+
getMissedWords,
15+
} from "./events/stats";
1316

1417
type Before = {
1518
mode: Mode | null;
@@ -91,7 +94,7 @@ export function init(
9194
if (slow) {
9295
const typedWords = TestWords.words
9396
.getText()
94-
.slice(0, TestInput.input.getHistory().length - 1);
97+
.slice(0, getInputHistory().length - 1);
9598

9699
const burstHistory = getBurstHistory();
97100

frontend/src/ts/test/test-input.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import { lastElementFromArray } from "../utils/arrays";
21
import { getInputElementValue } from "../input/input-element";
32

43
class Input {
54
current: string;
6-
private history: string[];
5+
// private history: string[];
76
koreanStatus: boolean;
87
constructor() {
98
this.current = "";
10-
this.history = [];
9+
// this.history = [];
1110
this.koreanStatus = false;
1211
}
1312

1413
reset(): void {
1514
this.current = "";
16-
this.history = [];
15+
// this.history = [];
1716
}
1817

19-
resetHistory(): void {
20-
this.history = [];
21-
}
18+
// resetHistory(): void {
19+
// this.history = [];
20+
// }
2221

2322
setKoreanStatus(val: boolean): void {
2423
this.koreanStatus = val;
@@ -28,33 +27,33 @@ class Input {
2827
return this.koreanStatus;
2928
}
3029

31-
pushHistory(): void {
32-
this.history.push(this.current);
33-
this.current = "";
34-
}
35-
36-
popHistory(): string {
37-
const ret = this.history.pop() ?? "";
38-
return ret;
39-
}
40-
41-
get(index: number): string | undefined {
42-
return this.history[index];
43-
}
44-
45-
getHistory(): string[];
46-
getHistory(i: number): string | undefined;
47-
getHistory(i?: number): unknown {
48-
if (i === undefined) {
49-
return this.history;
50-
} else {
51-
return this.history[i];
52-
}
53-
}
54-
55-
getHistoryLast(): string | undefined {
56-
return lastElementFromArray(this.history);
57-
}
30+
// pushHistory(): void {
31+
// this.history.push(this.current);
32+
// this.current = "";
33+
// }
34+
35+
// popHistory(): string {
36+
// const ret = this.history.pop() ?? "";
37+
// return ret;
38+
// }
39+
40+
// get(index: number): string | undefined {
41+
// return this.history[index];
42+
// }
43+
44+
// getHistory(): string[];
45+
// getHistory(i: number): string | undefined;
46+
// getHistory(i?: number): unknown {
47+
// if (i === undefined) {
48+
// return this.history;
49+
// } else {
50+
// return this.history[i];
51+
// }
52+
// }
53+
54+
// getHistoryLast(): string | undefined {
55+
// return lastElementFromArray(this.history);
56+
// }
5857

5958
syncWithInputElement(): void {
6059
this.current = getInputElementValue().inputValue;

frontend/src/ts/test/test-logic.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ async function init(): Promise<boolean> {
432432
Replay.stopReplayRecording();
433433
TestWords.words.reset();
434434
TestState.setActiveWordIndex(0);
435-
TestInput.input.resetHistory();
435+
// TestInput.input.resetHistory();
436436
TestInput.input.current = "";
437437

438438
showLoaderBar();
@@ -654,7 +654,7 @@ export function areAllTestWordsGenerated(): boolean {
654654
//add word during the test
655655
export async function addWord(): Promise<void> {
656656
if (Config.mode === "zen") {
657-
TestUI.appendEmptyWordElement();
657+
TestUI.appendEmptyWordElement(TestState.activeWordIndex + 1);
658658
return;
659659
}
660660

@@ -668,7 +668,7 @@ export async function addWord(): Promise<void> {
668668
const toPushCount = funboxToPush?.split(":")[1];
669669
if (toPushCount !== undefined) bound = +toPushCount - 1;
670670

671-
if (TestWords.words.length - TestInput.input.getHistory().length > bound) {
671+
if (TestWords.words.length - TestState.activeWordIndex > bound) {
672672
console.debug("Not adding word, enough words already");
673673
return;
674674
}
@@ -903,14 +903,14 @@ export async function finish(difficultyFailed = false): Promise<void> {
903903
// in case the tests ends with a keypress (not a word submission)
904904
// we need to push the current input to history
905905
if (TestInput.input.current.length !== 0) {
906-
TestInput.input.pushHistory();
907-
Replay.replayGetWordsList(TestInput.input.getHistory());
906+
// TestInput.input.pushHistory();
907+
Replay.replayGetWordsList(getInputHistory());
908908
}
909909

910910
// in zen mode, ensure the replay words list reflects the typed input history
911911
// even if the current input was empty at finish (e.g., after submitting a word).
912912
if (Config.mode === "zen") {
913-
Replay.replayGetWordsList(TestInput.input.getHistory());
913+
Replay.replayGetWordsList(getInputHistory());
914914
}
915915

916916
forceReleaseAllKeys();
@@ -1071,11 +1071,11 @@ export async function finish(difficultyFailed = false): Promise<void> {
10711071
// Let's update the custom text progress
10721072
if (
10731073
TestState.bailedOut ||
1074-
TestInput.input.getHistory().length < TestWords.words.length
1074+
getInputHistory().length < TestWords.words.length
10751075
) {
10761076
// They bailed out
10771077

1078-
const history = TestInput.input.getHistory();
1078+
const history = getInputHistory();
10791079
let historyLength = history?.length;
10801080
const wordIndex = historyLength - 1;
10811081

frontend/src/ts/test/test-timer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function checkIfTimeIsUp(): void {
193193
//times up
194194
if (timer !== null) clearTimeout(timer);
195195
Caret.hide();
196-
TestInput.input.pushHistory();
196+
// TestInput.input.pushHistory();
197197
SlowTimer.clear();
198198
slowTimerCount = 0;
199199
timerEvent.dispatch({ key: "finish" });

0 commit comments

Comments
 (0)