Skip to content

Build your personal knowledge base with Trilium Notes

note map refactoring

zadame3fc0968baa2
diff --git a/src/public/app/widgets/note_map.js b/src/public/app/widgets/note_map.js
index ae1d80314c..586be2c9c8 100644
--- a/src/public/app/widgets/note_map.js
+++ b/src/public/app/widgets/note_map.js
@@ -233,38 +233,40 @@ export default class NoteMapWidget extends NoteContextAwareWidget {
}
async loadNotesAndRelations(mapRootNoteId) {
- this.linkIdToLinkMap = {};
- this.noteIdToLinkCountMap = {};
-
const resp = await server.post(`note-map/${mapRootNoteId}/${this.mapType}`);
- this.noteIdToLinkCountMap = resp.noteIdToLinkCountMap;
-
- this.calculateSizes(resp.noteIdToDescendantCountMap);
+ this.calculateSizes(resp);
- for (const link of resp.links) {
- this.linkIdToLinkMap[link.id] = link;
- }
+ const links = this.getGroupedLinks(resp.links);
- const noteIdToLinkIdMap = {};
- noteIdToLinkIdMap[this.noteId] = new Set(); // for case there are no relations
- const linksGroupedBySourceTarget = {};
+ this.nodes = resp.notes.map(([noteId, title, type]) => ({
+ id: noteId,
+ name: title,
+ type: type,
+ }));
- for (const link of Object.values(this.linkIdToLinkMap)) {
- noteIdToLinkIdMap[link.sourceNoteId] = noteIdToLinkIdMap[link.sourceNoteId] || new Set();
- noteIdToLinkIdMap[link.sourceNoteId].add(link.id);
+ return {
+ nodes: this.nodes,
+ links: links.map(link => ({
+ id: link.id,
+ source: link.sourceNoteId,
+ target: link.targetNoteId,
+ name: link.names.join(", ")
+ }))
+ };
+ }
- noteIdToLinkIdMap[link.targetNoteId] = noteIdToLinkIdMap[link.targetNoteId] || new Set();
- noteIdToLinkIdMap[link.targetNoteId].add(link.id);
+ getGroupedLinks(links) {
+ const linksGroupedBySourceTarget = {};
+ for (const link of links) {
const key = `${link.sourceNoteId}-${link.targetNoteId}`;
if (key in linksGroupedBySourceTarget) {
if (!linksGroupedBySourceTarget[key].names.includes(link.name)) {
linksGroupedBySourceTarget[key].names.push(link.name);
}
- }
- else {
+ } else {
linksGroupedBySourceTarget[key] = {
id: key,
sourceNoteId: link.sourceNoteId,
@@ -274,24 +276,12 @@ export default class NoteMapWidget extends NoteContextAwareWidget {
}
}
- this.nodes = resp.notes.map(([noteId, title, type]) => ({
- id: noteId,
- name: title,
- type: type,
- }));
-
- return {
- nodes: this.nodes,
- links: Object.values(linksGroupedBySourceTarget).map(link => ({
- id: link.id,
- source: link.sourceNoteId,
- target: link.targetNoteId,
- name: link.names.join(", ")
- }))
- };
+ return Object.values(linksGroupedBySourceTarget);
}
- calculateSizes(noteIdToDescendantCountMap) {
+ calculateSizes(resp) {
+ const {noteIdToDescendantCountMap} = resp;
+
this.noteIdToSizeMap = {};
for (const noteId in noteIdToDescendantCountMap) {