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

Keyboard shortcut proof of concept #217

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

robskidmore
Copy link

Here's a quick proof of concept for customizable keyboard shortcuts.

If this approach looks good, I can clean up the types and finish the rest of the handlers.

if (e.key === "Tab" && !e.shiftKey) {
e.preventDefault();
focusNextElement(e.currentTarget);
return;
}
// Focus Previous Element
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left these comments here as placeholders for the handler names. Let me know if I need to change any of these. This would turn into handleFocusPreviousElement.

Comment on lines +12 to +13
shortcut: (e:any) => e.key === "Backspace",
function: (tree:any, e:any) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each handler consists of a shortcut and a function.

return;
}

Object.values(shortcutHandlers).find(handler => handler.shortcut(e))?.function(tree, e);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we run the function of the handler with a shortcut that returns true based on the pressed keys.

Comment on lines +30 to +44
const customShortcutHandlers = {
...shortcutHandlers,
// You can override shortcuts like this
handleSelectDownTree: {
shortcut: (e:any) => e.key === "ArrowUp" || e.key === "k",
function: shortcutHandlers.handleSelectDownTree.function
},
// You can even add custom shortcuts
customHandler: {
shortcut: (e:any) => e.key === "@",
function: (tree:any, e:any) => {
alert("@");
}
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how you override or even add custom shortcuts.

@jameskerr
Copy link
Member

Thank you for your work @robskidmore ! The concepts are there, but I'd like an api that is a bit more terse.

@robskidmore
Copy link
Author

Awesome! Could you elaborate on what you mean by "an API that is a bit more terse"?

For my use case, I needed a lot of flexibility to add shortcuts beyond those already provided with full access to the tree. This seemed like the most flexible way to do that without

Were you looking more for something that just allowed customization of the current shortcuts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants