From a68c61b2f0ca6cd52b101f01b62611a9a21ed95b Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 30 Nov 2020 23:20:12 +0100 Subject: [PATCH] improvements to saved search --- db/migrations/0172__migrate_saved_notes.js | 23 +++++++++++++++++++ src/public/app/services/note_list_renderer.js | 3 ++- src/public/app/widgets/note_list.js | 9 -------- src/public/app/widgets/note_tree.js | 3 +-- src/public/app/widgets/search_definition.js | 3 +-- src/routes/api/search.js | 15 +++++++----- src/services/app_info.js | 2 +- 7 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 db/migrations/0172__migrate_saved_notes.js diff --git a/db/migrations/0172__migrate_saved_notes.js b/db/migrations/0172__migrate_saved_notes.js new file mode 100644 index 000000000..833fba385 --- /dev/null +++ b/db/migrations/0172__migrate_saved_notes.js @@ -0,0 +1,23 @@ +const repository = require('../../src/services/repository'); + +module.exports = () => { + for (const note of repository.getEntities("SELECT * FROM notes WHERE type = 'search' AND isProtected = 0 AND isDeleted = 0")) { + try { + let origContent = note.getJsonContent(); + + if (!origContent) { + continue; + } + + note.addLabel('searchString', origContent.searchString); + + note.setContent(''); + + note.mime = 'text/plain'; + note.save(); + } + catch (e) { + console.log(`Changing note content for note ${note.noteId} failed with: ${e.message} ${e.stack}`); + } + } +}; diff --git a/src/public/app/services/note_list_renderer.js b/src/public/app/services/note_list_renderer.js index dd8711252..21bd45d62 100644 --- a/src/public/app/services/note_list_renderer.js +++ b/src/public/app/services/note_list_renderer.js @@ -152,7 +152,8 @@ class NoteListRenderer { this.viewType = parentNote.getLabelValue('viewType'); if (!['list', 'grid'].includes(this.viewType)) { - this.viewType = 'list'; // default + // when not explicitly set decide based on note type + this.viewType = parentNote.type === 'search' ? 'list' : 'grid'; } this.$noteList.addClass(this.viewType + '-view'); diff --git a/src/public/app/widgets/note_list.js b/src/public/app/widgets/note_list.js index c5a418eb8..c821ab190 100644 --- a/src/public/app/widgets/note_list.js +++ b/src/public/app/widgets/note_list.js @@ -32,15 +32,6 @@ export default class NoteListWidget extends TabAwareWidget { } async refreshWithNote(note) { - // this.tabContext.autoBookDisabled; - // - // const noteComplement = await this.tabContext.getNoteComplement(); - // - // if (utils.isHtmlEmpty(noteComplement.content)) { - // - // } - // - const noteListRenderer = new NoteListRenderer(note, note.getChildNoteIds()); this.$content.empty().append(await noteListRenderer.renderList()); diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js index 696383cd2..ee593f8e9 100644 --- a/src/public/app/widgets/note_tree.js +++ b/src/public/app/widgets/note_tree.js @@ -131,8 +131,7 @@ const TPL = ` } .tree-item-button { - font-size: 120%; - padding: 2px; + font-size: 130%; cursor: pointer; border-radius: 3px; border: 1px solid var(--main-background-color); diff --git a/src/public/app/widgets/search_definition.js b/src/public/app/widgets/search_definition.js index 1b9dce804..09f3f0ad2 100644 --- a/src/public/app/widgets/search_definition.js +++ b/src/public/app/widgets/search_definition.js @@ -110,9 +110,8 @@ export default class SearchDefinitionWidget extends TabAwareWidget { await treeCache.reloadNotes([this.noteId]); } - async doRefresh(note) { + async refreshWithNote(note) { this.$component.show(); - this.$searchString.val(this.note.getLabelValue('searchString')); this.$searchWithinNoteContent.prop('checked', this.note.getLabelValue('includeNoteContent') === 'true'); this.$limitSearchToSubtree.val(this.note.getLabelValue('subTreeNoteId')); diff --git a/src/routes/api/search.js b/src/routes/api/search.js index 4032ffc44..53bbcd226 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -46,16 +46,16 @@ async function searchFromNote(req) { return [400, `Note ${req.params.noteId} is not search note.`] } - const searchString = note.getLabelValue('searchString'); - let searchResultNoteIds; try { - if (searchString.startsWith('=')) { - const relationName = searchString.substr(1).trim(); + const searchScript = note.getRelationValue('searchScript'); + const searchString = note.getLabelValue('searchString'); - searchResultNoteIds = await searchFromRelation(note, relationName); - } else { + if (searchScript) { + searchResultNoteIds = await searchFromRelation(note, 'searchScript'); + } + else if (searchString) { const searchContext = new SearchContext({ includeNoteContent: note.getLabelValue('includeNoteContent') === 'true', excludeArchived: true, @@ -65,6 +65,9 @@ async function searchFromNote(req) { searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext) .map(sr => sr.noteId); } + else { + searchResultNoteIds = []; + } } catch (e) { log.error(`Search failed for note ${note.noteId}: ` + e.message + ": " + e.stack); diff --git a/src/services/app_info.js b/src/services/app_info.js index fc8fa27ba..2d1bce314 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -4,7 +4,7 @@ const build = require('./build'); const packageJson = require('../../package'); const {TRILIUM_DATA_DIR} = require('./data_dir'); -const APP_DB_VERSION = 171; +const APP_DB_VERSION = 172; const SYNC_VERSION = 16; const CLIPPER_PROTOCOL_VERSION = "1.0";