Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactoring recent notes into encapsulated module
azivneraf5ecd186915
diff --git a/public/javascripts/note.js b/public/javascripts/note.jsindex 040c1e1c37..ec04dd13d8 100644--- a/public/javascripts/note.js+++ b/public/javascripts/note.js@@ -217,7 +217,7 @@ async function loadNoteToEditor(noteId) {document.location.hash = noteId;- addRecentNote(noteId, note.detail.note_id);+ recentNotes.addRecentNote(noteId, note.detail.note_id);noteChangeDisabled = false;diff --git a/public/javascripts/recent_notes.js b/public/javascripts/recent_notes.jsindex ebdcc74519..92d933fa29 100644--- a/public/javascripts/recent_notes.js+++ b/public/javascripts/recent_notes.js@@ -1,103 +1,112 @@glob.recentNotes = [];-function addRecentNote(noteTreeId, noteContentId) {- setTimeout(() => {- // we include the note into recent list only if the user stayed on the note at least 5 seconds- if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) {- // if it's already there, remove the note- glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId);-- glob.recentNotes.unshift(noteTreeId);- }- }, 1500);-}+recentNotes = (function() {+ const recentNotesSelectBox = $('#recent-notes-select-box');+ const recentNotesDialog = $("#recent-notes-dialog");+ const recentNotesJumpTo = $('#recentNotesJumpTo');+ const recentNotesAddLink = $('#recentNotesAddLink');+ const noteDetail = $('#note-detail');-function showRecentNotes() {- $('#note-detail').summernote('editor.saveRange');+ function addRecentNote(noteTreeId, noteContentId) {+ setTimeout(() => {+ // we include the note into recent list only if the user stayed on the note at least 5 seconds+ if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) {+ // if it's already there, remove the note+ glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId);- $("#recent-notes-dialog").dialog({- modal: true,- width: 800- });+ glob.recentNotes.unshift(noteTreeId);+ }+ }, 1500);+ }- const recentNotesSelectBox = $('#recent-notes-select-box');+ function showRecentNotes() {+ noteDetail.summernote('editor.saveRange');- recentNotesSelectBox.find('option').remove();+ recentNotesDialog.dialog({+ modal: true,+ width: 800+ });- // remove the current note- const recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id);+ recentNotesSelectBox.find('option').remove();- $.each(recNotes, (key, valueNoteId) => {- const noteTitle = getFullName(valueNoteId);+ // remove the current note+ const recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id);- if (!noteTitle) {- return;- }+ $.each(recNotes, (key, valueNoteId) => {+ const noteTitle = getFullName(valueNoteId);- const option = $("<option></option>")+ 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");- }+ // select the first one (most recent one) by default+ if (key === 0) {+ option.attr("selected", "selected");+ }- recentNotesSelectBox.append(option);- });-}+ recentNotesSelectBox.append(option);+ });+ }-$(document).bind('keydown', 'alt+q', showRecentNotes);+ $(document).bind('keydown', 'alt+q', showRecentNotes);-function getSelectedNoteIdFromRecentNotes() {- return $("#recent-notes-select-box option:selected").val();-}+ function getSelectedNoteIdFromRecentNotes() {+ return recentNotesSelectBox.find("option:selected").val();+ }-function setActiveNoteBasedOnRecentNotes() {- const noteId = getSelectedNoteIdFromRecentNotes();+ function setActiveNoteBasedOnRecentNotes() {+ const noteId = getSelectedNoteIdFromRecentNotes();- getNodeByKey(noteId).setActive();+ getNodeByKey(noteId).setActive();- $("#recent-notes-dialog").dialog('close');-}+ recentNotesDialog.dialog('close');+ }-function addLinkBasedOnRecentNotes() {- const noteId = getSelectedNoteIdFromRecentNotes();+ function addLinkBasedOnRecentNotes() {+ const noteId = getSelectedNoteIdFromRecentNotes();- const linkTitle = getNoteTitle(noteId);- const noteDetail = $('#note-detail');+ const linkTitle = getNoteTitle(noteId);- $("#recent-notes-dialog").dialog("close");+ recentNotesDialog.dialog("close");- noteDetail.summernote('editor.restoreRange');+ noteDetail.summernote('editor.restoreRange');- noteDetail.summernote('createLink', {- text: linkTitle,- url: 'app#' + noteId,- isNewWindow: true- });-}+ noteDetail.summernote('createLink', {+ text: linkTitle,+ url: 'app#' + noteId,+ isNewWindow: true+ });+ }-$('#recent-notes-select-box').keydown(e => {- const key = e.which;+ recentNotesSelectBox.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- }+ if (key === 13)// the enter key code+ {+ setActiveNoteBasedOnRecentNotes();+ }+ else if (key === 76 /* l */) {+ addLinkBasedOnRecentNotes();+ }+ else {+ return; // avoid prevent default+ }++ e.preventDefault();+ });- e.preventDefault();-});+ recentNotesSelectBox.dblclick(e => {+ setActiveNoteBasedOnRecentNotes();+ });-$('#recent-notes-select-box').dblclick(e => {- setActiveNoteBasedOnRecentNotes();-});+ recentNotesJumpTo.click(setActiveNoteBasedOnRecentNotes);+ recentNotesAddLink.click(addLinkBasedOnRecentNotes);-$('#recentNotesJumpTo').click(setActiveNoteBasedOnRecentNotes);-$('#recentNotesAddLink').click(addLinkBasedOnRecentNotes);\ No newline at end of file+ return {+ addRecentNote+ };+})();\ No newline at end of file