Fix: activate the nearest path when opening a cloned note (#7552)

This commit is contained in:
SngAbc 2025-11-01 22:06:26 +08:00 committed by GitHub
parent 8391fd7534
commit bf0761a303
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 17 deletions

View File

@ -417,7 +417,7 @@ export default class FNote {
return notePaths; return notePaths;
} }
getSortedNotePathRecords(hoistedNoteId = "root"): NotePathRecord[] { getSortedNotePathRecords(hoistedNoteId = "root", activeNotePath: string | null = null): NotePathRecord[] {
const isHoistedRoot = hoistedNoteId === "root"; const isHoistedRoot = hoistedNoteId === "root";
const notePaths: NotePathRecord[] = this.getAllNotePaths().map((path) => ({ const notePaths: NotePathRecord[] = this.getAllNotePaths().map((path) => ({
@ -428,7 +428,23 @@ export default class FNote {
isHidden: path.includes("_hidden") isHidden: path.includes("_hidden")
})); }));
// Calculate the length of the prefix match between two arrays
const prefixMatchLength = (path: string[], target: string[]) => {
const diffIndex = path.findIndex((seg, i) => seg !== target[i]);
return diffIndex === -1 ? Math.min(path.length, target.length) : diffIndex;
};
notePaths.sort((a, b) => { notePaths.sort((a, b) => {
if (activeNotePath) {
const activeSegments = activeNotePath.split('/');
const aOverlap = prefixMatchLength(a.notePath, activeSegments);
const bOverlap = prefixMatchLength(b.notePath, activeSegments);
// Paths with more matching prefix segments are prioritized
// when the match count is equal, other criteria are used for sorting
if (bOverlap !== aOverlap) {
return bOverlap - aOverlap;
}
}
if (a.isInHoistedSubTree !== b.isInHoistedSubTree) { if (a.isInHoistedSubTree !== b.isInHoistedSubTree) {
return a.isInHoistedSubTree ? -1 : 1; return a.isInHoistedSubTree ? -1 : 1;
} else if (a.isArchived !== b.isArchived) { } else if (a.isArchived !== b.isArchived) {
@ -449,10 +465,11 @@ export default class FNote {
* Returns the note path considered to be the "best" * Returns the note path considered to be the "best"
* *
* @param {string} [hoistedNoteId='root'] * @param {string} [hoistedNoteId='root']
* @param {string|null} [activeNotePath=null]
* @return {string[]} array of noteIds constituting the particular note path * @return {string[]} array of noteIds constituting the particular note path
*/ */
getBestNotePath(hoistedNoteId = "root") { getBestNotePath(hoistedNoteId = "root", activeNotePath: string | null = null) {
return this.getSortedNotePathRecords(hoistedNoteId)[0]?.notePath; return this.getSortedNotePathRecords(hoistedNoteId, activeNotePath)[0]?.notePath;
} }
/** /**

View File

@ -26,21 +26,12 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
} }
const path = notePath.split("/").reverse(); const path = notePath.split("/").reverse();
if (!path.includes("root")) {
path.push("root");
}
const effectivePathSegments: string[] = []; const effectivePathSegments: string[] = [];
let childNoteId: string | null = null; let childNoteId: string | null = null;
let i = 0; let i = 0;
while (true) { for (let i = 0; i < path.length; i++) {
if (i >= path.length) { const parentNoteId = path[i];
break;
}
const parentNoteId = path[i++];
if (childNoteId !== null) { if (childNoteId !== null) {
const child = await froca.getNote(childNoteId, !logErrors); const child = await froca.getNote(childNoteId, !logErrors);
@ -65,7 +56,7 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
return null; return null;
} }
if (!parents.some((p) => p.noteId === parentNoteId)) { if (!parents.some(p => p.noteId === parentNoteId) || (i === path.length - 1 && parentNoteId !== 'root')) {
if (logErrors) { if (logErrors) {
const parent = froca.getNoteFromCache(parentNoteId); const parent = froca.getNoteFromCache(parentNoteId);
@ -77,7 +68,8 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
); );
} }
const bestNotePath = child.getBestNotePath(hoistedNoteId); const activeNotePath = appContext.tabManager.getActiveContextNotePath();
const bestNotePath = child.getBestNotePath(hoistedNoteId, activeNotePath);
if (bestNotePath) { if (bestNotePath) {
const pathToRoot = bestNotePath.reverse().slice(1); const pathToRoot = bestNotePath.reverse().slice(1);
@ -108,7 +100,9 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root
if (!note) { if (!note) {
throw new Error(`Unable to find note: ${notePath}.`); throw new Error(`Unable to find note: ${notePath}.`);
} }
const bestNotePath = note.getBestNotePath(hoistedNoteId);
const activeNotePath = appContext.tabManager.getActiveContextNotePath();
const bestNotePath = note.getBestNotePath(hoistedNoteId, activeNotePath);
if (!bestNotePath) { if (!bestNotePath) {
throw new Error(`Did not find any path segments for '${note.toString()}', hoisted note '${hoistedNoteId}'`); throw new Error(`Did not find any path segments for '${note.toString()}', hoisted note '${hoistedNoteId}'`);