Skip to content

Instantly share code, notes, and snippets.

@johnHackworth
Created September 9, 2022 07:00
Show Gist options
  • Select an option

  • Save johnHackworth/fe0ec2a5a66eed50233e5125221d2ee0 to your computer and use it in GitHub Desktop.

Select an option

Save johnHackworth/fe0ec2a5a66eed50233e5125221d2ee0 to your computer and use it in GitHub Desktop.
// Adds a hotkey and returns a function to remove that specific hotkey
private addHotkey = (hotkey: HotkeyType): (() => void) => {
const { key } = hotkey;
// if this is not a iOS system, we replace any hotkey that has been defined to be used with `meta`
// with a `ctrl` version of itself.
if (
hotkey.modifiers &&
hotkey.modifiers[HotkeyModifiers.Meta] &&
!isOsMac(this.props.browserInfo)
) {
hotkey.modifiers[HotkeyModifiers.Meta] = false;
hotkey.modifiers[HotkeyModifiers.Ctrl] = true;
}
const id = this.idGen.next();
if (this.hotkeys[key]) {
// If hotkey already exists, add it to the array of hotkey functions
this.hotkeys[key]?.push({ ...hotkey, id });
} else {
// If hotkey didn't exist, add it to the list
this.hotkeys[key] = [{ ...hotkey, id }];
}
return () => this.removeHotkey(key, id);
};
private removeHotkey = (key: Hotkeys, idToRemove: string): void => {
if (this.hotkeys[key]) {
// Remove hotkey of specific index
this.hotkeys[key] = this.hotkeys[key]?.filter(({ id }) => idToRemove !== id);
if (this.hotkeys[key]?.length === 0) {
// If array is empty make value null
this.hotkeys[key] = undefined;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment