Skip to content

Commit d207f8f

Browse files
authored
Merge branch 'monkeytypegame:master' into master
2 parents f6f61b4 + 0d9a1d9 commit d207f8f

File tree

3 files changed

+63
-31
lines changed

3 files changed

+63
-31
lines changed

frontend/src/ts/input/handlers/insert-text.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ type OnInsertTextParams = {
6262

6363
export async function onInsertText(options: OnInsertTextParams): Promise<void> {
6464
const { now, lastInMultiIndex, isCompositionEnding } = options;
65-
const { inputValue } = getInputElementValue();
6665

6766
if (options.data.length > 1) {
6867
// remove the entire data from the input value
6968
// make sure to not call TestInput.input.syncWithInputElement in here
7069
// it will be updated later in the body of onInsertText
70+
const { inputValue } = getInputElementValue();
7171
setInputElementValue(inputValue.slice(0, -options.data.length));
7272
for (let i = 0; i < options.data.length; i++) {
7373
const char = options.data[i] as string;
@@ -95,19 +95,17 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
9595
return;
9696
}
9797

98+
// input and target word
99+
const testInput = TestInput.input.current;
100+
const currentWord = TestWords.words.getCurrent();
101+
98102
// if the character is visually equal, replace it with the target character
99103
// this ensures all future equivalence checks work correctly
100-
let normalizedData: string | null = null;
101-
const targetChar =
102-
TestWords.words.getCurrent()[TestInput.input.current.length];
103-
if (
104-
targetChar !== undefined &&
105-
areCharactersVisuallyEqual(options.data, targetChar, Config.language)
106-
) {
107-
replaceInputElementLastValueChar(targetChar);
108-
normalizedData = targetChar;
109-
}
110-
104+
const normalizedData = normalizeDataAndUpdateInputIfNeeded(
105+
options.data,
106+
testInput,
107+
currentWord
108+
);
111109
const data = normalizedData ?? options.data;
112110

113111
// start if needed
@@ -118,8 +116,6 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
118116
// helper consts
119117
const lastInMultiOrSingle =
120118
lastInMultiIndex === true || lastInMultiIndex === undefined;
121-
const testInput = TestInput.input.current;
122-
const currentWord = TestWords.words.getCurrent();
123119
const wordIndex = TestState.activeWordIndex;
124120
const charIsSpace = isSpace(data);
125121
const charIsNewline = data === "\n";
@@ -135,7 +131,10 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
135131
// is char correct
136132
const funboxCorrect = findSingleActiveFunboxWithFunction(
137133
"isCharCorrect"
138-
)?.functions.isCharCorrect(data, currentWord[inputValue.length] ?? "");
134+
)?.functions.isCharCorrect(
135+
data,
136+
currentWord[(testInput + data).length - 1] ?? ""
137+
);
139138
const correct =
140139
funboxCorrect ??
141140
isCharCorrect({
@@ -148,7 +147,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
148147
// word navigation check
149148
const noSpaceForce =
150149
isFunboxActiveWithProperty("nospace") &&
151-
TestInput.input.current.length === TestWords.words.getCurrent().length;
150+
(testInput + data).length === TestWords.words.getCurrent().length;
152151
const shouldGoToNextWord =
153152
((charIsSpace || charIsNewline) && !shouldInsertSpace) || noSpaceForce;
154153

@@ -286,6 +285,23 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
286285
}
287286
}
288287

288+
function normalizeDataAndUpdateInputIfNeeded(
289+
data: string,
290+
testInput: string,
291+
currentWord: string
292+
): string | null {
293+
let normalizedData: string | null = null;
294+
const targetChar = currentWord[testInput.length];
295+
if (
296+
targetChar !== undefined &&
297+
areCharactersVisuallyEqual(data, targetChar, Config.language)
298+
) {
299+
replaceInputElementLastValueChar(targetChar);
300+
normalizedData = targetChar;
301+
}
302+
return normalizedData;
303+
}
304+
289305
export async function emulateInsertText(
290306
options: OnInsertTextParams
291307
): Promise<void> {

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,34 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
257257
},
258258
isCharCorrect(char: string, originalChar: string): boolean {
259259
if (
260-
(char === "a" || char === "ArrowLeft" || char === "j") &&
260+
(char === "a" ||
261+
char === "ArrowLeft" ||
262+
char === "j" ||
263+
char === "←") &&
261264
originalChar === "←"
262265
) {
263266
return true;
264267
}
265268
if (
266-
(char === "s" || char === "ArrowDown" || char === "k") &&
269+
(char === "s" ||
270+
char === "ArrowDown" ||
271+
char === "k" ||
272+
char === "↓") &&
267273
originalChar === "↓"
268274
) {
269275
return true;
270276
}
271277
if (
272-
(char === "w" || char === "ArrowUp" || char === "i") &&
278+
(char === "w" || char === "ArrowUp" || char === "i" || char === "↑") &&
273279
originalChar === "↑"
274280
) {
275281
return true;
276282
}
277283
if (
278-
(char === "d" || char === "ArrowRight" || char === "l") &&
284+
(char === "d" ||
285+
char === "ArrowRight" ||
286+
char === "l" ||
287+
char === "→") &&
279288
originalChar === "→"
280289
) {
281290
return true;

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,15 +1637,22 @@ export function getActiveWordTopAfterAppend(data: string): number {
16371637
}
16381638

16391639
// this means input, delete or composition
1640-
function afterAnyTestInput(correctInput: boolean | null): void {
1641-
if (
1642-
correctInput === true ||
1643-
Config.playSoundOnError === "off" ||
1644-
Config.blindMode
1645-
) {
1640+
function afterAnyTestInput(
1641+
type: "textInput" | "delete" | "compositionUpdate",
1642+
correctInput: boolean | null
1643+
): void {
1644+
if (type === "textInput") {
1645+
if (
1646+
correctInput === true ||
1647+
Config.playSoundOnError === "off" ||
1648+
Config.blindMode
1649+
) {
1650+
void SoundController.playClick();
1651+
} else {
1652+
void SoundController.playError();
1653+
}
1654+
} else if (type === "delete") {
16461655
void SoundController.playClick();
1647-
} else {
1648-
void SoundController.playError();
16491656
}
16501657

16511658
const acc: number = Numbers.roundTo2(TestStats.calculateAccuracy());
@@ -1684,18 +1691,18 @@ export function afterTestTextInput(
16841691
}
16851692
}
16861693

1687-
afterAnyTestInput(correct);
1694+
afterAnyTestInput("textInput", correct);
16881695
}
16891696

16901697
export function afterTestCompositionUpdate(): void {
16911698
void updateActiveWordLetters();
16921699
// correct needs to be true to get the normal click sound
1693-
afterAnyTestInput(true);
1700+
afterAnyTestInput("compositionUpdate", true);
16941701
}
16951702

16961703
export function afterTestDelete(): void {
16971704
void updateActiveWordLetters();
1698-
afterAnyTestInput(null);
1705+
afterAnyTestInput("delete", null);
16991706
}
17001707

17011708
export function beforeTestWordChange(

0 commit comments

Comments
 (0)