@@ -417,7 +417,7 @@ export default class FNote {
417417 return notePaths ;
418418 }
419419
420- getSortedNotePathRecords ( hoistedNoteId = "root" ) : NotePathRecord [ ] {
420+ getSortedNotePathRecords ( hoistedNoteId = "root" , activeNotePath : string | null = null ) : NotePathRecord [ ] {
421421 const isHoistedRoot = hoistedNoteId === "root" ;
422422
423423 const notePaths : NotePathRecord [ ] = this . getAllNotePaths ( ) . map ( ( path ) => ( {
@@ -428,7 +428,23 @@ export default class FNote {
428428 isHidden : path . includes ( "_hidden" )
429429 } ) ) ;
430430
431+ // Calculate the length of the prefix match between two arrays
432+ const prefixMatchLength = ( path : string [ ] , target : string [ ] ) => {
433+ const diffIndex = path . findIndex ( ( seg , i ) => seg !== target [ i ] ) ;
434+ return diffIndex === - 1 ? Math . min ( path . length , target . length ) : diffIndex ;
435+ } ;
436+
431437 notePaths . sort ( ( a , b ) => {
438+ if ( activeNotePath ) {
439+ const activeSegments = activeNotePath . split ( '/' ) ;
440+ const aOverlap = prefixMatchLength ( a . notePath , activeSegments ) ;
441+ const bOverlap = prefixMatchLength ( b . notePath , activeSegments ) ;
442+ // Paths with more matching prefix segments are prioritized
443+ // when the match count is equal, other criteria are used for sorting
444+ if ( bOverlap !== aOverlap ) {
445+ return bOverlap - aOverlap ;
446+ }
447+ }
432448 if ( a . isInHoistedSubTree !== b . isInHoistedSubTree ) {
433449 return a . isInHoistedSubTree ? - 1 : 1 ;
434450 } else if ( a . isArchived !== b . isArchived ) {
@@ -449,10 +465,11 @@ export default class FNote {
449465 * Returns the note path considered to be the "best"
450466 *
451467 * @param {string } [hoistedNoteId='root']
468+ * @param {string|null } [activeNotePath=null]
452469 * @return {string[] } array of noteIds constituting the particular note path
453470 */
454- getBestNotePath ( hoistedNoteId = "root" ) {
455- return this . getSortedNotePathRecords ( hoistedNoteId ) [ 0 ] ?. notePath ;
471+ getBestNotePath ( hoistedNoteId = "root" , activeNotePath : string | null = null ) {
472+ return this . getSortedNotePathRecords ( hoistedNoteId , activeNotePath ) [ 0 ] ?. notePath ;
456473 }
457474
458475 /**
0 commit comments