trilium/src/public/javascripts/services/hoisted_note.js

63 lines
1.7 KiB
JavaScript
Raw Normal View History

import options from './options.js';
2020-01-24 22:44:24 +08:00
import appContext from "./app_context.js";
2020-02-03 05:04:28 +08:00
import treeService from "./tree.js";
2018-12-12 04:53:56 +08:00
function getHoistedNoteId() {
return options.get('hoistedNoteId');
2018-12-12 04:53:56 +08:00
}
async function setHoistedNoteId(noteId) {
await options.save('hoistedNoteId', noteId);
2018-12-13 03:39:56 +08:00
// FIXME - just use option load event
appContext.trigger('hoistedNoteChanged', {noteId});
2018-12-12 04:53:56 +08:00
}
2018-12-16 03:29:08 +08:00
async function unhoist() {
await setHoistedNoteId('root');
}
2020-02-11 03:57:56 +08:00
function isTopLevelNode(node) {
return isRootNode(node.getParent());
}
2020-02-11 03:57:56 +08:00
function isRootNode(node) {
// even though check for 'root' should not be necessary, we keep it just in case
return node.data.noteId === "root"
2020-02-11 03:57:56 +08:00
|| node.data.noteId === getHoistedNoteId();
}
2020-02-03 05:04:28 +08:00
async function checkNoteAccess(notePath) {
// notePath argument can contain only noteId which is not good when hoisted since
// then we need to check the whole note path
const runNotePath = await treeService.getRunPath(notePath);
if (!runNotePath) {
console.log("Cannot activate " + notePath);
return false;
}
2020-02-11 03:57:56 +08:00
const hoistedNoteId = getHoistedNoteId();
2020-02-03 05:04:28 +08:00
if (hoistedNoteId !== 'root' && !runNotePath.includes(hoistedNoteId)) {
const confirmDialog = await import('../dialogs/confirm.js');
if (!await confirmDialog.confirm("Requested note is outside of hoisted note subtree and you must unhoist to access the note. Do you want to proceed with unhoisting?")) {
return false;
}
// unhoist so we can activate the note
await unhoist();
}
return true;
}
2018-12-12 04:53:56 +08:00
export default {
getHoistedNoteId,
2018-12-16 03:29:08 +08:00
setHoistedNoteId,
unhoist,
isTopLevelNode,
2020-02-03 05:04:28 +08:00
isRootNode,
checkNoteAccess
2018-12-12 04:53:56 +08:00
}