Skip to content

Commit ea37960

Browse files
authored
fix: composition issues with ending test and new line handling (@Miodec) (monkeytypegame#7210)
closes monkeytypegame#4968
1 parent 4f8d8c5 commit ea37960

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

frontend/src/ts/input/handlers/keydown.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import * as JSONData from "../../utils/json-data";
1010
import * as Notifications from "../../elements/notifications";
1111
import * as KeyConverter from "../../utils/key-converter";
1212
import * as ShiftTracker from "../../test/shift-tracker";
13-
import * as CompositionState from "../../states/composition";
1413
import * as ManualRestart from "../../test/manual-restart-tracker";
1514
import { canQuickRestart } from "../../utils/quick-restart";
1615
import * as CustomText from "../../test/custom-text";
@@ -45,7 +44,7 @@ export async function handleTab(e: KeyboardEvent, now: number): Promise<void> {
4544

4645
export async function handleEnter(
4746
e: KeyboardEvent,
48-
now: number,
47+
_now: number,
4948
): Promise<void> {
5049
if (e.shiftKey) {
5150
if (Config.mode === "zen") {
@@ -92,14 +91,6 @@ export async function handleEnter(
9291
return;
9392
}
9493
}
95-
if (
96-
TestWords.hasNewline ||
97-
(Config.mode === "zen" && !CompositionState.getComposing())
98-
) {
99-
await emulateInsertText({ data: "\n", now });
100-
e.preventDefault();
101-
return;
102-
}
10394
}
10495

10596
export async function handleOppositeShift(event: KeyboardEvent): Promise<void> {

frontend/src/ts/input/listeners/input.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import {
99
import * as TestUI from "../../test/test-ui";
1010
import { onBeforeInsertText } from "../handlers/before-insert-text";
1111
import { onBeforeDelete } from "../handlers/before-delete";
12+
import * as TestInput from "../../test/test-input";
13+
import * as TestWords from "../../test/test-words";
14+
import * as CompositionState from "../../states/composition";
15+
import { activeWordIndex } from "../../test/test-state";
16+
import { areAllTestWordsGenerated } from "../../test/test-logic";
1217

1318
const inputEl = getInputElement();
1419

@@ -114,6 +119,26 @@ inputEl.addEventListener("input", async (event) => {
114119
inputType === "insertCompositionText" ||
115120
inputType === "insertFromComposition"
116121
) {
122+
const allWordsTyped = activeWordIndex >= TestWords.words.length - 1;
123+
const inputPlusComposition =
124+
TestInput.input.current + (CompositionState.getData() ?? "");
125+
const inputPlusCompositionIsCorrect =
126+
TestWords.words.getCurrent() === inputPlusComposition;
127+
128+
// composition quick end
129+
// if the user typed the entire word correctly but is still in composition
130+
// dont wait for them to end the composition manually, just end the test
131+
// by dispatching a compositionend which will trigger onInsertText
132+
if (
133+
areAllTestWordsGenerated() &&
134+
allWordsTyped &&
135+
inputPlusCompositionIsCorrect
136+
) {
137+
getInputElement().dispatchEvent(
138+
new CompositionEvent("compositionend", { data: event.data ?? "" }),
139+
);
140+
}
141+
117142
// in case the data is the same as the last one, just ignore it
118143
if (getLastInsertCompositionTextData() !== event.data) {
119144
setLastInsertCompositionTextData(event.data ?? "");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,12 @@ export async function updateWordLetters({
884884
charToShow = compositionChar === " " ? "_" : compositionChar;
885885
}
886886

887-
ret += `<letter class="dead">${charToShow}</letter>`;
887+
let correctClass = "";
888+
if (compositionChar === currentWordChars[input.length + i]) {
889+
correctClass = "correct";
890+
}
891+
892+
ret += `<letter class="dead ${correctClass}">${charToShow}</letter>`;
888893
}
889894

890895
for (

0 commit comments

Comments
 (0)