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

Introduce Feature for Keyboard Shortcuts #7442

Merged
merged 6 commits into from
Apr 3, 2023
Merged

Conversation

jansav
Copy link
Contributor

@jansav jansav commented Mar 31, 2023

Motivation:

  1. Make creation of keyboard shortcuts easy
  2. Remove duplication from current keyboard shortcuts
  3. Extract a responsibility to separate, highly extendable Feature
  4. Allow future development e.g. for configurable shortcuts in the UI

New keyboard shortcuts can be introduced by any Feature simply by registering implementation for keyboardShortcutInjectionToken

Example:

import { getInjectable } from "@ogre-tools/injectable";
import { keyboardShortcutInjectionToken } from "./keyboard-shortcut-injection-token";

export const someKeyboardShortcutInjectable = getInjectable({
  id: "some-keyboard-shortcut",

  instantiate: () => ({
    binding: { ctrl: true, code: "KeyK" },

    invoke: () => {
      console.log("You pressed Ctrl + K");
    },
  }),

  injectionToken: keyboardShortcutInjectionToken,
});

Given injectable being registered, anywhere in the application where you press Ctrl + K, you will see the console.log.

The Feature also introduces something called KeyboardShortcutScope which is a React Component, which you can use to wrap your component to make keyboard shortcuts only work in that scope.

Example:

import { KeyboardShortcutScope } from "./keyboard-shortcut-injection-token";

const SomeComponent = () => (
  <KeyboardShortcutScope id="some-component">
    <div>Some stuff in the component</div>
  </KeyboardShortcutScope>
);
import { getInjectable } from "@ogre-tools/injectable";
import { keyboardShortcutInjectionToken } from "./keyboard-shortcut-injection-token";

export const someKeyboardShortcutInjectable = getInjectable({
  id: "some-keyboard-shortcut",

  instantiate: () => ({
    binding: { ctrl: true, code: "KeyK" },
    scope: "some-component"

    invoke: () => {
      console.log("You pressed Ctrl + K while focusing content inside SomeComponent");
    },
  }),

  injectionToken: keyboardShortcutInjectionToken,
});

@jansav jansav added the enhancement New feature or request label Mar 31, 2023
@jansav jansav added this to the 6.5.0 milestone Mar 31, 2023
@jansav jansav requested a review from a team as a code owner March 31, 2023 08:24
@jansav jansav requested review from ixrock and jim-docker and removed request for a team March 31, 2023 08:24
Base automatically changed from feat/react-application to master March 31, 2023 08:57
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@jansav jansav force-pushed the feat/keyboard-shortcuts branch from 9276d2b to bbfb7c9 Compare March 31, 2023 09:09
@github-actions
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

packages/business-features/keyboard-shortcuts/package.json Outdated Show resolved Hide resolved
Comment on lines 29 to 32
const toBindingWithDefaults = (binding: Binding) =>
isString(binding)
? { code: binding, shift: false, ctrl: false, altOrOption: false, meta: false }
: { ctrl: false, shift: false, altOrOption: false, meta: false, ...binding };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not just have the param be SetRequired<Partial<Binding>, "code">?

Furthermore, should do some checks here concerning trying to make sure someone doesn't do Ctrl+KeyK as the code field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Furthermore, should do some checks here concerning trying to make sure someone doesn't do Ctrl+KeyK as the code field?

We don't have to worry about it. Modifiers trigger their own event in the listener, which means that technically you can't say that the code is Ctrl+KeyK because that doesn't exist. However, if you wish, you are able to add keybinding to e.g. ControlLeft. But, I don't see that as a big deal, because that would be just developer doing weird stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why not just have the param be SetRequired<Partial, "code">?

I don't understand the question here.

@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

jansav added 6 commits April 3, 2023 07:12
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
… listener to be used in the application

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
@jansav jansav force-pushed the feat/keyboard-shortcuts branch from bbfb7c9 to 926e7e2 Compare April 3, 2023 05:10
@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2023

Conflicts have been resolved. A maintainer will review the pull request shortly.

@Nokel81 Nokel81 merged commit f4cb1d3 into master Apr 3, 2023
@Nokel81 Nokel81 deleted the feat/keyboard-shortcuts branch April 3, 2023 12:23
@Nokel81 Nokel81 linked an issue Apr 3, 2023 that may be closed by this pull request
@Nokel81 Nokel81 mentioned this pull request Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keyboard shortcuts as a feature
2 participants