@@ -35,12 +35,15 @@ export function hide(): void {
3535 caret . classList . add ( "hidden" ) ;
3636}
3737
38- export function getSpaceWidth ( wordElement ?: HTMLElement ) : number {
39- if ( ! wordElement )
40- wordElement = document
41- . getElementById ( "words" )
42- ?. querySelectorAll ( ".word" ) ?. [ 0 ] as HTMLElement | undefined ;
43- if ( ! wordElement ) return 0 ;
38+ function getSpaceWidth ( wordElement ?: HTMLElement ) : number {
39+ if ( ! wordElement ) {
40+ const el = document . querySelector < HTMLElement > ( "#words .word" ) ;
41+ if ( el ) {
42+ wordElement = el ;
43+ } else {
44+ return 0 ;
45+ }
46+ }
4447 const wordComputedStyle = window . getComputedStyle ( wordElement ) ;
4548 return (
4649 parseInt ( wordComputedStyle . marginRight ) +
@@ -52,8 +55,7 @@ function getTargetPositionLeft(
5255 fullWidthCaret : boolean ,
5356 isLanguageRightToLeft : boolean ,
5457 activeWordElement : HTMLElement ,
55- activeWordEmpty : boolean ,
56- currentWordNodeList : NodeListOf < Element > ,
58+ currentWordNodeList : NodeListOf < HTMLElement > ,
5759 fullWidthCaretWidth : number ,
5860 wordLen : number ,
5961 inputLen : number
@@ -64,21 +66,15 @@ function getTargetPositionLeft(
6466 if ( Config . tapeMode === "off" ) {
6567 let positionOffsetToWord = 0 ;
6668
67- const currentLetter = currentWordNodeList [ inputLen ] as
68- | HTMLElement
69- | undefined ;
70- const lastWordLetter = currentWordNodeList [ wordLen - 1 ] as
71- | HTMLElement
72- | undefined ;
73- const lastInputLetter = currentWordNodeList [ inputLen - 1 ] as
74- | HTMLElement
75- | undefined ;
69+ const currentLetter = currentWordNodeList [ inputLen ] ;
70+ const lastWordLetter = currentWordNodeList [ wordLen - 1 ] ;
71+ const lastInputLetter = currentWordNodeList [ inputLen - 1 ] ;
7672
7773 if ( isLanguageRightToLeft ) {
78- if ( inputLen < wordLen && currentLetter ) {
74+ if ( inputLen <= wordLen && currentLetter ) {
75+ // at word beginning in zen mode both lengths are 0, but currentLetter is defined "_"
7976 positionOffsetToWord =
80- currentLetter ?. offsetLeft +
81- ( fullWidthCaret ? 0 : fullWidthCaretWidth ) ;
77+ currentLetter . offsetLeft + ( fullWidthCaret ? 0 : fullWidthCaretWidth ) ;
8278 } else if ( ! invisibleExtraLetters ) {
8379 positionOffsetToWord =
8480 ( lastInputLetter ?. offsetLeft ?? 0 ) -
@@ -90,7 +86,7 @@ function getTargetPositionLeft(
9086 }
9187 } else {
9288 if ( inputLen < wordLen && currentLetter ) {
93- positionOffsetToWord = currentLetter ? .offsetLeft ;
89+ positionOffsetToWord = currentLetter . offsetLeft ;
9490 } else if ( ! invisibleExtraLetters ) {
9591 positionOffsetToWord =
9692 ( lastInputLetter ?. offsetLeft ?? 0 ) +
@@ -102,8 +98,6 @@ function getTargetPositionLeft(
10298 }
10399 }
104100 result = activeWordElement . offsetLeft + positionOffsetToWord ;
105- if ( activeWordEmpty && isLanguageRightToLeft )
106- result += activeWordElement . offsetWidth ;
107101 } else {
108102 const wordsWrapperWidth =
109103 $ ( document . querySelector ( "#wordsWrapper" ) as HTMLElement ) . width ( ) ?? 0 ;
@@ -150,22 +144,18 @@ export async function updatePosition(noAnim = false): Promise<void> {
150144 let wordLen = splitIntoCharacters ( TestWords . words . getCurrent ( ) ) . length ;
151145 const inputLen = splitIntoCharacters ( TestInput . input . current ) . length ;
152146 if ( Config . mode === "zen" ) wordLen = inputLen ;
153- const activeWordEl = document ?. querySelector ( "#words .active" ) as HTMLElement ;
154- let activeWordEmpty = false ;
155- if ( Config . mode === "zen" ) {
156- wordLen = inputLen ;
157- if ( inputLen === 0 ) activeWordEmpty = true ;
158- }
159-
160- const currentWordNodeList = activeWordEl ? .querySelectorAll ( "letter" ) ;
147+ const activeWordEl =
148+ document . querySelectorAll < HTMLElement > ( "#words .word" ) [
149+ TestState . activeWordIndex - TestState . removedUIWordCount
150+ ] ;
151+ if ( ! activeWordEl ) return ;
152+
153+ const currentWordNodeList =
154+ activeWordEl . querySelectorAll < HTMLElement > ( "letter" ) ;
161155 if ( ! currentWordNodeList ?. length ) return ;
162156
163- const currentLetter = currentWordNodeList [ inputLen ] as
164- | HTMLElement
165- | undefined ;
166- const lastWordLetter = currentWordNodeList [ wordLen - 1 ] as
167- | HTMLElement
168- | undefined ;
157+ const currentLetter = currentWordNodeList [ inputLen ] ;
158+ const lastWordLetter = currentWordNodeList [ wordLen - 1 ] ;
169159
170160 const currentLanguage = await JSONData . getCurrentLanguage ( Config . language ) ;
171161 const isLanguageRightToLeft = currentLanguage . rightToLeft ;
@@ -187,11 +177,11 @@ export async function updatePosition(noAnim = false): Promise<void> {
187177 }
188178
189179 let letterWidth = currentLetter ?. offsetWidth ;
190- if ( letterWidth === undefined || activeWordEmpty ) {
180+ if ( letterWidth === undefined || wordLen === 0 ) {
181+ // at word beginning in zen mode current letter is defined "_" but wordLen is 0
191182 letterWidth = getSpaceWidth ( activeWordEl ) ;
192183 } else if ( letterWidth === 0 ) {
193184 // current letter is a zero-width character e.g, diacritics)
194- letterWidth = 0 ;
195185 for ( let i = inputLen ; i >= 0 ; i -- ) {
196186 letterWidth = ( currentWordNodeList [ i ] as HTMLElement ) ?. offsetWidth ;
197187 if ( letterWidth ) break ;
@@ -203,23 +193,19 @@ export async function updatePosition(noAnim = false): Promise<void> {
203193 fullWidthCaret ,
204194 isLanguageRightToLeft ,
205195 activeWordEl ,
206- activeWordEmpty ,
207196 currentWordNodeList ,
208197 letterWidth ,
209198 wordLen ,
210199 inputLen
211200 ) ;
212201 const newLeft = letterPosLeft - ( fullWidthCaret ? 0 : caretWidth / 2 ) ;
213202
214- let smoothlinescroll = $ ( "#words .smoothScroller" ) . height ( ) ;
215- if ( smoothlinescroll === undefined ) smoothlinescroll = 0 ;
216-
217203 const jqcaret = $ ( caret ) ;
218204
219205 jqcaret . css ( "display" , "block" ) ; //for some goddamn reason adding width animation sets the display to none ????????
220206
221207 const animation : { top : number ; left : number ; width ?: string } = {
222- top : newTop - smoothlinescroll ,
208+ top : newTop - TestState . lineScrollDistance ,
223209 left : newLeft ,
224210 } ;
225211
0 commit comments