1- import { TagCategory , TagData , TagSource , autoCompleteData } from './data.js' ;
1+ import { TagCategory , TagData , TagSource , autoCompleteData , getEnabledTagSourceInPriorityOrder } from './data.js' ;
22import { settingValues } from './settings.js' ;
33import {
44 extractTagsFromTextArea ,
@@ -345,9 +345,6 @@ class RelatedTagsUI {
345345 this . target = textareaElement ;
346346
347347 this . relatedTags = searchRelatedTags ( this . currentTag ) ;
348- if ( this . selectedIndex == - 1 ) {
349- this . selectedIndex = 0 ; // Reset selection to the first item
350- }
351348
352349 this . #updateHeader( ) ;
353350 this . #updateContent( ) ;
@@ -389,10 +386,18 @@ class RelatedTagsUI {
389386 }
390387 }
391388
392- /** Moves the selection up or down */
389+ /** Moves the selection up or down
390+ * @param {direction } 1 for down, -1 for up
391+ */
393392 navigate ( direction ) {
394393 if ( this . relatedTags . length === 0 ) return ;
395- this . selectedIndex += direction ;
394+
395+ if ( this . selectedIndex == - 1 ) {
396+ // Initialize selection based on navigation direction
397+ this . selectedIndex = direction == 1 ? 0 : this . relatedTags . length - 1 ;
398+ } else {
399+ this . selectedIndex += direction ;
400+ }
396401
397402 if ( this . selectedIndex < 0 ) {
398403 this . selectedIndex = this . relatedTags . length - 1 ; // Wrap around to bottom
@@ -402,8 +407,25 @@ class RelatedTagsUI {
402407 this . #highlightItem( ) ;
403408 }
404409
405- /** Selects the currently highlighted item */
406- getSelectedTag ( ) {
410+ /**
411+ * Get TagData of the current tag
412+ * @returns {TagData|null }
413+ */
414+ getCurrentTagData ( ) {
415+ for ( const source of getEnabledTagSourceInPriorityOrder ( ) ) {
416+ if ( source in autoCompleteData && autoCompleteData [ source ] . tagMap . has ( this . currentTag ) ) {
417+ return autoCompleteData [ source ] . tagMap . get ( this . currentTag ) ;
418+ }
419+ }
420+
421+ return null ;
422+ }
423+
424+ /**
425+ * Selects the currently highlighted item
426+ * @return {TagData|null }
427+ */
428+ getSelectedTagData ( ) {
407429 if ( this . selectedIndex >= 0 && this . selectedIndex < this . relatedTags . length ) {
408430 return this . relatedTags [ this . selectedIndex ] ;
409431 }
@@ -424,14 +446,7 @@ class RelatedTagsUI {
424446 * Updates header content
425447 */
426448 #updateHeader( ) {
427- // Find the tag data for the current tag
428- let tagData = Object . values ( TagSource )
429- . map ( ( source ) => {
430- if ( source in autoCompleteData && autoCompleteData [ source ] . tagMap . has ( this . currentTag ) ) {
431- return autoCompleteData [ source ] . tagMap . get ( this . currentTag ) ;
432- }
433- } )
434- . find ( ( tagData ) => tagData !== undefined ) ;
449+ let tagData = this . getCurrentTagData ( ) ;
435450
436451 if ( ! tagData ) {
437452 // Create a dummy TagData if not found
@@ -629,7 +644,7 @@ class RelatedTagsUI {
629644
630645 /** Highlights the item (row) at the given index */
631646 #highlightItem( ) {
632- if ( this . getSelectedTag ( ) === null ) return ; // No valid selection
647+ if ( this . getSelectedTagData ( ) === null ) return ; // No valid selection
633648
634649 const items = this . tagsContainer . children ; // Get rows
635650 for ( let i = 0 ; i < items . length ; i ++ ) {
@@ -662,7 +677,7 @@ class RelatedTagsUI {
662677 }
663678
664679 insertSelectedTag ( ) {
665- const selectedTag = this . getSelectedTag ( ) ;
680+ const selectedTag = this . getSelectedTagData ( ) ;
666681 if ( selectedTag ) {
667682 this . #insertTag( selectedTag . tag ) ;
668683 }
@@ -810,7 +825,7 @@ export class RelatedTagsEventHandler {
810825 break ;
811826 case 'Enter' :
812827 case 'Tab' :
813- if ( this . relatedTagsUI . getSelectedTag ( ) !== null ) {
828+ if ( this . relatedTagsUI . getSelectedTagData ( ) !== null ) {
814829 event . preventDefault ( ) ; // Prevent Tab from changing focus
815830 this . relatedTagsUI . insertSelectedTag ( ) ;
816831 } else if ( ! this . relatedTagsUI . isPinned ) { // If nothing selected and not pinned, hide the panel
@@ -819,7 +834,7 @@ export class RelatedTagsEventHandler {
819834 break ;
820835 case 'F1' :
821836 event . preventDefault ( ) ;
822- const tagData = this . relatedTagsUI . getSelectedTag ( ) ;
837+ const tagData = this . relatedTagsUI . getSelectedTagData ( ) || this . relatedTagsUI . getCurrentTagData ( ) ;
823838 if ( tagData && tagData . hasWikiPage ) {
824839 openTagWikiUrl ( tagData . source , tagData . tag ) ;
825840 }
0 commit comments