@@ -119,31 +119,30 @@ function updateUI(): void {
119119function backspaceToPrevious ( ) : void {
120120 if ( ! TestState . isActive ) return ;
121121
122- const wordElementIndex =
123- TestState . activeWordIndex - TestState . removedUIWordCount ;
122+ const previousWordEl = TestUI . getWordElement ( TestState . activeWordIndex - 1 ) ;
124123
125- if ( TestInput . input . getHistory ( ) . length === 0 || wordElementIndex === 0 ) {
126- return ;
127- }
124+ const isFirstWord = TestInput . input . getHistory ( ) . length === 0 ;
125+ const isFirstVisibleWord = previousWordEl === null ;
126+ const isPreviousWordHidden = previousWordEl ?. classList . contains ( "hidden" ) ;
127+ const isPreviousWordCorrect =
128+ TestInput . input . getHistory ( TestState . activeWordIndex - 1 ) ===
129+ TestWords . words . get ( TestState . activeWordIndex - 1 ) ;
128130
129- const wordElements = document . querySelectorAll ( "#words > .word" ) ;
130131 if (
131- ( TestInput . input . getHistory ( TestState . activeWordIndex - 1 ) ===
132- TestWords . words . get ( TestState . activeWordIndex - 1 ) &&
133- ! Config . freedomMode ) ||
134- wordElements [ wordElementIndex - 1 ] ?. classList . contains ( "hidden" )
132+ isFirstWord ||
133+ isFirstVisibleWord ||
134+ isPreviousWordHidden ||
135+ ( isPreviousWordCorrect && ! Config . freedomMode ) ||
136+ Config . confidenceMode === "on" ||
137+ Config . confidenceMode === "max"
135138 ) {
136139 return ;
137140 }
138141
139- if ( Config . confidenceMode === "on" || Config . confidenceMode === "max" ) {
140- return ;
141- }
142+ const activeWordEl = TestUI . getActiveWordElement ( ) ;
142143
143144 const incorrectLetterBackspaced =
144- wordElements [ wordElementIndex ] ?. children [ 0 ] ?. classList . contains (
145- "incorrect"
146- ) ;
145+ activeWordEl ?. children [ 0 ] ?. classList . contains ( "incorrect" ) ;
147146 if ( Config . stopOnError === "letter" && incorrectLetterBackspaced ) {
148147 void TestUI . updateActiveWordLetters ( ) ;
149148 }
@@ -266,14 +265,10 @@ async function handleSpace(): Promise<void> {
266265 PaceCaret . handleSpace ( false , currentWord ) ;
267266 if ( Config . blindMode ) {
268267 if ( Config . highlightMode !== "off" ) {
269- TestUI . highlightAllLettersAsCorrect (
270- TestState . activeWordIndex - TestState . removedUIWordCount
271- ) ;
268+ TestUI . highlightAllLettersAsCorrect ( TestState . activeWordIndex ) ;
272269 }
273270 } else {
274- TestUI . highlightBadWord (
275- TestState . activeWordIndex - TestState . removedUIWordCount
276- ) ;
271+ TestUI . highlightBadWord ( TestState . activeWordIndex ) ;
277272 }
278273 TestInput . input . pushHistory ( ) ;
279274 TestState . increaseActiveWordIndex ( ) ;
@@ -340,17 +335,11 @@ async function handleSpace(): Promise<void> {
340335
341336 if ( ! Config . showAllLines || shouldLimitToThreeLines ) {
342337 const currentTop : number = Math . floor (
343- document . querySelectorAll < HTMLElement > ( "#words .word" ) [
344- TestState . activeWordIndex - TestState . removedUIWordCount - 1
345- ] ?. offsetTop ?? 0
338+ TestUI . getWordElement ( TestState . activeWordIndex - 1 ) ?. offsetTop ?? 0
346339 ) ;
347340
348341 const { data : nextTop } = tryCatchSync ( ( ) =>
349- Math . floor (
350- document . querySelectorAll < HTMLElement > ( "#words .word" ) [
351- TestState . activeWordIndex - TestState . removedUIWordCount
352- ] ?. offsetTop ?? 0
353- )
342+ Math . floor ( TestUI . getActiveWordElement ( ) ?. offsetTop ?? 0 )
354343 ) ;
355344
356345 if ( ( nextTop ?? 0 ) > currentTop ) {
@@ -665,9 +654,7 @@ async function handleChar(
665654 char
666655 ) ;
667656
668- const activeWord = document . querySelectorAll ( "#words .word" ) ?. [
669- TestState . activeWordIndex - TestState . removedUIWordCount
670- ] as HTMLElement ;
657+ const activeWord = TestUI . getActiveWordElement ( ) as HTMLElement ;
671658
672659 const testInputLength : number = ! isCharKorean
673660 ? TestInput . input . current . length
@@ -1126,11 +1113,7 @@ $(document).on("keydown", async (event) => {
11261113 //show dead keys
11271114 if ( event . key === "Dead" && ! CompositionState . getComposing ( ) ) {
11281115 void Sound . playClick ( ) ;
1129- const activeWord : HTMLElement | null = document . querySelectorAll (
1130- "#words .word"
1131- ) ?. [
1132- TestState . activeWordIndex - TestState . removedUIWordCount
1133- ] as HTMLElement ;
1116+ const activeWord = TestUI . getActiveWordElement ( ) ;
11341117 const len : number = TestInput . input . current . length ; // have to do this because prettier wraps the line and causes an error
11351118
11361119 // Check to see if the letter actually exists to toggle it as dead
0 commit comments