2018-11-07 09:51:14 +01:00
|
|
|
import noteAutocompleteService from '../services/note_autocomplete.js';
|
2019-06-10 22:45:03 +02:00
|
|
|
import utils from "../services/utils.js";
|
2020-02-02 22:04:28 +01:00
|
|
|
import appContext from "../services/app_context.js";
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
const $dialog = $("#jump-to-note-dialog");
|
|
|
|
const $autoComplete = $("#jump-to-note-autocomplete");
|
2018-06-05 23:28:10 -04:00
|
|
|
const $showInFullTextButton = $("#show-in-full-text-button");
|
2018-11-07 00:23:50 +01:00
|
|
|
|
2020-08-31 21:00:23 +02:00
|
|
|
let lastOpenedTs = 0;
|
|
|
|
const KEEP_LAST_SEARCH_FOR_X_SECONDS = 120;
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2020-08-31 21:00:23 +02:00
|
|
|
export async function showDialog() {
|
2020-02-09 10:00:13 +01:00
|
|
|
utils.openDialog($dialog);
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-11-14 11:28:52 +01:00
|
|
|
noteAutocompleteService.initNoteAutocomplete($autoComplete, { hideGoToSelectedNoteButton: true })
|
2018-11-07 09:51:14 +01:00
|
|
|
.on('autocomplete:selected', function(event, suggestion, dataset) {
|
2020-07-13 00:15:00 +02:00
|
|
|
if (!suggestion.notePath) {
|
2018-11-07 09:51:14 +01:00
|
|
|
return false;
|
2018-07-26 16:05:09 +02:00
|
|
|
}
|
2018-08-16 21:02:42 +02:00
|
|
|
|
2020-07-13 00:15:00 +02:00
|
|
|
appContext.tabManager.getActiveTabContext().setNote(suggestion.notePath);
|
2018-11-07 09:51:14 +01:00
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2020-08-31 21:00:23 +02:00
|
|
|
// if you open the Jump To dialog soon after using it previously it can often mean that you
|
|
|
|
// actually want to search for the same thing (e.g. you opened the wrong note at first try)
|
|
|
|
// so we'll keep the content.
|
|
|
|
// if it's outside of this time limit then we assume it's a completely new search and show recent notes instead.
|
|
|
|
if (Date.now() - lastOpenedTs > KEEP_LAST_SEARCH_FOR_X_SECONDS * 1000) {
|
|
|
|
noteAutocompleteService.showRecentNotes($autoComplete);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$autoComplete
|
|
|
|
.trigger('focus')
|
|
|
|
.trigger('select');
|
|
|
|
}
|
|
|
|
|
|
|
|
lastOpenedTs = Date.now();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-19 20:36:13 -05:00
|
|
|
|
2018-06-05 23:28:10 -04:00
|
|
|
function showInFullText(e) {
|
|
|
|
// stop from propagating upwards (dangerous especially with ctrl+enter executable javascript notes)
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const searchText = $autoComplete.val();
|
|
|
|
|
2020-08-31 20:46:55 +02:00
|
|
|
appContext.triggerCommand('showSearch', {searchText});
|
|
|
|
appContext.triggerCommand('searchForResults', {searchText});
|
2018-06-05 23:28:10 -04:00
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$dialog.modal('hide');
|
2018-06-05 23:28:10 -04:00
|
|
|
}
|
|
|
|
|
2018-11-07 09:51:14 +01:00
|
|
|
|
2019-11-09 17:39:48 +01:00
|
|
|
$showInFullTextButton.on('click', showInFullText);
|
2018-06-05 23:28:10 -04:00
|
|
|
|
2019-08-12 22:41:53 +02:00
|
|
|
utils.bindElShortcut($dialog, 'ctrl+return', showInFullText);
|