Skip to content

Commit 9ae2329

Browse files
Miodecnadalaba
andauthored
refactor: caret rewrite (@Miodec) (monkeytypegame#6955)
Moves common logic to a new Caret class with single responsibility principles. --------- Co-authored-by: Nad Alaba <[email protected]>
1 parent fa9c50c commit 9ae2329

File tree

12 files changed

+756
-535
lines changed

12 files changed

+756
-535
lines changed

frontend/__tests__/controllers/preset-controller.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ describe("PresetController", () => {
1212
vi.mock("../../src/ts/test/test-logic", () => ({
1313
restart: vi.fn(),
1414
}));
15+
vi.mock("../../src/ts/test/pace-caret", () => ({
16+
//
17+
}));
1518
const dbGetSnapshotMock = vi.spyOn(DB, "getSnapshot");
1619
const configApplyMock = vi.spyOn(UpdateConfig, "apply");
1720
const configSaveFullConfigMock = vi.spyOn(

frontend/src/html/popups.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<button class="showRealWordsInput">show real words input</button>
6868
<button class="xpBarTest">xp bar test</button>
6969
<button class="toggleFakeChartData">toggle fake chart data</button>
70+
<button class="toggleCaretDebug">toggle caret debug</button>
7071
</div>
7172
</dialog>
7273

frontend/src/styles/caret.scss

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020

2121
#caret,
2222
#paceCaret {
23+
&.debug {
24+
outline: 1px solid white;
25+
&.hidden {
26+
opacity: 0.5 !important;
27+
display: block !important;
28+
}
29+
}
30+
2331
&.off {
2432
width: 0;
2533
}
@@ -50,15 +58,15 @@
5058
width: 0.5em;
5159
border-radius: 0;
5260
z-index: -1;
53-
border-radius: calc(var(--roundness) / 4);
61+
border-radius: 0.05em;
5462
}
5563

5664
&.outline {
5765
@extend #caret, .block;
5866
animation-name: none;
5967
background: transparent;
6068
border: 0.05em solid var(--caret-color);
61-
border-radius: calc(var(--roundness) / 4);
69+
border-radius: 0.05em;
6270
}
6371

6472
&.underline {

frontend/src/styles/test.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@
246246
}
247247
}
248248

249+
&.debugCaret {
250+
outline: 1px solid var(--sub-color);
251+
}
252+
&.debugCaretTarget {
253+
outline: 2px solid red;
254+
}
255+
&.debugCaretTarget2 {
256+
outline: 2px solid yellow;
257+
}
249258
&.correct {
250259
color: var(--correct-letter-color);
251260
animation: var(--correct-letter-animation);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function backspaceToPrevious(): void {
178178
}
179179
}
180180

181-
void Caret.updatePosition();
181+
Caret.updatePosition();
182182
Replay.addReplayEvent("backWord");
183183
}
184184

@@ -258,7 +258,7 @@ async function handleSpace(): Promise<void> {
258258
dontInsertSpace = false;
259259
Replay.addReplayEvent("incorrectLetter", "_");
260260
void TestUI.updateActiveWordLetters();
261-
void Caret.updatePosition();
261+
Caret.updatePosition();
262262
}
263263
return;
264264
}
@@ -346,7 +346,7 @@ async function handleSpace(): Promise<void> {
346346
}
347347
} //end of line wrap
348348

349-
void Caret.updatePosition();
349+
Caret.updatePosition();
350350

351351
// enable if i decide that auto tab should also work after a space
352352
// if (
@@ -565,7 +565,7 @@ async function handleChar(
565565
) {
566566
TestInput.input.current = resultingWord;
567567
void TestUI.updateActiveWordLetters();
568-
void Caret.updatePosition();
568+
Caret.updatePosition();
569569
return;
570570
}
571571

@@ -768,7 +768,7 @@ async function handleChar(
768768
}, 0);
769769

770770
if (char !== "\n") {
771-
void Caret.updatePosition();
771+
Caret.updatePosition();
772772
}
773773
}
774774

@@ -1377,7 +1377,7 @@ $("#wordsInput").on("input", async (event) => {
13771377
}
13781378

13791379
void TestUI.updateActiveWordLetters();
1380-
void Caret.updatePosition();
1380+
Caret.updatePosition();
13811381
if (!CompositionState.getComposing()) {
13821382
const keyStroke = event?.originalEvent as InputEvent;
13831383
if (keyStroke.inputType === "deleteWordBackward") {

frontend/src/ts/modals/dev-options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { signIn } from "../auth";
77
import * as Loader from "../elements/loader";
88
import { update } from "../elements/xp-bar";
99
import { toggleUserFakeChartData } from "../test/result";
10+
import { toggleCaretDebug } from "../utils/caret";
1011

1112
let mediaQueryDebugLevel = 0;
1213

@@ -89,6 +90,9 @@ async function setup(modalEl: HTMLElement): Promise<void> {
8990
?.addEventListener("click", () => {
9091
toggleUserFakeChartData();
9192
});
93+
modalEl.querySelector(".toggleCaretDebug")?.addEventListener("click", () => {
94+
toggleCaretDebug();
95+
});
9296
}
9397

9498
const modal = new AnimatedModal({

0 commit comments

Comments
 (0)