@@ -62,12 +62,12 @@ type OnInsertTextParams = {
6262
6363export async function onInsertText ( options : OnInsertTextParams ) : Promise < void > {
6464 const { now, lastInMultiIndex, isCompositionEnding } = options ;
65- const { inputValue } = getInputElementValue ( ) ;
6665
6766 if ( options . data . length > 1 ) {
6867 // remove the entire data from the input value
6968 // make sure to not call TestInput.input.syncWithInputElement in here
7069 // it will be updated later in the body of onInsertText
70+ const { inputValue } = getInputElementValue ( ) ;
7171 setInputElementValue ( inputValue . slice ( 0 , - options . data . length ) ) ;
7272 for ( let i = 0 ; i < options . data . length ; i ++ ) {
7373 const char = options . data [ i ] as string ;
@@ -95,19 +95,17 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
9595 return ;
9696 }
9797
98+ // input and target word
99+ const testInput = TestInput . input . current ;
100+ const currentWord = TestWords . words . getCurrent ( ) ;
101+
98102 // if the character is visually equal, replace it with the target character
99103 // this ensures all future equivalence checks work correctly
100- let normalizedData : string | null = null ;
101- const targetChar =
102- TestWords . words . getCurrent ( ) [ TestInput . input . current . length ] ;
103- if (
104- targetChar !== undefined &&
105- areCharactersVisuallyEqual ( options . data , targetChar , Config . language )
106- ) {
107- replaceInputElementLastValueChar ( targetChar ) ;
108- normalizedData = targetChar ;
109- }
110-
104+ const normalizedData = normalizeDataAndUpdateInputIfNeeded (
105+ options . data ,
106+ testInput ,
107+ currentWord
108+ ) ;
111109 const data = normalizedData ?? options . data ;
112110
113111 // start if needed
@@ -118,8 +116,6 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
118116 // helper consts
119117 const lastInMultiOrSingle =
120118 lastInMultiIndex === true || lastInMultiIndex === undefined ;
121- const testInput = TestInput . input . current ;
122- const currentWord = TestWords . words . getCurrent ( ) ;
123119 const wordIndex = TestState . activeWordIndex ;
124120 const charIsSpace = isSpace ( data ) ;
125121 const charIsNewline = data === "\n" ;
@@ -135,7 +131,10 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
135131 // is char correct
136132 const funboxCorrect = findSingleActiveFunboxWithFunction (
137133 "isCharCorrect"
138- ) ?. functions . isCharCorrect ( data , currentWord [ inputValue . length ] ?? "" ) ;
134+ ) ?. functions . isCharCorrect (
135+ data ,
136+ currentWord [ ( testInput + data ) . length - 1 ] ?? ""
137+ ) ;
139138 const correct =
140139 funboxCorrect ??
141140 isCharCorrect ( {
@@ -148,7 +147,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
148147 // word navigation check
149148 const noSpaceForce =
150149 isFunboxActiveWithProperty ( "nospace" ) &&
151- TestInput . input . current . length === TestWords . words . getCurrent ( ) . length ;
150+ ( testInput + data ) . length === TestWords . words . getCurrent ( ) . length ;
152151 const shouldGoToNextWord =
153152 ( ( charIsSpace || charIsNewline ) && ! shouldInsertSpace ) || noSpaceForce ;
154153
@@ -286,6 +285,23 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
286285 }
287286}
288287
288+ function normalizeDataAndUpdateInputIfNeeded (
289+ data : string ,
290+ testInput : string ,
291+ currentWord : string
292+ ) : string | null {
293+ let normalizedData : string | null = null ;
294+ const targetChar = currentWord [ testInput . length ] ;
295+ if (
296+ targetChar !== undefined &&
297+ areCharactersVisuallyEqual ( data , targetChar , Config . language )
298+ ) {
299+ replaceInputElementLastValueChar ( targetChar ) ;
300+ normalizedData = targetChar ;
301+ }
302+ return normalizedData ;
303+ }
304+
289305export async function emulateInsertText (
290306 options : OnInsertTextParams
291307) : Promise < void > {
0 commit comments