Skip to content

Commit f3f1205

Browse files
committed
change var name to removedUIWordCount & add a helper function
1 parent 65d554e commit f3f1205

File tree

6 files changed

+30
-34
lines changed

6 files changed

+30
-34
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function backspaceToPrevious(): void {
121121
if (!TestState.isActive) return;
122122

123123
const wordElementIndex =
124-
TestState.activeWordIndex - TestState.activeWordElementOffset;
124+
TestState.activeWordIndex - TestState.removedUIWordCount;
125125

126126
if (TestInput.input.getHistory().length === 0 || wordElementIndex === 0) {
127127
return;
@@ -268,12 +268,12 @@ async function handleSpace(): Promise<void> {
268268
if (Config.blindMode) {
269269
if (Config.highlightMode !== "off") {
270270
TestUI.highlightAllLettersAsCorrect(
271-
TestState.activeWordIndex - TestState.activeWordElementOffset
271+
TestState.activeWordIndex - TestState.removedUIWordCount
272272
);
273273
}
274274
} else {
275275
TestUI.highlightBadWord(
276-
TestState.activeWordIndex - TestState.activeWordElementOffset
276+
TestState.activeWordIndex - TestState.removedUIWordCount
277277
);
278278
}
279279
TestInput.input.pushHistory();
@@ -342,14 +342,14 @@ async function handleSpace(): Promise<void> {
342342
if (!Config.showAllLines || shouldLimitToThreeLines) {
343343
const currentTop: number = Math.floor(
344344
document.querySelectorAll<HTMLElement>("#words .word")[
345-
TestState.activeWordIndex - TestState.activeWordElementOffset - 1
345+
TestState.activeWordIndex - TestState.removedUIWordCount - 1
346346
]?.offsetTop ?? 0
347347
);
348348

349349
const { data: nextTop } = tryCatchSync(() =>
350350
Math.floor(
351351
document.querySelectorAll<HTMLElement>("#words .word")[
352-
TestState.activeWordIndex - TestState.activeWordElementOffset
352+
TestState.activeWordIndex - TestState.removedUIWordCount
353353
]?.offsetTop ?? 0
354354
)
355355
);
@@ -667,7 +667,7 @@ function handleChar(
667667
);
668668

669669
const activeWord = document.querySelectorAll("#words .word")?.[
670-
TestState.activeWordIndex - TestState.activeWordElementOffset
670+
TestState.activeWordIndex - TestState.removedUIWordCount
671671
] as HTMLElement;
672672

673673
const testInputLength: number = !isCharKorean
@@ -1112,7 +1112,7 @@ $(document).on("keydown", async (event) => {
11121112
const activeWord: HTMLElement | null = document.querySelectorAll(
11131113
"#words .word"
11141114
)?.[
1115-
TestState.activeWordIndex - TestState.activeWordElementOffset
1115+
TestState.activeWordIndex - TestState.removedUIWordCount
11161116
] as HTMLElement;
11171117
const len: number = TestInput.input.current.length; // have to do this because prettier wraps the line and causes an error
11181118

frontend/src/ts/test/caret.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function updatePosition(noAnim = false): Promise<void> {
142142
if (Config.mode === "zen") wordLen = inputLen;
143143
const activeWordEl =
144144
document.querySelectorAll<HTMLElement>("#words .word")[
145-
TestState.activeWordIndex - TestState.activeWordElementOffset
145+
TestState.activeWordIndex - TestState.removedUIWordCount
146146
];
147147
if (!activeWordEl) return;
148148

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ export async function update(expectedStepEnd: number): Promise<void> {
200200
let newTop;
201201
let newLeft;
202202
try {
203-
const newIndex =
204-
settings.currentWordIndex - TestState.activeWordElementOffset;
203+
const newIndex = settings.currentWordIndex - TestState.removedUIWordCount;
205204
const word = document.querySelectorAll("#words .word")[
206205
newIndex
207206
] as HTMLElement;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export async function init(): Promise<void | null> {
422422
Replay.stopReplayRecording();
423423
TestWords.words.reset();
424424
TestState.setActiveWordIndex(0);
425-
TestState.setActiveWordElementOffset(0);
425+
TestState.setRemovedUIWordCount(0);
426426
TestInput.input.resetHistory();
427427
TestInput.input.current = "";
428428

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

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

1515
export function setRepeated(tf: boolean): void {
@@ -56,8 +56,12 @@ export function setTestInitSuccess(tf: boolean): void {
5656
testInitSuccess = tf;
5757
}
5858

59-
export function setActiveWordElementOffset(val: number): void {
60-
activeWordElementOffset = val;
59+
export function setRemovedUIWordCount(val: number): void {
60+
removedUIWordCount = val;
61+
}
62+
63+
export function incrementRemovedUIWordCount(by: number = 1): void {
64+
removedUIWordCount += by;
6165
}
6266

6367
export function setLineScrollDistance(val: number): void {

frontend/src/ts/test/test-ui.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export function setResultCalculating(val: boolean): void {
223223

224224
export function reset(): void {
225225
currentTestLine = 0;
226-
TestState.setActiveWordElementOffset(0);
226+
TestState.setRemovedUIWordCount(0);
227227
}
228228

229229
export function focusWords(): void {
@@ -248,7 +248,7 @@ export function updateActiveElement(
248248
active.classList.remove("active");
249249
}
250250
const newActiveWord = document.querySelectorAll("#words .word")[
251-
TestState.activeWordIndex - TestState.activeWordElementOffset
251+
TestState.activeWordIndex - TestState.removedUIWordCount
252252
] as HTMLElement | undefined;
253253

254254
if (newActiveWord === undefined) {
@@ -438,7 +438,7 @@ export async function updateWordsInputPosition(initial = false): Promise<void> {
438438

439439
const activeWord =
440440
document.querySelectorAll<HTMLElement>("#words .word")[
441-
TestState.activeWordIndex - TestState.activeWordElementOffset
441+
TestState.activeWordIndex - TestState.removedUIWordCount
442442
];
443443

444444
if (!activeWord) {
@@ -490,7 +490,7 @@ export async function centerActiveLine(): Promise<void> {
490490

491491
const wordElements = document.querySelectorAll<HTMLElement>("#words .word");
492492
const activeWordIndex =
493-
TestState.activeWordIndex - TestState.activeWordElementOffset;
493+
TestState.activeWordIndex - TestState.removedUIWordCount;
494494
const activeWordEl = wordElements[activeWordIndex];
495495
if (!activeWordEl) {
496496
resolve();
@@ -520,7 +520,7 @@ export function updateWordsWrapperHeight(force = false): void {
520520
) as HTMLElement;
521521
const wordElements = wrapperEl.querySelectorAll<HTMLElement>("#words .word");
522522
const activeWordEl =
523-
wordElements[TestState.activeWordIndex - TestState.activeWordElementOffset];
523+
wordElements[TestState.activeWordIndex - TestState.removedUIWordCount];
524524
if (!activeWordEl) return;
525525

526526
wrapperEl.classList.remove("hidden");
@@ -642,7 +642,7 @@ export async function updateActiveWordLetters(
642642
let ret = "";
643643
const activeWord =
644644
document.querySelectorAll<HTMLElement>("#words .word")?.[
645-
TestState.activeWordIndex - TestState.activeWordElementOffset
645+
TestState.activeWordIndex - TestState.removedUIWordCount
646646
];
647647
if (!activeWord) return;
648648
const hintIndices: number[][] = [];
@@ -832,7 +832,7 @@ export async function scrollTape(noRemove = false): Promise<void> {
832832

833833
// index of the active word in the collection of .word elements
834834
const wordElementIndex =
835-
TestState.activeWordIndex - TestState.activeWordElementOffset;
835+
TestState.activeWordIndex - TestState.removedUIWordCount;
836836
const wordsWrapperWidth = (
837837
document.querySelector("#wordsWrapper") as HTMLElement
838838
).offsetWidth;
@@ -948,10 +948,8 @@ export async function scrollTape(noRemove = false): Promise<void> {
948948

949949
/* remove overflown elements */
950950
if (toRemove.length > 0 && !noRemove) {
951-
TestState.setActiveWordElementOffset(
952-
TestState.activeWordElementOffset + wordsToRemoveCount
953-
);
954951
for (const el of toRemove) el.remove();
952+
TestState.incrementRemovedUIWordCount(wordsToRemoveCount);
955953
for (let i = 0; i < widthRemovedFromLine.length; i++) {
956954
const afterNewlineEl = afterNewLineEls[i] as HTMLElement;
957955
const currentLineIndent =
@@ -1071,7 +1069,7 @@ export async function lineJump(
10711069

10721070
// index of the active word in the collection of .word elements
10731071
const wordElementIndex =
1074-
TestState.activeWordIndex - TestState.activeWordElementOffset;
1072+
TestState.activeWordIndex - TestState.removedUIWordCount;
10751073
const wordsEl = document.getElementById("words") as HTMLElement;
10761074
const wordsChildrenArr = [...wordsEl.children];
10771075
const wordElements = wordsEl.querySelectorAll(".word");
@@ -1146,12 +1144,8 @@ export async function lineJump(
11461144
document.querySelectorAll<HTMLElement>("#words .word")?.[
11471145
wordElementIndex
11481146
]?.offsetTop ?? 0;
1149-
TestState.setActiveWordElementOffset(
1150-
TestState.activeWordElementOffset +
1151-
removeElementsBeforeWord(
1152-
lastElementToRemoveIndex,
1153-
wordsChildrenArr
1154-
)
1147+
TestState.incrementRemovedUIWordCount(
1148+
removeElementsBeforeWord(lastElementToRemoveIndex, wordsChildrenArr)
11551149
);
11561150
wordsEl.style.marginTop = "0";
11571151
lineTransition = false;
@@ -1160,9 +1154,8 @@ export async function lineJump(
11601154
});
11611155
jqWords.dequeue("topMargin");
11621156
} else {
1163-
TestState.setActiveWordElementOffset(
1164-
TestState.activeWordElementOffset +
1165-
removeElementsBeforeWord(lastElementToRemoveIndex, wordsChildrenArr)
1157+
TestState.incrementRemovedUIWordCount(
1158+
removeElementsBeforeWord(lastElementToRemoveIndex, wordsChildrenArr)
11661159
);
11671160
paceCaretElement.style.top = `${
11681161
paceCaretElement.offsetTop - wordHeight

0 commit comments

Comments
 (0)