Skip to content

Commit 6aeab4d

Browse files
committed
refactor: convert ui element index to offset
1 parent cfc810a commit 6aeab4d

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function backspaceToPrevious(): void {
120120

121121
if (
122122
TestInput.input.getHistory().length === 0 ||
123-
TestUI.activeWordElementIndex === 0
123+
TestState.activeWordIndex - TestUI.activeWordElementOffset === 0
124124
) {
125125
return;
126126
}
@@ -154,7 +154,6 @@ function backspaceToPrevious(): void {
154154
setWordsInput(" " + TestInput.input.current + " ");
155155
}
156156
TestState.decreaseActiveWordIndex();
157-
TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex - 1);
158157
TestUI.updateActiveElement(true);
159158
Funbox.toggleScript(TestWords.words.getCurrent());
160159
void TestUI.updateActiveWordLetters();
@@ -267,10 +266,14 @@ async function handleSpace(): Promise<void> {
267266
PaceCaret.handleSpace(false, currentWord);
268267
if (Config.blindMode) {
269268
if (Config.highlightMode !== "off") {
270-
TestUI.highlightAllLettersAsCorrect(TestUI.activeWordElementIndex);
269+
TestUI.highlightAllLettersAsCorrect(
270+
TestState.activeWordIndex - TestUI.activeWordElementOffset
271+
);
271272
}
272273
} else {
273-
TestUI.highlightBadWord(TestUI.activeWordElementIndex);
274+
TestUI.highlightBadWord(
275+
TestState.activeWordIndex - TestUI.activeWordElementOffset
276+
);
274277
}
275278
TestInput.input.pushHistory();
276279
TestState.increaseActiveWordIndex();
@@ -334,7 +337,6 @@ async function handleSpace(): Promise<void> {
334337
void TestLogic.addWord();
335338
}
336339
}
337-
TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex + 1);
338340
TestUI.updateActiveElement();
339341
void Caret.updatePosition();
340342

@@ -346,14 +348,14 @@ async function handleSpace(): Promise<void> {
346348
) {
347349
const currentTop: number = Math.floor(
348350
document.querySelectorAll<HTMLElement>("#words .word")[
349-
TestUI.activeWordElementIndex - 1
351+
TestState.activeWordIndex - TestUI.activeWordElementOffset - 1
350352
]?.offsetTop ?? 0
351353
);
352354
let nextTop: number;
353355
try {
354356
nextTop = Math.floor(
355357
document.querySelectorAll<HTMLElement>("#words .word")[
356-
TestUI.activeWordElementIndex
358+
TestState.activeWordIndex - TestUI.activeWordElementOffset
357359
]?.offsetTop ?? 0
358360
);
359361
} catch (e) {
@@ -673,7 +675,7 @@ function handleChar(
673675
);
674676

675677
const activeWord = document.querySelectorAll("#words .word")?.[
676-
TestUI.activeWordElementIndex
678+
TestState.activeWordIndex - TestUI.activeWordElementOffset
677679
] as HTMLElement;
678680

679681
const testInputLength: number = !isCharKorean
@@ -1116,7 +1118,9 @@ $(document).on("keydown", async (event) => {
11161118
void Sound.playClick();
11171119
const activeWord: HTMLElement | null = document.querySelectorAll(
11181120
"#words .word"
1119-
)?.[TestUI.activeWordElementIndex] as HTMLElement;
1121+
)?.[
1122+
TestState.activeWordIndex - TestUI.activeWordElementOffset
1123+
] as HTMLElement;
11201124
const len: number = TestInput.input.current.length; // have to do this because prettier wraps the line and causes an error
11211125

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

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export async function init(): Promise<void> {
401401
Replay.stopReplayRecording();
402402
TestWords.words.reset();
403403
TestState.setActiveWordIndex(0);
404-
TestUI.setActiveWordElementIndex(0);
404+
TestUI.setActiveWordElementOffset(0);
405405
TestInput.input.resetHistory();
406406
TestInput.input.current = "";
407407

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
203203
if (eventKey === "burstHeatmap") void applyBurstHeatmap();
204204
});
205205

206-
export let activeWordElementIndex = 0;
206+
export let activeWordElementOffset = 0;
207207
export let resultVisible = false;
208208
export let activeWordTop = 0;
209209
export let testRestarting = false;
@@ -216,8 +216,8 @@ export function setResultVisible(val: boolean): void {
216216
resultVisible = val;
217217
}
218218

219-
export function setActiveWordElementIndex(val: number): void {
220-
activeWordElementIndex = val;
219+
export function setActiveWordElementOffset(val: number): void {
220+
activeWordElementOffset = val;
221221
}
222222

223223
export function setActiveWordTop(val: number): void {
@@ -243,7 +243,7 @@ export function setResultCalculating(val: boolean): void {
243243

244244
export function reset(): void {
245245
currentTestLine = 0;
246-
activeWordElementIndex = 0;
246+
activeWordElementOffset = 0;
247247
}
248248

249249
export function focusWords(): void {
@@ -268,7 +268,7 @@ export function updateActiveElement(
268268
active.classList.remove("active");
269269
}
270270
const newActiveWord = document.querySelectorAll("#words .word")[
271-
activeWordElementIndex
271+
TestState.activeWordIndex - activeWordElementOffset
272272
] as HTMLElement | undefined;
273273

274274
if (newActiveWord == undefined) {
@@ -453,7 +453,7 @@ export async function updateWordsInputPosition(initial = false): Promise<void> {
453453
const el = document.querySelector("#wordsInput") as HTMLElement;
454454
const activeWord =
455455
document.querySelectorAll<HTMLElement>("#words .word")[
456-
activeWordElementIndex
456+
TestState.activeWordIndex - activeWordElementOffset
457457
];
458458

459459
if (!activeWord) {
@@ -926,7 +926,7 @@ export async function updateActiveWordLetters(
926926
}
927927

928928
const activeWord = document.querySelectorAll("#words .word")?.[
929-
activeWordElementIndex
929+
TestState.activeWordIndex - activeWordElementOffset
930930
] as HTMLElement;
931931

932932
activeWord.innerHTML = ret;
@@ -960,14 +960,15 @@ export function scrollTape(): void {
960960
return;
961961
}
962962

963+
const wordIndex = TestState.activeWordIndex - activeWordElementOffset;
963964
const wordsWrapperWidth = (
964965
document.querySelector("#wordsWrapper") as HTMLElement
965966
).offsetWidth;
966967
let fullWordsWidth = 0;
967968
const toHide: JQuery[] = [];
968969
let widthToHide = 0;
969-
if (activeWordElementIndex > 0) {
970-
for (let i = 0; i < activeWordElementIndex; i++) {
970+
if (wordIndex > 0) {
971+
for (let i = 0; i < wordIndex; i++) {
971972
const word = document.querySelectorAll("#words .word")[i] as HTMLElement;
972973
fullWordsWidth += $(word).outerWidth(true) ?? 0;
973974
const forWordLeft = Math.floor(word.offsetLeft);
@@ -979,7 +980,7 @@ export function scrollTape(): void {
979980
}
980981
}
981982
if (toHide.length > 0) {
982-
activeWordElementIndex -= toHide.length;
983+
activeWordElementOffset += toHide.length;
983984
toHide.forEach((e) => e.remove());
984985
fullWordsWidth -= widthToHide;
985986
const currentMargin = parseInt($("#words").css("margin-left"), 10);
@@ -990,7 +991,7 @@ export function scrollTape(): void {
990991
if (Config.tapeMode === "letter") {
991992
if (TestInput.input.current.length > 0) {
992993
const words = document.querySelectorAll("#words .word");
993-
const letters = words[activeWordElementIndex]?.querySelectorAll("letter");
994+
const letters = words[wordIndex]?.querySelectorAll("letter");
994995
if (!letters) return;
995996
for (let i = 0; i < TestInput.input.current.length; i++) {
996997
const letter = letters[i] as HTMLElement;
@@ -1045,9 +1046,10 @@ export function lineJump(currentTop: number): void {
10451046
) {
10461047
const hideBound = currentTop;
10471048

1049+
const wordIndex = TestState.activeWordIndex - activeWordElementOffset;
10481050
const toHide: JQuery[] = [];
10491051
const wordElements = $("#words .word");
1050-
for (let i = 0; i < activeWordElementIndex; i++) {
1052+
for (let i = 0; i < wordIndex; i++) {
10511053
const el = $(wordElements[i] as HTMLElement);
10521054
if (el.hasClass("hidden")) continue;
10531055
const forWordTop = Math.floor((el[0] as HTMLElement).offsetTop);
@@ -1114,18 +1116,18 @@ export function lineJump(currentTop: number): void {
11141116
currentLinesAnimating = 0;
11151117
activeWordTop = (
11161118
document.querySelectorAll("#words .word")?.[
1117-
activeWordElementIndex
1119+
wordIndex
11181120
] as HTMLElement
11191121
)?.offsetTop;
11201122

1121-
activeWordElementIndex -= toHide.length;
1123+
activeWordElementOffset += toHide.length;
11221124
lineTransition = false;
11231125
toHide.forEach((el) => el.remove());
11241126
$("#words").css("marginTop", "0");
11251127
});
11261128
} else {
11271129
toHide.forEach((el) => el.remove());
1128-
activeWordElementIndex -= toHide.length;
1130+
activeWordElementOffset += toHide.length;
11291131
$("#paceCaret").css({
11301132
top:
11311133
(document.querySelector("#paceCaret") as HTMLElement).offsetTop -

frontend/src/ts/ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const debouncedEvent = debounce(250, () => {
106106
} else {
107107
const word =
108108
document.querySelectorAll<HTMLElement>("#words .word")[
109-
TestUI.activeWordElementIndex - 1
109+
TestState.activeWordIndex - TestUI.activeWordElementOffset - 1
110110
];
111111
if (word) {
112112
const currentTop: number = Math.floor(word.offsetTop);

0 commit comments

Comments
 (0)