Skip to content

Commit 97fb9a5

Browse files
authored
Using the up and down keys scrolls the page (#843)
* Make methods in KeyHandler private * Using the up and down keys scrolls the page * Build * Changelog
1 parent e90c995 commit 97fb9a5

File tree

6 files changed

+2565
-2545
lines changed

6 files changed

+2565
-2545
lines changed

docs/_entries/general/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: Changelog
33
name: changelog
44
---
55

6+
### Development version
7+
8+
- Issue #840: using the up and down keys scrolls the page
9+
610
### 1.8.5 (september 28 2024)
711

812
- Update packages

src/keyHandler.ts

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,36 @@ export default class KeyHandler {
2222
private closeNode: CloseNode;
2323
private getSelectedNode: GetSelectedNode;
2424

25-
private handleKeyDown = (e: KeyboardEvent): boolean => {
25+
private handleKeyDown = (e: KeyboardEvent): void => {
2626
if (!this.canHandleKeyboard()) {
27-
return true;
27+
return;
2828
}
2929

30+
let isKeyHandled = false;
31+
3032
const selectedNode = this.getSelectedNode();
31-
if (!selectedNode) {
32-
return true;
33+
if (selectedNode) {
34+
switch (e.key) {
35+
case "ArrowDown":
36+
isKeyHandled = this.moveDown(selectedNode);
37+
break;
38+
39+
case "ArrowUp":
40+
isKeyHandled = this.moveUp(selectedNode);
41+
break;
42+
43+
case "ArrowRight":
44+
isKeyHandled = this.moveRight(selectedNode);
45+
break;
46+
47+
case "ArrowLeft":
48+
isKeyHandled = this.moveLeft(selectedNode);
49+
break;
50+
}
3351
}
3452

35-
switch (e.key) {
36-
case "ArrowDown":
37-
return this.moveDown(selectedNode);
38-
39-
case "ArrowUp":
40-
return this.moveUp(selectedNode);
41-
42-
case "ArrowRight":
43-
return this.moveRight(selectedNode);
44-
45-
case "ArrowLeft":
46-
return this.moveLeft(selectedNode);
47-
48-
default:
49-
return true;
53+
if (isKeyHandled) {
54+
e.preventDefault();
5055
}
5156
};
5257

@@ -72,40 +77,28 @@ export default class KeyHandler {
7277
this.originalSelectNode = selectNode;
7378

7479
if (keyboardSupport) {
75-
this.handleKeyDownHandler = this.handleKeyDown.bind(this);
76-
77-
document.addEventListener("keydown", this.handleKeyDownHandler);
80+
document.addEventListener("keydown", this.handleKeyDown);
7881
}
7982
}
8083

8184
private canHandleKeyboard(): boolean {
8285
return this.keyboardSupport && this.isFocusOnTree();
8386
}
8487

85-
public deinit(): void {
86-
if (this.handleKeyDownHandler) {
87-
document.removeEventListener("keydown", this.handleKeyDownHandler);
88-
}
89-
}
90-
91-
public moveDown(selectedNode: Node): boolean {
92-
return this.selectNode(selectedNode.getNextVisibleNode());
93-
}
94-
95-
public moveLeft(selectedNode: Node): boolean {
88+
private moveLeft(selectedNode: Node): boolean {
9689
if (selectedNode.isFolder() && selectedNode.is_open) {
9790
// Left on an open node closes the node
9891
this.closeNode(selectedNode);
99-
return false;
92+
return true;
10093
} else {
10194
// Left on a closed or end node moves focus to the node's parent
10295
return this.selectNode(selectedNode.getParent());
10396
}
10497
}
10598

106-
public moveRight(selectedNode: Node): boolean {
99+
private moveRight(selectedNode: Node): boolean {
107100
if (!selectedNode.isFolder()) {
108-
return true;
101+
return false;
109102
} else {
110103
// folder node
111104
if (selectedNode.is_open) {
@@ -114,22 +107,36 @@ export default class KeyHandler {
114107
} else {
115108
// Right expands a closed node
116109
this.openNode(selectedNode);
117-
return false;
110+
return true;
118111
}
119112
}
120113
}
121114

122-
public moveUp(selectedNode: Node): boolean {
123-
return this.selectNode(selectedNode.getPreviousVisibleNode());
124-
}
125-
126-
public selectNode(node: Node | null): boolean {
115+
/* Select the node.
116+
* Don't do anything if the node is null.
117+
* Result: a different node was selected.
118+
*/
119+
private selectNode(node: Node | null): boolean {
127120
if (!node) {
128-
return true;
121+
return false;
129122
} else {
130123
this.originalSelectNode(node);
131124

132-
return false;
125+
return true;
133126
}
134127
}
128+
129+
public deinit(): void {
130+
if (this.handleKeyDownHandler) {
131+
document.removeEventListener("keydown", this.handleKeyDownHandler);
132+
}
133+
}
134+
135+
public moveDown(selectedNode: Node): boolean {
136+
return this.selectNode(selectedNode.getNextVisibleNode());
137+
}
138+
139+
public moveUp(selectedNode: Node): boolean {
140+
return this.selectNode(selectedNode.getPreviousVisibleNode());
141+
}
135142
}

0 commit comments

Comments
 (0)