Skip to content

Build your personal knowledge base with Trilium Notes

exposing tree cache methods to reload note's parents/children to frontend API

zadam2b4413a1bd8a
diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js
index 349a90f2a6..1b315095a8 100644
--- a/src/public/javascripts/services/frontend_script_api.js
+++ b/src/public/javascripts/services/frontend_script_api.js
@@ -151,6 +151,18 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) {
*/
this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError);
+ /**
+ * @param {string} noteId
+ * @method
+ */
+ this.reloadChildren = async noteId => await treeCache.reloadChildren(noteId);
+
+ /**
+ * @param {string} noteId
+ * @method
+ */
+ this.reloadParents = async noteId => await treeCache.reloadParents(noteId);
+
/**
* Instance name identifies particular Trilium instance. It can be useful for scripts
* if some action needs to happen on only one specific instance.
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index 445a14c9c3..625aa8df77 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -375,8 +375,6 @@ async function treeInitialized() {
// this is weird but it looks like even though init event has been called, but we the tree still
// can't find nodes for given path which causes double loading of data. Little timeout fixes this.
setTimeout(async () => {
- console.log("activating ", startNotePath);
-
const node = await activateNote(startNotePath);
// looks like this this doesn't work when triggered immediatelly after activating node
@@ -761,7 +759,7 @@ async function checkFolderStatus(node) {
}
async function reloadNote(noteId) {
- await treeCache.reload(noteId);
+ await treeCache.reloadChildren(noteId);
for (const node of getNodesByNoteId(noteId)) {
await node.load(true);
diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js
index 01575da348..bb24aa3932 100644
--- a/src/public/javascripts/services/tree_cache.js
+++ b/src/public/javascripts/services/tree_cache.js
@@ -50,7 +50,10 @@ class TreeCache {
}
}
- async reload(noteId) {
+ /**
+ * Reload children of given noteId.
+ */
+ async reloadChildren(noteId) {
const resp = await server.post('tree/load', { noteIds: [noteId] });
for (const childNoteId of this.children[noteId] || []) {
@@ -69,6 +72,22 @@ class TreeCache {
this.addResp(resp.notes, resp.branches, resp.relations);
}
+ /**
+ * Reloads parents of given noteId - useful when new note is created to make sure note is loaded
+ * in a correct order.
+ */
+ async reloadParents(noteId) {
+ // to be able to find parents we need first to make sure it is actually loaded
+ await this.getNote(noteId);
+
+ for (const parentNoteId of this.parents[noteId] || []) {
+ await this.reloadChildren(parentNoteId);
+ }
+
+ // this is done to load the new parents for the noteId
+ await this.reloadChildren(noteId);
+ }
+
/** @return {Promise<NoteShort[]>} */
async getNotes(noteIds, silentNotFoundError = false) {
const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);