Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
note map refactoring
zadam476b8250c999
diff --git a/src/becca/entities/note.js b/src/becca/entities/note.jsindex d148363d74..e0b2227723 100644--- a/src/becca/entities/note.js+++ b/src/becca/entities/note.js@@ -745,11 +745,15 @@ class Note extends AbstractEntity {}/** @return {Note[]} */- getSubtreeNotes() {+ getSubtreeNotes(includeArchived = true) {+ if (this.isArchived) {+ return [];+ }+const arr = [[this]];for (const childNote of this.children) {- arr.push(childNote.getSubtreeNotes());+ arr.push(childNote.getSubtreeNotes(includeArchived));}return arr.flat();diff --git a/src/public/app/services/note_context.js b/src/public/app/services/note_context.jsindex b51db1ce85..b9588ebd2b 100644--- a/src/public/app/services/note_context.js+++ b/src/public/app/services/note_context.js@@ -39,7 +39,7 @@ class NoteContext extends Component {utils.closeActiveDialog();this.notePath = resolvedNotePath;- this.noteId = treeService.getNoteIdFromNotePath(resolvedNotePath);+ ({noteId: this.noteId, parentNoteId: this.parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(resolvedNotePath));this.readOnlyTemporarilyDisabled = false;diff --git a/src/public/app/widgets/type_widgets/note_map.js b/src/public/app/widgets/type_widgets/note_map.jsindex a746d0d7e9..e44c4dbdb1 100644--- a/src/public/app/widgets/type_widgets/note_map.js+++ b/src/public/app/widgets/type_widgets/note_map.js@@ -2,6 +2,8 @@ import TypeWidget from "./type_widget.js";import libraryLoader from "../../services/library_loader.js";import server from "../../services/server.js";import attributeService from "../../services/attributes.js";+import hoistedNoteService from "../../services/hoisted_note.js";+import appContext from "../../services/app_context.js";const TPL = `<div class="note-detail-note-map note-detail-printable" style="position: relative;"><style>@@ -114,7 +116,16 @@ export default class NoteMapTypeWidget extends TypeWidget {this.graph.d3Force('charge').distanceMax(1000);- const data = await this.loadNotesAndRelations();+ let mapRootNoteId = this.note.getLabelValue("mapRootNoteId");++ if (mapRootNoteId === 'hoisted') {+ mapRootNoteId = hoistedNoteService.getHoistedNoteId();+ }+ else if (!mapRootNoteId) {+ mapRootNoteId = appContext.tabManager.getActiveContext().parentNoteId;+ }++ const data = await this.loadNotesAndRelations(mapRootNoteId);this.renderData(data);}@@ -216,11 +227,11 @@ export default class NoteMapTypeWidget extends TypeWidget {ctx.restore();}- async loadNotesAndRelations() {+ async loadNotesAndRelations(mapRootNoteId) {this.linkIdToLinkMap = {};this.noteIdToLinkCountMap = {};- const resp = await server.post(`note-map/${this.mapType}`);+ const resp = await server.post(`note-map/${mapRootNoteId}/${this.mapType}`);this.noteIdToLinkCountMap = resp.noteIdToLinkCountMap;diff --git a/src/routes/api/link_map.js b/src/routes/api/link_map.jsindex 9352a14992..91fe31a0ad 100644--- a/src/routes/api/link_map.js+++ b/src/routes/api/link_map.js@@ -101,11 +101,12 @@ function buildDescendantCountMap() {return noteIdToCountMap;}-function getGlobalLinkMap() {+function getGlobalLinkMap(req) {+ const mapRootNote = becca.getNote(req.params.noteId);+const noteIds = new Set();- const notes = Object.values(becca.notes)- .filter(note => !note.isArchived)+ const notes = mapRootNote.getSubtreeNotes(false).map(note => [note.noteId,note.isContentAvailable() ? note.title : '[protected]',@@ -144,11 +145,12 @@ function getGlobalLinkMap() {};}-function getGlobalTreeMap() {+function getGlobalTreeMap(req) {+ const mapRootNote = becca.getNote(req.params.noteId);const noteIds = new Set();- const notes = Object.values(becca.notes)- .filter(note => !note.isArchived && !note.hasLabel('excludeFromTreeMap'))+ const notes = mapRootNote.getSubtreeNotes(false)+ .filter(note => !note.hasLabel('excludeFromTreeMap')).map(note => [note.noteId,note.isContentAvailable() ? note.title : '[protected]',diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 5302c83b1e..1d3d918f0c 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -221,8 +221,8 @@ function register(app) {apiRoute(GET, '/api/attributes/values/:attributeName', attributesRoute.getValuesForAttribute);apiRoute(POST, '/api/notes/:noteId/link-map', linkMapRoute.getLinkMap);- apiRoute(POST, '/api/note-map/tree', linkMapRoute.getGlobalTreeMap);- apiRoute(POST, '/api/note-map/link', linkMapRoute.getGlobalLinkMap);+ apiRoute(POST, '/api/note-map/:noteId/tree', linkMapRoute.getGlobalTreeMap);+ apiRoute(POST, '/api/note-map/:noteId/link', linkMapRoute.getGlobalLinkMap);apiRoute(GET, '/api/special-notes/inbox/:date', specialNotesRoute.getInboxNote);apiRoute(GET, '/api/special-notes/date/:date', specialNotesRoute.getDateNote);diff --git a/src/services/attributes.js b/src/services/attributes.jsindex 8168dbe540..c63ef821ca 100644--- a/src/services/attributes.js+++ b/src/services/attributes.js@@ -46,6 +46,7 @@ const BUILTIN_ATTRIBUTES = [{ type: 'label', name: 'datePattern' },{ type: 'label', name: 'pageSize' },{ type: 'label', name: 'viewType' },+ { type: 'label', name: 'mapRootNoteId' },// relation names{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true },diff --git a/src/services/special_notes.js b/src/services/special_notes.jsindex ef69d813a2..cde16cfa43 100644--- a/src/services/special_notes.js+++ b/src/services/special_notes.js@@ -81,20 +81,22 @@ function getSinglesNoteRoot() {return singlesNoteRoot;}-function getGlobalLinkMapNote() {- let globalLinkMapNote = becca.getNote('globalnotemap');+function getGlobalNoteMap() {+ let globalNoteMap = becca.getNote('globalnotemap');- if (!globalLinkMapNote) {- globalLinkMapNote = noteService.createNewNote({+ if (!globalNoteMap) {+ globalNoteMap = noteService.createNewNote({noteId: 'globalnotemap',title: 'Global Note Map',type: 'note-map',content: '',parentNoteId: getSinglesNoteRoot().noteId}).note;++ globalNoteMap.addLabel('mapRootNoteId', 'hoisted');}- return globalLinkMapNote;+ return globalNoteMap;}function getSqlConsoleRoot() {@@ -186,7 +188,7 @@ function createMissingSpecialNotes() {getSqlConsoleRoot();getSinglesNoteRoot();getSinglesNoteRoot();- getGlobalLinkMapNote();+ getGlobalNoteMap();}module.exports = {