-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add "Previous Test" functionality with button and state management (@abhaychandra123) #7698
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,6 +169,7 @@ type RestartOptions = { | |
| practiseMissed?: boolean; | ||
| noAnim?: boolean; | ||
| isQuickRestart?: boolean; | ||
| isPrevious?: boolean; | ||
| }; | ||
|
Comment on lines
169
to
173
|
||
|
|
||
| export function restart(options = {} as RestartOptions): void { | ||
|
|
@@ -346,7 +347,7 @@ export function restart(options = {} as RestartOptions): void { | |
| TestState.setPaceRepeat(repeatWithPace); | ||
| TestInitFailed.hide(); | ||
| TestState.setTestInitSuccess(true); | ||
| const initResult = await init(); | ||
| const initResult = await init(options.isPrevious ?? false); | ||
|
|
||
| if (!initResult) { | ||
| TestState.setTestRestarting(false); | ||
|
|
@@ -380,7 +381,7 @@ let lastInitError: Error | null = null; | |
| let showedLazyModeNotification: boolean = false; | ||
| let testReinitCount = 0; | ||
|
|
||
| async function init(): Promise<boolean> { | ||
| async function init(loadPreviousQuote = false): Promise<boolean> { | ||
| console.debug("Initializing test"); | ||
| testReinitCount++; | ||
| if (testReinitCount > 3) { | ||
|
|
@@ -413,7 +414,7 @@ async function init(): Promise<boolean> { | |
| } | ||
|
|
||
| if (!language || language.name !== Config.language) { | ||
| return await init(); | ||
| return await init(loadPreviousQuote); | ||
| } | ||
|
|
||
| if (getActivePage() === "test") { | ||
|
|
@@ -512,7 +513,9 @@ async function init(): Promise<boolean> { | |
| let generatedWords: string[] = []; | ||
| let generatedSectionIndexes: number[] = []; | ||
| try { | ||
| const gen = await WordsGenerator.generateWords(language); | ||
| const gen = await WordsGenerator.generateWords(language, { | ||
| loadPreviousQuote, | ||
| }); | ||
| generatedWords = gen.words; | ||
| generatedSectionIndexes = gen.sectionIndexes; | ||
| wordsHaveTab = gen.hasTab; | ||
|
|
@@ -537,7 +540,7 @@ async function init(): Promise<boolean> { | |
| }); | ||
| } | ||
|
|
||
| return await init(); | ||
| return await init(loadPreviousQuote); | ||
| } | ||
|
|
||
| let hasNumbers = false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -496,7 +496,8 @@ export function getLimit(): number { | |
|
|
||
| async function getQuoteWordList( | ||
| language: LanguageObject, | ||
| wordOrder?: FunboxWordOrder, | ||
| wordOrder: FunboxWordOrder | undefined, | ||
| options: GenerateWordsOptions, | ||
| ): Promise<string[]> { | ||
| if (TestState.isRepeated) { | ||
| if (currentWordset === null) { | ||
|
|
@@ -534,33 +535,59 @@ async function getQuoteWordList( | |
| } | ||
|
|
||
| let rq: Quote; | ||
| if (Config.quoteLength.includes(-2) && Config.quoteLength.length === 1) { | ||
| const targetQuote = QuotesController.getQuoteById( | ||
| TestState.selectedQuoteId, | ||
| ); | ||
| if (targetQuote === undefined) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError( | ||
| `Quote ${TestState.selectedQuoteId} does not exist`, | ||
| let wasPrevious = false; | ||
|
|
||
| function pickQuoteFromCurrentSettings(): Quote { | ||
| if (Config.quoteLength.includes(-2) && Config.quoteLength.length === 1) { | ||
| const targetQuote = QuotesController.getQuoteById( | ||
| TestState.selectedQuoteId, | ||
| ); | ||
| if (targetQuote === undefined) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError( | ||
| `Quote ${TestState.selectedQuoteId} does not exist`, | ||
| ); | ||
| } | ||
| return targetQuote; | ||
| } | ||
| rq = targetQuote; | ||
| } else if (Config.quoteLength.includes(-3)) { | ||
| const randomQuote = QuotesController.getRandomFavoriteQuote( | ||
| Config.language, | ||
| ); | ||
| if (randomQuote === null) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError("No favorite quotes found"); | ||
| if (Config.quoteLength.includes(-3)) { | ||
| const randomQuote = QuotesController.getRandomFavoriteQuote( | ||
| Config.language, | ||
| ); | ||
| if (randomQuote === null) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError("No favorite quotes found"); | ||
| } | ||
| return randomQuote; | ||
| } | ||
| rq = randomQuote; | ||
| } else { | ||
| const randomQuote = QuotesController.getRandomQuote(); | ||
| if (randomQuote === null) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError("No quotes found for selected quote length"); | ||
| } | ||
| rq = randomQuote; | ||
| return randomQuote; | ||
| } | ||
|
|
||
| if (options.loadPreviousQuote) { | ||
| const prevQuoteId = TestState.peekPreviousQuoteId(); | ||
| if (prevQuoteId !== null) { | ||
| const targetQuote = QuotesController.getQuoteById(prevQuoteId); | ||
| if (targetQuote === undefined) { | ||
| setQuoteLengthAll(); | ||
| throw new WordGenError(`Quote ${prevQuoteId} does not exist`); | ||
| } | ||
| TestState.commitPreviousNavigation(); | ||
| rq = targetQuote; | ||
| wasPrevious = true; | ||
|
Comment on lines
+571
to
+581
|
||
| } else { | ||
| rq = pickQuoteFromCurrentSettings(); | ||
| } | ||
| } else { | ||
| rq = pickQuoteFromCurrentSettings(); | ||
| } | ||
|
|
||
| if (!TestState.isRepeated && !wasPrevious) { | ||
| TestState.pushQuoteToHistory(rq.id); | ||
| } | ||
|
|
||
| rq.language = Strings.removeLanguageSize(Config.language); | ||
|
|
@@ -605,10 +632,17 @@ type GenerateWordsReturn = { | |
| allLigatures?: boolean; | ||
| }; | ||
|
|
||
| /** Options for a single generation pass (explicit intent, not global test flags). */ | ||
| export type GenerateWordsOptions = { | ||
| /** Load the previous quote from session history (quote mode only). */ | ||
| loadPreviousQuote?: boolean; | ||
| }; | ||
|
|
||
| let previousRandomQuote: QuoteWithTextSplit | null = null; | ||
|
|
||
| export async function generateWords( | ||
| language: LanguageObject, | ||
| options: GenerateWordsOptions = {}, | ||
| ): Promise<GenerateWordsReturn> { | ||
| if (!TestState.isRepeated) { | ||
| previousGetNextWordReturns = []; | ||
|
|
@@ -637,7 +671,7 @@ export async function generateWords( | |
| if (Config.mode === "custom") { | ||
| wordList = CustomText.getText(); | ||
| } else if (Config.mode === "quote") { | ||
| wordList = await getQuoteWordList(language, wordOrder); | ||
| wordList = await getQuoteWordList(language, wordOrder, options); | ||
| } else if (Config.mode === "zen") { | ||
| wordList = []; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.