Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions frontend/src/ts/input/handlers/insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ type OnInsertTextParams = {

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

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

// input and target word
const testInput = TestInput.input.current;
const currentWord = TestWords.words.getCurrent();

// if the character is visually equal, replace it with the target character
// this ensures all future equivalence checks work correctly
let normalizedData: string | null = null;
const targetChar =
TestWords.words.getCurrent()[TestInput.input.current.length];
if (
targetChar !== undefined &&
areCharactersVisuallyEqual(options.data, targetChar, Config.language)
) {
replaceInputElementLastValueChar(targetChar);
normalizedData = targetChar;
}

const normalizedData = normalizeDataAndUpdateInputIfNeeded(
options.data,
testInput,
currentWord
);
const data = normalizedData ?? options.data;

// start if needed
Expand All @@ -118,8 +116,6 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
// helper consts
const lastInMultiOrSingle =
lastInMultiIndex === true || lastInMultiIndex === undefined;
const testInput = TestInput.input.current;
const currentWord = TestWords.words.getCurrent();
const wordIndex = TestState.activeWordIndex;
const charIsSpace = isSpace(data);
const charIsNewline = data === "\n";
Expand All @@ -135,7 +131,10 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
// is char correct
const funboxCorrect = findSingleActiveFunboxWithFunction(
"isCharCorrect"
)?.functions.isCharCorrect(data, currentWord[inputValue.length] ?? "");
)?.functions.isCharCorrect(
data,
currentWord[(testInput + data).length - 1] ?? ""
);
const correct =
funboxCorrect ??
isCharCorrect({
Expand All @@ -148,7 +147,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
// word navigation check
const noSpaceForce =
isFunboxActiveWithProperty("nospace") &&
TestInput.input.current.length === TestWords.words.getCurrent().length;
(testInput + data).length === TestWords.words.getCurrent().length;
const shouldGoToNextWord =
((charIsSpace || charIsNewline) && !shouldInsertSpace) || noSpaceForce;

Expand Down Expand Up @@ -286,6 +285,23 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
}
}

function normalizeDataAndUpdateInputIfNeeded(
data: string,
testInput: string,
currentWord: string
): string | null {
let normalizedData: string | null = null;
const targetChar = currentWord[testInput.length];
if (
targetChar !== undefined &&
areCharactersVisuallyEqual(data, targetChar, Config.language)
) {
replaceInputElementLastValueChar(targetChar);
normalizedData = targetChar;
}
return normalizedData;
}

export async function emulateInsertText(
options: OnInsertTextParams
): Promise<void> {
Expand Down
17 changes: 13 additions & 4 deletions frontend/src/ts/test/funbox/funbox-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,34 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
},
isCharCorrect(char: string, originalChar: string): boolean {
if (
(char === "a" || char === "ArrowLeft" || char === "j") &&
(char === "a" ||
char === "ArrowLeft" ||
char === "j" ||
char === "←") &&
originalChar === "←"
) {
return true;
}
if (
(char === "s" || char === "ArrowDown" || char === "k") &&
(char === "s" ||
char === "ArrowDown" ||
char === "k" ||
char === "↓") &&
originalChar === "↓"
) {
return true;
}
if (
(char === "w" || char === "ArrowUp" || char === "i") &&
(char === "w" || char === "ArrowUp" || char === "i" || char === "↑") &&
originalChar === "↑"
) {
return true;
}
if (
(char === "d" || char === "ArrowRight" || char === "l") &&
(char === "d" ||
char === "ArrowRight" ||
char === "l" ||
char === "→") &&
originalChar === "→"
) {
return true;
Expand Down
Loading