trilium/src/public/javascripts/dialogs/recent_notes.js

102 lines
3 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 01:39:26 +08:00
const recentNotes = (function() {
const $dialog = $("#recent-notes-dialog");
const $searchInput = $('#recent-notes-search-input');
// list of recent note paths
2017-11-05 01:39:26 +08:00
let list = [];
2017-12-03 23:42:23 +08:00
async function reload() {
const result = await server.get('recent-notes');
2018-01-29 08:30:14 +08:00
list = result.map(r => r.notePath);
2017-12-03 23:42:23 +08:00
}
2017-11-05 11:46:50 +08:00
function addRecentNote(noteTreeId, notePath) {
setTimeout(async () => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
if (notePath && notePath === noteTree.getCurrentNotePath()) {
const result = await server.put('recent-notes/' + noteTreeId + '/' + encodeURIComponent(notePath));
2018-01-29 08:30:14 +08:00
list = result.map(r => r.notePath);
}
}, 1500);
}
2017-11-05 01:39:26 +08:00
function showDialog() {
glob.activeDialog = $dialog;
$dialog.dialog({
modal: true,
width: 800,
height: 100,
position: { my: "center top+100", at: "top", of: window }
});
$searchInput.val('');
// remove the current note
2017-11-20 01:06:48 +08:00
const recNotes = list.filter(note => note !== noteTree.getCurrentNotePath());
$searchInput.autocomplete({
source: recNotes.map(notePath => {
let noteTitle;
try {
noteTitle = noteTree.getNotePathTitle(notePath);
}
catch (e) {
noteTitle = "[error - can't find note title]";
messaging.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack);
}
return {
label: noteTitle,
value: notePath
}
}),
minLength: 0,
autoFocus: true,
select: function (event, ui) {
noteTree.activateNode(ui.item.value);
$searchInput.autocomplete('destroy');
$dialog.dialog('close');
},
focus: function (event, ui) {
event.preventDefault();
},
close: function (event, ui) {
if (event.keyCode === 27) { // escape closes dialog
$searchInput.autocomplete('destroy');
$dialog.dialog('close');
}
else {
// keep autocomplete open
// we're kind of abusing autocomplete to work in a way which it's not designed for
$searchInput.autocomplete("search", "");
}
},
create: () => $searchInput.autocomplete("search", ""),
classes: {
"ui-autocomplete": "recent-notes-autocomplete"
}
});
}
2017-10-07 10:46:30 +08:00
2017-12-03 23:42:23 +08:00
reload();
2017-12-19 12:41:13 +08:00
$(document).bind('keydown', 'ctrl+e', e => {
showDialog();
e.preventDefault();
});
2017-11-05 01:39:26 +08:00
return {
2017-11-05 01:39:26 +08:00
showDialog,
2017-12-03 23:42:23 +08:00
addRecentNote,
reload
};
})();