Skip to content

fix(funbox): rtl language in polyglot doesn't work correctly (@byseif21) #6645

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 10 additions & 2 deletions frontend/src/ts/test/funbox/funbox-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import { Language } from "@monkeytype/contracts/schemas/languages";
export type FunboxFunctions = {
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
punctuateWord?: (word: string) => string;
withWords?: (words?: string[]) => Promise<Wordset>;
withWords?: (
words?: string[]
) => Promise<
| Wordset
| { wordset: Wordset; allRightToLeft: boolean; allLigatures: boolean }
>;
alterText?: (word: string, wordIndex: number, wordsBound: number) => string;
applyConfig?: () => void;
applyGlobalCSS?: () => void;
Expand Down Expand Up @@ -689,7 +694,10 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {

const wordSet = languages.flatMap((it) => it.words);
Arrays.shuffle(wordSet);
return new Wordset(wordSet);
// compute RTL and ligature info
const allRightToLeft = languages.every((lang) => lang.rightToLeft);
const allLigatures = languages.every((lang) => lang.ligatures);
return { wordset: new Wordset(wordSet), allRightToLeft, allLigatures };
},
},
};
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,16 @@ export async function init(): Promise<void | null> {
let generatedSectionIndexes: number[];
let wordsHaveTab = false;
let wordsHaveNewline = false;
let allRightToLeft: boolean | undefined = undefined;
let allLigatures: boolean | undefined = undefined;
try {
const gen = await WordsGenerator.generateWords(language);
generatedWords = gen.words;
generatedSectionIndexes = gen.sectionIndexes;
wordsHaveTab = gen.hasTab;
wordsHaveNewline = gen.hasNewline;
allRightToLeft = gen.allRightToLeft;
allLigatures = gen.allLigatures;
} catch (e) {
Loader.hide();
if (e instanceof WordGenError || e instanceof Error) {
Expand Down Expand Up @@ -555,8 +559,14 @@ export async function init(): Promise<void | null> {
);
}
Funbox.toggleScript(TestWords.words.getCurrent());
TestUI.setRightToLeft(language.rightToLeft);
TestUI.setLigatures(language.ligatures ?? false);
TestUI.setRightToLeft(
typeof allRightToLeft === "boolean" ? allRightToLeft : language.rightToLeft
);
TestUI.setLigatures(
typeof allLigatures === "boolean"
? allLigatures
: language.ligatures ?? false
);
TestUI.showWords();
console.debug("Test initialized with words", generatedWords);
console.debug(
Expand Down
50 changes: 44 additions & 6 deletions frontend/src/ts/test/words-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config, * as UpdateConfig from "../config";
import * as CustomText from "./custom-text";
import * as Wordset from "./wordset";
import * as WordsetModule from "./wordset";
import { Wordset } from "./wordset";
import QuotesController, {
Quote,
QuoteWithTextSplit,
Expand Down Expand Up @@ -307,7 +308,9 @@ async function applyEnglishPunctuationToWord(word: string): Promise<string> {
return EnglishPunctuation.replace(word);
}

function getFunboxWordsFrequency(): Wordset.FunboxWordsFrequency | undefined {
function getFunboxWordsFrequency():
| WordsetModule.FunboxWordsFrequency
| undefined {
const funbox = findSingleActiveFunboxWithFunction("getWordsFrequencyMode");
if (funbox) {
return funbox.functions.getWordsFrequencyMode();
Expand Down Expand Up @@ -341,7 +344,7 @@ async function getFunboxSection(): Promise<string[]> {
function getFunboxWord(
word: string,
wordIndex: number,
wordset?: Wordset.Wordset
wordset?: Wordset
): string {
const funbox = findSingleActiveFunboxWithFunction("getWord");

Expand Down Expand Up @@ -575,7 +578,7 @@ async function getQuoteWordList(
return TestWords.currentQuote.textSplit;
}

let currentWordset: Wordset.Wordset | null = null;
let currentWordset: Wordset | null = null;
let currentLanguage: LanguageObject | null = null;
let isCurrentlyUsingFunboxSection = false;

Expand All @@ -584,13 +587,31 @@ type GenerateWordsReturn = {
sectionIndexes: number[];
hasTab: boolean;
hasNewline: boolean;
allRightToLeft?: boolean;
allLigatures?: boolean;
};

let previousRandomQuote: QuoteWithTextSplit | null = null;

export async function generateWords(
language: LanguageObject
): Promise<GenerateWordsReturn> {
function isPolyglotResult(
obj: unknown
): obj is {
wordset: Wordset;
allRightToLeft: boolean;
allLigatures: boolean;
} {
return (
Boolean(obj) &&
typeof obj === "object" &&
obj !== null &&
"wordset" in obj &&
"allRightToLeft" in obj &&
"allLigatures" in obj
);
}
if (!TestState.isRepeated) {
previousGetNextWordReturns = [];
}
Expand All @@ -605,6 +626,8 @@ export async function generateWords(
sectionIndexes: [],
hasTab: false,
hasNewline: false,
allRightToLeft: language.rightToLeft,
allLigatures: language.ligatures ?? false,
};

isCurrentlyUsingFunboxSection = isFunboxActiveWithFunction("pullSection");
Expand Down Expand Up @@ -635,9 +658,24 @@ export async function generateWords(

const funbox = findSingleActiveFunboxWithFunction("withWords");
if (funbox) {
currentWordset = await funbox.functions.withWords(wordList);
// check if polyglot funbox (returns object)
const result = await funbox.functions.withWords(wordList);
const isPolyglot = Array.isArray(Config.funbox)
? Config.funbox.includes("polyglot")
: Config.funbox === "polyglot";
if (isPolyglot && isPolyglotResult(result)) {
currentWordset = result.wordset;
ret.allRightToLeft = result.allRightToLeft;
ret.allLigatures = result.allLigatures;
} else {
if (result instanceof Wordset) {
currentWordset = result;
} else {
throw new Error("withWords did not return a Wordset");
}
}
} else {
currentWordset = await Wordset.withWords(wordList);
currentWordset = await WordsetModule.withWords(wordList);
}

console.debug("Wordset", currentWordset);
Expand Down