From 61b5049186afabcbea08bf972c0d6ec3666400c4 Mon Sep 17 00:00:00 2001 From: Dawid Gliwka Date: Tue, 10 Nov 2020 17:21:10 +0100 Subject: [PATCH] Add keyboard navigation history similar to ranger --- src/App.tsx | 23 +++++++++++++++++------ src/core/tree.ts | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 8c39042..13a7b7b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -87,9 +87,16 @@ export default class App extends Component<{ selectNode = (node_id: NodeID) => { - this.setState({ - chosenNode: node_id, - }); + const root = this.state.ourTree.root + const node = this.state.ourTree._selectNodeById(node_id) + if (node !== root) + { + const parent = root.getParent(node, root) + parent.lastSelectedChild = node_id + this.setState({ + chosenNode: node_id, + }); + } } selectParentNode = () => @@ -99,7 +106,7 @@ export default class App extends Component<{ if (node !== root) { const parent = root.getParent(node, root) - + parent.lastSelectedChild = node.id this.setState({ chosenNode: parent.id }) @@ -114,7 +121,7 @@ export default class App extends Component<{ const child = node.children[0] this.setState({ - chosenNode: child.id + chosenNode: node.lastSelectedChild || child.id }) } } @@ -129,7 +136,9 @@ export default class App extends Component<{ const indexOfNode = parent.children.indexOf(node) const indexOfNextChild = Math.min(indexOfNode+1, parent.children.length-1) const nextChild = parent.children[indexOfNextChild] - + + parent.lastSelectedChild = nextChild.id + this.setState({ chosenNode: nextChild.id }) @@ -147,6 +156,8 @@ export default class App extends Component<{ const indexOfPrevChild = Math.max(indexOfNode-1, 0) const prevChild = parent.children[indexOfPrevChild] + parent.lastSelectedChild = prevChild.id + this.setState({ chosenNode: prevChild.id }) diff --git a/src/core/tree.ts b/src/core/tree.ts index 49282bc..6e9a565 100644 --- a/src/core/tree.ts +++ b/src/core/tree.ts @@ -196,12 +196,14 @@ export class TreeNode // TODO: how about children being Sets? children: TreeNode[]; data: T; + lastSelectedChild: NodeID | null; constructor(id: NodeID, data: T, children: TreeNode[] = []) { this.data = data; this.id = id; this.children = children; + this.lastSelectedChild = null } _append(node: TreeNode)