@@ -789,14 +789,12 @@ <h2>查碼練習</h2>
789789 if ( currentPage > 0 ) {
790790 currentPage -- ;
791791 showCandidates ( ) ;
792- updatePhraseLookup ( ) ;
793792 }
794793 } else if ( direction === 'next' ) {
795794 const totalPages = Math . ceil ( currentCandidates . length / candidatesPerPage ) ;
796795 if ( currentPage + 1 < totalPages ) {
797796 currentPage ++ ;
798797 showCandidates ( ) ;
799- updatePhraseLookup ( ) ;
800798 }
801799 }
802800 }
@@ -1003,21 +1001,29 @@ <h2>查碼練習</h2>
10031001 } ) ;
10041002
10051003 function updatePhraseLookup ( ) {
1006- const textToSearch = phraseInput . value ;
1007- const textToCompare = textOutput . value ;
1004+ let textToSearch ;
1005+ let cursorPosition ;
1006+
1007+ // 判斷哪個輸入區塊有焦點
1008+ if ( phraseInput === document . activeElement ) {
1009+ textToSearch = phraseInput . value ;
1010+ cursorPosition = phraseInput . selectionStart ;
1011+ } else if ( textOutput === document . activeElement ) {
1012+ textToSearch = textOutput . value ;
1013+ cursorPosition = textOutput . selectionStart ;
1014+ } else {
1015+ // 如果都沒有焦點,則使用 phraseInput 的內容
1016+ textToSearch = phraseInput . value ;
1017+ cursorPosition = 0 ;
1018+ }
1019+
10081020 encodingResults . innerHTML = '' ;
10091021
1010- let correctCharsCount = 0 ;
1011- for ( let i = 0 ; i < textToCompare . length && i < textToSearch . length ; i ++ ) {
1012- if ( textToCompare [ i ] === textToSearch [ i ] ) {
1013- correctCharsCount ++ ;
1014- } else {
1015- break ;
1016- }
1017- }
1018- const lookupStartIndex = Math . floor ( correctCharsCount / 10 ) * 10 ;
1022+ // 根據游標位置計算查碼區塊的起始索引
1023+ const lookupStartIndex = Math . floor ( cursorPosition / 10 ) * 10 ;
10191024 const characters = textToSearch . split ( '' ) . slice ( lookupStartIndex , lookupStartIndex + 10 ) ;
10201025
1026+ // 固定顯示10個欄位
10211027 for ( let i = 0 ; i < 10 ; i ++ ) {
10221028 const column = document . createElement ( 'div' ) ;
10231029 column . className = 'char-encoding-column' ;
@@ -1028,14 +1034,10 @@ <h2>查碼練習</h2>
10281034 if ( i < characters . length ) {
10291035 const char = characters [ i ] ;
10301036 charLabel . textContent = char ;
1031- const globalIndex = lookupStartIndex + i ;
1037+ const results = currentReverseCodemap [ char ] ;
10321038
1033- if ( globalIndex < textToCompare . length ) {
1034- if ( char === textToCompare [ globalIndex ] ) {
1035- charLabel . classList . add ( 'correct-char' ) ;
1036- } else {
1037- charLabel . classList . add ( 'incorrect-char' ) ;
1038- }
1039+ if ( results && results . length > 0 ) {
1040+ charLabel . classList . add ( 'correct-char' ) ;
10391041 } else {
10401042 charLabel . classList . add ( 'incorrect-char' ) ;
10411043 }
@@ -1092,7 +1094,12 @@ <h2>查碼練習</h2>
10921094 }
10931095
10941096 phraseInput . addEventListener ( 'input' , updatePhraseLookup ) ;
1097+ phraseInput . addEventListener ( 'keyup' , updatePhraseLookup ) ;
1098+ phraseInput . addEventListener ( 'click' , updatePhraseLookup ) ;
1099+
10951100 textOutput . addEventListener ( 'input' , updatePhraseLookup ) ;
1101+ textOutput . addEventListener ( 'keyup' , updatePhraseLookup ) ;
1102+ textOutput . addEventListener ( 'click' , updatePhraseLookup ) ;
10961103
10971104 switchInputMethod ( 'primary' ) ;
10981105 updateKeyboardDisplay ( ) ;
0 commit comments