Skip to content

Commit 091ec15

Browse files
feat: Improve related tags selection with F1 key wiki access support
1 parent c87d92d commit 091ec15

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

web/js/autocomplete.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class AutocompleteUI {
490490
/** Selects the currently highlighted item
491491
* @returns {TagData|null} The selected tag data.
492492
*/
493-
getSelectedTag() {
493+
getSelectedTagData() {
494494
if (this.selectedIndex >= 0 && this.selectedIndex < this.candidates.length) {
495495
return this.candidates[this.selectedIndex];
496496
}
@@ -695,7 +695,7 @@ class AutocompleteUI {
695695

696696
/** Highlights the item (row) at the given index */
697697
#highlightItem() {
698-
if (!this.getSelectedTag()) return; // No valid selection
698+
if (!this.getSelectedTagData()) return; // No valid selection
699699

700700
const items = this.tagsList.children; // Get rows from tbody
701701
for (let i = 0; i < items.length; i++) {
@@ -996,15 +996,15 @@ export class AutocompleteEventHandler {
996996
case 'Enter':
997997
case 'Tab':
998998
const modifierKeyPressed = event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
999-
if (!modifierKeyPressed && this.autocompleteUI.getSelectedTag() !== null) {
999+
if (!modifierKeyPressed && this.autocompleteUI.getSelectedTagData() !== null) {
10001000
event.preventDefault();
1001-
insertTagToTextArea(event.target, this.autocompleteUI.getSelectedTag());
1001+
insertTagToTextArea(event.target, this.autocompleteUI.getSelectedTagData());
10021002
}
10031003
this.autocompleteUI.hide();
10041004
break;
10051005
case 'F1':
10061006
event.preventDefault();
1007-
const tagData = this.autocompleteUI.getSelectedTag();
1007+
const tagData = this.autocompleteUI.getSelectedTagData();
10081008
if (tagData && tagData.hasWikiPage) {
10091009
openTagWikiUrl(tagData.source, tagData.tag);
10101010
}

web/js/related-tags.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TagCategory, TagData, TagSource, autoCompleteData } from './data.js';
1+
import { TagCategory, TagData, TagSource, autoCompleteData, getEnabledTagSourceInPriorityOrder } from './data.js';
22
import { settingValues } from './settings.js';
33
import {
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

Comments
 (0)