Add full text search in autocomplete

This commit is contained in:
SiriusXT 2024-11-23 20:51:51 +08:00
parent 2d8fb4eff5
commit 5370e1e27c
3 changed files with 34 additions and 7 deletions

View File

@ -32,8 +32,7 @@ async function autocompleteSourceForCKEditor(queryText) {
async function autocompleteSource(term, cb, options = {}) {
const activeNoteId = appContext.tabManager.getActiveContextNoteId();
let results = await server.get(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}`);
let results = await server.get(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${options.fastSearch}`);
if (term.trim().length >= 1 && options.allowCreatingNotes) {
results = [
{
@ -103,6 +102,15 @@ function showRecentNotes($el) {
$el.trigger(e);
}
function fullTextSearch($el,options){
const searchString = $el.autocomplete("val")
clearText($el);
options.fastSearch = false;
$el.autocomplete("val", searchString);
$el.autocomplete('open');
options.fastSearch = true;
}
function initNoteAutocomplete($el, options) {
if ($el.hasClass("note-autocomplete-input") || utils.isMobile()) {
// clear any event listener added in previous invocation of this function
@ -123,10 +131,14 @@ function initNoteAutocomplete($el, options) {
.addClass("input-group-text show-recent-notes-button bx bx-time")
.prop("title", "Show recent notes");
const $fullTextSearchButton = $("<button>")
.addClass("input-group-text full-text-search-button bx bx-search")
.prop("title", "Full text search. (Shift+Enter)");
const $goToSelectedNoteButton = $("<button>")
.addClass("input-group-text go-to-selected-note-button bx bx-arrow-to-right");
$el.after($clearTextButton).after($showRecentNotesButton);
$el.after($clearTextButton).after($showRecentNotesButton).after($fullTextSearchButton);
if (!options.hideGoToSelectedNoteButton) {
$el.after($goToSelectedNoteButton);
@ -142,6 +154,10 @@ function initNoteAutocomplete($el, options) {
return false;
});
$fullTextSearchButton.on('click', e => {
fullTextSearch($el, options);
});
let autocompleteOptions = {};
if (options.container) {
autocompleteOptions.dropdownMenuContainer = options.container;
@ -158,7 +174,16 @@ function initNoteAutocomplete($el, options) {
}
});
}
$el.on('keydown', async (event) => {
if (event.shiftKey && event.key === 'Enter') {
// Prevent Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
fullTextSearch($el,options)
}
});
options.fastSearch = true; // Perform fast search by default
$el.autocomplete({
...autocompleteOptions,
appendTo: document.querySelector('body'),

View File

@ -14,6 +14,8 @@ function getAutocomplete(req: Request) {
throw new ValidationError("Invalid query data type.");
}
const query = (req.query.query || "").trim();
const fastSearch = String(req.query.fastSearch).toLowerCase() === "false" ? false : true;
const activeNoteId = req.query.activeNoteId || 'none';
let results;
@ -24,7 +26,7 @@ function getAutocomplete(req: Request) {
results = getRecentNotes(activeNoteId);
}
else {
results = searchService.searchNotesForAutocomplete(query);
results = searchService.searchNotesForAutocomplete(query, fastSearch);
}
const msTaken = Date.now() - timestampStarted;

View File

@ -340,9 +340,9 @@ function findFirstNoteWithQuery(query: string, searchContext: SearchContext): BN
return searchResults.length > 0 ? becca.notes[searchResults[0].noteId] : null;
}
function searchNotesForAutocomplete(query: string) {
function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) {
const searchContext = new SearchContext({
fastSearch: true,
fastSearch: fastSearch,
includeArchivedNotes: false,
includeHiddenNotes: true,
fuzzyAttributeSearch: true,