mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	widgets can now listen to sync data changes
This commit is contained in:
		| @@ -3,7 +3,8 @@ import infoService from "./info.js"; | |||||||
|  |  | ||||||
| const $outstandingSyncsCount = $("#outstanding-syncs-count"); | const $outstandingSyncsCount = $("#outstanding-syncs-count"); | ||||||
|  |  | ||||||
| const syncMessageHandlers = []; | const allSyncMessageHandlers = []; | ||||||
|  | const outsideSyncMessageHandlers = []; | ||||||
| const messageHandlers = []; | const messageHandlers = []; | ||||||
|  |  | ||||||
| let ws; | let ws; | ||||||
| @@ -26,8 +27,12 @@ function subscribeToMessages(messageHandler) { | |||||||
|     messageHandlers.push(messageHandler); |     messageHandlers.push(messageHandler); | ||||||
| } | } | ||||||
|  |  | ||||||
| function subscribeToSyncMessages(messageHandler) { | function subscribeToOutsideSyncMessages(messageHandler) { | ||||||
|     syncMessageHandlers.push(messageHandler); |     outsideSyncMessageHandlers.push(messageHandler); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function subscribeToAllSyncMessages(messageHandler) { | ||||||
|  |     allSyncMessageHandlers.push(messageHandler); | ||||||
| } | } | ||||||
|  |  | ||||||
| function handleMessage(event) { | function handleMessage(event) { | ||||||
| @@ -46,9 +51,13 @@ function handleMessage(event) { | |||||||
|             lastSyncId = message.data[message.data.length - 1].id; |             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); |         const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId); | ||||||
|  |  | ||||||
|         for (const syncMessageHandler of syncMessageHandlers) { |         for (const syncMessageHandler of outsideSyncMessageHandlers) { | ||||||
|             syncMessageHandler(syncData); |             syncMessageHandler(syncData); | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -102,5 +111,6 @@ setTimeout(() => { | |||||||
| export default { | export default { | ||||||
|     logError, |     logError, | ||||||
|     subscribeToMessages, |     subscribeToMessages, | ||||||
|     subscribeToSyncMessages |     subscribeToAllSyncMessages, | ||||||
|  |     subscribeToOutsideSyncMessages | ||||||
| }; | }; | ||||||
| @@ -357,13 +357,14 @@ function fireDetailLoaded() { | |||||||
|     detailLoadedListeners = []; |     detailLoadedListeners = []; | ||||||
| } | } | ||||||
|  |  | ||||||
| messagingService.subscribeToSyncMessages(syncData => { | messagingService.subscribeToOutsideSyncMessages(syncData => { | ||||||
|     const noteIdsToRefresh = new Set(); |     const noteIdsToRefresh = new Set(); | ||||||
|  |  | ||||||
|     syncData |     syncData | ||||||
|         .filter(sync => sync.entityName === 'notes') |         .filter(sync => sync.entityName === 'notes') | ||||||
|         .forEach(sync => noteIdsToRefresh.add(sync.entityId)); |         .forEach(sync => noteIdsToRefresh.add(sync.entityId)); | ||||||
|  |  | ||||||
|  |     // we need to reload because of promoted attributes | ||||||
|     syncData |     syncData | ||||||
|         .filter(sync => sync.entityName === 'attributes') |         .filter(sync => sync.entityName === 'attributes') | ||||||
|         .forEach(sync => noteIdsToRefresh.add(sync.noteId)); |         .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("dragover", e => e.preventDefault()); | ||||||
|  |  | ||||||
| $tabContentsContainer.on("dragleave", e => e.preventDefault()); | $tabContentsContainer.on("dragleave", e => e.preventDefault()); | ||||||
|   | |||||||
| @@ -27,6 +27,7 @@ class Sidebar { | |||||||
|      */ |      */ | ||||||
|     constructor(ctx) { |     constructor(ctx) { | ||||||
|         this.ctx = ctx; |         this.ctx = ctx; | ||||||
|  |         this.widgets = []; | ||||||
|         this.$sidebar = ctx.$tabContent.find(".note-detail-sidebar"); |         this.$sidebar = ctx.$tabContent.find(".note-detail-sidebar"); | ||||||
|         this.$widgets = this.$sidebar.find(".note-detail-widgets"); |         this.$widgets = this.$sidebar.find(".note-detail-widgets"); | ||||||
|         this.$showSideBarButton = this.ctx.$tabContent.find(".show-sidebar-button"); |         this.$showSideBarButton = this.ctx.$tabContent.find(".show-sidebar-button"); | ||||||
| @@ -46,6 +47,7 @@ class Sidebar { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     async noteLoaded() { |     async noteLoaded() { | ||||||
|  |         this.widgets = []; | ||||||
|         this.$widgets.empty(); |         this.$widgets.empty(); | ||||||
|  |  | ||||||
|         const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget]; |         const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget]; | ||||||
| @@ -54,6 +56,8 @@ class Sidebar { | |||||||
|             const $widget = this.createWidgetElement(); |             const $widget = this.createWidgetElement(); | ||||||
|  |  | ||||||
|             const attributesWidget = new widgetClass(this.ctx, $widget); |             const attributesWidget = new widgetClass(this.ctx, $widget); | ||||||
|  |             this.widgets.push(attributesWidget); | ||||||
|  |  | ||||||
|             attributesWidget.renderBody(); // let it run in parallel |             attributesWidget.renderBody(); // let it run in parallel | ||||||
|  |  | ||||||
|             this.$widgets.append($widget); |             this.$widgets.append($widget); | ||||||
| @@ -69,6 +73,14 @@ class Sidebar { | |||||||
|  |  | ||||||
|         return $widget; |         return $widget; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     syncDataReceived(syncData) { | ||||||
|  |         for (const widget of this.widgets) { | ||||||
|  |             if (widget.syncDataReceived) { | ||||||
|  |                 widget.syncDataReceived(syncData); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| export default Sidebar; | export default Sidebar; | ||||||
| @@ -244,7 +244,10 @@ class TabContext { | |||||||
|  |  | ||||||
|         treeService.setNoteTitle(this.note.noteId, this.note.title); |         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) { |         if (this.note.isProtected) { | ||||||
|             protectedSessionHolder.touchProtectedSession(); |             protectedSessionHolder.touchProtectedSession(); | ||||||
| @@ -352,6 +355,12 @@ class TabContext { | |||||||
|             this.$tabContent.find('.aa-input').autocomplete('close'); |             this.$tabContent.find('.aa-input').autocomplete('close'); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     syncDataReceived(syncData) { | ||||||
|  |         if (this.sidebar) { | ||||||
|  |             this.sidebar.syncDataReceived(syncData); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| export default TabContext; | export default TabContext; | ||||||
| @@ -759,7 +759,7 @@ messagingService.subscribeToMessages(message => { | |||||||
|    } |    } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| messagingService.subscribeToSyncMessages(syncData => { | messagingService.subscribeToOutsideSyncMessages(syncData => { | ||||||
|     if (syncData.some(sync => sync.entityName === 'branches') |     if (syncData.some(sync => sync.entityName === 'branches') | ||||||
|         || syncData.some(sync => sync.entityName === 'notes')) { |         || syncData.some(sync => sync.entityName === 'notes')) { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -54,6 +54,14 @@ class NoteInfoWidget { | |||||||
|         $type.text(note.type); |         $type.text(note.type); | ||||||
|         $mime.text(note.mime); |         $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; | export default NoteInfoWidget; | ||||||
| @@ -68,7 +68,7 @@ async function updateNote(req) { | |||||||
|     const note = req.body; |     const note = req.body; | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|     await noteService.updateNote(noteId, note); |     return await noteService.updateNote(noteId, note); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteNote(req) { | async function deleteNote(req) { | ||||||
|   | |||||||
| @@ -371,6 +371,11 @@ async function updateNote(noteId, noteUpdates) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     await protectNoteRevisions(note); |     await protectNoteRevisions(note); | ||||||
|  |  | ||||||
|  |     return { | ||||||
|  |         dateModified: note.dateModified, | ||||||
|  |         utcDateModified: note.utcDateModified | ||||||
|  |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @return {boolean} - true if note has been deleted, false otherwise */ | /** @return {boolean} - true if note has been deleted, false otherwise */ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user