Add full text search in autocomplete

This commit is contained in:
SiriusXT 2024-11-26 15:41:18 +08:00
parent 836fa2deee
commit 889c1e0346
2 changed files with 16 additions and 12 deletions

View File

@ -29,10 +29,10 @@ async function autocompleteSourceForCKEditor(queryText) {
}); });
} }
async function autocompleteSource(term, cb, options = {}) { async function autocompleteSource(term, cb, options = {}, fastSearch = true) {
const activeNoteId = appContext.tabManager.getActiveContextNoteId(); const activeNoteId = appContext.tabManager.getActiveContextNoteId();
let results = await server.get(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${options.fastSearch}`); let results = await server.get(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${fastSearch}`);
if (term.trim().length >= 1 && options.allowCreatingNotes) { if (term.trim().length >= 1 && options.allowCreatingNotes) {
results = [ results = [
{ {
@ -98,16 +98,17 @@ function showRecentNotes($el) {
$el.trigger('focus'); $el.trigger('focus');
} }
function fullTextSearch($el,options){ async function fullTextSearch($el, options){
if (!options.container) {
// If no container is specified, the dropdown might remain closed. Calling `$el.autocomplete('open')` triggers a search by name and needs to wait for completion. Otherwise, if `$el.autocomplete('open')` executes too slowly, it will overwrite the full-text search results.
$el.autocomplete('open');
await new Promise(resolve => setTimeout(resolve, 100));
}
const searchString = $el.autocomplete('val'); const searchString = $el.autocomplete('val');
if (searchString.trim().length >= 1) { if (searchString.trim().length >= 1) {
const originalFastSearch = options.fastSearch; $el.setSelectedNotePath("");
clearText($el); autocompleteSource(searchString, $el.data('autocompleteCallback'), options, false);
options.fastSearch = false;
$el.autocomplete('val', searchString);
$el.autocomplete('open');
$el.trigger('focus'); $el.trigger('focus');
options.fastSearch = originalFastSearch;
} }
} }
@ -120,7 +121,6 @@ function initNoteAutocomplete($el, options) {
} }
options = options || {}; options = options || {};
options.fastSearch = true; // Perform fast search by default
$el.addClass("note-autocomplete-input"); $el.addClass("note-autocomplete-input");
@ -157,6 +157,7 @@ function initNoteAutocomplete($el, options) {
$fullTextSearchButton.on('click', e => { $fullTextSearchButton.on('click', e => {
fullTextSearch($el, options); fullTextSearch($el, options);
return false;
}); });
let autocompleteOptions = {}; let autocompleteOptions = {};
@ -196,7 +197,10 @@ function initNoteAutocomplete($el, options) {
tabAutocomplete: false tabAutocomplete: false
}, [ }, [
{ {
source: (term, cb) => autocompleteSource(term, cb, options), source: (term, cb) => {
$el.data('autocompleteCallback', cb);
autocompleteSource(term, cb, options);
},
displayKey: 'notePathTitle', displayKey: 'notePathTitle',
templates: { templates: {
suggestion: suggestion => suggestion.highlightedNotePathTitle suggestion: suggestion => suggestion.highlightedNotePathTitle

View File

@ -444,7 +444,7 @@ pre:not(.CodeMirror-line):not(.hljs) {
padding-top: 8px; padding-top: 8px;
} }
.show-recent-notes-button { .show-recent-notes-button, .full-text-search-button {
cursor: pointer; cursor: pointer;
font-size: 1.3em; font-size: 1.3em;
padding-left: 5px; padding-left: 5px;