From df40accdd4b2aae4ac4e7ef3c184c02bbda137af Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 4 Nov 2019 20:20:21 +0100 Subject: [PATCH] fix bug when context menu sometimes does not show up, closes #682 --- src/public/javascripts/services/branches.js | 10 ++---- .../javascripts/services/tree_builder.js | 20 +---------- src/public/javascripts/services/tree_cache.js | 33 +++++++++++++++++-- .../javascripts/services/tree_context_menu.js | 4 +-- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/public/javascripts/services/branches.js b/src/public/javascripts/services/branches.js index 519a04e24..dda7097cd 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 d183a6410..8ff5e2ae0 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 256ad4784..fdaf813c3 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} */ @@ -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 1a48dffa5..fcace29df 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 },