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
converted note history to module
azivner394fd6f5274a
diff --git a/public/javascripts/note_history.js b/public/javascripts/note_history.jsindex 7764ce89a0..a3264cce4b 100644--- a/public/javascripts/note_history.js+++ b/public/javascripts/note_history.js@@ -1,61 +1,76 @@-glob.historyItems = null;+const noteHistory = (function() {+ const dialogEl = $("#note-history-dialog");+ const listEl = $("#note-history-list");+ const contentEl = $("#note-history-content");+ const titleEl = $("#note-history-title");-function showCurrentNoteHistory() {- showNoteHistoryDialog(glob.currentNote.detail.note_id);-}+ let historyItems = [];-function showNoteHistoryDialog(noteId, noteHistoryId) {- $("#note-history-dialog").dialog({- modal: true,- width: 800,- height: 700- });+ async function showCurrentNoteHistory() {+ await showNoteHistoryDialog(glob.currentNote.detail.note_id);+ }++ // weird hack because browser doesn't like we're returning promise and displays promise page+ function showNoteHistoryDialogNotAsync(noteId, noteHistoryId) {+ showNoteHistoryDialog(noteId, noteHistoryId);+ }- $("#note-history-list").empty();- $("#note-history-content").empty();+ async function showNoteHistoryDialog(noteId, noteHistoryId) {+ dialogEl.dialog({+ modal: true,+ width: 800,+ height: 700+ });- $.ajax({- url: baseApiUrl + 'notes-history/' + noteId,- type: 'GET',- success: result => {- glob.historyItems = result;+ listEl.empty();+ contentEl.empty();- for (const row of result) {- const dateModified = getDateFromTS(row.date_modified_to);+ historyItems = await $.ajax({+ url: baseApiUrl + 'notes-history/' + noteId,+ type: 'GET',+ error: () => error("Error getting note history.")+ });- $("#note-history-list").append($('<option>', {- value: row.note_history_id,- text: formatDateTime(dateModified)- }));- }+ for (const item of historyItems) {+ const dateModified = getDateFromTS(item.date_modified_to);- if (result.length > 0) {- if (!noteHistoryId) {- noteHistoryId = $("#note-history-list option:first").val();- }+ $("#note-history-list").append($('<option>', {+ value: item.note_history_id,+ text: formatDateTime(dateModified)+ }));+ }- $("#note-history-list").val(noteHistoryId).trigger('change');+ if (historyItems.length > 0) {+ if (!noteHistoryId) {+ noteHistoryId = listEl.find("option:first").val();}- },- error: () => error("Error getting note history.")- });-}-$(document).bind('keydown', 'alt+h', showCurrentNoteHistory);+ listEl.val(noteHistoryId).trigger('change');+ }+ }-$("#note-history-list").on('change', () => {- const optVal = $("#note-history-list").find(":selected").val();+ $(document).bind('keydown', 'alt+h', showCurrentNoteHistory);- const historyItem = glob.historyItems.find(r => r.note_history_id === optVal);+ listEl.on('change', () => {+ const optVal = listEl.find(":selected").val();- let noteTitle = historyItem.note_title;- let noteText = historyItem.note_text;+ const historyItem = historyItems.find(r => r.note_history_id === optVal);- if (historyItem.encryption > 0) {- noteTitle = decryptString(noteTitle);- noteText = decryptString(noteText);- }+ let noteTitle = historyItem.note_title;+ let noteText = historyItem.note_text;++ if (historyItem.encryption > 0) {+ noteTitle = decryptString(noteTitle);+ noteText = decryptString(noteText);+ }++ titleEl.html(noteTitle);+ contentEl.html(noteText);+ });- $("#note-history-title").html(noteTitle);- $("#note-history-content").html(noteText);-});\ No newline at end of file+ return {+ showCurrentNoteHistory,+ showNoteHistoryDialog,+ showNoteHistoryDialogNotAsync+ };+})();\ No newline at end of filediff --git a/public/javascripts/recent_changes.js b/public/javascripts/recent_changes.jsindex 05fe012738..4f1df57765 100644--- a/public/javascripts/recent_changes.js+++ b/public/javascripts/recent_changes.js@@ -32,7 +32,7 @@ const recentChanges = (function() {});const revLink = $("<a>", {- href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');",+ href: "javascript: noteHistory.showNoteHistoryDialogNotAsync('" + change.note_id + "', '" + change.note_history_id + "');",text: 'rev'});diff --git a/views/index.ejs b/views/index.ejsindex b724879dba..a520f6f308 100644--- a/views/index.ejs+++ b/views/index.ejs@@ -84,7 +84,7 @@<input autocomplete="off" value="" id="note-title" style="font-size: x-large; border: 0; flex-grow: 100;" tabindex="1">- <button class="btn btn-xs" onclick="showCurrentNoteHistory();">History</button>+ <button class="btn btn-xs" onclick="noteHistory.showCurrentNoteHistory();">History</button></div></div>