Skip to content

Commit 33a6bc0

Browse files
authored
refactor(caret): move active word element offset to test-state and remove .smoothScroller (@nadalaba) (monkeytypegame#6541)
1 parent ced4b6e commit 33a6bc0

File tree

6 files changed

+85
-108
lines changed

6 files changed

+85
-108
lines changed

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

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

124124
const wordElementIndex =
125-
TestState.activeWordIndex - TestUI.activeWordElementOffset;
125+
TestState.activeWordIndex - TestState.removedUIWordCount;
126126

127127
if (TestInput.input.getHistory().length === 0 || wordElementIndex === 0) {
128128
return;
@@ -269,12 +269,12 @@ async function handleSpace(): Promise<void> {
269269
if (Config.blindMode) {
270270
if (Config.highlightMode !== "off") {
271271
TestUI.highlightAllLettersAsCorrect(
272-
TestState.activeWordIndex - TestUI.activeWordElementOffset
272+
TestState.activeWordIndex - TestState.removedUIWordCount
273273
);
274274
}
275275
} else {
276276
TestUI.highlightBadWord(
277-
TestState.activeWordIndex - TestUI.activeWordElementOffset
277+
TestState.activeWordIndex - TestState.removedUIWordCount
278278
);
279279
}
280280
TestInput.input.pushHistory();
@@ -343,14 +343,14 @@ async function handleSpace(): Promise<void> {
343343
if (!Config.showAllLines || shouldLimitToThreeLines) {
344344
const currentTop: number = Math.floor(
345345
document.querySelectorAll<HTMLElement>("#words .word")[
346-
TestState.activeWordIndex - TestUI.activeWordElementOffset - 1
346+
TestState.activeWordIndex - TestState.removedUIWordCount - 1
347347
]?.offsetTop ?? 0
348348
);
349349

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

670670
const activeWord = document.querySelectorAll("#words .word")?.[
671-
TestState.activeWordIndex - TestUI.activeWordElementOffset
671+
TestState.activeWordIndex - TestState.removedUIWordCount
672672
] as HTMLElement;
673673

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

frontend/src/ts/test/caret.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ export function hide(): void {
3535
caret.classList.add("hidden");
3636
}
3737

38-
export function getSpaceWidth(wordElement?: HTMLElement): number {
39-
if (!wordElement)
40-
wordElement = document
41-
.getElementById("words")
42-
?.querySelectorAll(".word")?.[0] as HTMLElement | undefined;
43-
if (!wordElement) return 0;
38+
function getSpaceWidth(wordElement?: HTMLElement): number {
39+
if (!wordElement) {
40+
const el = document.querySelector<HTMLElement>("#words .word");
41+
if (el) {
42+
wordElement = el;
43+
} else {
44+
return 0;
45+
}
46+
}
4447
const wordComputedStyle = window.getComputedStyle(wordElement);
4548
return (
4649
parseInt(wordComputedStyle.marginRight) +
@@ -52,8 +55,7 @@ function getTargetPositionLeft(
5255
fullWidthCaret: boolean,
5356
isLanguageRightToLeft: boolean,
5457
activeWordElement: HTMLElement,
55-
activeWordEmpty: boolean,
56-
currentWordNodeList: NodeListOf<Element>,
58+
currentWordNodeList: NodeListOf<HTMLElement>,
5759
fullWidthCaretWidth: number,
5860
wordLen: number,
5961
inputLen: number
@@ -64,21 +66,15 @@ function getTargetPositionLeft(
6466
if (Config.tapeMode === "off") {
6567
let positionOffsetToWord = 0;
6668

67-
const currentLetter = currentWordNodeList[inputLen] as
68-
| HTMLElement
69-
| undefined;
70-
const lastWordLetter = currentWordNodeList[wordLen - 1] as
71-
| HTMLElement
72-
| undefined;
73-
const lastInputLetter = currentWordNodeList[inputLen - 1] as
74-
| HTMLElement
75-
| undefined;
69+
const currentLetter = currentWordNodeList[inputLen];
70+
const lastWordLetter = currentWordNodeList[wordLen - 1];
71+
const lastInputLetter = currentWordNodeList[inputLen - 1];
7672

7773
if (isLanguageRightToLeft) {
78-
if (inputLen < wordLen && currentLetter) {
74+
if (inputLen <= wordLen && currentLetter) {
75+
// at word beginning in zen mode both lengths are 0, but currentLetter is defined "_"
7976
positionOffsetToWord =
80-
currentLetter?.offsetLeft +
81-
(fullWidthCaret ? 0 : fullWidthCaretWidth);
77+
currentLetter.offsetLeft + (fullWidthCaret ? 0 : fullWidthCaretWidth);
8278
} else if (!invisibleExtraLetters) {
8379
positionOffsetToWord =
8480
(lastInputLetter?.offsetLeft ?? 0) -
@@ -90,7 +86,7 @@ function getTargetPositionLeft(
9086
}
9187
} else {
9288
if (inputLen < wordLen && currentLetter) {
93-
positionOffsetToWord = currentLetter?.offsetLeft;
89+
positionOffsetToWord = currentLetter.offsetLeft;
9490
} else if (!invisibleExtraLetters) {
9591
positionOffsetToWord =
9692
(lastInputLetter?.offsetLeft ?? 0) +
@@ -102,8 +98,6 @@ function getTargetPositionLeft(
10298
}
10399
}
104100
result = activeWordElement.offsetLeft + positionOffsetToWord;
105-
if (activeWordEmpty && isLanguageRightToLeft)
106-
result += activeWordElement.offsetWidth;
107101
} else {
108102
const wordsWrapperWidth =
109103
$(document.querySelector("#wordsWrapper") as HTMLElement).width() ?? 0;
@@ -150,22 +144,18 @@ export async function updatePosition(noAnim = false): Promise<void> {
150144
let wordLen = splitIntoCharacters(TestWords.words.getCurrent()).length;
151145
const inputLen = splitIntoCharacters(TestInput.input.current).length;
152146
if (Config.mode === "zen") wordLen = inputLen;
153-
const activeWordEl = document?.querySelector("#words .active") as HTMLElement;
154-
let activeWordEmpty = false;
155-
if (Config.mode === "zen") {
156-
wordLen = inputLen;
157-
if (inputLen === 0) activeWordEmpty = true;
158-
}
159-
160-
const currentWordNodeList = activeWordEl?.querySelectorAll("letter");
147+
const activeWordEl =
148+
document.querySelectorAll<HTMLElement>("#words .word")[
149+
TestState.activeWordIndex - TestState.removedUIWordCount
150+
];
151+
if (!activeWordEl) return;
152+
153+
const currentWordNodeList =
154+
activeWordEl.querySelectorAll<HTMLElement>("letter");
161155
if (!currentWordNodeList?.length) return;
162156

163-
const currentLetter = currentWordNodeList[inputLen] as
164-
| HTMLElement
165-
| undefined;
166-
const lastWordLetter = currentWordNodeList[wordLen - 1] as
167-
| HTMLElement
168-
| undefined;
157+
const currentLetter = currentWordNodeList[inputLen];
158+
const lastWordLetter = currentWordNodeList[wordLen - 1];
169159

170160
const currentLanguage = await JSONData.getCurrentLanguage(Config.language);
171161
const isLanguageRightToLeft = currentLanguage.rightToLeft;
@@ -187,11 +177,11 @@ export async function updatePosition(noAnim = false): Promise<void> {
187177
}
188178

189179
let letterWidth = currentLetter?.offsetWidth;
190-
if (letterWidth === undefined || activeWordEmpty) {
180+
if (letterWidth === undefined || wordLen === 0) {
181+
// at word beginning in zen mode current letter is defined "_" but wordLen is 0
191182
letterWidth = getSpaceWidth(activeWordEl);
192183
} else if (letterWidth === 0) {
193184
// current letter is a zero-width character e.g, diacritics)
194-
letterWidth = 0;
195185
for (let i = inputLen; i >= 0; i--) {
196186
letterWidth = (currentWordNodeList[i] as HTMLElement)?.offsetWidth;
197187
if (letterWidth) break;
@@ -203,23 +193,19 @@ export async function updatePosition(noAnim = false): Promise<void> {
203193
fullWidthCaret,
204194
isLanguageRightToLeft,
205195
activeWordEl,
206-
activeWordEmpty,
207196
currentWordNodeList,
208197
letterWidth,
209198
wordLen,
210199
inputLen
211200
);
212201
const newLeft = letterPosLeft - (fullWidthCaret ? 0 : caretWidth / 2);
213202

214-
let smoothlinescroll = $("#words .smoothScroller").height();
215-
if (smoothlinescroll === undefined) smoothlinescroll = 0;
216-
217203
const jqcaret = $(caret);
218204

219205
jqcaret.css("display", "block"); //for some goddamn reason adding width animation sets the display to none ????????
220206

221207
const animation: { top: number; left: number; width?: string } = {
222-
top: newTop - smoothlinescroll,
208+
top: newTop - TestState.lineScrollDistance,
223209
left: newLeft,
224210
};
225211

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

Lines changed: 2 additions & 6 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 - TestUI.activeWordElementOffset;
203+
const newIndex = settings.currentWordIndex - TestState.removedUIWordCount;
205204
const word = document.querySelectorAll("#words .word")[
206205
newIndex
207206
] as HTMLElement;
@@ -257,11 +256,8 @@ export async function update(expectedStepEnd: number): Promise<void> {
257256
const duration = expectedStepEnd - performance.now();
258257

259258
if (newTop !== undefined) {
260-
let smoothlinescroll = $("#words .smoothScroller").height();
261-
if (smoothlinescroll === undefined) smoothlinescroll = 0;
262-
263259
$("#paceCaret").css({
264-
top: newTop - smoothlinescroll,
260+
top: newTop - TestState.lineScrollDistance,
265261
});
266262

267263
if (Config.smoothCaret !== "off") {

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

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export let bailedOut = false;
99
export let selectedQuoteId = 1;
1010
export let activeWordIndex = 0;
1111
export let testInitSuccess = true;
12+
export let removedUIWordCount = 0;
13+
export let lineScrollDistance = 0;
1214

1315
export function setRepeated(tf: boolean): void {
1416
isRepeated = tf;
@@ -53,3 +55,15 @@ export function decreaseActiveWordIndex(): void {
5355
export function setTestInitSuccess(tf: boolean): void {
5456
testInitSuccess = tf;
5557
}
58+
59+
export function setRemovedUIWordCount(val: number): void {
60+
removedUIWordCount = val;
61+
}
62+
63+
export function incrementRemovedUIWordCount(by: number = 1): void {
64+
removedUIWordCount += by;
65+
}
66+
67+
export function setLineScrollDistance(val: number): void {
68+
lineScrollDistance = val;
69+
}

0 commit comments

Comments
 (0)