From e276e6d1563a720f25d094c2c01578fb55e6480f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Sun, 9 Feb 2025 11:14:26 +0100 Subject: [PATCH] Added navigation to top/bottom, normalized toplevel node parent to root node --- static/js/node.mjs | 8 ++++++- static/js/node_store.mjs | 8 +++---- static/js/notes2.mjs | 49 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/static/js/node.mjs b/static/js/node.mjs index 0dde132..4db59a0 100644 --- a/static/js/node.mjs +++ b/static/js/node.mjs @@ -336,7 +336,13 @@ export class Node { this.data = nodeData this.UUID = nodeData.UUID - this.ParentUUID = nodeData.ParentUUID + + // Toplevel nodes are normalized to have the ROOT_NODE as parent. + if (nodeData.UUID !== ROOT_NODE && nodeData.ParentUUID === '') { + this.ParentUUID = ROOT_NODE + this.data.ParentUUID = ROOT_NODE + } else + this.ParentUUID = nodeData.ParentUUID this._children_fetched = false this.Children = [] diff --git a/static/js/node_store.mjs b/static/js/node_store.mjs index 68ffce4..43f40f3 100644 --- a/static/js/node_store.mjs +++ b/static/js/node_store.mjs @@ -222,11 +222,9 @@ export class NodeStore { }//}}} async getTreeNodes(parent, newLevel) {//{{{ return new Promise((resolve, reject) => { - // Parent of toplevel nodes is '' in indexedDB, - // but can also be set to the ROOT_NODE uuid. + // Parent of toplevel nodes is ROOT_NODE in indexedDB. + // Only the root node has '' as parent. let storeParent = parent - if (parent === ROOT_NODE) - storeParent = '' const trx = this.db.transaction('nodes', 'readonly') const nodeStore = trx.objectStore('nodes') @@ -305,7 +303,7 @@ export class NodeStore { .transaction('nodes', 'readonly') .objectStore('nodes') - if (node.ParentUUID === '') { + if (node.ParentUUID === ROOT_NODE) { resolve(accumulated) return } diff --git a/static/js/notes2.mjs b/static/js/notes2.mjs index 6f963ec..3155043 100644 --- a/static/js/notes2.mjs +++ b/static/js/notes2.mjs @@ -123,7 +123,7 @@ class Tree extends Component { // The root node isn't supposed to be shown in the tree. if (node.UUID === ROOT_NODE) continue - if (node.ParentUUID === '') + if (node.ParentUUID === ROOT_NODE) this.treeTrunk.push(node) } this.forceUpdate() @@ -203,6 +203,16 @@ class Tree extends Component { this.setNodeExpanded(n.UUID, !expanded) break + case 'g': + case 'Home': + this.navigateTop() + break + + case 'G': + case 'End': + this.navigateBottom() + break + case 'j': case 'ArrowDown': await this.navigateDown(this.selectedNode) @@ -234,6 +244,9 @@ class Tree extends Component { } }//}}} async navigateLeft(n) {//{{{ + if (n === null) + return + const expanded = this.getNodeExpanded(n.UUID) if (expanded && n.hasChildren()) { this.setNodeExpanded(n.UUID, false) @@ -256,6 +269,9 @@ class Tree extends Component { await _notes2.current.goToNode(n.getSiblingBefore()?.UUID, true, true) }//}}} async navigateRight(n) {//{{{ + if (n === null) + return + const siblingAfter = n.getSiblingAfter() const expanded = this.getNodeExpanded(n.UUID) @@ -278,6 +294,9 @@ class Tree extends Component { await _notes2.current.goToNode(n.getSiblingAfter()?.UUID, true, true) }//}}} async navigateUp(n) {//{{{ + if (n === null) + return + let parent = null const siblingBefore = n.getSiblingBefore() let siblingExpanded = false @@ -303,14 +322,15 @@ class Tree extends Component { } }//}}} async navigateDown(n) {//{{{ + if (n === null) + return + const nodeExpanded = this.getNodeExpanded(n.UUID) // Last node, not expanded, so it matters not whether it has children or not. // Traverse upward to nearest parent with next sibling. if (!nodeExpanded && n.isLastSibling()) { const wantedNode = this.getParentWithNextSibling(n) - if (wantedNode?.UUID === ROOT_NODE) - return await _notes2.current.goToNode(wantedNode?.UUID, true, true) return } @@ -320,11 +340,10 @@ class Tree extends Component { await _notes2.current.goToNode(wantedNode?.UUID, true, true) return } + // Node not expanded. Go to this node's next sibling. // GoToNode will abort if given null. if (!nodeExpanded || !n.hasChildren()) { - if (n.getSiblingAfter()?.UUID === ROOT_NODE) - return await _notes2.current.goToNode(n.getSiblingAfter()?.UUID, true, true) return } @@ -336,6 +355,26 @@ class Tree extends Component { return } }//}}} + async navigateTop() {//{{{ + const root = await nodeStore.get(ROOT_NODE) + if (root.Children.length === 0) + return + await _notes2.current.goToNode(root.Children[0]?.UUID, true, true) + }//}}} + async navigateBottom() {//{{{ + const root = await nodeStore.get(ROOT_NODE) + if (root.Children.length === 0) + return + + const toplevel = root.Children[root.Children.length - 1] + const toplevelExpanded = this.getNodeExpanded(toplevel?.UUID) + + if (toplevelExpanded) { + const lastnode = this.getLastExpandedNode(toplevel) + await _notes2.current.goToNode(lastnode?.UUID, true, true) + } else + await _notes2.current.goToNode(root.Children[root.Children.length - 1]?.UUID, true, true) + }//}}} } class TreeNode extends Component {