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

82 lines
2.1 KiB
JavaScript
Raw Normal View History

import linkService from '../services/link.js';
import utils from '../services/utils.js';
import server from '../services/server.js';
const $dialog = $("#recent-changes-dialog");
const $content = $("#recent-changes-content");
2017-11-05 05:03:15 +08:00
async function showDialog() {
utils.closeActiveDialog();
glob.activeDialog = $dialog;
$dialog.modal();
const result = await server.get('recent-changes');
$content.empty();
if (result.length === 0) {
$content.append("No changes yet ...");
}
const groupedByDate = groupByDate(result);
for (const [dateDay, dayChanges] of groupedByDate) {
const changesListEl = $('<ul>');
const dayEl = $('<div>').append($('<b>').html(utils.formatDate(dateDay))).append(changesListEl);
2017-10-07 10:46:30 +08:00
for (const change of dayChanges) {
2019-03-13 03:58:31 +08:00
const formattedTime = utils.formatTime(utils.parseDate(change.utcDateModifiedTo));
let noteLink;
2017-12-04 06:46:56 +08:00
if (change.current_isDeleted) {
noteLink = change.current_title;
2017-11-05 01:39:26 +08:00
}
else {
2018-04-20 08:59:44 +08:00
noteLink = await linkService.createNoteLink(change.noteId, change.title);
2017-11-05 01:39:26 +08:00
}
changesListEl.append($('<li>')
.append(formattedTime + ' - ')
.append(noteLink));
}
$content.append(dayEl);
}
}
function groupByDate(result) {
const groupedByDate = new Map();
const dayCache = {};
for (const row of result) {
2019-03-13 03:58:31 +08:00
let dateDay = utils.parseDate(row.utcDateModifiedTo);
dateDay.setHours(0);
dateDay.setMinutes(0);
dateDay.setSeconds(0);
dateDay.setMilliseconds(0);
// this stupidity is to make sure that we always use the same day object because Map uses only
// reference equality
if (dayCache[dateDay]) {
dateDay = dayCache[dateDay];
}
else {
dayCache[dateDay] = dateDay;
}
2017-11-05 01:39:26 +08:00
if (!groupedByDate.has(dateDay)) {
groupedByDate.set(dateDay, []);
}
groupedByDate.get(dateDay).push(row);
}
return groupedByDate;
}
export default {
showDialog
};