Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
continuing work on hoisting notes
azivner17d030e800dc
diff --git a/src/public/javascripts/services/hoisted_note.js b/src/public/javascripts/services/hoisted_note.jsindex 26c8632230..2a64ad822e 100644--- a/src/public/javascripts/services/hoisted_note.js+++ b/src/public/javascripts/services/hoisted_note.js@@ -1,5 +1,6 @@import optionsInit from './options_init.js';import server from "./server.js";+import tree from "./tree.js";let hoistedNoteId;@@ -17,6 +18,8 @@ async function setHoistedNoteId(noteId) {hoistedNoteId = noteId;await server.put('options/hoistedNoteId/' + noteId);++ await tree.reload();}export default {diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.jsindex c7004e0f5e..39d64b652f 100644--- a/src/public/javascripts/services/tree.js+++ b/src/public/javascripts/services/tree.js@@ -14,6 +14,7 @@ import treeBuilder from "./tree_builder.js";import treeKeyBindings from "./tree_keybindings.js";import Branch from '../entities/branch.js';import NoteShort from '../entities/note_short.js';+import hoistedNoteService from '../services/hoisted_note.js';const $tree = $("#tree");const $createTopLevelNoteButton = $("#create-top-level-note-button");@@ -88,10 +89,11 @@ async function expandToNote(notePath, expandOpts) {const noteId = treeUtils.getNoteIdFromNotePath(notePath);- let parentNoteId = 'none';+ let parentNoteId = null;for (const childNoteId of runPath) {- const node = getNodesByNoteId(childNoteId).find(node => node.data.parentNoteId === parentNoteId);+ // for first node (!parentNoteId) it doesn't matter which node is found+ const node = getNodesByNoteId(childNoteId).find(node => !parentNoteId || node.data.parentNoteId === parentNoteId);if (!node) {console.error(`Can't find node for noteId=${childNoteId} with parentNoteId=${parentNoteId}`);@@ -143,6 +145,8 @@ async function getRunPath(notePath) {path.push('root');}+ const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();+const effectivePath = [];let childNoteId = null;let i = 0;@@ -195,13 +199,12 @@ async function getRunPath(notePath) {}}- if (parentNoteId === 'none') {+ effectivePath.push(parentNoteId);+ childNoteId = parentNoteId;++ if (parentNoteId === hoistedNoteId) {break;}- else {- effectivePath.push(parentNoteId);- childNoteId = parentNoteId;- }}return effectivePath.reverse();diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.jsindex 945ea33e74..63ff8fec71 100644--- a/src/public/javascripts/services/tree_builder.js+++ b/src/public/javascripts/services/tree_builder.js@@ -12,8 +12,16 @@ async function prepareTree(noteRows, branchRows, relations) {treeCache.load(noteRows, branchRows, relations);const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();- const hoistedNote = await treeCache.getNote(hoistedNoteId);- const hoistedBranch = (await hoistedNote.getBranches())[0];++ let hoistedBranch;++ if (hoistedNoteId === 'root') {+ hoistedBranch = await treeCache.getBranch('root');+ }+ else {+ const hoistedNote = await treeCache.getNote(hoistedNoteId);+ hoistedBranch = (await hoistedNote.getBranches())[0];+ }return [ await prepareNode(hoistedBranch) ];}diff --git a/src/routes/api/tree.js b/src/routes/api/tree.jsindex 97dbb527c2..4887314242 100644--- a/src/routes/api/tree.js+++ b/src/routes/api/tree.js@@ -38,6 +38,7 @@ async function getNotes(noteIds) {async function getRelations(noteIds) {// we need to fetch both parentNoteId and noteId matches because we can have loaded child// of which only some of the parents has been loaded.+ // also now with note hoisting, it is possible to have the note displayed without its parent chain being loadedconst relations = await sql.getManyRows(`SELECT branchId, noteId AS 'childNoteId', parentNoteId, notePosition FROM branches WHERE isDeleted = 0AND (parentNoteId IN (???) OR noteId IN (???))`, noteIds);@@ -65,6 +66,9 @@ async function getTree() {)SELECT branches.* FROM tree JOIN branches USING(noteId) WHERE branches.isDeleted = 0 ORDER BY branches.notePosition`, [hoistedNoteId]);+ // we also want root branch in there because all the paths start with root+ branches.push(await sql.getRow(`SELECT * FROM branches WHERE branchId = 'root'`));+const noteIds = Array.from(new Set(branches.map(b => b.noteId)));const notes = await getNotes(noteIds);