@@ -430,22 +430,25 @@ <h2>輸入法模式:</h2>
430430 modeDisplay . textContent = `模式:${ selectedMode } ` ;
431431 }
432432
433- function parseCin ( cinText ) {
433+ function parseFile ( fileText ) {
434434 currentCodemap = { } ;
435435 currentReverseCodemap = { } ;
436436
437- const lines = cinText . split ( / \r ? \n / ) ;
437+ const lines = fileText . split ( / \r ? \n / ) ;
438438 let inDataSection = false ;
439439
440440 for ( const line of lines ) {
441441 const trimmedLine = line . trim ( ) ;
442- if ( trimmedLine . includes ( '[data]' ) || trimmedLine . includes ( '%chardef begin' ) ) {
442+
443+ // 檢查兩種檔案的資料區段開頭
444+ if ( trimmedLine . startsWith ( '%chardef begin' ) || trimmedLine . startsWith ( '[data]' ) ) {
443445 inDataSection = true ;
444446 continue ;
445447 }
446- if ( trimmedLine . includes ( '%chardef end' ) ) {
448+
449+ // 檢查資料區段結尾
450+ if ( trimmedLine . startsWith ( '%chardef end' ) || trimmedLine === '' ) {
447451 inDataSection = false ;
448- break ;
449452 }
450453
451454 if ( inDataSection && trimmedLine !== '' && ! trimmedLine . startsWith ( '#' ) ) {
@@ -457,7 +460,12 @@ <h2>輸入法模式:</h2>
457460 if ( ! currentCodemap [ code ] ) {
458461 currentCodemap [ code ] = [ ] ;
459462 }
460- currentCodemap [ code ] . push ( ...characters ) ;
463+ // 確保不重複添加
464+ characters . forEach ( char => {
465+ if ( ! currentCodemap [ code ] . includes ( char ) ) {
466+ currentCodemap [ code ] . push ( char ) ;
467+ }
468+ } ) ;
461469
462470 characters . forEach ( char => {
463471 if ( ! currentReverseCodemap [ char ] ) {
@@ -473,17 +481,17 @@ <h2>輸入法模式:</h2>
473481 console . log ( "編碼資料解析完成。" ) ;
474482 resetInputState ( ) ;
475483 }
476-
477- function loadCinFile ( filename ) {
484+
485+ function loadFile ( filename ) {
478486 fetch ( filename )
479487 . then ( response => {
480488 if ( ! response . ok ) {
481- throw new Error ( `載入編碼檔失敗,請檢查檔案路徑。 ` ) ;
489+ throw new Error ( `載入編碼檔失敗,請檢查檔案路徑: ${ filename } ` ) ;
482490 }
483491 return response . text ( ) ;
484492 } )
485493 . then ( text => {
486- parseCin ( text ) ;
494+ parseFile ( text ) ;
487495 } )
488496 . catch ( error => {
489497 console . error ( error ) ;
@@ -531,9 +539,9 @@ <h2>輸入法模式:</h2>
531539
532540 function switchInputMethod ( methodName ) {
533541 if ( methodName === 'primary' ) {
534- loadCinFile ( '3code.cin' ) ;
542+ loadFile ( '3code.cin' ) ;
535543 } else if ( methodName === 'secondary' ) {
536- loadCinFile ( '3code2.txt' ) ;
544+ loadFile ( '3code2.txt' ) ;
537545 }
538546 updateModeDisplay ( ) ;
539547 }
@@ -560,7 +568,7 @@ <h2>輸入法模式:</h2>
560568 if ( event . key >= '1' && event . key <= '9' ) {
561569 const selectedIndex = parseInt ( event . key ) - 1 ;
562570 if ( selectedIndex < candidates . length ) {
563- textOutput . textContent += candidates [ selectedIndex ] . substring ( candidates [ selectedIndex ] . indexOf ( '.' ) + 2 ) ;
571+ textOutput . textContent += candidates [ selectedIndex ] ;
564572 resetInputState ( ) ;
565573 }
566574 return ;
@@ -578,7 +586,7 @@ <h2>輸入法模式:</h2>
578586 if ( key === ' ' || key === 'space' ) {
579587 event . preventDefault ( ) ;
580588 if ( candidates . length > 0 ) {
581- textOutput . textContent += candidates [ 0 ] . substring ( candidates [ 0 ] . indexOf ( '.' ) + 2 ) ;
589+ textOutput . textContent += candidates [ 0 ] ;
582590 }
583591 resetInputState ( ) ;
584592 return ;
@@ -630,7 +638,7 @@ <h2>輸入法模式:</h2>
630638
631639 if ( code === 'space' ) {
632640 if ( candidates . length > 0 ) {
633- textOutput . textContent += candidates [ 0 ] . substring ( candidates [ 0 ] . indexOf ( '.' ) + 2 ) ;
641+ textOutput . textContent += candidates [ 0 ] ;
634642 }
635643 resetInputState ( ) ;
636644 return ;
0 commit comments