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

146 lines
4 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');
2017-12-10 03:11:35 +08:00
const jumpToButtonEl = $('#recent-notes-jump-to');
const addLinkButtonEl = $('#recent-notes-add-link');
2017-11-20 05:35:35 +08:00
const addCurrentAsChildEl = $("#recent-notes-add-current-as-child");
const addRecentAsChildEl = $("#recent-notes-add-recent-as-child");
2017-11-05 05:03:15 +08:00
const noteDetailEl = $('#note-detail');
// 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() {
2017-11-05 05:03:15 +08:00
glob.activeDialog = dialogEl;
2017-11-05 05:03:15 +08:00
dialogEl.dialog({
modal: true,
width: 800
});
2017-11-05 05:03:15 +08:00
selectBoxEl.find('option').remove();
// remove the current note
2017-11-20 01:06:48 +08:00
const recNotes = list.filter(note => note !== noteTree.getCurrentNotePath());
2017-11-20 01:06:48 +08:00
$.each(recNotes, (key, valueNotePath) => {
const noteTitle = noteTree.getNotePathTitle(valueNotePath);
const option = $("<option></option>")
2017-11-20 01:06:48 +08:00
.attr("value", valueNotePath)
.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
2017-11-20 05:35:35 +08:00
function getSelectedNotePath() {
2017-11-05 05:03:15 +08:00
return selectBoxEl.find("option:selected").val();
}
function getSelectedNoteId() {
const notePath = getSelectedNotePath();
return treeUtils.getNoteIdFromNotePath(notePath);
}
function setActiveNoteBasedOnRecentNotes() {
2017-11-20 05:35:35 +08:00
const notePath = getSelectedNotePath();
2017-11-20 01:06:48 +08:00
noteTree.activateNode(notePath);
2017-11-05 05:03:15 +08:00
dialogEl.dialog('close');
}
function addLinkBasedOnRecentNotes() {
2017-11-20 05:35:35 +08:00
const notePath = getSelectedNotePath();
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
const linkTitle = noteTree.getNoteTitle(noteId);
2017-11-05 05:03:15 +08:00
dialogEl.dialog("close");
2017-12-10 03:11:35 +08:00
link.addLinkToEditor(linkTitle, '#' + notePath);
}
2017-11-20 11:31:30 +08:00
async function addCurrentAsChild() {
2018-01-14 07:02:41 +08:00
await cloning.cloneNoteTo(noteEditor.getCurrentNoteId(), getSelectedNoteId());
2017-11-20 05:35:35 +08:00
dialogEl.dialog("close");
}
async function addRecentAsChild() {
2018-01-14 07:02:41 +08:00
await cloning.cloneNoteTo(getSelectedNoteId(), noteEditor.getCurrentNoteId());
2017-11-20 11:31:30 +08:00
dialogEl.dialog("close");
2017-11-20 05:35:35 +08:00
}
2017-11-05 05:03:15 +08:00
selectBoxEl.keydown(e => {
const key = e.which;
2017-11-20 05:35:35 +08:00
// to get keycodes use http://keycode.info/
if (key === 13)// the enter key code
{
setActiveNoteBasedOnRecentNotes();
}
else if (key === 76 /* l */) {
addLinkBasedOnRecentNotes();
}
2017-11-20 05:35:35 +08:00
else if (key === 67 /* c */) {
addCurrentAsChild();
}
else if (key === 82 /* r */) {
addRecentAsChild()
}
else {
return; // avoid prevent default
}
e.preventDefault();
});
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
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);
2017-11-20 05:35:35 +08:00
addCurrentAsChildEl.click(addCurrentAsChild);
addRecentAsChildEl.click(addRecentAsChild);
return {
2017-11-05 01:39:26 +08:00
showDialog,
2017-12-03 23:42:23 +08:00
addRecentNote,
reload
};
})();