Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frontend/src/ts/commandline/lists/result-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
showErrorNotification,
showSuccessNotification,
} from "../../states/notifications";
import * as TestInput from "../../test/test-input";
import { getInputHistory } from "../../test/test-input";
import * as TestState from "../../test/test-state";
import * as TestWords from "../../test/test-words";
import { Config } from "../../config/store";
Expand Down Expand Up @@ -141,8 +141,8 @@ const commands: Command[] = [
exec: (): void => {
const words = (
Config.mode === "zen"
? TestInput.input.getHistory()
: TestWords.words.list.slice(0, TestInput.input.getHistory().length)
? getInputHistory()
: TestWords.words.list.slice(0, getInputHistory().length)
).join(" ");

navigator.clipboard.writeText(words).then(
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ts/input/handlers/before-delete.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Config } from "../../config/store";
import * as TestInput from "../../test/test-input";
import * as TestState from "../../test/test-state";
import * as TestWords from "../../test/test-words";
import { getInputElementValue } from "../input-element";
import * as TestUI from "../../test/test-ui";
import { isAwaitingNextWord } from "../state";
import { getInputForWord } from "../../test/test-input";

export function onBeforeDelete(event: InputEvent): void {
if (!TestState.isActive) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function onBeforeDelete(event: InputEvent): void {

const confidence = Config.confidenceMode;
const previousWordCorrect =
(TestInput.input.get(TestState.activeWordIndex - 1) ?? "") ===
(getInputForWord(TestState.activeWordIndex - 1) ?? "") ===
TestWords.words.getText(TestState.activeWordIndex - 1);

if (confidence === "on" && inputIsEmpty && !previousWordCorrect) {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/ts/input/handlers/before-insert-text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../../config/store";
import * as TestInput from "../../test/test-input";
import { getCurrentInput } from "../../test/test-input";
import * as TestState from "../../test/test-state";
import * as TestUI from "../../test/test-ui";
import * as TestWords from "../../test/test-words";
Expand Down Expand Up @@ -61,7 +61,7 @@ export function onBeforeInsertText(data: string): boolean {
// block input if the word is too long
const inputLimit =
Config.mode === "zen" ? 30 : TestWords.words.getCurrentText().length + 20;
const overLimit = TestInput.input.current.length >= inputLimit;
const overLimit = getCurrentInput().length >= inputLimit;
if (overLimit && (shouldInsertSpaceAsCharacter === true || !dataIsSpace)) {
console.error("Hitting word limit");
return true;
Expand All @@ -71,7 +71,7 @@ export function onBeforeInsertText(data: string): boolean {
// this will not work for the first word of each line, but that has a low chance of happening
const dataIsNotFalsy = data !== null && data !== "";
const inputIsLongerThanOrEqualToWord =
TestInput.input.current.length >= TestWords.words.getCurrentText().length;
getCurrentInput().length >= TestWords.words.getCurrentText().length;

if (
!SlowTimer.get() && // don't do this check if slow timer is active
Expand All @@ -91,7 +91,7 @@ export function onBeforeInsertText(data: string): boolean {
);
const { top: topAfterAppend, height: heightAfterAppend } =
TestUI.getActiveWordTopAndHeightWithDifferentData(
(pendingWordData ?? TestInput.input.current) + data,
(pendingWordData ?? getCurrentInput()) + data,
);
if (topAfterAppend > TestUI.activeWordTop) {
//word jumped to next line
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/ts/input/handlers/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as TestUI from "../../test/test-ui";
import * as TestWords from "../../test/test-words";
import * as TestInput from "../../test/test-input";
import { getCurrentInput } from "../../test/test-input";
import { getInputElementValue, setInputElementValue } from "../input-element";

import * as Replay from "../../test/replay";
Expand All @@ -13,20 +14,20 @@ import { activeWordIndex } from "../../test/test-state";
export function onDelete(inputType: DeleteInputType, now: number): void {
const { realInputValue } = getInputElementValue();

const inputBeforeDelete = TestInput.input.current;
const inputBeforeDelete = getCurrentInput();
const activeWordIndexBeforeDelete = activeWordIndex;

TestInput.input.syncWithInputElement();

const inputAfterDelete = TestInput.input.current;
const inputAfterDelete = getCurrentInput();

Replay.addReplayEvent("setLetterIndex", TestInput.input.current.length);
Replay.addReplayEvent("setLetterIndex", getCurrentInput().length);
TestInput.setCurrentNotAfk();

const beforeDeleteOnlyTabs = /^\t*$/.test(inputBeforeDelete);
const allTabsCorrect = TestWords.words
.getCurrentText()
.startsWith(TestInput.input.current);
.startsWith(getCurrentInput());

//special check for code languages
if (
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/ts/input/handlers/insert-text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as TestUI from "../../test/test-ui";
import * as TestWords from "../../test/test-words";
import * as TestInput from "../../test/test-input";
import { getCurrentInput } from "../../test/test-input";
import {
getInputElementValue,
replaceInputElementLastValueChar,
Expand Down Expand Up @@ -86,8 +87,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
const charOverride = charOverrides.get(options.data);
if (
charOverride !== undefined &&
TestWords.words.getCurrentText()[TestInput.input.current.length] !==
options.data
TestWords.words.getCurrentText()[getCurrentInput().length] !== options.data
) {
// replace the data with the override
setInputElementValue(
Expand All @@ -101,7 +101,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
}

// input and target word
const testInput = TestInput.input.current;
const testInput = getCurrentInput();
const currentWord = TestWords.words.getCurrentText();

// if the character is visually equal, replace it with the target character
Expand Down Expand Up @@ -215,7 +215,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
}

// capture DOM before goToNextWord clears it for the new word
const inputValueAfterEvent = TestInput.input.current;
const inputValueAfterEvent = getCurrentInput();

// Log the event BEFORE goToNextWord so readers inside the navigation
// (e.g. beforeTestWordChange's updateWordLetters, getWordBurst) see the
Expand Down Expand Up @@ -264,7 +264,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
//this COULD be the next word because we are awaiting goToNextWord
const nextWord = TestWords.words.getCurrentText();
const doesNextWordHaveTab = /^\t+/.test(nextWord);
const isCurrentCharTab = nextWord[TestInput.input.current.length] === "\t";
const isCurrentCharTab = nextWord[getCurrentInput().length] === "\t";

//code mode - auto insert tabs
if (
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ts/input/helpers/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isSpace } from "../../utils/strings";
* Check if the input data is correct
* @param options - Options object
* @param options.data - Input data
* @param options.inputValue - Current input value (use TestInput.input.current, not input element value)
* @param options.inputValue - Current input value (use getCurrentInput(), not input element value)
* @param options.targetWord - Target word
* @param options.correctShiftUsed - Whether the correct shift state was used. Null means disabled
*/
Expand Down Expand Up @@ -47,7 +47,7 @@ export function isCharCorrect(options: {
* as a "control character" (moving to the next word)
* @param options - Options object
* @param options.data - Input data
* @param options.inputValue - Current input value (use TestInput.input.current, not input element value)
* @param options.inputValue - Current input value (use getCurrentInput(), not input element value)
* @param options.targetWord - Target word
* @returns Boolean if data is space, null if not
*/
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/ts/input/listeners/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as CompositionState from "../../legacy-states/composition";
import * as TestState from "../../test/test-state";
import * as TestLogic from "../../test/test-logic";
import * as TestInput from "../../test/test-input";
import { getCurrentInput } from "../../test/test-input";
import { setLastInsertCompositionTextData } from "../state";
import * as CompositionDisplay from "../../elements/composition-display";
import { onInsertText } from "../handlers/insert-text";
Expand All @@ -25,7 +26,7 @@ inputEl.addEventListener("compositionstart", (event) => {
if (!TestState.isActive) {
TestLogic.startTest(now);
}
if (TestInput.input.current.length === 0) {
if (getCurrentInput().length === 0) {
TestInput.setBurstStart(now);
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ts/input/listeners/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import * as TestUI from "../../test/test-ui";
import { onBeforeInsertText } from "../handlers/before-insert-text";
import { onBeforeDelete } from "../handlers/before-delete";
import * as TestInput from "../../test/test-input";
import { getCurrentInput } from "../../test/test-input";
import * as TestWords from "../../test/test-words";
import * as CompositionState from "../../legacy-states/composition";
import * as TestState from "../../test/test-state";
Expand Down Expand Up @@ -127,7 +127,7 @@ inputEl.addEventListener("input", async (event) => {
) {
const allWordsTyped = activeWordIndex >= TestWords.words.length - 1;
const inputPlusComposition =
TestInput.input.current + (CompositionState.getData() ?? "");
getCurrentInput() + (CompositionState.getData() ?? "");
const inputPlusCompositionIsCorrect =
TestWords.words.getCurrentText() === inputPlusComposition;

Expand Down
5 changes: 2 additions & 3 deletions frontend/src/ts/test/caret.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Config } from "../config/store";
import * as TestInput from "./test-input";
import * as TestState from "../test/test-state";
import { configEvent } from "../events/config";
import { Caret } from "../elements/caret";
import * as CompositionState from "../legacy-states/composition";
import { qsr } from "../utils/dom";
import { getCurrentInput } from "./test-input";

export function stopAnimation(): void {
caret.stopBlinking();
Expand Down Expand Up @@ -33,8 +33,7 @@ export function resetPosition(): void {
export function updatePosition(noAnim = false): void {
caret.goTo({
wordIndex: TestState.activeWordIndex,
letterIndex:
TestInput.input.current.length + CompositionState.getData().length,
letterIndex: getCurrentInput().length + CompositionState.getData().length,
isLanguageRightToLeft: TestState.isLanguageRightToLeft,
isDirectionReversed: TestState.isDirectionReversed,
animate: Config.smoothCaret !== "off" && !noAnim,
Expand Down
12 changes: 0 additions & 12 deletions frontend/src/ts/test/events/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,6 @@ export function getPressedKeys(): Map<
return pressedKeys;
}

export function getInputEventsForWord(wordIndex: number): InputEventNoMs[] {
const events = getAllTestEvents();
const result: InputEventNoMs[] = [];
for (const event of events) {
if (event.type !== "input") continue;
if (event.data.wordIndex === wordIndex) {
result.push(event);
}
}
return result;
}

export function getInputEventsPerWord(
startMs?: number,
testMsLimit?: number,
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/ts/test/events/stats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
getAllTestEvents,
getInputEvents,
getInputEventsForWord,
getInputEventsPerWord,
getPressedKeys,
logTestEvent,
Expand Down Expand Up @@ -352,9 +351,16 @@ export function getChars(): CharCounts {
);
}

export function getInputForWord(wordIndex: number): string {
const events = getInputEventsForWord(wordIndex);
return getInputFromDom(events).trimEnd();
export function getInputHistory(): string[] {
const eventsPerWordIndex = getInputEventsPerWord();
const history: string[] = [];

for (const events of eventsPerWordIndex.values()) {
const simulatedInput = getInputFromDom(events);
history.push(simulatedInput);
}

return history;
}

export function getAccuracy(): {
Expand Down
15 changes: 7 additions & 8 deletions frontend/src/ts/test/funbox/funbox-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../../states/notifications";
import * as DDR from "../../utils/ddr";
import * as TestWords from "../test-words";
import * as TestInput from "../test-input";
import { getCurrentInput, getInputForWord } from "../test-input";
import * as LayoutfluidFunboxTimer from "./layoutfluid-funbox-timer";
import { highlight } from "../../events/keymap";
import * as MemoryTimer from "./memory-funbox-timer";
Expand Down Expand Up @@ -52,17 +52,18 @@ export type FunboxFunctions = {
};

async function readAheadHandleKeydown(event: KeyboardEvent): Promise<void> {
const inputCurrentChar = (TestInput.input.current ?? "").slice(-1);
const currentInput = getCurrentInput();
const inputCurrentChar = (currentInput ?? "").slice(-1);
const wordCurrentChar = TestWords.words
.getCurrentText()
.slice(TestInput.input.current.length - 1, TestInput.input.current.length);
.slice(currentInput.length - 1, currentInput.length);
const isCorrect = inputCurrentChar === wordCurrentChar;

if (
event.key === "Backspace" &&
!isCorrect &&
(TestInput.input.current !== "" ||
TestInput.input.getHistory(TestState.activeWordIndex - 1) !==
(currentInput !== "" ||
getInputForWord(TestState.activeWordIndex - 1) !==
TestWords.words.getText(TestState.activeWordIndex - 1) ||
Config.freedomMode)
) {
Expand Down Expand Up @@ -452,9 +453,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
}
setTimeout(() => {
highlight(
TestWords.words
.getCurrentText()
.charAt(TestInput.input.current.length),
TestWords.words.getCurrentText().charAt(getCurrentInput().length),
);
}, 1);
}
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/ts/test/practise-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Config } from "../config/store";
import { setConfig } from "../config/setters";
import * as CustomText from "./custom-text";
import * as TestInput from "./test-input";
import { getMissedWords, getInputHistory } from "./test-input";
import { configEvent } from "../events/config";
import { Mode } from "@monkeytype/schemas/shared";
import { CustomTextSettings } from "@monkeytype/schemas/results";
Expand Down Expand Up @@ -37,11 +38,13 @@ export function init(
limit = 10;
}

const missedWords = getMissedWords();

// missed word, previous word, count
let sortableMissedWords: [string, number][] = [];
if (missed === "words") {
Object.keys(TestInput.missedWords).forEach((missedWord) => {
const missedWordCount = TestInput.missedWords[missedWord];
Object.keys(missedWords).forEach((missedWord) => {
const missedWordCount = missedWords[missedWord];
if (missedWordCount !== undefined) {
sortableMissedWords.push([missedWord, missedWordCount]);
}
Expand All @@ -56,7 +59,7 @@ export function init(
if (missed === "biwords") {
for (let i = 0; i < TestWords.words.length; i++) {
const missedWord = TestWords.words.getText(i);
const missedWordCount = TestInput.missedWords[missedWord];
const missedWordCount = missedWords[missedWord];
if (missedWordCount !== undefined) {
if (i === 0) {
sortableMissedBiwords.push([missedWord, "", missedWordCount]);
Expand Down Expand Up @@ -88,7 +91,7 @@ export function init(
if (slow) {
const typedWords = TestWords.words
.getText()
.slice(0, TestInput.input.getHistory().length - 1);
.slice(0, getInputHistory().length - 1);

sortableSlowWords = typedWords.map((e, i) => [
e,
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/ts/test/test-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,23 @@ export function restart(): void {

resetKeypressTimings();
}

export function getCurrentInput(): string {
return input.current;
}

export function getInputForWord(wordIndex: number): string | undefined {
return input.get(wordIndex);
}

export function resetCurrentInput(): void {
input.current = "";
}

export function getMissedWords(): MissedWordsType {
return missedWords;
}

export function getInputHistory(): string[] {
return input.getHistory();
}
Loading
Loading