Skip to content

Build your personal knowledge base with Trilium Notes

widgets can now listen to sync data changes

zadambfbc124e78f1
diff --git a/src/public/javascripts/services/messaging.js b/src/public/javascripts/services/messaging.js
index 09c7556ace..977342058a 100644
--- a/src/public/javascripts/services/messaging.js
+++ b/src/public/javascripts/services/messaging.js
@@ -3,7 +3,8 @@ import infoService from "./info.js";
const $outstandingSyncsCount = $("#outstanding-syncs-count");
-const syncMessageHandlers = [];
+const allSyncMessageHandlers = [];
+const outsideSyncMessageHandlers = [];
const messageHandlers = [];
let ws;
@@ -26,8 +27,12 @@ function subscribeToMessages(messageHandler) {
messageHandlers.push(messageHandler);
}
-function subscribeToSyncMessages(messageHandler) {
- syncMessageHandlers.push(messageHandler);
+function subscribeToOutsideSyncMessages(messageHandler) {
+ outsideSyncMessageHandlers.push(messageHandler);
+}
+
+function subscribeToAllSyncMessages(messageHandler) {
+ allSyncMessageHandlers.push(messageHandler);
}
function handleMessage(event) {
@@ -46,9 +51,13 @@ function handleMessage(event) {
lastSyncId = message.data[message.data.length - 1].id;
}
+ for (const syncMessageHandler of allSyncMessageHandlers) {
+ syncMessageHandler(message.data);
+ }
+
const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId);
- for (const syncMessageHandler of syncMessageHandlers) {
+ for (const syncMessageHandler of outsideSyncMessageHandlers) {
syncMessageHandler(syncData);
}
@@ -102,5 +111,6 @@ setTimeout(() => {
export default {
logError,
subscribeToMessages,
- subscribeToSyncMessages
+ subscribeToAllSyncMessages,
+ subscribeToOutsideSyncMessages
};
\ No newline at end of file
diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js
index 4196cbb787..c791bcfb8c 100644
--- a/src/public/javascripts/services/note_detail.js
+++ b/src/public/javascripts/services/note_detail.js
@@ -357,13 +357,14 @@ function fireDetailLoaded() {
detailLoadedListeners = [];
}
-messagingService.subscribeToSyncMessages(syncData => {
+messagingService.subscribeToOutsideSyncMessages(syncData => {
const noteIdsToRefresh = new Set();
syncData
.filter(sync => sync.entityName === 'notes')
.forEach(sync => noteIdsToRefresh.add(sync.entityId));
+ // we need to reload because of promoted attributes
syncData
.filter(sync => sync.entityName === 'attributes')
.forEach(sync => noteIdsToRefresh.add(sync.noteId));
@@ -373,6 +374,12 @@ messagingService.subscribeToSyncMessages(syncData => {
}
});
+messagingService.subscribeToAllSyncMessages(syncData => {
+ for (const tc of tabContexts) {
+ tc.syncDataReceived(syncData);
+ }
+});
+
$tabContentsContainer.on("dragover", e => e.preventDefault());
$tabContentsContainer.on("dragleave", e => e.preventDefault());
diff --git a/src/public/javascripts/services/sidebar.js b/src/public/javascripts/services/sidebar.js
index 115870d9c8..612558181b 100644
--- a/src/public/javascripts/services/sidebar.js
+++ b/src/public/javascripts/services/sidebar.js
@@ -27,6 +27,7 @@ class Sidebar {
*/
constructor(ctx) {
this.ctx = ctx;
+ this.widgets = [];
this.$sidebar = ctx.$tabContent.find(".note-detail-sidebar");
this.$widgets = this.$sidebar.find(".note-detail-widgets");
this.$showSideBarButton = this.ctx.$tabContent.find(".show-sidebar-button");
@@ -46,6 +47,7 @@ class Sidebar {
}
async noteLoaded() {
+ this.widgets = [];
this.$widgets.empty();
const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget];
@@ -54,6 +56,8 @@ class Sidebar {
const $widget = this.createWidgetElement();
const attributesWidget = new widgetClass(this.ctx, $widget);
+ this.widgets.push(attributesWidget);
+
attributesWidget.renderBody(); // let it run in parallel
this.$widgets.append($widget);
@@ -69,6 +73,14 @@ class Sidebar {
return $widget;
}
+
+ syncDataReceived(syncData) {
+ for (const widget of this.widgets) {
+ if (widget.syncDataReceived) {
+ widget.syncDataReceived(syncData);
+ }
+ }
+ }
}
export default Sidebar;
\ No newline at end of file
diff --git a/src/public/javascripts/services/tab_context.js b/src/public/javascripts/services/tab_context.js
index 5751a54f8d..7e80f67bbd 100644
--- a/src/public/javascripts/services/tab_context.js
+++ b/src/public/javascripts/services/tab_context.js
@@ -244,7 +244,10 @@ class TabContext {
treeService.setNoteTitle(this.note.noteId, this.note.title);
- await server.put('notes/' + this.note.noteId, this.note.dto);
+ const resp = await server.put('notes/' + this.note.noteId, this.note.dto);
+
+ this.note.dateModified = resp.dateModified;
+ this.note.utcDateModified = resp.utcDateModified;
if (this.note.isProtected) {
protectedSessionHolder.touchProtectedSession();
@@ -352,6 +355,12 @@ class TabContext {
this.$tabContent.find('.aa-input').autocomplete('close');
}
}
+
+ syncDataReceived(syncData) {
+ if (this.sidebar) {
+ this.sidebar.syncDataReceived(syncData);
+ }
+ }
}
export default TabContext;
\ No newline at end of file
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index 1cc5093dc2..4a6d6850d9 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -759,7 +759,7 @@ messagingService.subscribeToMessages(message => {
}
});
-messagingService.subscribeToSyncMessages(syncData => {
+messagingService.subscribeToOutsideSyncMessages(syncData => {
if (syncData.some(sync => sync.entityName === 'branches')
|| syncData.some(sync => sync.entityName === 'notes')) {
diff --git a/src/public/javascripts/widgets/note_info.js b/src/public/javascripts/widgets/note_info.js
index d412a3ca23..12113a8092 100644
--- a/src/public/javascripts/widgets/note_info.js
+++ b/src/public/javascripts/widgets/note_info.js
@@ -54,6 +54,14 @@ class NoteInfoWidget {
$type.text(note.type);
$mime.text(note.mime);
}
+
+ syncDataReceived(syncData) {
+ if (syncData.find(sd => sd.entityName === 'notes' && sd.entityId === this.ctx.note.noteId)) {
+ console.log("Re-rendering note info");
+
+ this.renderBody();
+ }
+ }
}
export default NoteInfoWidget;
\ No newline at end of file
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 64b658e92a..72b8654db7 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -68,7 +68,7 @@ async function updateNote(req) {
const note = req.body;
const noteId = req.params.noteId;
- await noteService.updateNote(noteId, note);
+ return await noteService.updateNote(noteId, note);
}
async function deleteNote(req) {
diff --git a/src/services/notes.js b/src/services/notes.js
index 02b8ed77ca..25bbf7e28a 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -371,6 +371,11 @@ async function updateNote(noteId, noteUpdates) {
}
await protectNoteRevisions(note);
+
+ return {
+ dateModified: note.dateModified,
+ utcDateModified: note.utcDateModified
+ };
}
/** @return {boolean} - true if note has been deleted, false otherwise */