Skip to content

Commit 6c10c13

Browse files
authored
refactor: remove removedUIWordCount, add wordIndex to word dom elements (@Miodec) (monkeytypegame#6932)
Removes confusing `removedUIWordCount`, simplifying "word at index" access.
1 parent 81ea9e6 commit 6c10c13

File tree

6 files changed

+85
-138
lines changed

6 files changed

+85
-138
lines changed

frontend/src/ts/controllers/input-controller.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,30 @@ function updateUI(): void {
119119
function backspaceToPrevious(): void {
120120
if (!TestState.isActive) return;
121121

122-
const wordElementIndex =
123-
TestState.activeWordIndex - TestState.removedUIWordCount;
122+
const previousWordEl = TestUI.getWordElement(TestState.activeWordIndex - 1);
124123

125-
if (TestInput.input.getHistory().length === 0 || wordElementIndex === 0) {
126-
return;
127-
}
124+
const isFirstWord = TestInput.input.getHistory().length === 0;
125+
const isFirstVisibleWord = previousWordEl === null;
126+
const isPreviousWordHidden = previousWordEl?.classList.contains("hidden");
127+
const isPreviousWordCorrect =
128+
TestInput.input.getHistory(TestState.activeWordIndex - 1) ===
129+
TestWords.words.get(TestState.activeWordIndex - 1);
128130

129-
const wordElements = document.querySelectorAll("#words > .word");
130131
if (
131-
(TestInput.input.getHistory(TestState.activeWordIndex - 1) ===
132-
TestWords.words.get(TestState.activeWordIndex - 1) &&
133-
!Config.freedomMode) ||
134-
wordElements[wordElementIndex - 1]?.classList.contains("hidden")
132+
isFirstWord ||
133+
isFirstVisibleWord ||
134+
isPreviousWordHidden ||
135+
(isPreviousWordCorrect && !Config.freedomMode) ||
136+
Config.confidenceMode === "on" ||
137+
Config.confidenceMode === "max"
135138
) {
136139
return;
137140
}
138141

139-
if (Config.confidenceMode === "on" || Config.confidenceMode === "max") {
140-
return;
141-
}
142+
const activeWordEl = TestUI.getActiveWordElement();
142143

143144
const incorrectLetterBackspaced =
144-
wordElements[wordElementIndex]?.children[0]?.classList.contains(
145-
"incorrect"
146-
);
145+
activeWordEl?.children[0]?.classList.contains("incorrect");
147146
if (Config.stopOnError === "letter" && incorrectLetterBackspaced) {
148147
void TestUI.updateActiveWordLetters();
149148
}
@@ -266,14 +265,10 @@ async function handleSpace(): Promise<void> {
266265
PaceCaret.handleSpace(false, currentWord);
267266
if (Config.blindMode) {
268267
if (Config.highlightMode !== "off") {
269-
TestUI.highlightAllLettersAsCorrect(
270-
TestState.activeWordIndex - TestState.removedUIWordCount
271-
);
268+
TestUI.highlightAllLettersAsCorrect(TestState.activeWordIndex);
272269
}
273270
} else {
274-
TestUI.highlightBadWord(
275-
TestState.activeWordIndex - TestState.removedUIWordCount
276-
);
271+
TestUI.highlightBadWord(TestState.activeWordIndex);
277272
}
278273
TestInput.input.pushHistory();
279274
TestState.increaseActiveWordIndex();
@@ -340,17 +335,11 @@ async function handleSpace(): Promise<void> {
340335

341336
if (!Config.showAllLines || shouldLimitToThreeLines) {
342337
const currentTop: number = Math.floor(
343-
document.querySelectorAll<HTMLElement>("#words .word")[
344-
TestState.activeWordIndex - TestState.removedUIWordCount - 1
345-
]?.offsetTop ?? 0
338+
TestUI.getWordElement(TestState.activeWordIndex - 1)?.offsetTop ?? 0
346339
);
347340

348341
const { data: nextTop } = tryCatchSync(() =>
349-
Math.floor(
350-
document.querySelectorAll<HTMLElement>("#words .word")[
351-
TestState.activeWordIndex - TestState.removedUIWordCount
352-
]?.offsetTop ?? 0
353-
)
342+
Math.floor(TestUI.getActiveWordElement()?.offsetTop ?? 0)
354343
);
355344

356345
if ((nextTop ?? 0) > currentTop) {
@@ -665,9 +654,7 @@ async function handleChar(
665654
char
666655
);
667656

668-
const activeWord = document.querySelectorAll("#words .word")?.[
669-
TestState.activeWordIndex - TestState.removedUIWordCount
670-
] as HTMLElement;
657+
const activeWord = TestUI.getActiveWordElement() as HTMLElement;
671658

672659
const testInputLength: number = !isCharKorean
673660
? TestInput.input.current.length
@@ -1126,11 +1113,7 @@ $(document).on("keydown", async (event) => {
11261113
//show dead keys
11271114
if (event.key === "Dead" && !CompositionState.getComposing()) {
11281115
void Sound.playClick();
1129-
const activeWord: HTMLElement | null = document.querySelectorAll(
1130-
"#words .word"
1131-
)?.[
1132-
TestState.activeWordIndex - TestState.removedUIWordCount
1133-
] as HTMLElement;
1116+
const activeWord = TestUI.getActiveWordElement();
11341117
const len: number = TestInput.input.current.length; // have to do this because prettier wraps the line and causes an error
11351118

11361119
// Check to see if the letter actually exists to toggle it as dead

frontend/src/ts/test/caret.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ export async function updatePosition(noAnim = false): Promise<void> {
173173
let wordLen = splitIntoCharacters(TestWords.words.getCurrent()).length;
174174
const inputLen = splitIntoCharacters(TestInput.input.current).length;
175175
if (Config.mode === "zen") wordLen = inputLen;
176-
const activeWordEl =
177-
document.querySelectorAll<HTMLElement>("#words .word")[
178-
TestState.activeWordIndex - TestState.removedUIWordCount
179-
];
176+
const activeWordEl = document.querySelector<HTMLElement>(
177+
`#words .word[data-wordindex='${TestState.activeWordIndex}']`
178+
);
180179
if (!activeWordEl) return;
181180

182181
const currentWordNodeList =

frontend/src/ts/test/pace-caret.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,14 @@ export async function update(expectedStepEnd: number): Promise<void> {
208208
let newTop;
209209
let newLeft;
210210
try {
211-
const newIndex = settings.currentWordIndex - TestState.removedUIWordCount;
212-
const word = document.querySelectorAll("#words .word")[
213-
newIndex
214-
] as HTMLElement;
211+
const word = document.querySelector<HTMLElement>(
212+
`#words .word[data-wordindex='${settings.currentWordIndex}']`
213+
);
214+
215+
if (!word) {
216+
throw new Error("Word element not found");
217+
}
218+
215219
if (settings.currentLetterIndex === -1) {
216220
currentLetter = word.querySelectorAll("letter")[0] as HTMLElement;
217221
} else {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ export async function init(): Promise<boolean> {
436436
Replay.stopReplayRecording();
437437
TestWords.words.reset();
438438
TestState.setActiveWordIndex(0);
439-
TestState.setRemovedUIWordCount(0);
440439
TestInput.input.resetHistory();
441440
TestInput.input.current = "";
442441

frontend/src/ts/test/test-state.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export let bailedOut = false;
99
export let selectedQuoteId = 1;
1010
export let activeWordIndex = 0;
1111
export let testInitSuccess = true;
12-
export let removedUIWordCount = 0;
1312
export let lineScrollDistance = 0;
1413

1514
export function setRepeated(tf: boolean): void {
@@ -56,14 +55,6 @@ export function setTestInitSuccess(tf: boolean): void {
5655
testInitSuccess = tf;
5756
}
5857

59-
export function setRemovedUIWordCount(val: number): void {
60-
removedUIWordCount = val;
61-
}
62-
63-
export function incrementRemovedUIWordCount(by: number = 1): void {
64-
removedUIWordCount += by;
65-
}
66-
6758
export function setLineScrollDistance(val: number): void {
6859
lineScrollDistance = val;
6960
}

0 commit comments

Comments
 (0)