Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger full text search when Ctrl + Enter is pressed in note autocomplete #585

Merged
merged 7 commits into from
Nov 20, 2024
27 changes: 27 additions & 0 deletions src/public/app/services/note_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ async function autocompleteSource(term, cb, options = {}) {
].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) {
results = [
{
Expand Down Expand Up @@ -138,6 +148,17 @@ function initNoteAutocomplete($el, options) {
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({
...autocompleteOptions,
appendTo: document.querySelector('body'),
Expand Down Expand Up @@ -192,6 +213,12 @@ function initNoteAutocomplete($el, options) {
suggestion.notePath = note.getBestNotePathString(hoistedNoteId);
}

if (suggestion.action === 'search-notes') {
const searchString = suggestion.noteTitle;
appContext.triggerCommand('searchNotes', { searchString });
return;
}

$el.setSelectedNotePath(suggestion.notePath);
$el.setSelectedExternalLink(null);

Expand Down
1 change: 1 addition & 0 deletions src/public/app/widgets/dialogs/jump_to_note.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default class JumpToNoteDialog extends BasicWidget {
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
allowCreatingNotes: true,
hideGoToSelectedNoteButton: true,
allowSearchNotes: true,
container: this.$results
})
// clear any event listener added in previous invocation of this function
Expand Down
1 change: 1 addition & 0 deletions src/public/app/widgets/type_widgets/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default class EmptyTypeWidget extends TypeWidget {
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
hideGoToSelectedNoteButton: true,
allowCreatingNotes: true,
allowSearchNotes: true,
container: this.$results
})
.on('autocomplete:noteselected', function(event, suggestion, dataset) {
Expand Down