2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/app_context.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import treeService, { type Node } from "./tree.js";
|
2022-11-25 15:29:57 +01:00
|
|
|
import dialogService from "./dialog.js";
|
2022-10-16 23:11:46 +02:00
|
|
|
import froca from "./froca.js";
|
2024-08-04 13:34:29 +03:00
|
|
|
import NoteContext from "../components/note_context.js";
|
2024-10-20 10:49:50 +03:00
|
|
|
import { t } from "./i18n.js";
|
2018-12-11 21:53:56 +01:00
|
|
|
|
2020-02-05 22:08:45 +01:00
|
|
|
function getHoistedNoteId() {
|
2021-05-22 12:35:41 +02:00
|
|
|
const activeNoteContext = appContext.tabManager.getActiveContext();
|
2020-10-13 23:41:55 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return activeNoteContext ? activeNoteContext.hoistedNoteId : "root";
|
2018-12-11 21:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-15 20:29:08 +01:00
|
|
|
async function unhoist() {
|
2021-05-22 12:35:41 +02:00
|
|
|
const activeNoteContext = appContext.tabManager.getActiveContext();
|
2020-11-23 22:52:48 +01:00
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
if (activeNoteContext) {
|
|
|
|
await activeNoteContext.unhoist();
|
2020-11-23 22:52:48 +01:00
|
|
|
}
|
2018-12-15 20:29:08 +01:00
|
|
|
}
|
|
|
|
|
2024-08-04 13:34:29 +03:00
|
|
|
function isTopLevelNode(node: Node) {
|
2021-03-06 20:23:29 +01:00
|
|
|
return isHoistedNode(node.getParent());
|
2019-04-16 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
2024-08-04 13:34:29 +03:00
|
|
|
function isHoistedNode(node: Node) {
|
2019-04-16 21:40:04 +02:00
|
|
|
// even though check for 'root' should not be necessary, we keep it just in case
|
2025-01-09 18:07:02 +02:00
|
|
|
return node.data.noteId === "root" || node.data.noteId === getHoistedNoteId();
|
2019-04-16 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 23:11:46 +02:00
|
|
|
async function isHoistedInHiddenSubtree() {
|
|
|
|
const hoistedNoteId = getHoistedNoteId();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (hoistedNoteId === "root") {
|
2022-10-16 23:11:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hoistedNote = await froca.getNote(hoistedNoteId);
|
2024-08-04 13:34:29 +03:00
|
|
|
return hoistedNote?.isHiddenCompletely();
|
2022-10-16 23:11:46 +02:00
|
|
|
}
|
|
|
|
|
2024-08-04 13:34:29 +03:00
|
|
|
async function checkNoteAccess(notePath: string, noteContext: NoteContext) {
|
2021-05-22 12:26:45 +02:00
|
|
|
const resolvedNotePath = await treeService.resolveNotePath(notePath, noteContext.hoistedNoteId);
|
2020-02-02 22:04:28 +01:00
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
if (!resolvedNotePath) {
|
2023-02-14 16:06:49 +01:00
|
|
|
console.log(`Cannot activate '${notePath}'`);
|
2020-02-02 22:04:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
const hoistedNoteId = noteContext.hoistedNoteId;
|
2020-02-02 22:04:28 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!resolvedNotePath.includes(hoistedNoteId) && (!resolvedNotePath.includes("_hidden") || resolvedNotePath.includes("_lbBookmarks"))) {
|
2024-08-04 13:34:29 +03:00
|
|
|
const noteId = treeService.getNoteIdFromUrl(resolvedNotePath);
|
|
|
|
if (!noteId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const requestedNote = await froca.getNote(noteId);
|
2022-12-22 23:38:57 +01:00
|
|
|
const hoistedNote = await froca.getNote(hoistedNoteId);
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (
|
|
|
|
(!hoistedNote?.hasAncestor("_hidden") || resolvedNotePath.includes("_lbBookmarks")) &&
|
|
|
|
!(await dialogService.confirm(t("hoisted_note.confirm_unhoisting", { requestedNote: requestedNote?.title, hoistedNote: hoistedNote?.title })))
|
|
|
|
) {
|
2020-02-02 22:04:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unhoist so we can activate the note
|
|
|
|
await unhoist();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-11 21:53:56 +01:00
|
|
|
export default {
|
|
|
|
getHoistedNoteId,
|
2019-04-16 21:40:04 +02:00
|
|
|
unhoist,
|
|
|
|
isTopLevelNode,
|
2021-03-06 20:23:29 +01:00
|
|
|
isHoistedNode,
|
2022-10-16 23:11:46 +02:00
|
|
|
checkNoteAccess,
|
|
|
|
isHoistedInHiddenSubtree
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|