116 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-04-26 09:40:02 +02:00
import server from "../../services/server.js";
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 {
isEnabled() {
return super.isEnabled() && !this.note.hasLabel('noteRevisionsWidgetDisabled');
}
2020-03-16 22:14:18 +01:00
get widgetTitle() { return "Note revisions"; }
2019-07-25 22:31:09 +02:00
2020-03-16 22:14:18 +01:00
get help() {
return {
title: "Note revisions track changes in the note across the time.",
url: "https://github.com/zadam/trilium/wiki/Note-revisions"
};
}
2020-03-16 22:14:18 +01:00
get headerActions() {
const $showFullButton = $("<a>")
.addClass("bx bx-list-ul")
.addClass('widget-header-action')
.attr('title', 'Show Note revisions dialog');
2019-11-09 17:39:48 +01:00
$showFullButton.on('click', async () => {
2020-04-26 09:40:02 +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-09-05 23:35:24 +02:00
const groupedRevisionItems = this.getGroupedRevisionItems(revisionItems);
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
2020-09-05 23:35:24 +02:00
for (const [date, items] of groupedRevisionItems) {
const $listItem = $('<li>').append($("<strong>").text(date + ": "));
const revsWithinADay = [];
for (const item of items) {
const $rev = $("<span>");
$rev.append($("<a>", {
'data-action': 'note-revision',
'data-note-path': note.noteId,
'data-note-revision-id': item.noteRevisionId,
title: 'This revision was last edited on ' + item.dateLastEdited,
href: 'javascript:'
})
.text(item.dateLastEdited.substr(11, 5)));
revsWithinADay.push($rev.html());
}
2020-09-05 23:35:24 +02:00
$listItem.append(revsWithinADay.join(", "));
$list.append($listItem);
2019-07-25 22:31:09 +02:00
}
}
2020-09-05 23:35:24 +02:00
getGroupedRevisionItems(revisionItems) {
const grouped = new Map(); // map preserves order of insertion
for (const item of revisionItems) {
const [date] = item.dateLastEdited.substr(0, 16).split(" ");
if (!grouped.has(date)) {
grouped.set(date, []);
}
grouped.get(date).push(item);
}
return grouped;
}
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;