Skip to content

Commit 1479a26

Browse files
committed
refactor: move resultVisible state to TestState
1 parent 2fc2c7b commit 1479a26

File tree

10 files changed

+44
-43
lines changed

10 files changed

+44
-43
lines changed

frontend/src/ts/commandline/lists/result-screen.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as TestUI from "../../test/test-ui";
33
import * as PractiseWordsModal from "../../modals/practise-words";
44
import * as Notifications from "../../elements/notifications";
55
import * as TestInput from "../../test/test-input";
6+
import * as TestState from "../../test/test-state";
67
import * as TestWords from "../../test/test-words";
78
import Config from "../../config";
89
import * as PractiseWords from "../../test/practise-words";
@@ -53,7 +54,7 @@ const commands: Command[] = [
5354
alias: "restart start begin type test typing",
5455
icon: "fa-chevron-right",
5556
available: (): boolean => {
56-
return TestUI.resultVisible;
57+
return TestState.resultVisible;
5758
},
5859
exec: (): void => {
5960
TestLogic.restart();
@@ -69,7 +70,7 @@ const commands: Command[] = [
6970
});
7071
},
7172
available: (): boolean => {
72-
return TestUI.resultVisible;
73+
return TestState.resultVisible;
7374
},
7475
},
7576
{
@@ -78,7 +79,7 @@ const commands: Command[] = [
7879
icon: "fa-exclamation-triangle",
7980
subgroup: practiceSubgroup,
8081
available: (): boolean => {
81-
return TestUI.resultVisible;
82+
return TestState.resultVisible;
8283
},
8384
},
8485
{
@@ -89,7 +90,7 @@ const commands: Command[] = [
8990
TestUI.toggleResultWords();
9091
},
9192
available: (): boolean => {
92-
return TestUI.resultVisible;
93+
return TestState.resultVisible;
9394
},
9495
},
9596
{
@@ -103,7 +104,7 @@ const commands: Command[] = [
103104
}, 500);
104105
},
105106
available: (): boolean => {
106-
return TestUI.resultVisible;
107+
return TestState.resultVisible;
107108
},
108109
},
109110
{
@@ -117,7 +118,7 @@ const commands: Command[] = [
117118
}, 500);
118119
},
119120
available: (): boolean => {
120-
return TestUI.resultVisible;
121+
return TestState.resultVisible;
121122
},
122123
},
123124
{
@@ -141,7 +142,7 @@ const commands: Command[] = [
141142
);
142143
},
143144
available: (): boolean => {
144-
return TestUI.resultVisible;
145+
return TestState.resultVisible;
145146
},
146147
},
147148
];

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ async function handleChar(
447447
charIndex: number,
448448
realInputValue?: string
449449
): Promise<void> {
450-
if (TestUI.resultCalculating || TestUI.resultVisible) {
450+
if (TestUI.resultCalculating || TestState.resultVisible) {
451451
return;
452452
}
453453

@@ -802,7 +802,7 @@ async function handleTab(
802802

803803
event.preventDefault();
804804
// insert tab character if needed (only during the test)
805-
if (!TestUI.resultVisible && shouldInsertTabCharacter) {
805+
if (!TestState.resultVisible && shouldInsertTabCharacter) {
806806
await handleChar("\t", TestInput.input.current.length);
807807
setWordsInput(" " + TestInput.input.current);
808808
return;
@@ -828,7 +828,7 @@ async function handleTab(
828828
}
829829

830830
// insert tab character if needed (only during the test)
831-
if (!TestUI.resultVisible && shouldInsertTabCharacter) {
831+
if (!TestState.resultVisible && shouldInsertTabCharacter) {
832832
event.preventDefault();
833833
await handleChar("\t", TestInput.input.current.length);
834834
setWordsInput(" " + TestInput.input.current);
@@ -844,7 +844,7 @@ async function handleTab(
844844

845845
//only special handlig on the test page
846846
if (ActivePage.get() !== "test") return;
847-
if (TestUI.resultVisible) return;
847+
if (TestState.resultVisible) return;
848848

849849
// insert tab character if needed
850850
if (shouldInsertTabCharacter) {
@@ -870,7 +870,7 @@ $("#wordsInput").on("keydown", (event) => {
870870
pageTestActive &&
871871
!commandLineVisible &&
872872
!popupVisible &&
873-
!TestUI.resultVisible &&
873+
!TestState.resultVisible &&
874874
event.key !== "Enter" &&
875875
!awaitingNextWord &&
876876
TestState.testInitSuccess;
@@ -911,7 +911,7 @@ $(document).on("keydown", async (event) => {
911911
pageTestActive &&
912912
!commandLineVisible &&
913913
!popupVisible &&
914-
!TestUI.resultVisible &&
914+
!TestState.resultVisible &&
915915
(wordsFocused || event.key !== "Enter") &&
916916
!awaitingNextWord;
917917

@@ -983,7 +983,7 @@ $(document).on("keydown", async (event) => {
983983
return;
984984
}
985985

986-
if (TestUI.resultVisible) {
986+
if (TestState.resultVisible) {
987987
TestLogic.restart({
988988
event,
989989
});
@@ -1258,7 +1258,7 @@ $("#wordsInput").on("keyup", (event) => {
12581258

12591259
if (IgnoredKeys.includes(event.key)) return;
12601260

1261-
if (TestUI.resultVisible) return;
1261+
if (TestState.resultVisible) return;
12621262
});
12631263

12641264
$("#wordsInput").on("beforeinput", (event) => {

frontend/src/ts/controllers/pw-ad-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Config from "../config";
77
import * as ActivePage from "../states/active-page";
8-
import * as TestUI from "../test/test-ui";
8+
import * as TestState from "../test/test-state";
99

1010
// Step 1: Create the Ramp Object, NOTE: selector id needed for tagged units only
1111
const resultUnits = [
@@ -207,7 +207,7 @@ function getUnits(): unknown {
207207

208208
export async function reinstate(): boolean {
209209
if (!rampReady) return;
210-
if (ActivePage.get() === "test" && !TestUI.resultVisible) {
210+
if (ActivePage.get() === "test" && !TestState.resultVisible) {
211211
ramp.destroyUnits("all");
212212
return;
213213
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as TestWords from "./test-words";
2-
import * as TestUI from "./test-ui";
32
import Config from "../config";
43
import * as DB from "../db";
54
import * as SlowTimer from "../states/slow-timer";
@@ -146,7 +145,7 @@ export async function init(): Promise<void> {
146145
}
147146

148147
export async function update(expectedStepEnd: number): Promise<void> {
149-
if (settings === null || !TestState.isActive || TestUI.resultVisible) {
148+
if (settings === null || !TestState.isActive || TestState.resultVisible) {
150149
return;
151150
}
152151
// if ($("#paceCaret").hasClass("hidden")) {

frontend/src/ts/test/result.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { Language } from "@monkeytype/schemas/languages";
4444
import { canQuickRestart as canQuickRestartFn } from "../utils/quick-restart";
4545
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
4646
import { z } from "zod";
47+
import * as TestState from "./test-state";
4748

4849
let result: CompletedEvent;
4950
let maxChartVal: number;
@@ -57,7 +58,7 @@ let quoteId = "";
5758
export function toggleSmoothedBurst(): void {
5859
useSmoothedBurst = !useSmoothedBurst;
5960
Notifications.add(useSmoothedBurst ? "on" : "off", 1);
60-
if (TestUI.resultVisible) {
61+
if (TestState.resultVisible) {
6162
void updateGraph().then(() => {
6263
ChartController.result.update("resize");
6364
});
@@ -67,7 +68,7 @@ export function toggleSmoothedBurst(): void {
6768
export function toggleUserFakeChartData(): void {
6869
useFakeChartData = !useFakeChartData;
6970
Notifications.add(useFakeChartData ? "on" : "off", 1);
70-
if (TestUI.resultVisible) {
71+
if (TestState.resultVisible) {
7172
void updateGraph().then(() => {
7273
ChartController.result.update("resize");
7374
});
@@ -1314,7 +1315,7 @@ $(".pageTest #favoriteQuoteButton").on("click", async () => {
13141315
ConfigEvent.subscribe(async (eventKey) => {
13151316
if (
13161317
["typingSpeedUnit", "startGraphsAtZero"].includes(eventKey) &&
1317-
TestUI.resultVisible
1318+
TestState.resultVisible
13181319
) {
13191320
resultAnnotation = [];
13201321

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export function restart(options = {} as RestartOptions): void {
294294
TestUI.reset();
295295
CompositionState.setComposing(false);
296296

297-
if (TestUI.resultVisible) {
297+
if (TestState.resultVisible) {
298298
if (Config.randomTheme !== "off") {
299299
void ThemeController.randomizeTheme();
300300
}
@@ -306,14 +306,14 @@ export function restart(options = {} as RestartOptions): void {
306306
}
307307

308308
let el = null;
309-
if (TestUI.resultVisible) {
309+
if (TestState.resultVisible) {
310310
//results are being displayed
311311
el = $("#result");
312312
} else {
313313
//words are being displayed
314314
el = $("#typingTest");
315315
}
316-
TestUI.setResultVisible(false);
316+
TestState.setResultVisible(false);
317317
TestState.setTestRestarting(true);
318318
el.stop(true, true).animate(
319319
{
@@ -898,7 +898,7 @@ export async function finish(difficultyFailed = false): Promise<void> {
898898
TestStats.setEnd(TestInput.keypressTimings.spacing.last);
899899
}
900900

901-
TestUI.setResultVisible(true);
901+
TestState.setResultVisible(true);
902902
TestState.setActive(false);
903903
Replay.stopReplayRecording();
904904
Caret.hide();
@@ -1304,7 +1304,7 @@ async function saveResult(
13041304
void XPBar.update(
13051305
snapxp,
13061306
data.xp,
1307-
TestUI.resultVisible ? data.xpBreakdown : undefined
1307+
TestState.resultVisible ? data.xpBreakdown : undefined
13081308
);
13091309
dataToSave.xp = data.xp;
13101310
}

frontend/src/ts/test/test-screenshot.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { getActiveFunboxesWithFunction } from "./funbox/list";
66
import * as DB from "../db";
77
import * as ThemeColors from "../elements/theme-colors";
88
import { format } from "date-fns/format";
9-
import * as TestUI from "./test-ui";
109
import * as ActivePage from "../states/active-page";
1110
import { getHtmlByUserFlags } from "../controllers/user-flag-controller";
1211
import * as Notifications from "../elements/notifications";
1312
import { convertRemToPixels } from "../utils/numbers";
13+
import * as TestState from "./test-state";
1414

1515
let revealReplay = false;
1616
let revertCookie = false;
@@ -364,15 +364,15 @@ $(".pageTest").on("click", "#saveScreenshotButton", (event) => {
364364
});
365365

366366
$(document).on("keydown", (event) => {
367-
if (!(TestUI.resultVisible && ActivePage.get() === "test")) return;
367+
if (!(TestState.resultVisible && ActivePage.get() === "test")) return;
368368
if (event.key !== "Shift") return;
369369
$("#result #saveScreenshotButton i")
370370
.removeClass("far fa-image")
371371
.addClass("fas fa-download");
372372
});
373373

374374
$(document).on("keyup", (event) => {
375-
if (!(TestUI.resultVisible && ActivePage.get() === "test")) return;
375+
if (!(TestState.resultVisible && ActivePage.get() === "test")) return;
376376
if (event.key !== "Shift") return;
377377
$("#result #saveScreenshotButton i")
378378
.removeClass("fas fa-download")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export let lineScrollDistance = 0;
1414
export let isLanguageRightToLeft = false;
1515
export let isDirectionReversed = false;
1616
export let testRestarting = false;
17+
export let resultVisible = false;
1718

1819
export function setRepeated(tf: boolean): void {
1920
isRepeated = tf;
@@ -85,3 +86,7 @@ export function setTestRestarting(val: boolean): void {
8586
restartingResolve();
8687
}
8788
}
89+
90+
export function setResultVisible(val: boolean): void {
91+
resultVisible = val;
92+
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,11 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
116116
if (eventKey === "burstHeatmap") void applyBurstHeatmap();
117117
});
118118

119-
export let resultVisible = false;
120119
export let activeWordTop = 0;
121120
export let lineTransition = false;
122121
export let currentTestLine = 0;
123122
export let resultCalculating = false;
124123

125-
export function setResultVisible(val: boolean): void {
126-
resultVisible = val;
127-
}
128-
129124
export function setActiveWordTop(val: number): void {
130125
activeWordTop = val;
131126
}
@@ -331,7 +326,7 @@ async function joinOverlappingHints(
331326
async function updateHintsPosition(): Promise<void> {
332327
if (
333328
ActivePage.get() !== "test" ||
334-
resultVisible ||
329+
TestState.resultVisible ||
335330
Config.indicateTypos !== "below"
336331
)
337332
return;
@@ -585,7 +580,7 @@ export async function centerActiveLine(): Promise<void> {
585580
}
586581

587582
export function updateWordsWrapperHeight(force = false): void {
588-
if (ActivePage.get() !== "test" || resultVisible) return;
583+
if (ActivePage.get() !== "test" || TestState.resultVisible) return;
589584
if (!force && Config.mode !== "custom") return;
590585
const wrapperEl = document.getElementById("wordsWrapper") as HTMLElement;
591586
const outOfFocusEl = document.querySelector(
@@ -904,7 +899,7 @@ export async function scrollTape(
904899
noRemove = false,
905900
afterCompleteFn?: () => void
906901
): Promise<void> {
907-
if (ActivePage.get() !== "test" || resultVisible) return;
902+
if (ActivePage.get() !== "test" || TestState.resultVisible) return;
908903

909904
await centeringActiveLine;
910905

@@ -1394,7 +1389,7 @@ async function loadWordsHistory(): Promise<boolean> {
13941389
}
13951390

13961391
export function toggleResultWords(noAnimation = false): void {
1397-
if (resultVisible) {
1392+
if (TestState.resultVisible) {
13981393
ResultWordHighlight.updateToggleWordsHistoryTime();
13991394
if ($("#resultWordsHistory").stop(true, true).hasClass("hidden")) {
14001395
//show
@@ -1699,7 +1694,7 @@ $(".pageTest #result #wpmChart").on("mouseenter", () => {
16991694
});
17001695

17011696
$(".pageTest #resultWordsHistory").on("mouseenter", ".words .word", (e) => {
1702-
if (resultVisible) {
1697+
if (TestState.resultVisible) {
17031698
const input = $(e.currentTarget).attr("input");
17041699
const burst = parseInt($(e.currentTarget).attr("burst") as string);
17051700
if (input !== undefined) {
@@ -1729,14 +1724,14 @@ addEventListener("resize", () => {
17291724
$("#wordsInput").on("focus", (e) => {
17301725
const wordsFocused = e.target === document.activeElement;
17311726
if (!wordsFocused) return;
1732-
if (!resultVisible && Config.showOutOfFocusWarning) {
1727+
if (!TestState.resultVisible && Config.showOutOfFocusWarning) {
17331728
OutOfFocus.hide();
17341729
}
17351730
Caret.show(true);
17361731
});
17371732

17381733
$("#wordsInput").on("focusout", () => {
1739-
if (!resultVisible && Config.showOutOfFocusWarning) {
1734+
if (!TestState.resultVisible && Config.showOutOfFocusWarning) {
17401735
OutOfFocus.show();
17411736
}
17421737
Caret.hide();

frontend/src/ts/ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ window.addEventListener("beforeunload", (event) => {
9898
});
9999

100100
const debouncedEvent = debounce(250, () => {
101-
if (getActivePage() === "test" && !TestUI.resultVisible) {
101+
if (getActivePage() === "test" && !TestState.resultVisible) {
102102
if (Config.tapeMode !== "off") {
103103
void TestUI.scrollTape();
104104
} else {

0 commit comments

Comments
 (0)