From 119d7367da5f5ec91cc360c238931f739dc0c16f Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 19 May 2019 18:21:29 +0200 Subject: [PATCH] tree fixes --- libraries/codemirror/addon/lint/eslint.js | 2 +- src/public/javascripts/desktop.js | 4 ++- src/public/javascripts/services/tree.js | 37 +++++++++++++++-------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/libraries/codemirror/addon/lint/eslint.js b/libraries/codemirror/addon/lint/eslint.js index 7fdb34553..ce04f0bc4 100644 --- a/libraries/codemirror/addon/lint/eslint.js +++ b/libraries/codemirror/addon/lint/eslint.js @@ -28,7 +28,7 @@ } async function validatorJavaScript(text, options) { - if (glob.getActiveNote().mime === 'application/json') { + if (glob.getActiveNote() == null || glob.getActiveNote().mime === 'application/json') { // eslint doesn't seem to validate pure JSON well return []; } diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js index f1ff1f57b..bfc694839 100644 --- a/src/public/javascripts/desktop.js +++ b/src/public/javascripts/desktop.js @@ -118,7 +118,9 @@ $("body").on("click", "a.external", function () { if (utils.isElectron()) { require('electron').ipcRenderer.on('create-day-sub-note', async function(event) { const todayNote = await dateNoteService.getTodayNote(); - const node = await treeService.expandToNote(todayNote.noteId); + const notePath = await treeService.getSomeNotePath(todayNote); + + const node = await treeService.expandToNote(notePath); await noteDetailService.openEmptyTab(false); diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index 5b0ae6997..dff16a303 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -80,6 +80,18 @@ async function expandToNote(notePath, expandOpts) { return await getNodeFromPath(notePath, true, expandOpts); } +function findChildNode(parentNode, childNoteId) { + let foundChildNode = null; + + for (const childNode of parentNode.getChildren()) { + if (childNode.data.noteId === childNoteId) { + foundChildNode = childNode; + break; + } + } + return foundChildNode; +} + async function getNodeFromPath(notePath, expand = false, expandOpts = {}) { utils.assertArguments(notePath); @@ -104,20 +116,19 @@ async function getNodeFromPath(notePath, expand = false, expandOpts = {}) { parentNode.setExpanded(true, expandOpts); } - let foundChildNode = null; + let foundChildNode = findChildNode(parentNode, childNoteId); - for (const childNode of parentNode.getChildren()) { - if (childNode.data.noteId === childNoteId) { - foundChildNode = childNode; - break; + if (!foundChildNode) { // note might be recently created so we'll force reload and try again + await parentNode.load(true); + + foundChildNode = findChildNode(parentNode, childNoteId); + + if (!foundChildNode) { + messagingService.logError(`Can't find node for child node of noteId=${childNoteId} for parent of noteId=${parentNode.data.noteId} and hoistedNoteId=${hoistedNoteId}, requested path is ${notePath}`); + return; } } - if (!foundChildNode) { - console.error(`Can't find node for child node of noteId=${childNoteId} for parent of noteId=${parentNode.data.noteId} and hoistedNoteId=${hoistedNoteId}`); - return; - } - parentNode = foundChildNode; } } @@ -317,7 +328,8 @@ async function getSomeNotePath(note) { const parents = await cur.getParentNotes(); if (!parents.length) { - infoService.throwError("Can't find parents for " + cur); + infoService.throwError("Can't find parents for ", cur); + return; } cur = parents[0]; @@ -900,5 +912,6 @@ export default { loadTreeCache, expandToNote, getNodeFromPath, - resolveNotePath + resolveNotePath, + getSomeNotePath }; \ No newline at end of file