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 {
|
2020-09-13 21:59:31 +02:00
|
|
|
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() {
|
2019-09-09 21:23:04 +02:00
|
|
|
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() {
|
2020-10-15 20:37:55 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2020-02-03 21:16:33 +01:00
|
|
|
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) {
|
2020-01-14 21:23:32 +01:00
|
|
|
const revisionItems = await server.get(`notes/${note.noteId}/revisions`);
|
2019-07-25 22:31:09 +02:00
|
|
|
|
|
|
|
if (revisionItems.length === 0) {
|
2019-08-15 21:18:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:18:33 +02:00
|
|
|
this.$body.html(TPL);
|
2019-07-25 22:31:09 +02:00
|
|
|
|
2019-08-15 21:18:33 +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());
|
2019-09-08 09:17:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 23:35:24 +02:00
|
|
|
$listItem.append(revsWithinADay.join(", "));
|
2019-09-08 09:17:16 +02:00
|
|
|
$list.append($listItem);
|
2019-07-25 22:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 23:20:27 +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
|
|
|
}
|
|
|
|
|
2020-08-16 22:57:48 +02:00
|
|
|
export default NoteRevisionsWidget;
|