trilium/public/javascripts/dialogs/note_history.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-11-05 01:55:46 +08:00
const noteHistory = (function() {
const dialogEl = $("#note-history-dialog");
const listEl = $("#note-history-list");
const contentEl = $("#note-history-content");
const titleEl = $("#note-history-title");
2017-11-05 01:55:46 +08:00
let historyItems = [];
2017-10-09 23:28:56 +08:00
2017-11-05 01:55:46 +08:00
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);
}
2017-09-22 10:20:10 +08:00
2017-11-05 01:55:46 +08:00
async function showNoteHistoryDialog(noteId, noteHistoryId) {
dialogEl.dialog({
modal: true,
width: 800,
height: 700
});
2017-09-22 10:20:10 +08:00
2017-11-05 01:55:46 +08:00
listEl.empty();
contentEl.empty();
2017-11-05 01:55:46 +08:00
historyItems = await $.ajax({
url: baseApiUrl + 'notes-history/' + noteId,
type: 'GET',
error: () => error("Error getting note history.")
});
2017-11-05 01:55:46 +08:00
for (const item of historyItems) {
const dateModified = getDateFromTS(item.date_modified_to);
2017-09-22 10:20:10 +08:00
2017-11-05 01:55:46 +08:00
$("#note-history-list").append($('<option>', {
value: item.note_history_id,
text: formatDateTime(dateModified)
}));
}
2017-09-22 10:20:10 +08:00
2017-11-05 01:55:46 +08:00
if (historyItems.length > 0) {
if (!noteHistoryId) {
noteHistoryId = listEl.find("option:first").val();
2017-09-22 10:20:10 +08:00
}
2017-11-05 01:55:46 +08:00
listEl.val(noteHistoryId).trigger('change');
}
}
2017-11-05 01:55:46 +08:00
$(document).bind('keydown', 'alt+h', showCurrentNoteHistory);
2017-10-25 08:00:54 +08:00
2017-11-05 01:55:46 +08:00
listEl.on('change', () => {
const optVal = listEl.find(":selected").val();
2017-11-05 01:55:46 +08:00
const historyItem = historyItems.find(r => r.note_history_id === optVal);
2017-11-05 01:55:46 +08:00
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);
});
2017-11-05 01:55:46 +08:00
return {
showCurrentNoteHistory,
showNoteHistoryDialog,
showNoteHistoryDialogNotAsync
};
})();