@@ -6,6 +6,7 @@ import { t } from "./i18n.js";
66import commandRegistry from "./command_registry.js" ;
77import type { MentionFeedObjectItem } from "@triliumnext/ckeditor5" ;
88import { CreateNoteAction } from "@triliumnext/commons"
9+ import FNote from "../entities/fnote.js" ;
910
1011/**
1112 * Extends CKEditor's MentionFeedObjectItem with extra fields used by Trilium.
@@ -463,20 +464,49 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
463464
464465 // TODO: Types fail due to "autocomplete:selected" not being registered in type definitions.
465466 ( $el as any ) . on ( "autocomplete:selected" , async ( event : Event , suggestion : Suggestion ) => {
466- $el . setSelectedNotePath ( suggestion . notePath ) ;
467- $el . setSelectedExternalLink ( null ) ;
468- $el . autocomplete ( "val" , suggestion . noteTitle ) ;
467+ async function doCommand ( ) {
468+ $el . autocomplete ( "close" ) ;
469+ $el . trigger ( "autocomplete:commandselected" , [ suggestion ] ) ;
470+ }
471+
472+ async function doExternalLink ( ) {
473+ $el . setSelectedNotePath ( null ) ;
474+ $el . setSelectedExternalLink ( suggestion . externalLink ) ;
475+ $el . autocomplete ( "val" , suggestion . externalLink ) ;
476+ $el . autocomplete ( "close" ) ;
477+ $el . trigger ( "autocomplete:externallinkselected" , [ suggestion ] ) ;
478+ }
479+
480+ async function resolveSuggestionNotePathUnderCurrentHoist ( note : FNote ) {
481+ const hoisted = appContext . tabManager . getActiveContext ( ) ?. hoistedNoteId ;
482+ suggestion . notePath = note . getBestNotePathString ( hoisted ) ;
483+ }
484+
485+ async function doSearchNotes ( ) {
486+ const searchString = suggestion . noteTitle ;
487+ appContext . triggerCommand ( "searchNotes" , { searchString } ) ;
488+ }
489+
490+ async function selectNoteFromAutocomplete ( suggestion : Suggestion ) {
491+ $el . setSelectedNotePath ( suggestion . notePath ) ;
492+ $el . setSelectedExternalLink ( null ) ;
493+
494+ $el . autocomplete ( "val" , suggestion . noteTitle ) ;
495+
496+ $el . autocomplete ( "close" ) ;
497+
498+ $el . trigger ( "autocomplete:noteselected" , [ suggestion ] ) ;
499+ }
469500
470501 switch ( suggestion . action ) {
471- case SuggestionAction . Command : {
472- break ;
473- }
502+ case SuggestionAction . Command :
503+ await doCommand ( ) ;
504+ return ;
474505
475- case SuggestionAction . ExternalLink : {
506+ case SuggestionAction . ExternalLink :
507+ await doExternalLink ( ) ;
476508 break ;
477- }
478509
479- // --- CREATE NOTE INTO INBOX ---
480510 case SuggestionAction . CreateNote : {
481511 const { note } = await noteCreateService . createNote (
482512 {
@@ -487,11 +517,10 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
487517 }
488518 ) ;
489519
490- if ( ! note )
491- break ;
520+ if ( ! note ) break ;
492521
493- const hoistedNoteId = appContext . tabManager . getActiveContext ( ) ?. hoistedNoteId ;
494- suggestion . notePath = note ?. getBestNotePathString ( hoistedNoteId ) ;
522+ await resolveSuggestionNotePathUnderCurrentHoist ( note ) ;
523+ await selectNoteFromAutocomplete ( suggestion ) ;
495524 break ;
496525 }
497526
@@ -505,22 +534,19 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
505534 }
506535 ) ;
507536
508- if ( ! note )
509- break ;
510-
511- const hoistedNoteId = appContext . tabManager . getActiveContext ( ) ?. hoistedNoteId ;
512- suggestion . notePath = note ?. getBestNotePathString ( hoistedNoteId ) ;
537+ if ( ! note ) break ;
513538
514- $el . autocomplete ( "close" ) ;
515- $el . trigger ( "autocomplete:noteselected" , [ suggestion ] ) ;
516- return ;
539+ await resolveSuggestionNotePathUnderCurrentHoist ( note ) ;
540+ await selectNoteFromAutocomplete ( suggestion ) ;
541+ break ;
517542 }
518543
519544 case SuggestionAction . CreateChildNote : {
520545 if ( ! suggestion . parentNoteId ) {
521546 console . warn ( "Missing parentNoteId for CreateNoteIntoPath" ) ;
522547 return ;
523548 }
549+
524550 const { note } = await noteCreateService . createNote (
525551 {
526552 target : "into" ,
@@ -533,8 +559,8 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
533559
534560 if ( ! note ) break ;
535561
536- const hoistedNoteId = appContext . tabManager . getActiveContext ( ) ?. hoistedNoteId ;
537- suggestion . notePath = note ?. getBestNotePathString ( hoistedNoteId ) ;
562+ await resolveSuggestionNotePathUnderCurrentHoist ( note ) ;
563+ await selectNoteFromAutocomplete ( suggestion ) ;
538564 break ;
539565 }
540566
@@ -543,6 +569,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
543569 console . warn ( "Missing parentNoteId for CreateNoteIntoPath" ) ;
544570 return ;
545571 }
572+
546573 const { note } = await noteCreateService . createNote (
547574 {
548575 target : "into" ,
@@ -555,26 +582,18 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
555582
556583 if ( ! note ) break ;
557584
558- const hoistedNoteId = appContext . tabManager . getActiveContext ( ) ?. hoistedNoteId ;
559- suggestion . notePath = note ?. getBestNotePathString ( hoistedNoteId ) ;
560- $el . autocomplete ( "close" ) ;
561- $el . trigger ( "autocomplete:noteselected" , [ suggestion ] ) ;
562- return ;
585+ await resolveSuggestionNotePathUnderCurrentHoist ( note ) ;
586+ await selectNoteFromAutocomplete ( suggestion ) ;
587+ break ;
563588 }
564589
565- case SuggestionAction . SearchNotes : {
566- const searchString = suggestion . noteTitle ;
567- appContext . triggerCommand ( "searchNotes" , { searchString } ) ;
590+ case SuggestionAction . SearchNotes :
591+ await doSearchNotes ( ) ;
568592 break ;
569- }
570593
571- default : {
572- }
573- }
574- if ( suggestion . notePath ) {
575- $el . trigger ( "autocomplete:noteselected" , [ suggestion ] ) ;
594+ default :
595+ await selectNoteFromAutocomplete ( suggestion ) ;
576596 }
577- $el . autocomplete ( "close" ) ;
578597 } ) ;
579598
580599 $el . on ( "autocomplete:closed" , ( ) => {
0 commit comments