mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 18:05:55 +01:00 
			
		
		
		
	script api search ignores hoisting by default
This commit is contained in:
		| @@ -74,12 +74,8 @@ function goToLink(e) { | |||||||
|  |  | ||||||
|     const $link = $(e.target).closest("a,.block-link"); |     const $link = $(e.target).closest("a,.block-link"); | ||||||
|  |  | ||||||
|     console.log("zzzzz", $link); |  | ||||||
|  |  | ||||||
|     const notePath = getNotePathFromLink($link); |     const notePath = getNotePathFromLink($link); | ||||||
|  |  | ||||||
|     console.log() |  | ||||||
|  |  | ||||||
|     if (notePath) { |     if (notePath) { | ||||||
|         if ((e.which === 1 && e.ctrlKey) || e.which === 2) { |         if ((e.which === 1 && e.ctrlKey) || e.which === 2) { | ||||||
|             appContext.tabManager.openTabWithNoteWithHoisting(notePath); |             appContext.tabManager.openTabWithNoteWithHoisting(notePath); | ||||||
| @@ -96,8 +92,6 @@ function goToLink(e) { | |||||||
|         ) { |         ) { | ||||||
|             const address = $link.attr('href'); |             const address = $link.attr('href'); | ||||||
|  |  | ||||||
|             console.log("address", address); |  | ||||||
|  |  | ||||||
|             if (address) { |             if (address) { | ||||||
|                 if (address.toLowerCase().startsWith('http')) { |                 if (address.toLowerCase().startsWith('http')) { | ||||||
|                     window.open(address, '_blank'); |                     window.open(address, '_blank'); | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ async function searchForNoteIds(searchString) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function searchForNotes(searchString) { | async function searchForNotes(searchString) { | ||||||
|     const noteIds = await server.get('search/' + encodeURIComponent(searchString)); |     const noteIds = await searchForNoteIds(searchString); | ||||||
|  |  | ||||||
|     return await treeCache.getNotes(noteIds); |     return await treeCache.getNotes(noteIds); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| import TabAwareWidget from "./tab_aware_widget.js"; | import TabAwareWidget from "./tab_aware_widget.js"; | ||||||
| import treeService from "../services/tree.js"; | import treeService from "../services/tree.js"; | ||||||
| import linkService from "../services/link.js"; | import linkService from "../services/link.js"; | ||||||
| import hoistedNoteService from "../services/hoisted_note.js"; |  | ||||||
|  |  | ||||||
| const TPL = ` | const TPL = ` | ||||||
| <div class="dropdown note-paths-widget"> | <div class="dropdown note-paths-widget"> | ||||||
|   | |||||||
| @@ -225,7 +225,8 @@ function search(req) { | |||||||
|     const searchContext = new SearchContext({ |     const searchContext = new SearchContext({ | ||||||
|         fastSearch: false, |         fastSearch: false, | ||||||
|         includeArchivedNotes: true, |         includeArchivedNotes: true, | ||||||
|         fuzzyAttributeSearch: false |         fuzzyAttributeSearch: false, | ||||||
|  |         ignoreHoistedNote: true | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     return searchService.findNotesWithQuery(searchString, searchContext) |     return searchService.findNotesWithQuery(searchString, searchContext) | ||||||
|   | |||||||
| @@ -102,6 +102,10 @@ function BackendScriptApi(currentNote, apiParams) { | |||||||
|             searchParams.includeArchivedNotes = true; |             searchParams.includeArchivedNotes = true; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         if (searchParams.ignoreHoistedNote === undefined) { | ||||||
|  |             searchParams.ignoreHoistedNote = true; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         const noteIds = searchService.findNotesWithQuery(query, new SearchContext(searchParams)) |         const noteIds = searchService.findNotesWithQuery(query, new SearchContext(searchParams)) | ||||||
|             .map(sr => sr.noteId); |             .map(sr => sr.noteId); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -125,12 +125,18 @@ function getNoteTitleForPath(notePathArray) { | |||||||
|  * - this means that archived paths is returned only if there's no non-archived path |  * - this means that archived paths is returned only if there's no non-archived path | ||||||
|  * - you can check whether returned path is archived using isArchived() |  * - you can check whether returned path is archived using isArchived() | ||||||
|  */ |  */ | ||||||
| function getSomePath(note, path = []) { | function getSomePath(note) { | ||||||
|  |     // first try to find note within hoisted note, otherwise take any existing note path | ||||||
|  |     return getSomePathInner(note, [], true) | ||||||
|  |         || getSomePathInner(note, [], false); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getSomePathInner(note, path, respectHoistng) { | ||||||
|     if (note.noteId === 'root') { |     if (note.noteId === 'root') { | ||||||
|         path.push(note.noteId); |         path.push(note.noteId); | ||||||
|         path.reverse(); |         path.reverse(); | ||||||
|  |  | ||||||
|         if (!path.includes(cls.getHoistedNoteId())) { |         if (respectHoistng && !path.includes(cls.getHoistedNoteId())) { | ||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -143,7 +149,7 @@ function getSomePath(note, path = []) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     for (const parentNote of parents) { |     for (const parentNote of parents) { | ||||||
|         const retPath = getSomePath(parentNote, path.concat([note.noteId])); |         const retPath = getSomePathInner(parentNote, path.concat([note.noteId]), respectHoistng); | ||||||
|  |  | ||||||
|         if (retPath) { |         if (retPath) { | ||||||
|             return retPath; |             return retPath; | ||||||
|   | |||||||
| @@ -5,9 +5,15 @@ const cls = require('../cls'); | |||||||
| class SearchContext { | class SearchContext { | ||||||
|     constructor(params = {}) { |     constructor(params = {}) { | ||||||
|         this.fastSearch = !!params.fastSearch; |         this.fastSearch = !!params.fastSearch; | ||||||
|         this.ancestorNoteId = params.ancestorNoteId || cls.getHoistedNoteId(); |  | ||||||
|         this.ancestorDepth = params.ancestorDepth; |  | ||||||
|         this.includeArchivedNotes = !!params.includeArchivedNotes; |         this.includeArchivedNotes = !!params.includeArchivedNotes; | ||||||
|  |         this.ignoreHoistedNote = !!params.ignoreHoistedNote; | ||||||
|  |         this.ancestorNoteId = params.ancestorNoteId; | ||||||
|  |  | ||||||
|  |         if (!this.ancestorNoteId && !this.ignoreHoistedNote) { | ||||||
|  |             this.ancestorNoteId = cls.getHoistedNoteId(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         this.ancestorDepth = params.ancestorDepth; | ||||||
|         this.orderBy = params.orderBy; |         this.orderBy = params.orderBy; | ||||||
|         this.orderDirection = params.orderDirection; |         this.orderDirection = params.orderDirection; | ||||||
|         this.limit = params.limit; |         this.limit = params.limit; | ||||||
|   | |||||||
| @@ -9,7 +9,6 @@ const SearchContext = require("../search_context.js"); | |||||||
| const noteCache = require('../../note_cache/note_cache.js'); | const noteCache = require('../../note_cache/note_cache.js'); | ||||||
| const noteCacheService = require('../../note_cache/note_cache_service.js'); | const noteCacheService = require('../../note_cache/note_cache_service.js'); | ||||||
| const utils = require('../../utils.js'); | const utils = require('../../utils.js'); | ||||||
| const cls = require('../../cls.js'); |  | ||||||
| const log = require('../../log.js'); | const log = require('../../log.js'); | ||||||
|  |  | ||||||
| function loadNeededInfoFromDatabase() { | function loadNeededInfoFromDatabase() { | ||||||
| @@ -65,10 +64,7 @@ function loadNeededInfoFromDatabase() { | |||||||
|  * @return {SearchResult[]} |  * @return {SearchResult[]} | ||||||
|  */ |  */ | ||||||
| function findNotesWithExpression(expression, searchContext) { | function findNotesWithExpression(expression, searchContext) { | ||||||
|     const hoistedNote = noteCache.notes[cls.getHoistedNoteId()]; |     let allNotes = Object.values(noteCache.notes); | ||||||
|     let allNotes = (hoistedNote && hoistedNote.noteId !== 'root') |  | ||||||
|         ? hoistedNote.subtreeNotes |  | ||||||
|         : Object.values(noteCache.notes); |  | ||||||
|  |  | ||||||
|     if (searchContext.dbLoadNeeded) { |     if (searchContext.dbLoadNeeded) { | ||||||
|         loadNeededInfoFromDatabase(); |         loadNeededInfoFromDatabase(); | ||||||
| @@ -87,9 +83,9 @@ function findNotesWithExpression(expression, searchContext) { | |||||||
|     const noteSet = expression.execute(allNoteSet, executionContext); |     const noteSet = expression.execute(allNoteSet, executionContext); | ||||||
|  |  | ||||||
|     const searchResults = noteSet.notes |     const searchResults = noteSet.notes | ||||||
|         .map(note => executionContext.noteIdToNotePath[note.noteId] || noteCacheService.getSomePath(note)) |         .map(note => new SearchResult( | ||||||
|         .filter(notePathArray => notePathArray && notePathArray.includes(cls.getHoistedNoteId())) |             executionContext.noteIdToNotePath[note.noteId] || noteCacheService.getSomePath(note) | ||||||
|         .map(notePathArray => new SearchResult(notePathArray)); |         )); | ||||||
|  |  | ||||||
|     for (const res of searchResults) { |     for (const res of searchResults) { | ||||||
|         res.computeScore(searchContext.highlightedTokens); |         res.computeScore(searchContext.highlightedTokens); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user