From f8089ba3706bc8cdb913ce8af0fb6942da8021dc Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 17 Dec 2020 21:19:52 +0100 Subject: [PATCH] fixed saved search refresh --- src/public/app/services/tree_cache.js | 13 ++++--------- src/public/app/widgets/note_list.js | 4 ++-- src/routes/api/search.js | 5 ++++- src/services/search/services/search.js | 12 +++++------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/public/app/services/tree_cache.js b/src/public/app/services/tree_cache.js index d68b9d17e..97a217be1 100644 --- a/src/public/app/services/tree_cache.js +++ b/src/public/app/services/tree_cache.js @@ -75,7 +75,7 @@ class TreeCache { } note.children = []; - note.childToBranch = []; + note.childToBranch = {}; } // we want to remove all "real" branches (represented in the database) since those will be created @@ -164,12 +164,8 @@ class TreeCache { this.addResp(resp); - const searchNoteIds = []; - for (const note of resp.notes) { if (note.type === 'search') { - searchNoteIds.push(note.noteId); - const searchResultNoteIds = await server.get('search-note/' + note.noteId); if (!Array.isArray(searchResultNoteIds)) { @@ -182,13 +178,14 @@ class TreeCache { // reset all the virtual branches from old search results if (note.noteId in treeCache.notes) { treeCache.notes[note.noteId].children = []; + treeCache.notes[note.noteId].childToBranch = {}; } const branches = resp.branches.filter(b => b.noteId === note.noteId || b.parentNoteId === note.noteId); searchResultNoteIds.forEach((resultNoteId, index) => branches.push({ // branchId should be repeatable since sometimes we reload some notes without rerendering the tree - branchId: "virt" + resultNoteId + '-' + note.noteId, + branchId: "virt-" + note.noteId + '-' + resultNoteId, noteId: resultNoteId, parentNoteId: note.noteId, notePosition: (index + 1) * 10, @@ -204,9 +201,7 @@ class TreeCache { } } - if (searchNoteIds.length > 0) { - appContext.triggerEvent('searchResultsUpdated', {searchNoteIds}); - } + appContext.triggerEvent('notesReloaded', {noteIds}); } /** @return {NoteShort[]} */ diff --git a/src/public/app/widgets/note_list.js b/src/public/app/widgets/note_list.js index cabf5088f..fe4048de7 100644 --- a/src/public/app/widgets/note_list.js +++ b/src/public/app/widgets/note_list.js @@ -46,8 +46,8 @@ export default class NoteListWidget extends TabAwareWidget { } } - searchResultsUpdatedEvent({searchNoteIds}) { - if (searchNoteIds.includes(this.noteId)) { + notesReloadedEvent({noteIds}) { + if (noteIds.includes(this.noteId)) { this.refresh(); } } diff --git a/src/routes/api/search.js b/src/routes/api/search.js index c24067c93..373d7e2f4 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -22,11 +22,12 @@ async function searchFromNote(req) { return [400, `Note ${req.params.noteId} is not search note.`] } + let searchString; let searchResultNoteIds; try { const searchScript = note.getRelationValue('searchScript'); - const searchString = note.getLabelValue('searchString'); + searchString = note.getLabelValue('searchString'); if (searchScript) { searchResultNoteIds = await searchFromRelation(note, 'searchScript'); @@ -60,6 +61,8 @@ async function searchFromNote(req) { searchResultNoteIds = searchResultNoteIds.slice(0, 200); } + console.log(`Search with query "${searchString}" with results: ${searchResultNoteIds}`); + return searchResultNoteIds; } diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js index f49165d15..6f3afdec6 100644 --- a/src/services/search/services/search.js +++ b/src/services/search/services/search.js @@ -90,15 +90,13 @@ function findNotesWithQuery(query, searchContext) { searchContext.originalQuery = query; - return utils.stopWatch(`Search with query "${query}"`, () => { - const expression = parseQueryToExpression(query, searchContext); + const expression = parseQueryToExpression(query, searchContext); - if (!expression) { - return []; - } + if (!expression) { + return []; + } - return findNotesWithExpression(expression, searchContext); - }, 20); + return findNotesWithExpression(expression, searchContext); } function searchTrimmedNotes(query, searchContext) {