diff --git a/node.go b/node.go index c33a513..9ddebae 100644 --- a/node.go +++ b/node.go @@ -325,6 +325,7 @@ func generateOneTestNode(parentUUID, parentPath string) (hash, name string, err row = db.QueryRow(query, sqlParentUUID, parentPath) err = row.Scan(&hash, &name) if err != nil { + Log.Error("generate", "error", err) return } diff --git a/static/js/notes2.mjs b/static/js/notes2.mjs index 66a328b..6f963ec 100644 --- a/static/js/notes2.mjs +++ b/static/js/notes2.mjs @@ -175,19 +175,34 @@ class Tree extends Component { this.getNodeExpanded(UUID) this.expandedNodes[UUID].value = value }//}}} - getParentNodeWithNextSibling(node) {//{{{ + getParentWithNextSibling(node) {//{{{ let currNode = node while (currNode !== null && currNode.UUID !== ROOT_NODE && currNode.getSiblingAfter() === null) { currNode = currNode.getParent() } return currNode?.getSiblingAfter() }//}}} + getLastExpandedNode(node) {//{{{ + let currNode = node + while (this.getNodeExpanded(currNode.UUID) && currNode.hasChildren()) { + currNode = currNode.Children[currNode.Children.length - 1] + } + return currNode + }//}}} async keyHandler(event) {//{{{ let handled = true const n = this.selectedNode + const Space = ' ' switch (event.key) { + // Space is toggling expansion. + case Space: + case 'Enter': + const expanded = this.getNodeExpanded(n.UUID) + this.setNodeExpanded(n.UUID, !expanded) + break + case 'j': case 'ArrowDown': await this.navigateDown(this.selectedNode) @@ -209,6 +224,7 @@ class Tree extends Component { break default: + // nonsole.log(event.key) handled = false } @@ -229,6 +245,14 @@ class Tree extends Component { return } + const siblingBefore = n.getSiblingBefore() + const siblingExpanded = this.getNodeExpanded(siblingBefore?.UUID) + if (siblingBefore !== null && siblingExpanded && siblingBefore.hasChildren()) { + const siblingAbove = this.getLastExpandedNode(siblingBefore) + await _notes2.current.goToNode(siblingAbove?.UUID, true, true) + return + } + await _notes2.current.goToNode(n.getSiblingBefore()?.UUID, true, true) }//}}} async navigateRight(n) {//{{{ @@ -246,7 +270,7 @@ class Tree extends Component { } if (n.isLastSibling()) { - const nextNode = this.getParentNodeWithNextSibling(n) + const nextNode = this.getParentWithNextSibling(n) await _notes2.current.goToNode(nextNode?.UUID, true, true) return } @@ -284,7 +308,7 @@ class Tree extends Component { // 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.getParentNodeWithNextSibling(n) + const wantedNode = this.getParentWithNextSibling(n) if (wantedNode?.UUID === ROOT_NODE) return await _notes2.current.goToNode(wantedNode?.UUID, true, true) @@ -292,13 +316,15 @@ class Tree extends Component { } if (nodeExpanded && n.isLastSibling() && !n.hasChildren()) { - const wantedNode = this.getParentNodeWithNextSibling(n) + const wantedNode = this.getParentWithNextSibling(n) 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 }