Notes/src/public/javascripts/widgets/note_revisions.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-25 22:31:09 +02:00
import server from "../services/server.js";
2020-02-02 20:02:08 +01:00
import CollapsibleWidget from "./collapsible_widget.js";
2019-07-25 22:31:09 +02:00
const TPL = `
<ul class="note-revision-list" style="max-height: 150px; overflow: auto;">
</ul>
`;
2020-02-02 18:46:50 +01:00
class NoteRevisionsWidget extends CollapsibleWidget {
2019-08-17 10:45:20 +02:00
getWidgetTitle() { return "Note revisions"; }
2019-07-25 22:31:09 +02:00
getHelp() {
return {
title: "Note revisions track changes in the note across the time.",
url: "https://github.com/zadam/trilium/wiki/Note-revisions"
};
}
2019-08-29 23:08:30 +02:00
getHeaderActions() {
const $showFullButton = $("<a>").append("show dialog").addClass('widget-header-action');
2019-11-09 17:39:48 +01:00
$showFullButton.on('click', async () => {
2019-08-29 23:08:30 +02:00
const attributesDialog = await import("../dialogs/note_revisions.js");
2020-02-25 19:19:10 +01:00
attributesDialog.showCurrentNoteRevisions(this.noteId);
2019-08-29 23:08:30 +02:00
});
return [$showFullButton];
}
noteSwitched() {
const noteId = this.noteId;
// avoid executing this expensive operation multiple times when just going through notes (with keyboard especially)
// until the users settles on a note
setTimeout(() => {
if (this.noteId === noteId) {
this.refresh();
}
}, 1000);
}
2020-02-02 21:16:20 +01:00
async refreshWithNote(note) {
const revisionItems = await server.get(`notes/${note.noteId}/revisions`);
2019-07-25 22:31:09 +02:00
if (revisionItems.length === 0) {
this.$body.text("No revisions yet...");
2019-07-25 22:31:09 +02:00
return;
}
2020-02-02 21:16:20 +01:00
if (note.noteId !== this.noteId) {
return;
}
this.$body.html(TPL);
2019-07-25 22:31:09 +02:00
const $list = this.$body.find('.note-revision-list');
2019-07-25 22:31:09 +02:00
for (const item of revisionItems) {
const $listItem = $('<li>').append($("<a>", {
2019-07-25 22:31:09 +02:00
'data-action': 'note-revision',
'data-note-path': note.noteId,
2019-07-25 22:31:09 +02:00
'data-note-revision-id': item.noteRevisionId,
href: 'javascript:'
2019-11-01 19:21:48 +01:00
}).text(item.dateLastEdited.substr(0, 16)));
if (item.contentLength !== null) {
2019-11-09 08:53:13 +01:00
$listItem.append($("<span>").text(` (${item.contentLength} bytes)`))
}
$list.append($listItem);
2019-07-25 22:31:09 +02:00
}
}
2020-02-16 19:23:49 +01:00
entitiesReloadedEvent({loadResults}) {
2020-01-29 21:38:58 +01:00
if (loadResults.hasNoteRevisionForNote(this.noteId)) {
this.refresh();
}
}
2019-07-25 22:31:09 +02:00
}
export default NoteRevisionsWidget;