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

Olli KeyboardManager and Help Modal #84

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9d267f3
Add keyboard manager types
Matt-Blanco Jul 8, 2023
fc97227
Create KeyboardManager class
Matt-Blanco Jul 8, 2023
07e4e27
Use KeyboardManager instead of handleKeyEvent
Matt-Blanco Jul 8, 2023
962cb21
Merge branch 'main' into olli-keyboard-manager
Matt-Blanco Jul 8, 2023
625bbf7
Add key action to show help modal
Matt-Blanco Jul 8, 2023
f8414ef
Add key description for spacebar action
Matt-Blanco Jul 10, 2023
18e4e33
Set initial actions value in KeyboardManager
Matt-Blanco Jul 10, 2023
5b3ab6b
Rename help dialog modal
Matt-Blanco Jul 10, 2023
4a33b6d
Add element to open help under initial tree item
Matt-Blanco Jul 10, 2023
11b6e12
Remove dead code
Matt-Blanco Jul 10, 2023
ef24d25
Right align control descriptions
Matt-Blanco Jul 10, 2023
bbfcca0
Merge branch 'main' into matt-blanco/keyboard-manager
Matt-Blanco Jul 14, 2023
fee21bf
Remove help modal HTML element
Matt-Blanco Jul 14, 2023
db310c1
Move KeyboardManager types to class file
Matt-Blanco Jul 14, 2023
bace8f7
Initialize KeyboardManager in KeyboardManager.ts
Matt-Blanco Jul 15, 2023
9a0e542
Delete KeyboardManager from OlliRuntimeTreeItem.ts
Matt-Blanco Jul 15, 2023
5c6618f
Add KeyboardManager to OlliGlobalState
Matt-Blanco Jul 15, 2023
8ab9cf2
Update KeyboardAction type
Matt-Blanco Aug 2, 2023
e895554
Update checkKeys function
Matt-Blanco Aug 2, 2023
a764ef4
Add extra keys to addAction parameters
Matt-Blanco Aug 2, 2023
1cb726a
Update handleEvents function usages
Matt-Blanco Aug 2, 2023
8307ccc
Update initial actions
Matt-Blanco Aug 2, 2023
513f884
Call KeyboardManager handleEvents from tree items
Matt-Blanco Aug 2, 2023
ddc179f
Simplify checkKeys function
Matt-Blanco Aug 3, 2023
b9b1fef
Render help table in separate file
Matt-Blanco Aug 3, 2023
e8ad01e
Add openHelpDialog function
Matt-Blanco Aug 3, 2023
9ebbe6b
Move dialog rendering outside KeyboardManager
Matt-Blanco Aug 3, 2023
1fbb9f7
Merge branch 'main' into matt-blanco/keyboard-manager
Matt-Blanco Aug 3, 2023
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
10 changes: 10 additions & 0 deletions packages/core/src/Render/Dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { OlliRuntime } from '../../Runtime/OlliRuntime';
import './dialog.css';
import { makeSelectionMenu } from './selectionMenu';
import { makeTargetedNavMenu } from './targetedNavMenu';
import { renderHelpDialog } from '../Help';
import { getOlliGlobalState } from '../../util/globalState';
import { getSpecForNode } from '../../Structure';

export function makeDialog(
Expand Down Expand Up @@ -95,6 +97,14 @@ function openDialog(dialog: HTMLElement, renderContainer: HTMLElement) {
});
}

export function openHelpDialog(tree: OlliRuntime) {
const { keyboardManager } = getOlliGlobalState();
const table = renderHelpDialog(keyboardManager);
const dialog = makeDialog(tree, 'Olli Help Menu', 'Below are the controls to navigate the Olli tree.', table);

openDialog(dialog, tree.renderContainer)
}

export function openTableDialog(olliNode: ElaboratedOlliNode, tree: OlliRuntime) {
const olliSpec: UnitOlliSpec = getSpecForNode(olliNode, tree.olliSpec);
const table = renderTable(selectionTest(olliSpec.data, olliNode.fullPredicate), olliSpec.fields);
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/Render/Help/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { KeyboardManager } from "../../Runtime/KeyboardManager";

/**
* Build a help dialog
*/
export function renderHelpDialog(keyboardManager: KeyboardManager): HTMLElement {
const table = document.createElement("table");
const tbody = document.createElement("tbody");

Object.entries(keyboardManager.getActions()).forEach(([keystroke, details]) => {
const tr = document.createElement("tr");
const th = document.createElement("th");
th.style.textAlign = 'left';
th.scope = "row";
th.textContent = details.keyDescription ?? keystroke;
tr.appendChild(th);

const tdKey = document.createElement("td");
tdKey.textContent = details.description;
tr.appendChild(tdKey);

const tdTitle = document.createElement("td");
tdTitle.textContent = details.title;
tr.appendChild(tdTitle);

tbody.appendChild(tr);
});
table.appendChild(tbody);

return table;
}
248 changes: 248 additions & 0 deletions packages/core/src/Runtime/KeyboardManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import { openHelpDialog, openSelectionDialog, openTableDialog } from "../Render/Dialog";
import { OlliRuntime } from "./OlliRuntime";
import { OlliRuntimeTreeItem } from "./OlliRuntimeTreeItem";

export type KeyboardAction = {
action: (treeItem: OlliRuntimeTreeItem) => void;
title?: string;
keyDescription?: string;
description?: string;
force?: boolean;
caseSensitive?: boolean;
shiftKey?: boolean;
ctrlKey?: boolean;
altKey?: boolean;
metaKey?: boolean;
}

export type KeyRegistration = {
key: string;
} & KeyboardAction;

export const checkKeys = (e: KeyboardEvent, action: KeyboardAction) => {
let hasKeys = {
altKey: true,
shiftKey: true,
ctrlKEy: true,
metaKey: true,
}

Object.keys(hasKeys).forEach((key: string) => {
if (action[key]) {
hasKeys[key] = e[key] === action[key];
}
})

return Object.keys(hasKeys).every((key: string) => hasKeys[key]);
};

/**
* The KeyboardManager handles adding new keyboard events and controls,
* and displaying a help documentation modal
*/
export class KeyboardManager {
private actions: {
[event: string]: KeyboardAction;
};
private target: HTMLElement;
private helpModal: HTMLDialogElement | null;

constructor(target: HTMLElement) {
this.target = target;
if (!this.target.hasAttribute("tabIndex")) {
this.target.setAttribute("tabIndex", "0");
}
this.helpModal = null;
this.actions = {};
}

public handleEvents(e: KeyboardEvent, tree: OlliRuntimeTreeItem): void {
const keyPress = e.key;
let keyboardAction: KeyboardAction;

if (keyPress in this.actions) {
keyboardAction = this.actions[keyPress];
} else if (keyPress.toUpperCase() in this.actions) {
keyboardAction = this.actions[keyPress.toUpperCase()];
}

if (checkKeys(e, keyboardAction)) {
keyboardAction.action(tree);
e.stopPropagation();
e.preventDefault();
}
}

public addAction({
key,
action,
caseSensitive,
description,
force,
keyDescription,
title,
shiftKey,
ctrlKey,
altKey,
metaKey,
}: KeyRegistration): void {
const checkKey = caseSensitive ? key : key.toUpperCase();
if (!force && checkKey in this.actions) {
return;
}
this.actions[checkKey] = {
title,
description,
action,
keyDescription,
shiftKey,
ctrlKey,
altKey,
metaKey,
};
}

public addActions(keyList: KeyRegistration[]): void {
keyList.forEach((key: KeyRegistration) => {
this.addAction(key);
})
}

/**
* Return list of possible actions
*/
public getActions(): { [event: string]: KeyboardAction; } {
return this.actions;
}
}

export const initKeyboardManager = (olliInstance: OlliRuntime) => {

const kb = new KeyboardManager(olliInstance.rootDomNode);

kb.addActions([
{
key: 'h',
title: 'Display help documentation modal',
action: (treeItem) => {
openHelpDialog(treeItem.tree);
},
},
{
key: 'Enter',
title: 'Expand and collapse the current layer of the tree',
action: (treeItem) => {
if (treeItem.isExpandable) {
if (treeItem.isExpanded()) {
treeItem.tree.collapseTreeItem(treeItem);
} else {
treeItem.tree.expandTreeItem(treeItem);
}
}
},
},
{
key: ' ',
keyDescription: 'Space',
title: 'Expand and collapse the current layer of the tree',
action: (treeItem) => {
if (treeItem.tree.lastFocusedTreeItem.isExpandable) {
if (treeItem.tree.lastFocusedTreeItem.isExpanded()) {
treeItem.tree.collapseTreeItem(treeItem);
} else {
treeItem.tree.expandTreeItem(treeItem);
}
}
},
},
{
key: 'ArrowDown',
title: 'Focus on the next layer of the tree',
action: (treeItem) => {
if (treeItem.children.length > 0 && treeItem.isExpandable) {
treeItem.tree.setFocusToNextLayer(treeItem);
}
},
},
{
key: 'ArrowUp',
title: 'Focus on the previous layer of the tree',
action: (treeItem) => {
if (treeItem.inGroup) {
treeItem.tree.setFocusToParentItem(treeItem);
}
},
},
{
key: 'Escape',
title: 'Focus on the previous layer of the tree',
action: (treeItem) => {
if (treeItem.inGroup) {
treeItem.tree.setFocusToParentItem(treeItem);
}
},
},
{
key: 'ArrowLeft',
title: 'Focus on the previous child element of the tree',
action: (treeItem) => {
treeItem.tree.setFocusToPreviousItem(treeItem);
},
},
{
key: 'ArrowRight',
title: 'Focus on the next child element of the tree',
action: (treeItem) => {
treeItem.tree.setFocusToNextItem(treeItem);
},
},
{
key: 'Home',
title: 'Focus top of the tree',
action: (treeItem) => {
if (treeItem.parent) {
treeItem.tree.setFocusToFirstInLayer(treeItem);
}
},
},
{
key: 'x',
title: 'Navigate to the x-axis of the graph',
action: (treeItem) => {
treeItem.tree.focusOnNodeType('xAxis', treeItem);
},
},
{
key: 'y',
title: 'Navigate to the y-axis of the graph',
action: (treeItem) => {
treeItem.tree.focusOnNodeType('yAxis', treeItem);
},
},
{
key: 'l',
title: 'Navigate to the legend of the graph',
action: (treeItem) => {
treeItem.tree.focusOnNodeType('legend', treeItem);
},
},
{
key: 't',
title: 'Open table dialog',
action: (treeItem) => {
if ('predicate' in treeItem.olliNode || treeItem.olliNode.nodeType === 'root') {
openTableDialog(treeItem.olliNode, treeItem.tree);
}
},
},
{
key: 'f',
title: 'Open selection dialog',
action: (treeItem) => {
openSelectionDialog(treeItem.tree);
},
},
])

return kb;
}
Loading