From 2b4413a1bd8a74f4a0b98450371342c0f15396ad Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 13 Apr 2019 22:56:45 +0200 Subject: [PATCH] exposing tree cache methods to reload note's parents/children to frontend API --- .../services/frontend_script_api.js | 12 +++++++++++ src/public/javascripts/services/tree.js | 4 +--- src/public/javascripts/services/tree_cache.js | 21 ++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js index 349a90f2a..1b315095a 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 445a14c9c..625aa8df7 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 01575da34..bb24aa393 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} */ async getNotes(noteIds, silentNotFoundError = false) { const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);