Skip to content

Commit bf0761a

Browse files
authored
Fix: activate the nearest path when opening a cloned note (#7552)
1 parent 8391fd7 commit bf0761a

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

apps/client/src/entities/fnote.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

apps/client/src/services/tree.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,12 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
2626
}
2727

2828
const path = notePath.split("/").reverse();
29-
30-
if (!path.includes("root")) {
31-
path.push("root");
32-
}
33-
3429
const effectivePathSegments: string[] = [];
3530
let childNoteId: string | null = null;
3631
let i = 0;
3732

38-
while (true) {
39-
if (i >= path.length) {
40-
break;
41-
}
42-
43-
const parentNoteId = path[i++];
33+
for (let i = 0; i < path.length; i++) {
34+
const parentNoteId = path[i];
4435

4536
if (childNoteId !== null) {
4637
const child = await froca.getNote(childNoteId, !logErrors);
@@ -65,7 +56,7 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
6556
return null;
6657
}
6758

68-
if (!parents.some((p) => p.noteId === parentNoteId)) {
59+
if (!parents.some(p => p.noteId === parentNoteId) || (i === path.length - 1 && parentNoteId !== 'root')) {
6960
if (logErrors) {
7061
const parent = froca.getNoteFromCache(parentNoteId);
7162

@@ -77,7 +68,8 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
7768
);
7869
}
7970

80-
const bestNotePath = child.getBestNotePath(hoistedNoteId);
71+
const activeNotePath = appContext.tabManager.getActiveContextNotePath();
72+
const bestNotePath = child.getBestNotePath(hoistedNoteId, activeNotePath);
8173

8274
if (bestNotePath) {
8375
const pathToRoot = bestNotePath.reverse().slice(1);
@@ -108,7 +100,9 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
108100
if (!note) {
109101
throw new Error(`Unable to find note: ${notePath}.`);
110102
}
111-
const bestNotePath = note.getBestNotePath(hoistedNoteId);
103+
104+
const activeNotePath = appContext.tabManager.getActiveContextNotePath();
105+
const bestNotePath = note.getBestNotePath(hoistedNoteId, activeNotePath);
112106

113107
if (!bestNotePath) {
114108
throw new Error(`Did not find any path segments for '${note.toString()}', hoisted note '${hoistedNoteId}'`);

0 commit comments

Comments
 (0)