mirror of
				https://github.com/monkeytypegame/monkeytype.git
				synced 2025-10-30 02:39:05 +08:00 
			
		
		
		
	refactor: move active word state to test-state
This commit is contained in:
		
							parent
							
								
									01dee3fe15
								
							
						
					
					
						commit
						a0a09cc141
					
				
					 9 changed files with 48 additions and 57 deletions
				
			
		|  | @ -123,10 +123,10 @@ function backspaceToPrevious(): void { | |||
| 
 | ||||
|   const wordElements = document.querySelectorAll("#words > .word"); | ||||
|   if ( | ||||
|     (TestInput.input.history[TestWords.words.currentIndex - 1] == | ||||
|       TestWords.words.get(TestWords.words.currentIndex - 1) && | ||||
|     (TestInput.input.history[TestState.activeWordIndex - 1] == | ||||
|       TestWords.words.get(TestState.activeWordIndex - 1) && | ||||
|       !Config.freedomMode) || | ||||
|     wordElements[TestWords.words.currentIndex - 1]?.classList.contains("hidden") | ||||
|     wordElements[TestState.activeWordIndex - 1]?.classList.contains("hidden") | ||||
|   ) { | ||||
|     return; | ||||
|   } | ||||
|  | @ -136,7 +136,7 @@ function backspaceToPrevious(): void { | |||
|   } | ||||
| 
 | ||||
|   const incorrectLetterBackspaced = | ||||
|     wordElements[TestWords.words.currentIndex]?.children[0]?.classList.contains( | ||||
|     wordElements[TestState.activeWordIndex]?.children[0]?.classList.contains( | ||||
|       "incorrect" | ||||
|     ); | ||||
|   if (Config.stopOnError === "letter" && incorrectLetterBackspaced) { | ||||
|  | @ -149,7 +149,7 @@ function backspaceToPrevious(): void { | |||
|     TestInput.input.current = TestInput.input.current.slice(0, -1); | ||||
|     setWordsInput(" " + TestInput.input.current + " "); | ||||
|   } | ||||
|   TestWords.words.decreaseCurrentIndex(); | ||||
|   TestState.decreaseActiveWordIndex(); | ||||
|   TestUI.setActiveWordElementIndex(TestUI.activeWordElementIndex - 1); | ||||
|   TestUI.updateActiveElement(true); | ||||
|   Funbox.toggleScript(TestWords.words.getCurrent()); | ||||
|  | @ -219,10 +219,10 @@ async function handleSpace(): Promise<void> { | |||
|     } | ||||
|     PaceCaret.handleSpace(true, currentWord); | ||||
|     TestInput.input.pushHistory(); | ||||
|     TestWords.words.increaseCurrentIndex(); | ||||
|     TestState.increaseActiveWordIndex(); | ||||
|     Funbox.toggleScript(TestWords.words.getCurrent()); | ||||
|     TestInput.incrementKeypressCount(); | ||||
|     TestInput.pushKeypressWord(TestWords.words.currentIndex); | ||||
|     TestInput.pushKeypressWord(TestState.activeWordIndex); | ||||
|     if (!nospace) { | ||||
|       void Sound.playClick(); | ||||
|     } | ||||
|  | @ -271,10 +271,10 @@ async function handleSpace(): Promise<void> { | |||
|       TestUI.highlightBadWord(TestUI.activeWordElementIndex); | ||||
|     } | ||||
|     TestInput.input.pushHistory(); | ||||
|     TestWords.words.increaseCurrentIndex(); | ||||
|     TestState.increaseActiveWordIndex(); | ||||
|     Funbox.toggleScript(TestWords.words.getCurrent()); | ||||
|     TestInput.incrementKeypressCount(); | ||||
|     TestInput.pushKeypressWord(TestWords.words.currentIndex); | ||||
|     TestInput.pushKeypressWord(TestState.activeWordIndex); | ||||
|     Replay.addReplayEvent("submitErrorWord"); | ||||
|     if (Config.difficulty === "expert" || Config.difficulty === "master") { | ||||
|       TestLogic.fail("difficulty"); | ||||
|  | @ -283,7 +283,7 @@ async function handleSpace(): Promise<void> { | |||
| 
 | ||||
|   TestInput.corrected.pushHistory(); | ||||
| 
 | ||||
|   const isLastWord = TestWords.words.currentIndex === TestWords.words.length; | ||||
|   const isLastWord = TestState.activeWordIndex === TestWords.words.length; | ||||
|   if (TestLogic.areAllTestWordsGenerated() && isLastWord) { | ||||
|     void TestLogic.finish(); | ||||
|     return; | ||||
|  | @ -656,7 +656,7 @@ function handleChar( | |||
|   } | ||||
| 
 | ||||
|   TestInput.incrementKeypressCount(); | ||||
|   TestInput.pushKeypressWord(TestWords.words.currentIndex); | ||||
|   TestInput.pushKeypressWord(TestState.activeWordIndex); | ||||
| 
 | ||||
|   if ( | ||||
|     Config.difficulty !== "master" && | ||||
|  | @ -682,7 +682,7 @@ function handleChar( | |||
|     ? TestInput.input.current.length | ||||
|     : Hangul.disassemble(TestInput.input.current).length; | ||||
|   //update the active word top, but only once
 | ||||
|   if (testInputLength === 1 && TestWords.words.currentIndex === 0) { | ||||
|   if (testInputLength === 1 && TestState.activeWordIndex === 0) { | ||||
|     TestUI.setActiveWordTop(activeWord?.offsetTop); | ||||
|   } | ||||
| 
 | ||||
|  | @ -707,7 +707,7 @@ function handleChar( | |||
|     //auto stop the test if the last word is correct
 | ||||
|     //do not stop if not all characters have been parsed by handleChar yet
 | ||||
|     const currentWord = TestWords.words.getCurrent(); | ||||
|     const lastWordIndex = TestWords.words.currentIndex; | ||||
|     const lastWordIndex = TestState.activeWordIndex; | ||||
|     const lastWord = lastWordIndex === TestWords.words.length - 1; | ||||
|     const allWordGenerated = TestLogic.areAllTestWordsGenerated(); | ||||
|     const wordIsTheSame = currentWord === TestInput.input.current; | ||||
|  | @ -1061,8 +1061,8 @@ $(document).on("keydown", async (event) => { | |||
|         TestInput.input.current.slice(-1), | ||||
|         TestInput.input.current.length - 1 | ||||
|       ) && | ||||
|       (TestInput.input.history[TestWords.words.currentIndex - 1] != | ||||
|         TestWords.words.get(TestWords.words.currentIndex - 1) || | ||||
|       (TestInput.input.history[TestState.activeWordIndex - 1] != | ||||
|         TestWords.words.get(TestState.activeWordIndex - 1) || | ||||
|         Config.freedomMode) | ||||
|     ) { | ||||
|       TestInput.input.current = ""; | ||||
|  |  | |||
|  | @ -21,6 +21,7 @@ import * as JSONData from "../../utils/json-data"; | |||
| import { getSection } from "../wikipedia"; | ||||
| import * as WeakSpot from "../weak-spot"; | ||||
| import * as IPAddresses from "../../utils/ip-addresses"; | ||||
| import * as TestState from "../test-state"; | ||||
| 
 | ||||
| export type FunboxFunctions = { | ||||
|   getWord?: (wordset?: Wordset, wordIndex?: number) => string; | ||||
|  | @ -62,8 +63,8 @@ async function readAheadHandleKeydown( | |||
|     event.key == "Backspace" && | ||||
|     !isCorrect && | ||||
|     (TestInput.input.current != "" || | ||||
|       TestInput.input.history[TestWords.words.currentIndex - 1] != | ||||
|         TestWords.words.get(TestWords.words.currentIndex - 1) || | ||||
|       TestInput.input.history[TestState.activeWordIndex - 1] != | ||||
|         TestWords.words.get(TestState.activeWordIndex - 1) || | ||||
|       Config.freedomMode) | ||||
|   ) { | ||||
|     $("#words").addClass("read_ahead_disabled"); | ||||
|  | @ -350,8 +351,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = { | |||
|           (TestInput.input.history.length + 1) / wordsPerLayout | ||||
|         ); | ||||
|         const mod = | ||||
|           wordsPerLayout - | ||||
|           ((TestWords.words.currentIndex + 1) % wordsPerLayout); | ||||
|           wordsPerLayout - ((TestState.activeWordIndex + 1) % wordsPerLayout); | ||||
| 
 | ||||
|         if (layouts[index] as string) { | ||||
|           if (mod <= 3 && (layouts[index + 1] as string)) { | ||||
|  |  | |||
|  | @ -202,7 +202,7 @@ export async function update(expectedStepEnd: number): Promise<void> { | |||
|     try { | ||||
|       const newIndex = | ||||
|         settings.currentWordIndex - | ||||
|         (TestWords.words.currentIndex - TestUI.activeWordElementIndex); | ||||
|         (TestState.activeWordIndex - TestUI.activeWordElementIndex); | ||||
|       const word = document.querySelectorAll("#words .word")[ | ||||
|         newIndex | ||||
|       ] as HTMLElement; | ||||
|  | @ -305,19 +305,19 @@ export function handleSpace(correct: boolean, currentWord: string): void { | |||
|   if (correct) { | ||||
|     if ( | ||||
|       settings !== null && | ||||
|       settings.wordsStatus[TestWords.words.currentIndex] === true && | ||||
|       settings.wordsStatus[TestState.activeWordIndex] === true && | ||||
|       !Config.blindMode | ||||
|     ) { | ||||
|       settings.wordsStatus[TestWords.words.currentIndex] = undefined; | ||||
|       settings.wordsStatus[TestState.activeWordIndex] = undefined; | ||||
|       settings.correction -= currentWord.length + 1; | ||||
|     } | ||||
|   } else { | ||||
|     if ( | ||||
|       settings !== null && | ||||
|       settings.wordsStatus[TestWords.words.currentIndex] === undefined && | ||||
|       settings.wordsStatus[TestState.activeWordIndex] === undefined && | ||||
|       !Config.blindMode | ||||
|     ) { | ||||
|       settings.wordsStatus[TestWords.words.currentIndex] = true; | ||||
|       settings.wordsStatus[TestState.activeWordIndex] = true; | ||||
|       settings.correction += currentWord.length + 1; | ||||
|     } | ||||
|   } | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| import * as TestWords from "./test-words"; | ||||
| import { lastElementFromArray } from "../utils/arrays"; | ||||
| import { mean, roundTo2 } from "@monkeytype/util/numbers"; | ||||
| import * as TestState from "./test-state"; | ||||
| 
 | ||||
| const keysToTrack = [ | ||||
|   "NumpadMultiply", | ||||
|  | @ -97,31 +97,24 @@ type ErrorHistoryObject = { | |||
| class Input { | ||||
|   current: string; | ||||
|   history: string[]; | ||||
|   historyLength: number; | ||||
|   koreanStatus: boolean; | ||||
|   length: number; | ||||
|   constructor() { | ||||
|     this.current = ""; | ||||
|     this.history = []; | ||||
|     this.historyLength = 0; | ||||
|     this.length = 0; | ||||
|     this.koreanStatus = false; | ||||
|   } | ||||
| 
 | ||||
|   reset(): void { | ||||
|     this.current = ""; | ||||
|     this.history = []; | ||||
|     this.length = 0; | ||||
|   } | ||||
| 
 | ||||
|   resetHistory(): void { | ||||
|     this.history = []; | ||||
|     this.length = 0; | ||||
|   } | ||||
| 
 | ||||
|   setCurrent(val: string): void { | ||||
|     this.current = val; | ||||
|     this.length = this.current.length; | ||||
|   } | ||||
| 
 | ||||
|   setKoreanStatus(val: boolean): void { | ||||
|  | @ -130,7 +123,6 @@ class Input { | |||
| 
 | ||||
|   appendCurrent(val: string): void { | ||||
|     this.current += val; | ||||
|     this.length = this.current.length; | ||||
|   } | ||||
| 
 | ||||
|   resetCurrent(): void { | ||||
|  | @ -147,13 +139,11 @@ class Input { | |||
| 
 | ||||
|   pushHistory(): void { | ||||
|     this.history.push(this.current); | ||||
|     this.historyLength = this.history.length; | ||||
|     this.resetCurrent(); | ||||
|   } | ||||
| 
 | ||||
|   popHistory(): string { | ||||
|     const ret = this.history.pop() ?? ""; | ||||
|     this.historyLength = this.history.length; | ||||
|     return ret; | ||||
|   } | ||||
| 
 | ||||
|  | @ -427,11 +417,11 @@ export function pushToRawHistory(raw: number): void { | |||
| } | ||||
| 
 | ||||
| export function pushBurstToHistory(speed: number): void { | ||||
|   if (burstHistory[TestWords.words.currentIndex] === undefined) { | ||||
|   if (burstHistory[TestState.activeWordIndex] === undefined) { | ||||
|     burstHistory.push(speed); | ||||
|   } else { | ||||
|     //repeated word - override
 | ||||
|     burstHistory[TestWords.words.currentIndex] = speed; | ||||
|     burstHistory[TestState.activeWordIndex] = speed; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -393,6 +393,7 @@ export async function init(): Promise<void> { | |||
|   MonkeyPower.reset(); | ||||
|   Replay.stopReplayRecording(); | ||||
|   TestWords.words.reset(); | ||||
|   TestState.setActiveWordIndex(0); | ||||
|   TestUI.setActiveWordElementIndex(0); | ||||
|   TestInput.input.resetHistory(); | ||||
|   TestInput.input.resetCurrent(); | ||||
|  | @ -563,7 +564,7 @@ export async function addWord(): Promise<void> { | |||
|   ); | ||||
| 
 | ||||
|   if (sectionFunbox?.functions?.pullSection) { | ||||
|     if (TestWords.words.length - TestWords.words.currentIndex < 20) { | ||||
|     if (TestWords.words.length - TestState.activeWordIndex < 20) { | ||||
|       const section = await sectionFunbox.functions.pullSection( | ||||
|         Config.language | ||||
|       ); | ||||
|  |  | |||
|  | @ -7,6 +7,7 @@ export let activeChallenge: null | Challenge = null; | |||
| export let savingEnabled = true; | ||||
| export let bailedOut = false; | ||||
| export let selectedQuoteId = 1; | ||||
| export let activeWordIndex = 0; | ||||
| 
 | ||||
| export function setRepeated(tf: boolean): void { | ||||
|   isRepeated = tf; | ||||
|  | @ -35,3 +36,15 @@ export function setBailedOut(tf: boolean): void { | |||
| export function setSelectedQuoteId(id: number): void { | ||||
|   selectedQuoteId = id; | ||||
| } | ||||
| 
 | ||||
| export function setActiveWordIndex(index: number): void { | ||||
|   activeWordIndex = index; | ||||
| } | ||||
| 
 | ||||
| export function increaseActiveWordIndex(): void { | ||||
|   activeWordIndex++; | ||||
| } | ||||
| 
 | ||||
| export function decreaseActiveWordIndex(): void { | ||||
|   activeWordIndex--; | ||||
| } | ||||
|  |  | |||
|  | @ -7,7 +7,6 @@ import * as TimerProgress from "./timer-progress"; | |||
| import * as LiveWpm from "./live-speed"; | ||||
| import * as TestStats from "./test-stats"; | ||||
| import * as TestInput from "./test-input"; | ||||
| import * as TestWords from "./test-words"; | ||||
| import * as Monkey from "./monkey"; | ||||
| import * as Numbers from "@monkeytype/util/numbers"; | ||||
| import * as Notifications from "../elements/notifications"; | ||||
|  | @ -137,7 +136,7 @@ function checkIfFailed( | |||
|   if ( | ||||
|     Config.minWpm === "custom" && | ||||
|     wpmAndRaw.wpm < Config.minWpmCustomSpeed && | ||||
|     TestWords.words.currentIndex > 3 | ||||
|     TestState.activeWordIndex > 3 | ||||
|   ) { | ||||
|     if (timer !== null) clearTimeout(timer); | ||||
|     SlowTimer.clear(); | ||||
|  |  | |||
|  | @ -1,16 +1,15 @@ | |||
| import { QuoteWithTextSplit } from "../controllers/quotes-controller"; | ||||
| import * as TestState from "./test-state"; | ||||
| 
 | ||||
| class Words { | ||||
|   public list: string[]; | ||||
|   public sectionIndexList: number[]; | ||||
|   public length: number; | ||||
|   public currentIndex: number; | ||||
| 
 | ||||
|   constructor() { | ||||
|     this.list = []; | ||||
|     this.sectionIndexList = []; | ||||
|     this.length = 0; | ||||
|     this.currentIndex = 0; | ||||
|   } | ||||
| 
 | ||||
|   get(i?: undefined, raw?: boolean): string[]; | ||||
|  | @ -27,7 +26,7 @@ class Words { | |||
|     } | ||||
|   } | ||||
|   getCurrent(): string { | ||||
|     return this.list[this.currentIndex] ?? ""; | ||||
|     return this.list[TestState.activeWordIndex] ?? ""; | ||||
|   } | ||||
|   getLast(): string { | ||||
|     return this.list[this.list.length - 1] as string; | ||||
|  | @ -41,18 +40,8 @@ class Words { | |||
|   reset(): void { | ||||
|     this.list = []; | ||||
|     this.sectionIndexList = []; | ||||
|     this.currentIndex = 0; | ||||
|     this.length = this.list.length; | ||||
|   } | ||||
|   resetCurrentIndex(): void { | ||||
|     this.currentIndex = 0; | ||||
|   } | ||||
|   decreaseCurrentIndex(): void { | ||||
|     this.currentIndex--; | ||||
|   } | ||||
|   increaseCurrentIndex(): void { | ||||
|     this.currentIndex++; | ||||
|   } | ||||
|   clean(): void { | ||||
|     for (const s of this.list) { | ||||
|       if (/ +/.test(s)) { | ||||
|  |  | |||
|  | @ -90,9 +90,8 @@ const miniTimerNumberElement = miniEl[0] as HTMLElement; | |||
| function getCurrentCount(): number { | ||||
|   if (Config.mode === "custom" && CustomText.getLimitMode() === "section") { | ||||
|     return ( | ||||
|       (TestWords.words.sectionIndexList[ | ||||
|         TestWords.words.currentIndex | ||||
|       ] as number) - 1 | ||||
|       (TestWords.words.sectionIndexList[TestState.activeWordIndex] as number) - | ||||
|       1 | ||||
|     ); | ||||
|   } else { | ||||
|     return TestInput.input.history.length; | ||||
|  | @ -152,7 +151,7 @@ export function update(): void { | |||
|     } | ||||
|     if (Config.timerStyle === "bar") { | ||||
|       const percent = Math.floor( | ||||
|         ((TestWords.words.currentIndex + 1) / outof) * 100 | ||||
|         ((TestState.activeWordIndex + 1) / outof) * 100 | ||||
|       ); | ||||
|       barEl.stop(true, true).animate( | ||||
|         { | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue