Skip to content

Build your personal knowledge base with Trilium Notes

fix bug when context menu sometimes does not show up, closes #682

zadamdf40accdd4b2
diff --git a/src/public/javascripts/services/branches.js b/src/public/javascripts/services/branches.js
index 519a04e24b..dda7097cde 100644
--- a/src/public/javascripts/services/branches.js
+++ b/src/public/javascripts/services/branches.js
@@ -26,9 +26,7 @@ async function moveBeforeNode(nodesToMove, beforeNode) {
await changeNode(
node => node.moveTo(beforeNode, 'before'),
- nodeToMove,
- beforeNode.data.noteId,
- null);
+ nodeToMove);
}
}
@@ -52,9 +50,7 @@ async function moveAfterNode(nodesToMove, afterNode) {
await changeNode(
node => node.moveTo(afterNode, 'after'),
- nodeToMove,
- null,
- afterNode.data.noteId);
+ nodeToMove);
}
}
@@ -177,7 +173,7 @@ async function moveNodeUpInHierarchy(node) {
node);
}
-async function changeNode(func, node, beforeNoteId = null, afterNoteId = null) {
+async function changeNode(func, node) {
utils.assertArguments(func, node);
const childNoteId = node.data.noteId;
diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js
index d183a64104..8ff5e2ae07 100644
--- a/src/public/javascripts/services/tree_builder.js
+++ b/src/public/javascripts/services/tree_builder.js
@@ -1,6 +1,4 @@
import utils from "./utils.js";
-import Branch from "../entities/branch.js";
-import server from "./server.js";
import treeCache from "./tree_cache.js";
import ws from "./ws.js";
import hoistedNoteService from "./hoisted_note.js";
@@ -110,24 +108,8 @@ async function prepareRealBranch(parentNote) {
}
async function prepareSearchBranch(note) {
- const results = await server.get('search-note/' + note.noteId);
+ await treeCache.reloadNotes([note.noteId]);
- // force to load all the notes at once instead of one by one
- await treeCache.getNotes(results.map(res => res.noteId));
-
- const {notes, branches} = await server.post('tree/load', { noteIds: [note.noteId] });
-
- results.forEach((result, index) => branches.push({
- branchId: "virt" + utils.randomString(10),
- noteId: result.noteId,
- parentNoteId: note.noteId,
- prefix: treeCache.getBranch(result.branchId).prefix,
- notePosition: (index + 1) * 10
- }));
-
- treeCache.addResp(notes, branches);
-
- // note in cache changed
const newNote = await treeCache.getNote(note.noteId);
return await prepareRealBranch(newNote);
diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js
index 256ad4784b..fdaf813c32 100644
--- a/src/public/javascripts/services/tree_cache.js
+++ b/src/public/javascripts/services/tree_cache.js
@@ -102,6 +102,35 @@ class TreeCache {
const resp = await server.post('tree/load', { noteIds });
this.addResp(resp.notes, resp.branches);
+
+ for (const note of resp.notes) {
+ if (note.type === 'search') {
+ const someExpanded = resp.branches.find(b => b.noteId === note.noteId && b.isExpanded);
+
+ if (!someExpanded) {
+ continue;
+ }
+
+ const searchResults = await server.get('search-note/' + note.noteId);
+
+ // force to load all the notes at once instead of one by one
+ await treeCache.getNotes(searchResults.map(res => res.noteId));
+
+ const branches = resp.branches.filter(b => b.noteId === note.noteId || b.parentNoteId === note.noteId);
+
+ searchResults.forEach((result, index) => branches.push({
+ // branchId should be repeatable since sometimes we reload some notes without rerendering the tree
+ branchId: "virt" + result.noteId + '-' + note.noteId,
+ noteId: result.noteId,
+ parentNoteId: note.noteId,
+ prefix: treeCache.getBranch(result.branchId).prefix,
+ notePosition: (index + 1) * 10
+ }));
+
+ // update this note with standard (parent) branches + virtual (children) branches
+ treeCache.addResp([note], branches);
+ }
+ }
}
/** @return {Promise<NoteShort[]>} */
@@ -109,9 +138,7 @@ class TreeCache {
const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);
if (missingNoteIds.length > 0) {
- const resp = await server.post('tree/load', { noteIds: missingNoteIds });
-
- this.addResp(resp.notes, resp.branches);
+ await this.reloadNotes(missingNoteIds);
}
return noteIds.map(noteId => {
diff --git a/src/public/javascripts/services/tree_context_menu.js b/src/public/javascripts/services/tree_context_menu.js
index 1a48dffa55..fcace29df3 100644
--- a/src/public/javascripts/services/tree_context_menu.js
+++ b/src/public/javascripts/services/tree_context_menu.js
@@ -26,8 +26,8 @@ class TreeContextMenu {
}
async getContextMenuItems() {
- const branch = treeCache.getBranch(this.node.data.branchId);
const note = await treeCache.getNote(this.node.data.noteId);
+ const branch = treeCache.getBranch(this.node.data.branchId);
const parentNote = await treeCache.getNote(branch.parentNoteId);
const isNotRoot = note.noteId !== 'root';
const isHoisted = note.noteId === await hoistedNoteService.getHoistedNoteId();
@@ -39,9 +39,9 @@ class TreeContextMenu {
const noSelectedNotes = selNodes.length === 0
|| (selNodes.length === 1 && selNodes[0] === this.node);
+ const notSearch = note.type !== 'search';
const parentNotSearch = parentNote.type !== 'search';
const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
- const notSearch = note.type !== 'search';
return [
{ title: "Open in new tab", cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },