Skip to content

Build your personal knowledge base with Trilium Notes

fixes in working with top level nodes and root node

azivner8bc12a2654b8
diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js
index 0746cac43d..bb0e328052 100644
--- a/public/javascripts/note_tree.js
+++ b/public/javascripts/note_tree.js
@@ -222,6 +222,7 @@ const noteTree = (function() {
assertArguments(notePath);
const runPath = getRunPath(notePath);
+
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
let parentNoteId = 'root';
@@ -277,10 +278,14 @@ const noteTree = (function() {
if (parents.length > 0) {
console.log(now(), "Available parents:", parents);
- const pathToRoot = getSomeNotePath(parents[0]).split("/").reverse();
+ const someNotePath = getSomeNotePath(parents[0]);
+
+ if (someNotePath) { // in case it's root the path may be empty
+ const pathToRoot = someNotePath.split("/").reverse();
- for (const noteId of pathToRoot) {
- effectivePath.push(noteId);
+ for (const noteId of pathToRoot) {
+ effectivePath.push(noteId);
+ }
}
break;
diff --git a/public/javascripts/tree_changes.js b/public/javascripts/tree_changes.js
index fcb387e0b2..d6c11b93e8 100644
--- a/public/javascripts/tree_changes.js
+++ b/public/javascripts/tree_changes.js
@@ -58,7 +58,7 @@ const treeChanges = (function() {
await server.remove('notes/' + node.data.note_tree_id);
- if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
+ if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
@@ -69,27 +69,30 @@ const treeChanges = (function() {
next = node.getPrevSibling();
}
- if (!next) {
+ if (!next && !isTopLevelNode(node)) {
next = node.getParent();
}
node.remove();
- // activate next element after this one is deleted so we don't lose focus
- next.setActive();
+ if (next) {
+ // activate next element after this one is deleted so we don't lose focus
+ next.setActive();
+
+ noteTree.setCurrentNotePathToHash(next);
+ }
- noteTree.setCurrentNotePathToHash(next);
noteTree.reload();
}
async function moveNodeUpInHierarchy(node) {
- if (node.getParent() === null) {
+ if (isTopLevelNode(node)) {
return;
}
await server.put('notes/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id);
- if (node.getParent() !== null && node.getParent().getChildren().length <= 1) {
+ if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
@@ -98,11 +101,13 @@ const treeChanges = (function() {
}
function changeNode(node, func) {
+ assertArguments(node.data.parent_note_id, node.data.note_id);
+
noteTree.removeParentChildRelation(node.data.parent_note_id, node.data.note_id);
func(node);
- node.data.parent_note_id = node.getParent() === null ? 'root' : node.getParent().data.note_id;
+ node.data.parent_note_id = isTopLevelNode(node) ? 'root' : node.getParent().data.note_id;
noteTree.setParentChildRelation(node.data.note_tree_id, node.data.parent_note_id, node.data.note_id);
diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js
index c144065e2e..673eaf9fc3 100644
--- a/public/javascripts/tree_utils.js
+++ b/public/javascripts/tree_utils.js
@@ -4,7 +4,7 @@ const treeUtils = (function() {
const treeEl = $("#tree");
function getParentProtectedStatus(node) {
- return node.getParent() === null ? 0 : node.getParent().data.is_protected;
+ return isTopLevelNode(node) ? 0 : node.getParent().data.is_protected;
}
function getNodeByKey(key) {
@@ -20,7 +20,7 @@ const treeUtils = (function() {
function getNotePath(node) {
const path = [];
- while (node) {
+ while (node && !isRootNode(node)) {
if (node.data.note_id) {
path.push(node.data.note_id);
}
diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js
index 384c5999f6..1f67844adc 100644
--- a/public/javascripts/utils.js
+++ b/public/javascripts/utils.js
@@ -75,7 +75,9 @@ function isElectron() {
function assertArguments() {
for (const i in arguments) {
- assert(arguments[i], `argument ${i} should not be falsy. Argument list: ${arguments}`);
+ if (!arguments[i]) {
+ throwError(`Argument idx#${i} should not be falsy: ${arguments[i]}`);
+ }
}
}
@@ -83,4 +85,12 @@ function assert(expr, message) {
if (!expr) {
throwError(message);
}
+}
+
+function isTopLevelNode(node) {
+ return isRootNode(node.getParent());
+}
+
+function isRootNode(node) {
+ return node.key === "root_1";
}
\ No newline at end of file