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

This commit is contained in:
zadam 2019-04-13 22:56:45 +02:00
parent dae674a7cd
commit 2b4413a1bd
3 changed files with 33 additions and 4 deletions

View file

@ -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.

View file

@ -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);

View file

@ -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);