Skip to content

Commit

Permalink
refactor: rename some functions and variables for easier understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
Miodec committed Aug 28, 2024
1 parent 0d5ff3e commit 9eb927b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
36 changes: 18 additions & 18 deletions frontend/src/ts/controllers/input-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function backspaceToPrevious(): void {

if (
TestInput.input.history.length === 0 ||
TestUI.currentWordElementIndex === 0
TestUI.activeWordElementIndex === 0
) {
return;
}
Expand All @@ -137,7 +137,7 @@ function backspaceToPrevious(): void {
"incorrect"
);
if (Config.stopOnError === "letter" && incorrectLetterBackspaced) {
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
}

TestInput.input.current = TestInput.input.popHistory();
Expand All @@ -149,10 +149,10 @@ function backspaceToPrevious(): void {
setWordsInput(" " + TestInput.input.current + " ");
}
TestWords.words.decreaseCurrentIndex();
TestUI.setCurrentWordElementIndex(TestUI.currentWordElementIndex - 1);
TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex - 1);
TestUI.updateActiveElement(true);
Funbox.toggleScript(TestWords.words.getCurrent());
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();

if (Config.mode === "zen") {
TimerProgress.update();
Expand Down Expand Up @@ -217,7 +217,7 @@ async function handleSpace(): Promise<void> {
TestInput.incrementAccuracy(isWordCorrect);
if (isWordCorrect) {
if (Config.stopOnError === "letter") {
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
}
PaceCaret.handleSpace(true, currentWord);
TestInput.input.pushHistory();
Expand Down Expand Up @@ -259,18 +259,18 @@ async function handleSpace(): Promise<void> {
if (Config.stopOnError === "word") {
dontInsertSpace = false;
Replay.addReplayEvent("incorrectLetter", "_");
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
void Caret.updatePosition();
}
return;
}
PaceCaret.handleSpace(false, currentWord);
if (Config.blindMode) {
if (Config.highlightMode !== "off") {
TestUI.highlightAllLettersAsCorrect(TestUI.currentWordElementIndex);
TestUI.highlightAllLettersAsCorrect(TestUI.activeWordElementIndex);
}
} else {
TestUI.highlightBadWord(TestUI.currentWordElementIndex);
TestUI.highlightBadWord(TestUI.activeWordElementIndex);
}
TestInput.input.pushHistory();
TestWords.words.increaseCurrentIndex();
Expand Down Expand Up @@ -328,7 +328,7 @@ async function handleSpace(): Promise<void> {
) {
await TestLogic.addWord();
}
TestUI.setCurrentWordElementIndex(TestUI.currentWordElementIndex + 1);
TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex + 1);
TestUI.updateActiveElement();
void Caret.updatePosition();

Expand All @@ -340,14 +340,14 @@ async function handleSpace(): Promise<void> {
) {
const currentTop: number = Math.floor(
document.querySelectorAll<HTMLElement>("#words .word")[
TestUI.currentWordElementIndex - 1
TestUI.activeWordElementIndex - 1
]?.offsetTop ?? 0
);
let nextTop: number;
try {
nextTop = Math.floor(
document.querySelectorAll<HTMLElement>("#words .word")[
TestUI.currentWordElementIndex
TestUI.activeWordElementIndex
]?.offsetTop ?? 0
);
} catch (e) {
Expand Down Expand Up @@ -578,7 +578,7 @@ function handleChar(
!Config.language.startsWith("korean")
) {
TestInput.input.current = resultingWord;
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
void Caret.updatePosition();
return;
}
Expand Down Expand Up @@ -659,7 +659,7 @@ function handleChar(
!thisCharCorrect
) {
if (!Config.blindMode) {
void TestUI.updateWordElement(TestInput.input.current + char);
void TestUI.updateActiveWordLetters(TestInput.input.current + char);
}
return;
}
Expand Down Expand Up @@ -727,7 +727,7 @@ function handleChar(
const activeWordTopBeforeJump = document.querySelector<HTMLElement>(
"#words .word.active"
)?.offsetTop as number;
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();

const newActiveTop = document.querySelector<HTMLElement>(
"#words .word.active"
Expand All @@ -741,13 +741,13 @@ function handleChar(
if (Config.mode === "zen") {
const currentTop = Math.floor(
document.querySelectorAll<HTMLElement>("#words .word")[
TestUI.currentWordElementIndex - 1
TestUI.activeWordElementIndex - 1
]?.offsetTop ?? 0
);
if (!Config.showAllLines) TestUI.lineJump(currentTop);
} else {
TestInput.input.current = TestInput.input.current.slice(0, -1);
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
}
}

Expand Down Expand Up @@ -1343,7 +1343,7 @@ $("#wordsInput").on("input", (event) => {
TestInput.input.current = inputValue;
}

void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
void Caret.updatePosition();
if (!CompositionState.getComposing()) {
const keyStroke = event?.originalEvent as InputEvent;
Expand Down Expand Up @@ -1381,7 +1381,7 @@ $("#wordsInput").on("input", (event) => {

const stateafter = CompositionState.getComposing();
if (statebefore !== stateafter) {
void TestUI.updateWordElement();
void TestUI.updateActiveWordLetters();
}

// force caret at end of input
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/test/pace-caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export async function update(expectedStepEnd: number): Promise<void> {
try {
const newIndex =
settings.currentWordIndex -
(TestWords.words.currentIndex - TestUI.currentWordElementIndex);
(TestWords.words.currentIndex - TestUI.activeWordElementIndex);
const word = document.querySelectorAll("#words .word")[
newIndex
] as HTMLElement;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export async function init(): Promise<void> {
MonkeyPower.reset();
Replay.stopReplayRecording();
TestWords.words.reset();
TestUI.setCurrentWordElementIndex(0);
TestUI.setActiveWordElementIndex(0);
TestInput.input.resetHistory();
TestInput.input.resetCurrent();

Expand Down
43 changes: 22 additions & 21 deletions frontend/src/ts/test/test-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
if (eventKey === "burstHeatmap") void applyBurstHeatmap();
});

export let currentWordElementIndex = 0;
export let activeWordElementIndex = 0;
export let resultVisible = false;
export let activeWordTop = 0;
export let testRestarting = false;
Expand All @@ -207,8 +207,8 @@ export function setResultVisible(val: boolean): void {
resultVisible = val;
}

export function setCurrentWordElementIndex(val: number): void {
currentWordElementIndex = val;
export function setActiveWordElementIndex(val: number): void {
activeWordElementIndex = val;
}

export function setActiveWordTop(val: number): void {
Expand All @@ -234,7 +234,7 @@ export function setResultCalculating(val: boolean): void {

export function reset(): void {
currentTestLine = 0;
currentWordElementIndex = 0;
activeWordElementIndex = 0;
}

export function focusWords(): void {
Expand All @@ -258,19 +258,19 @@ export function updateActiveElement(
} else if (active !== null && !initial) {
active.classList.remove("active");
}
const activeWord =
document.querySelectorAll("#words .word")[currentWordElementIndex];
const newActiveWord = document.querySelectorAll("#words .word")[
activeWordElementIndex
] as HTMLElement | undefined;

if (activeWord == undefined) {
if (newActiveWord == undefined) {
throw new Error("activeWord is undefined - can't update active element");
}

activeWord.classList.add("active");
activeWord.classList.remove("error");
activeWord.classList.remove("typed");
newActiveWord.classList.add("active");
newActiveWord.classList.remove("error");
newActiveWord.classList.remove("typed");

activeWordTop = (document.querySelector("#words .active") as HTMLElement)
.offsetTop;
activeWordTop = newActiveWord.offsetTop;

if (!initial && shouldUpdateWordsInputPosition()) {
void updateWordsInputPosition();
Expand Down Expand Up @@ -775,7 +775,9 @@ export async function screenshot(): Promise<void> {
}, 3000);
}

export async function updateWordElement(inputOverride?: string): Promise<void> {
export async function updateActiveWordLetters(
inputOverride?: string
): Promise<void> {
const input = inputOverride ?? TestInput.input.current;
const wordAtIndex = document.querySelector(
"#words .word.active"
Expand Down Expand Up @@ -937,8 +939,8 @@ export function scrollTape(): void {
let fullWordsWidth = 0;
const toHide: JQuery[] = [];
let widthToHide = 0;
if (currentWordElementIndex > 0) {
for (let i = 0; i < currentWordElementIndex; i++) {
if (activeWordElementIndex > 0) {
for (let i = 0; i < activeWordElementIndex; i++) {
const word = document.querySelectorAll("#words .word")[i] as HTMLElement;
fullWordsWidth += $(word).outerWidth(true) ?? 0;
const forWordLeft = Math.floor(word.offsetLeft);
Expand All @@ -950,7 +952,7 @@ export function scrollTape(): void {
}
}
if (toHide.length > 0) {
currentWordElementIndex -= toHide.length;
activeWordElementIndex -= toHide.length;
toHide.forEach((e) => e.remove());
fullWordsWidth -= widthToHide;
const currentMargin = parseInt($("#words").css("margin-left"), 10);
Expand All @@ -961,8 +963,7 @@ export function scrollTape(): void {
if (Config.tapeMode === "letter") {
if (TestInput.input.current.length > 0) {
const words = document.querySelectorAll("#words .word");
const letters =
words[currentWordElementIndex]?.querySelectorAll("letter");
const letters = words[activeWordElementIndex]?.querySelectorAll("letter");
if (!letters) return;
for (let i = 0; i < TestInput.input.current.length; i++) {
const letter = letters[i] as HTMLElement;
Expand Down Expand Up @@ -1017,7 +1018,7 @@ export function lineJump(currentTop: number): void {

const toHide: JQuery[] = [];
const wordElements = $("#words .word");
for (let i = 0; i < currentWordElementIndex; i++) {
for (let i = 0; i < activeWordElementIndex; i++) {
const el = $(wordElements[i] as HTMLElement);
if (el.hasClass("hidden")) continue;
const forWordTop = Math.floor((el[0] as HTMLElement).offsetTop);
Expand Down Expand Up @@ -1086,14 +1087,14 @@ export function lineJump(currentTop: number): void {
document.querySelector("#words .active") as HTMLElement
).offsetTop;

currentWordElementIndex -= toHide.length;
activeWordElementIndex -= toHide.length;
lineTransition = false;
toHide.forEach((el) => el.remove());
$("#words").css("marginTop", "0");
});
} else {
toHide.forEach((el) => el.remove());
currentWordElementIndex -= toHide.length;
activeWordElementIndex -= toHide.length;
$("#paceCaret").css({
top:
(document.querySelector("#paceCaret") as HTMLElement).offsetTop -
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const debouncedEvent = debounce(250, () => {
} else {
const word =
document.querySelectorAll<HTMLElement>("#words .word")[
TestUI.currentWordElementIndex - 1
TestUI.activeWordElementIndex - 1
];
if (word) {
const currentTop: number = Math.floor(word.offsetTop);
Expand Down

0 comments on commit 9eb927b

Please sign in to comment.