Add full text search in autocomplete

This commit is contained in:
SiriusXT 2024-11-20 14:22:39 +08:00
parent 002839176e
commit c51adbc449
4 changed files with 30 additions and 23 deletions

View File

@ -45,6 +45,16 @@ async function autocompleteSource(term, cb, options = {}) {
].concat(results); ].concat(results);
} }
if (term.trim().length >= 1 && options.allowSearchNotes) {
results = results.concat([
{
action: 'search-notes',
noteTitle: term,
highlightedNotePathTitle: `Search for "${utils.escapeHtml(term)}" <kbd style='color: var(--muted-text-color); background-color: transparent; float: right;'>Ctrl+Enter</kbd>`
}
]);
}
if (term.match(/^[a-z]+:\/\/.+/i) && options.allowExternalLinks) { if (term.match(/^[a-z]+:\/\/.+/i) && options.allowExternalLinks) {
results = [ results = [
{ {
@ -138,6 +148,17 @@ function initNoteAutocomplete($el, options) {
autocompleteOptions.debug = true; // don't close on blur autocompleteOptions.debug = true; // don't close on blur
} }
if (options.allowSearchNotes) {
$el.on('keydown', (event) => {
if (event.ctrlKey && event.key === 'Enter') {
// Prevent Ctrl + Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
$el.trigger('autocomplete:selected', { action: 'search-notes', noteTitle: $el.autocomplete("val")});
}
});
}
$el.autocomplete({ $el.autocomplete({
...autocompleteOptions, ...autocompleteOptions,
appendTo: document.querySelector('body'), appendTo: document.querySelector('body'),
@ -192,6 +213,12 @@ function initNoteAutocomplete($el, options) {
suggestion.notePath = note.getBestNotePathString(hoistedNoteId); suggestion.notePath = note.getBestNotePathString(hoistedNoteId);
} }
if (suggestion.action === 'search-notes') {
const searchString = suggestion.noteTitle;
appContext.triggerCommand('searchNotes', { searchString });
return;
}
$el.setSelectedNotePath(suggestion.notePath); $el.setSelectedNotePath(suggestion.notePath);
$el.setSelectedExternalLink(null); $el.setSelectedExternalLink(null);

View File

@ -38,16 +38,6 @@ export default class JumpToNoteDialog extends BasicWidget {
this.modal = bootstrap.Modal.getOrCreateInstance(this.$widget); this.modal = bootstrap.Modal.getOrCreateInstance(this.$widget);
this.$autoComplete = this.$widget.find(".jump-to-note-autocomplete"); this.$autoComplete = this.$widget.find(".jump-to-note-autocomplete");
this.$autoComplete.on('keydown', (event) => {
if (event.ctrlKey && event.key === 'Enter') {
// Prevent Ctrl + Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
const searchString = this.$autoComplete.val();
appContext.triggerCommand('searchNotes', { searchString });
}
});
this.$results = this.$widget.find(".jump-to-note-results"); this.$results = this.$widget.find(".jump-to-note-results");
this.$showInFullTextButton = this.$widget.find(".show-in-full-text-button"); this.$showInFullTextButton = this.$widget.find(".show-in-full-text-button");
this.$showInFullTextButton.on('click', e => this.showInFullText(e)); this.$showInFullTextButton.on('click', e => this.showInFullText(e));
@ -68,6 +58,7 @@ export default class JumpToNoteDialog extends BasicWidget {
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, { noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
allowCreatingNotes: true, allowCreatingNotes: true,
hideGoToSelectedNoteButton: true, hideGoToSelectedNoteButton: true,
allowSearchNotes: true,
container: this.$results container: this.$results
}) })
// clear any event listener added in previous invocation of this function // clear any event listener added in previous invocation of this function

View File

@ -65,23 +65,12 @@ export default class EmptyTypeWidget extends TypeWidget {
this.$widget = $(TPL); this.$widget = $(TPL);
this.$autoComplete = this.$widget.find(".note-autocomplete"); this.$autoComplete = this.$widget.find(".note-autocomplete");
this.$autoComplete.on('keydown', (event) => {
if (event.ctrlKey && event.key === 'Enter') {
// Prevent Ctrl + Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
const searchString = this.$autoComplete.val();
appContext.triggerCommand('searchNotes', { searchString });
}
});
this.$results = this.$widget.find(".note-detail-empty-results"); this.$results = this.$widget.find(".note-detail-empty-results");
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, { noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
hideGoToSelectedNoteButton: true, hideGoToSelectedNoteButton: true,
allowCreatingNotes: true, allowCreatingNotes: true,
allowSearchNotes: true,
container: this.$results container: this.$results
}) })
.on('autocomplete:noteselected', function(event, suggestion, dataset) { .on('autocomplete:noteselected', function(event, suggestion, dataset) {

View File

@ -922,7 +922,7 @@
}, },
"empty": { "empty": {
"open_note_instruction": "Open a note by typing the note's title into the input below or choose a note in the tree.", "open_note_instruction": "Open a note by typing the note's title into the input below or choose a note in the tree.",
"search_placeholder": "search for a note by its name, Ctrl+Enter for full-text search.", "search_placeholder": "search for a note by its name",
"enter_workspace": "Enter workspace {{title}}" "enter_workspace": "Enter workspace {{title}}"
}, },
"file": { "file": {