trilium/public/javascripts/dialogs/recent_notes.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 01:39:26 +08:00
const recentNotes = (function() {
2017-11-05 05:03:15 +08:00
const dialogEl = $("#recent-notes-dialog");
const selectBoxEl = $('#recent-notes-select-box');
const jumpToButtonEl = $('#recentNotesJumpTo');
const addLinkButtonEl = $('#recentNotesAddLink');
const noteDetailEl = $('#note-detail');
2017-11-05 01:39:26 +08:00
let list = [];
function addRecentNote(noteTreeId, noteContentId) {
setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
2017-11-05 05:54:27 +08:00
if (noteTreeId === noteEditor.getCurrentNoteId() || noteContentId === noteEditor.getCurrentNoteId()) {
// if it's already there, remove the note
2017-11-05 01:39:26 +08:00
list = list.filter(note => note !== noteTreeId);
2017-11-05 01:39:26 +08:00
list.unshift(noteTreeId);
}
}, 1500);
}
2017-11-05 01:39:26 +08:00
function removeRecentNote(noteIdToRemove) {
list = list.filter(note => note !== noteIdToRemove);
}
function showDialog() {
2017-11-05 05:03:15 +08:00
glob.activeDialog = dialogEl;
2017-11-05 05:03:15 +08:00
noteDetailEl.summernote('editor.saveRange');
dialogEl.dialog({
modal: true,
width: 800
});
2017-11-05 05:03:15 +08:00
selectBoxEl.find('option').remove();
// remove the current note
2017-11-05 05:54:27 +08:00
const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId());
$.each(recNotes, (key, valueNoteId) => {
const noteTitle = getFullName(valueNoteId);
if (!noteTitle) {
return;
}
const option = $("<option></option>")
.attr("value", valueNoteId)
.text(noteTitle);
// select the first one (most recent one) by default
if (key === 0) {
option.attr("selected", "selected");
}
2017-11-05 05:03:15 +08:00
selectBoxEl.append(option);
});
}
2017-10-07 10:46:30 +08:00
function getSelectedNoteIdFromRecentNotes() {
2017-11-05 05:03:15 +08:00
return selectBoxEl.find("option:selected").val();
}
function setActiveNoteBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes();
getNodeByKey(noteId).setActive();
2017-11-05 05:03:15 +08:00
dialogEl.dialog('close');
}
function addLinkBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes();
const linkTitle = getNoteTitle(noteId);
2017-11-05 05:03:15 +08:00
dialogEl.dialog("close");
2017-11-05 05:03:15 +08:00
noteDetailEl.summernote('editor.restoreRange');
2017-11-05 05:03:15 +08:00
noteDetailEl.summernote('createLink', {
text: linkTitle,
url: 'app#' + noteId,
isNewWindow: true
});
}
2017-11-05 05:03:15 +08:00
selectBoxEl.keydown(e => {
const key = e.which;
if (key === 13)// the enter key code
{
setActiveNoteBasedOnRecentNotes();
}
else if (key === 76 /* l */) {
addLinkBasedOnRecentNotes();
}
else {
return; // avoid prevent default
}
e.preventDefault();
});
2017-11-05 01:39:26 +08:00
$(document).bind('keydown', 'alt+q', showDialog);
2017-11-05 05:03:15 +08:00
selectBoxEl.dblclick(e => {
setActiveNoteBasedOnRecentNotes();
});
2017-11-05 05:03:15 +08:00
jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes);
addLinkButtonEl.click(addLinkBasedOnRecentNotes);
return {
2017-11-05 01:39:26 +08:00
showDialog,
addRecentNote,
removeRecentNote
};
})();