diff --git a/docs/frontend_api/FrontendScriptApi.html b/docs/frontend_api/FrontendScriptApi.html index 6f60a7867..d4415c6ca 100644 --- a/docs/frontend_api/FrontendScriptApi.html +++ b/docs/frontend_api/FrontendScriptApi.html @@ -1390,7 +1390,7 @@
Source:
@@ -1523,7 +1523,7 @@
Source:
@@ -1629,7 +1629,7 @@
Source:
@@ -1735,7 +1735,7 @@
Source:
@@ -1894,7 +1894,7 @@
Source:
@@ -2001,7 +2001,7 @@ if some action needs to happen on only one specific instance.
Source:
@@ -2156,7 +2156,7 @@ if some action needs to happen on only one specific instance.
Source:
@@ -2619,7 +2619,7 @@ otherwise (by e.g. createNoteLink())
Source:
@@ -2774,7 +2774,7 @@ otherwise (by e.g. createNoteLink())
Source:
@@ -2883,7 +2883,7 @@ note.
Source:
@@ -3038,7 +3038,7 @@ note.
Source:
@@ -3171,7 +3171,7 @@ note.
Source:
@@ -3277,7 +3277,7 @@ note.
Source:
@@ -3365,7 +3365,7 @@ note.
Source:
@@ -3419,7 +3419,7 @@ note. -

reloadNotesAndTheirChildren(noteId)

+

reloadNotes(noteIds)

@@ -3459,13 +3459,13 @@ note. - noteId + noteIds -string +Array.<string> @@ -3546,139 +3546,6 @@ note. - - - - - - -

reloadParents(noteId)

- - - - - - - - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
noteId - - -string - - - -
- - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - - - - - - - - @@ -4281,7 +4148,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -4432,7 +4299,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -4569,7 +4436,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -4706,7 +4573,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
diff --git a/docs/frontend_api/NoteShort.html b/docs/frontend_api/NoteShort.html index 11675a2c8..1192154d2 100644 --- a/docs/frontend_api/NoteShort.html +++ b/docs/frontend_api/NoteShort.html @@ -28,7 +28,7 @@
-

NoteShort()

+

NoteShort(treeCache, row, branches)

FIXME: rethink how attributes are cached in Note entities since they are long lived inside the cache. Attribute cache should be limited to "transaction". @@ -48,7 +48,7 @@ This note's representation is used in note tree and is kept in TreeCache.
-

new NoteShort()

+

new NoteShort(treeCache, row, branches)

@@ -63,6 +63,101 @@ This note's representation is used in note tree and is kept in TreeCache. +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
treeCache + + +TreeCache + + + +
row + + +Object.<string, Object> + + + +
branches + + +Array.<Branch> + + + + all relevant branches, i.e. where this note is either child or parent
+ + @@ -96,7 +191,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -186,7 +281,143 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
+ + + + + + + + + + + + + + + + +

children :Array.<string>

+ + + + + + +
Type:
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + +

childToBranch :Object.<string, string>

+ + + + + + +
Type:
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
@@ -244,7 +475,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -302,7 +533,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -360,7 +591,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -418,7 +649,143 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
+ + + + + + + +
+ + + + + + + + +

parents :Array.<string>

+ + + + + + +
Type:
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + +

parentToBranch :Object.<string, string>

+ + + + + + +
Type:
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
@@ -476,7 +843,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -534,7 +901,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -682,7 +1049,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -849,7 +1216,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1023,7 +1390,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1129,7 +1496,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1231,7 +1598,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1333,7 +1700,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1435,7 +1802,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1586,7 +1953,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1753,7 +2120,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -1920,7 +2287,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2075,7 +2442,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2181,7 +2548,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2283,7 +2650,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2434,7 +2801,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2601,7 +2968,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2768,7 +3135,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -2923,7 +3290,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3093,7 +3460,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3244,7 +3611,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3354,7 +3721,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3528,7 +3895,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3634,7 +4001,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3785,7 +4152,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -3940,7 +4307,7 @@ This note's representation is used in note tree and is kept in TreeCache.
Source:
@@ -4051,7 +4418,7 @@ Cache is note instance scoped.
Source:
@@ -4135,7 +4502,7 @@ Cache is note instance scoped.
Source:
diff --git a/docs/frontend_api/entities_note_full.js.html b/docs/frontend_api/entities_note_full.js.html index ceca67bc9..b66927b7b 100644 --- a/docs/frontend_api/entities_note_full.js.html +++ b/docs/frontend_api/entities_note_full.js.html @@ -32,8 +32,8 @@ * Represents full note, specifically including note's content. */ class NoteFull extends NoteShort { - constructor(treeCache, row) { - super(treeCache, row); + constructor(treeCache, row, noteShort) { + super(treeCache, row, []); /** @param {string} */ this.content = row.content; @@ -49,6 +49,12 @@ class NoteFull extends NoteShort { /** @param {string} */ this.utcDateModified = row.utcDateModified; + + /* ugly */ + this.parents = noteShort.parents; + this.parentToBranch = noteShort.parentToBranch; + this.children = noteShort.children; + this.childToBranch = noteShort.childToBranch; } } diff --git a/docs/frontend_api/entities_note_short.js.html b/docs/frontend_api/entities_note_short.js.html index a61030a29..debf67855 100644 --- a/docs/frontend_api/entities_note_short.js.html +++ b/docs/frontend_api/entities_note_short.js.html @@ -28,6 +28,7 @@
import server from '../services/server.js';
 import Attribute from './attribute.js';
+import branches from "../services/branches.js";
 
 const LABEL = 'label';
 const LABEL_DEFINITION = 'label-definition';
@@ -41,7 +42,12 @@ const RELATION_DEFINITION = 'relation-definition';
  * This note's representation is used in note tree and is kept in TreeCache.
  */
 class NoteShort {
-    constructor(treeCache, row) {
+    /**
+     * @param {TreeCache} treeCache
+     * @param {Object.<string, Object>} row
+     * @param {Branch[]} branches - all relevant branches, i.e. where this note is either child or parent
+     */
+    constructor(treeCache, row, branches) {
         this.treeCache = treeCache;
         /** @param {string} */
         this.noteId = row.noteId;
@@ -57,6 +63,55 @@ class NoteShort {
         this.archived = row.archived;
         /** @param {string} */
         this.cssClass = row.cssClass;
+
+        /** @type {string[]} */
+        this.parents = [];
+        /** @type {string[]} */
+        this.children = [];
+
+        /** @type {Object.<string, string>} */
+        this.parentToBranch = {};
+
+        /** @type {Object.<string, string>} */
+        this.childToBranch = {};
+
+        for (const branch of branches) {
+            if (this.noteId === branch.noteId) {
+                this.parents.push(branch.parentNoteId);
+                this.parentToBranch[branch.parentNoteId] = branch.branchId;
+            }
+            else if (this.noteId === branch.parentNoteId) {
+                this.children.push(branch.noteId);
+                this.childToBranch[branch.noteId] = branch.branchId;
+            }
+            else {
+                throw new Error(`Unknown branch ${branch.branchId} for note ${this.noteId}`);
+            }
+        }
+    }
+
+    addParent(parentNoteId, branchId) {
+        if (!this.parents.includes(parentNoteId)) {
+            this.parents.push(parentNoteId);
+        }
+
+        this.parentToBranch[parentNoteId] = branchId;
+    }
+
+    addChild(childNoteId, branchId) {
+        if (!this.children.includes(childNoteId)) {
+            this.children.push(childNoteId);
+        }
+
+        this.childToBranch[childNoteId] = branchId;
+
+        const branchIdPos = {};
+
+        for (const branchId of Object.values(this.childToBranch)) {
+            branchIdPos[branchId] = this.treeCache.branches[branchId].notePosition;
+        }
+
+        this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] < branchIdPos[this.childToBranch[b]] ? -1 : 1);
     }
 
     /** @returns {boolean} */
@@ -86,48 +141,41 @@ class NoteShort {
 
     /** @returns {Promise<Branch[]>} */
     async getBranches() {
-        const branchIds = this.treeCache.parents[this.noteId].map(
-            parentNoteId => this.treeCache.getBranchIdByChildParent(this.noteId, parentNoteId));
+        const branchIds = Object.values(this.parentToBranch);
 
         return this.treeCache.getBranches(branchIds);
     }
 
     /** @returns {boolean} */
     hasChildren() {
-        return this.treeCache.children[this.noteId]
-            && this.treeCache.children[this.noteId].length > 0;
+        return this.children.length > 0;
     }
 
     /** @returns {Promise<Branch[]>} */
     async getChildBranches() {
-        if (!this.treeCache.children[this.noteId]) {
-            return [];
-        }
+        const branchIds = Object.values(this.childToBranch);
 
-        const branchIds = this.treeCache.children[this.noteId].map(
-            childNoteId => this.treeCache.getBranchIdByChildParent(childNoteId, this.noteId));
-
-        return await this.treeCache.getBranches(branchIds);
+        return this.treeCache.getBranches(branchIds);
     }
 
     /** @returns {string[]} */
     getParentNoteIds() {
-        return this.treeCache.parents[this.noteId] || [];
+        return this.parents;
     }
 
     /** @returns {Promise<NoteShort[]>} */
     async getParentNotes() {
-        return await this.treeCache.getNotes(this.getParentNoteIds());
+        return await this.treeCache.getNotes(this.parents);
     }
 
     /** @returns {string[]} */
     getChildNoteIds() {
-        return this.treeCache.children[this.noteId] || [];
+        return this.children;
     }
 
     /** @returns {Promise<NoteShort[]>} */
     async getChildNotes() {
-        return await this.treeCache.getNotes(this.getChildNoteIds());
+        return await this.treeCache.getNotes(this.children);
     }
 
     /**
diff --git a/docs/frontend_api/services_frontend_script_api.js.html b/docs/frontend_api/services_frontend_script_api.js.html
index 9d0d2c600..8f42be765 100644
--- a/docs/frontend_api/services_frontend_script_api.js.html
+++ b/docs/frontend_api/services_frontend_script_api.js.html
@@ -240,16 +240,10 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, tabConte
     this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError);
 
     /**
-     * @param {string} noteId
+     * @param {string[]} noteIds
      * @method
      */
-    this.reloadNotesAndTheirChildren = async noteId => await treeCache.reloadNotesAndTheirChildren(noteId);
-
-    /**
-     * @param {string} noteId
-     * @method
-     */
-    this.reloadParents = async noteId => await treeCache.reloadParents(noteId);
+    this.reloadNotes = async noteIds => await treeCache.reloadNotes(noteIds);
 
     /**
      * Instance name identifies particular Trilium instance. It can be useful for scripts
diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js
index 252e3837f..a71955c24 100644
--- a/src/public/javascripts/services/frontend_script_api.js
+++ b/src/public/javascripts/services/frontend_script_api.js
@@ -212,10 +212,12 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, tabConte
     this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError);
 
     /**
-     * @param {string} noteId
+     * Update frontend tree (note) cache from the backend.
+     *
+     * @param {string[]} noteIds
      * @method
      */
-    this.reloadNotes = async noteId => await treeCache.reloadNotes(noteId);
+    this.reloadNotes = async noteIds => await treeCache.reloadNotes(noteIds);
 
     /**
      * Instance name identifies particular Trilium instance. It can be useful for scripts
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index c6ce1ec67..202d248f6 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -832,8 +832,6 @@ async function reloadNotes(noteIds) {
         return;
     }
 
-    console.debug("Reloading notes", noteIds);
-
     await treeCache.reloadNotes(noteIds);
 
     const activeNotePath = noteDetailService.getActiveTabNotePath();