-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(input): support oe and ae ligature input (@Dawn-Fighter) #7953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dawn-Fighter
wants to merge
4
commits into
monkeytypegame:master
Choose a base branch
from
Dawn-Fighter:fix-ligature-input-overrides
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+279
−7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ab7ea19
fix(input): support oe and ae ligature input
Dawn-Fighter e7bec61
test(input): cover ligature completion handling
Dawn-Fighter 452b327
fix(input): track ligature completion input
Dawn-Fighter f055aac
style(input): format ligature completion check
Dawn-Fighter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| getInputElementValue, | ||
| setInputElementValue, | ||
| } from "../../../src/ts/input/input-element"; | ||
| import { onInsertText } from "../../../src/ts/input/handlers/insert-text"; | ||
| import { | ||
| getLigatureCompletion, | ||
| getMatchingLigatureOverride, | ||
| resetPendingLigatureCompletion, | ||
| } from "../../../src/ts/input/helpers/ligatures"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| currentWord: "", | ||
| input: { | ||
| current: "", | ||
| syncWithInputElement: vi.fn(), | ||
| }, | ||
| incrementKeypressErrors: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../../../src/ts/test/test-ui", () => ({})); | ||
| vi.mock("../../../src/ts/test/test-state", () => ({ | ||
| activeWordIndex: 0, | ||
| isActive: true, | ||
| })); | ||
| vi.mock("../../../src/ts/test/test-logic", () => ({ | ||
| startTest: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/test/test-input", () => ({ | ||
| input: mocks.input, | ||
| corrected: { update: vi.fn() }, | ||
| incrementAccuracy: vi.fn(), | ||
| incrementKeypressCount: vi.fn(), | ||
| incrementKeypressErrors: mocks.incrementKeypressErrors, | ||
| pushKeypressWord: vi.fn(), | ||
| pushMissedWord: vi.fn(), | ||
| setBurstStart: vi.fn(), | ||
| setCurrentNotAfk: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/test/test-words", () => ({ | ||
| words: { | ||
| getCurrentText: vi.fn(() => mocks.currentWord), | ||
| }, | ||
| })); | ||
| vi.mock("../../../src/ts/input/helpers/fail-or-finish", () => ({ | ||
| checkIfFailedDueToDifficulty: vi.fn(), | ||
| checkIfFailedDueToMinBurst: vi.fn(), | ||
| checkIfFinished: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/test/funbox/list", () => ({ | ||
| findSingleActiveFunboxWithFunction: vi.fn(), | ||
| isFunboxActiveWithProperty: vi.fn(() => false), | ||
| })); | ||
| vi.mock("../../../src/ts/test/replay", () => ({ | ||
| addReplayEvent: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/config/store", () => ({ | ||
| Config: { | ||
| blindMode: false, | ||
| keymapMode: "off", | ||
| language: "english", | ||
| mode: "words", | ||
| oppositeShiftMode: "off", | ||
| stopOnError: "off", | ||
| }, | ||
| })); | ||
| vi.mock("../../../src/ts/events/keymap", () => ({ | ||
| flash: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/test/weak-spot", () => ({ | ||
| updateScore: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/legacy-states/composition", () => ({ | ||
| getData: vi.fn(() => ""), | ||
| })); | ||
| vi.mock("../../../src/ts/input/state", () => ({ | ||
| getIncorrectShiftsInARow: vi.fn(() => 0), | ||
| incrementIncorrectShiftsInARow: vi.fn(), | ||
| isCorrectShiftUsed: vi.fn(() => true), | ||
| resetIncorrectShiftsInARow: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/states/notifications", () => ({ | ||
| showNoticeNotification: vi.fn(), | ||
| })); | ||
| vi.mock("../../../src/ts/input/helpers/word-navigation", () => ({ | ||
| goToNextWord: vi.fn(async () => ({ | ||
| increasedWordIndex: false, | ||
| lastBurst: null, | ||
| })), | ||
| })); | ||
| vi.mock("../../../src/ts/input/handlers/before-insert-text", () => ({ | ||
| onBeforeInsertText: vi.fn(), | ||
| })); | ||
|
|
||
| describe("insert-text ligature input overrides", () => { | ||
| beforeEach(() => { | ||
| mocks.currentWord = ""; | ||
| mocks.input.current = ""; | ||
| mocks.input.syncWithInputElement.mockImplementation(() => { | ||
| mocks.input.current = getInputElementValue().inputValue; | ||
| }); | ||
| setInputElementValue(""); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| resetPendingLigatureCompletion(); | ||
| setInputElementValue(""); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["o", "œ", "œ"], | ||
| ["O", "Œ", "Œ"], | ||
| ["a", "æ", "æ"], | ||
| ["A", "Æ", "Æ"], | ||
| ])( | ||
| "normalizes '%s' to '%s' when target is '%s'", | ||
| (data, target, expected) => { | ||
| expect(getMatchingLigatureOverride(data, target)).toBe(expected); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| ["œ", "e"], | ||
| ["Œ", "E"], | ||
| ["æ", "e"], | ||
| ["Æ", "E"], | ||
| ])("gets completion '%s' after '%s'", (target, completion) => { | ||
| expect(getLigatureCompletion(target)).toBe(completion); | ||
| }); | ||
|
|
||
| it("does not normalize unrelated input", () => { | ||
| expect(getMatchingLigatureOverride("e", "œ")).toBeNull(); | ||
| expect(getLigatureCompletion("o")).toBeNull(); | ||
| }); | ||
|
|
||
| it("removes the completion character and keeps input state synced", async () => { | ||
| mocks.currentWord = "œuvre"; | ||
|
|
||
| setInputElementValue("o"); | ||
| await onInsertText({ | ||
| now: performance.now(), | ||
| data: "o", | ||
| }); | ||
|
|
||
| setInputElementValue("œe"); | ||
|
|
||
| await onInsertText({ | ||
| now: performance.now(), | ||
| data: "e", | ||
| }); | ||
|
|
||
| expect(getInputElementValue().inputValue).toBe("œ"); | ||
| expect(mocks.input.current).toBe("œ"); | ||
| expect(mocks.input.syncWithInputElement).toHaveBeenCalledTimes(2); | ||
| expect(mocks.incrementKeypressErrors).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("penalizes skipping the ligature completion character", async () => { | ||
| mocks.currentWord = "œuvre"; | ||
|
|
||
| setInputElementValue("o"); | ||
| await onInsertText({ | ||
| now: performance.now(), | ||
| data: "o", | ||
| }); | ||
|
|
||
| setInputElementValue("œu"); | ||
| await onInsertText({ | ||
| now: performance.now(), | ||
| data: "u", | ||
| }); | ||
|
|
||
| expect(mocks.input.current).toBe("œu"); | ||
| expect(mocks.incrementKeypressErrors).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| const ligatureInputOverrides = new Map<string, string>([ | ||
| ["œ", "oe"], | ||
| ["Œ", "OE"], | ||
| ["æ", "ae"], | ||
| ["Æ", "AE"], | ||
| ]); | ||
|
|
||
| let pendingLigatureCompletion: { | ||
| completion: string; | ||
| inputLength: number; | ||
| } | null = null; | ||
|
|
||
| type PendingLigatureCompletionStatus = "complete" | "skipped" | null; | ||
|
|
||
| export function getMatchingLigatureOverride( | ||
| data: string, | ||
| targetChar: string | undefined, | ||
| ): string | null { | ||
| if (targetChar === undefined) return null; | ||
|
|
||
| const override = ligatureInputOverrides.get(targetChar); | ||
| if (override?.[0] !== data) return null; | ||
|
|
||
| return targetChar; | ||
| } | ||
|
|
||
| export function getLigatureCompletion( | ||
| targetChar: string | undefined, | ||
| ): string | null { | ||
| if (targetChar === undefined) return null; | ||
|
|
||
| const override = ligatureInputOverrides.get(targetChar); | ||
| return override?.slice(1) ?? null; | ||
| } | ||
|
|
||
| export function setPendingLigatureCompletion( | ||
| completion: string, | ||
| inputLength: number, | ||
| ): void { | ||
| pendingLigatureCompletion = { completion, inputLength }; | ||
| } | ||
|
|
||
| export function resetPendingLigatureCompletion(): void { | ||
| pendingLigatureCompletion = null; | ||
| } | ||
|
|
||
| export function getPendingLigatureCompletionStatus( | ||
| data: string, | ||
| currentInput: string, | ||
| ): PendingLigatureCompletionStatus { | ||
| if (pendingLigatureCompletion === null) return null; | ||
|
|
||
| if (currentInput.length !== pendingLigatureCompletion.inputLength) { | ||
| resetPendingLigatureCompletion(); | ||
| return null; | ||
| } | ||
|
|
||
| const completionMatched = data === pendingLigatureCompletion.completion; | ||
| resetPendingLigatureCompletion(); | ||
|
|
||
| return completionMatched ? "complete" : "skipped"; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.