refactoring of "some path" WIP

This commit is contained in:
zadam
2023-04-15 00:06:13 +02:00
parent 5e88e24693
commit 7aa26580ba
17 changed files with 193 additions and 124 deletions

View File

@@ -79,14 +79,10 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
You can ignore this message as it is mostly harmless.`);
}
const someNotePath = getSomeNotePath(child, hoistedNoteId);
const bestNotePath = child.getBestNotePath(hoistedNoteId);
if (someNotePath) { // in case it's root the path may be empty
const pathToRoot = someNotePath.split("/").reverse().slice(1);
if (!pathToRoot.includes("root")) {
pathToRoot.push('root');
}
if (bestNotePath) {
const pathToRoot = bestNotePath.reverse().slice(1);
for (const noteId of pathToRoot) {
effectivePathSegments.push(noteId);
@@ -109,31 +105,17 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
else {
const note = await froca.getNote(getNoteIdFromNotePath(notePath));
const someNotePathSegments = getSomeNotePathSegments(note, hoistedNoteId);
const bestNotePath = note.getBestNotePath(hoistedNoteId);
if (!someNotePathSegments) {
throw new Error(`Did not find any path segments for ${note.toString()}, hoisted note ${hoistedNoteId}`);
if (!bestNotePath) {
throw new Error(`Did not find any path segments for '${note.toString()}', hoisted note '${hoistedNoteId}'`);
}
// if there isn't actually any note path with hoisted note then return the original resolved note path
return someNotePathSegments.includes(hoistedNoteId) ? someNotePathSegments : effectivePathSegments;
return bestNotePath.includes(hoistedNoteId) ? bestNotePath : effectivePathSegments;
}
}
function getSomeNotePathSegments(note, hoistedNotePath = 'root') {
utils.assertArguments(note);
const notePaths = note.getSortedNotePaths(hoistedNotePath);
return notePaths.length > 0 ? notePaths[0].notePath : null;
}
function getSomeNotePath(note, hoistedNotePath = 'root') {
const notePath = getSomeNotePathSegments(note, hoistedNotePath);
return notePath === null ? null : notePath.join('/');
}
ws.subscribeToMessages(message => {
if (message.type === 'openNote') {
appContext.tabManager.activateOrOpenNote(message.noteId);
@@ -311,16 +293,6 @@ function isNotePathInAddress() {
|| (notePath === '' && !!ntxId);
}
function parseNotePath(notePath) {
let noteIds = notePath.split('/');
if (noteIds[0] !== 'root') {
noteIds = ['root'].concat(noteIds);
}
return noteIds;
}
function isNotePathInHiddenSubtree(notePath) {
return notePath?.includes("root/_hidden");
}
@@ -328,8 +300,6 @@ function isNotePathInHiddenSubtree(notePath) {
export default {
resolveNotePath,
resolveNotePathToSegments,
getSomeNotePath,
getSomeNotePathSegments,
getParentProtectedStatus,
getNotePath,
getNoteIdFromNotePath,
@@ -340,6 +310,5 @@ export default {
getNoteTitleWithPathAsSuffix,
getHashValueFromAddress,
isNotePathInAddress,
parseNotePath,
isNotePathInHiddenSubtree
};