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

83 lines
2.6 KiB
JavaScript
Raw Normal View History

import noteDetailService from '../services/note_detail.js';
import utils from '../services/utils.js';
import server from '../services/server.js';
const $dialog = $("#note-revisions-dialog");
const $list = $("#note-revision-list");
const $content = $("#note-revision-content");
const $title = $("#note-revision-title");
2019-11-09 15:53:13 +08:00
const $titleButtons = $("#note-revision-title-buttons");
let revisionItems = [];
let note;
2019-09-03 01:56:52 +08:00
export async function showCurrentNoteRevisions() {
2019-10-20 18:29:34 +08:00
await showNoteRevisionsDialog(noteDetailService.getActiveTabNoteId());
}
export async function showNoteRevisionsDialog(noteId, noteRevisionId) {
utils.closeActiveDialog();
glob.activeDialog = $dialog;
$dialog.modal();
$list.empty();
$content.empty();
2019-10-20 18:29:34 +08:00
note = noteDetailService.getActiveTabNote();
2019-11-02 02:21:48 +08:00
revisionItems = await server.get(`notes/${noteId}/revisions`);
for (const item of revisionItems) {
$list.append($('<option>', {
value: item.noteRevisionId,
2019-11-02 02:21:48 +08:00
text: item.dateLastEdited
}));
}
if (revisionItems.length > 0) {
if (!noteRevisionId) {
noteRevisionId = $list.find("option:first").val();
}
$list.val(noteRevisionId).trigger('change');
}
else {
$title.text("No revisions for this note yet...");
}
}
2019-11-02 02:21:48 +08:00
$list.on('change', async () => {
const optVal = $list.find(":selected").val();
const revisionItem = revisionItems.find(r => r.noteRevisionId === optVal);
$title.html(revisionItem.title);
2019-11-09 15:53:13 +08:00
const $downloadButton = $('<button class="btn btn-sm btn-primary" type="button">Download</button>');
$downloadButton.on('click', () => {
utils.download(utils.getHost() + `/api/notes/${revisionItem.noteId}/revisions/${revisionItem.noteRevisionId}/download`);
});
$titleButtons.html($downloadButton);
2019-11-02 02:21:48 +08:00
const fullNoteRevision = await server.get(`notes/${revisionItem.noteId}/revisions/${revisionItem.noteRevisionId}`);
if (note.type === 'text') {
2019-11-02 02:21:48 +08:00
$content.html(fullNoteRevision.content);
}
else if (note.type === 'code') {
2019-11-02 02:21:48 +08:00
$content.html($("<pre>").text(fullNoteRevision.content));
}
2019-11-09 06:09:57 +08:00
else if (note.type === 'image') {
$content.html($("<img>")
2019-11-09 15:53:13 +08:00
// reason why we put this inline as base64 is that we do not want to let user to copy this
// as a URL to be used in a note. Instead if they copy and paste it into a note, it will be a uploaded as a new note
2019-11-09 06:09:57 +08:00
.attr("src", `data:${note.mime};base64,` + fullNoteRevision.content)
.css("width", "100%"));
}
else {
$content.text("Preview isn't available for this note type.");
}
});