Notes/src/becca/becca_service.ts

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
import becca from "./becca.js";
import cls from "../services/cls.js";
import log from "../services/log.js";
2020-05-17 09:48:24 +02:00
2024-02-17 20:30:21 +02:00
function isNotePathArchived(notePath: string[]) {
2020-05-16 23:12:29 +02:00
const noteId = notePath[notePath.length - 1];
2021-04-16 23:00:08 +02:00
const note = becca.notes[noteId];
2020-05-16 23:12:29 +02:00
2021-05-18 20:56:49 +02:00
if (note.isArchived) {
2020-05-16 23:12:29 +02:00
return true;
}
for (let i = 0; i < notePath.length - 1; i++) {
2021-04-16 23:00:08 +02:00
const note = becca.notes[notePath[i]];
2020-05-16 23:12:29 +02:00
// this is going through parents so archived must be inheritable
2023-01-27 08:46:04 +01:00
if (note.hasInheritableArchivedLabel()) {
2020-05-16 23:12:29 +02:00
return true;
}
}
return false;
}
2024-02-17 20:30:21 +02:00
function getNoteTitle(childNoteId: string, parentNoteId?: string) {
2021-04-16 23:00:08 +02:00
const childNote = becca.notes[childNoteId];
2024-02-17 20:30:21 +02:00
const parentNote = parentNoteId ? becca.notes[parentNoteId] : null;
2020-05-16 23:12:29 +02:00
if (!childNote) {
2023-04-16 09:22:24 +02:00
log.info(`Cannot find note '${childNoteId}'`);
return "[error fetching title]";
}
const title = childNote.getTitleOrProtected();
2020-05-16 23:12:29 +02:00
const branch = parentNote ? becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId) : null;
2020-05-16 23:12:29 +02:00
2025-01-09 18:07:02 +02:00
return `${branch && branch.prefix ? `${branch.prefix} - ` : ""}${title}`;
2020-05-16 23:12:29 +02:00
}
2024-02-17 20:30:21 +02:00
function getNoteTitleArrayForPath(notePathArray: string[]) {
2021-09-07 22:37:03 +02:00
if (!notePathArray || !Array.isArray(notePathArray)) {
throw new Error(`${notePathArray} is not an array.`);
}
if (notePathArray.length === 1) {
return [getNoteTitle(notePathArray[0])];
2020-11-23 23:11:21 +01:00
}
2020-05-16 23:12:29 +02:00
const titles = [];
2025-01-09 18:07:02 +02:00
let parentNoteId = "root";
2020-05-16 23:12:29 +02:00
let hoistedNotePassed = false;
2023-05-05 23:41:11 +02:00
// this is a notePath from outside of hoisted subtree, so the full title path needs to be returned
const hoistedNoteId = cls.getHoistedNoteId();
2022-11-26 14:57:39 +01:00
const outsideOfHoistedSubtree = !notePathArray.includes(hoistedNoteId);
2020-05-16 23:12:29 +02:00
for (const noteId of notePathArray) {
// start collecting path segment titles only after hoisted note
if (hoistedNotePassed) {
const title = getNoteTitle(noteId, parentNoteId);
titles.push(title);
}
2022-11-26 14:57:39 +01:00
if (!hoistedNotePassed && (noteId === hoistedNoteId || outsideOfHoistedSubtree)) {
2020-05-16 23:12:29 +02:00
hoistedNotePassed = true;
}
parentNoteId = noteId;
}
return titles;
}
2024-02-17 20:30:21 +02:00
function getNoteTitleForPath(notePathArray: string[]) {
2020-05-16 23:12:29 +02:00
const titles = getNoteTitleArrayForPath(notePathArray);
2025-01-09 18:07:02 +02:00
return titles.join(" / ");
2020-05-16 23:12:29 +02:00
}
export default {
2020-05-17 10:11:19 +02:00
getNoteTitle,
2020-05-16 23:12:29 +02:00
getNoteTitleForPath,
2020-09-15 16:46:03 +02:00
isNotePathArchived
2020-05-16 23:12:29 +02:00
};