Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the up and down keys scrolls the page #843

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/_entries/general/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title: Changelog
name: changelog
---

### Development version

- Issue #840: using the up and down keys scrolls the page

### 1.8.5 (september 28 2024)

- Update packages
Expand Down
95 changes: 51 additions & 44 deletions src/keyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,36 @@
private closeNode: CloseNode;
private getSelectedNode: GetSelectedNode;

private handleKeyDown = (e: KeyboardEvent): boolean => {
private handleKeyDown = (e: KeyboardEvent): void => {
if (!this.canHandleKeyboard()) {
return true;
return;
}

let isKeyHandled = false;

const selectedNode = this.getSelectedNode();
if (!selectedNode) {
return true;
if (selectedNode) {
switch (e.key) {
case "ArrowDown":
isKeyHandled = this.moveDown(selectedNode);
break;

case "ArrowUp":
isKeyHandled = this.moveUp(selectedNode);
break;

case "ArrowRight":
isKeyHandled = this.moveRight(selectedNode);
break;

case "ArrowLeft":
isKeyHandled = this.moveLeft(selectedNode);
break;
}
}

switch (e.key) {
case "ArrowDown":
return this.moveDown(selectedNode);

case "ArrowUp":
return this.moveUp(selectedNode);

case "ArrowRight":
return this.moveRight(selectedNode);

case "ArrowLeft":
return this.moveLeft(selectedNode);

default:
return true;
if (isKeyHandled) {
e.preventDefault();
}
};

Expand All @@ -72,40 +77,28 @@
this.originalSelectNode = selectNode;

if (keyboardSupport) {
this.handleKeyDownHandler = this.handleKeyDown.bind(this);

document.addEventListener("keydown", this.handleKeyDownHandler);
document.addEventListener("keydown", this.handleKeyDown);
}
}

private canHandleKeyboard(): boolean {
return this.keyboardSupport && this.isFocusOnTree();
}

public deinit(): void {
if (this.handleKeyDownHandler) {
document.removeEventListener("keydown", this.handleKeyDownHandler);
}
}

public moveDown(selectedNode: Node): boolean {
return this.selectNode(selectedNode.getNextVisibleNode());
}

public moveLeft(selectedNode: Node): boolean {
private moveLeft(selectedNode: Node): boolean {
if (selectedNode.isFolder() && selectedNode.is_open) {
// Left on an open node closes the node
this.closeNode(selectedNode);
return false;
return true;
} else {
// Left on a closed or end node moves focus to the node's parent
return this.selectNode(selectedNode.getParent());
}
}

public moveRight(selectedNode: Node): boolean {
private moveRight(selectedNode: Node): boolean {
if (!selectedNode.isFolder()) {
return true;
return false;
} else {
// folder node
if (selectedNode.is_open) {
Expand All @@ -114,22 +107,36 @@
} else {
// Right expands a closed node
this.openNode(selectedNode);
return false;
return true;
}
}
}

public moveUp(selectedNode: Node): boolean {
return this.selectNode(selectedNode.getPreviousVisibleNode());
}

public selectNode(node: Node | null): boolean {
/* Select the node.
* Don't do anything if the node is null.
* Result: a different node was selected.
*/
private selectNode(node: Node | null): boolean {
if (!node) {
return true;
return false;
} else {
this.originalSelectNode(node);

return false;
return true;
}
}

public deinit(): void {
if (this.handleKeyDownHandler) {
document.removeEventListener("keydown", this.handleKeyDownHandler);

Check warning on line 131 in src/keyHandler.ts

View check run for this annotation

Codecov / codecov/patch

src/keyHandler.ts#L131

Added line #L131 was not covered by tests
}
}

public moveDown(selectedNode: Node): boolean {
return this.selectNode(selectedNode.getNextVisibleNode());
}

public moveUp(selectedNode: Node): boolean {
return this.selectNode(selectedNode.getPreviousVisibleNode());
}
}
Loading