2018-01-13 04:01:27 +02:00
|
|
|
import Mousetrap from 'mousetrap';
|
2022-05-20 06:15:43 +02:00
|
|
|
import React, { Component } from 'react';
|
2018-01-13 04:01:27 +02:00
|
|
|
import getDisplayName from 'Helpers/getDisplayName';
|
|
|
|
|
|
|
|
export const shortcuts = {
|
|
|
|
OPEN_KEYBOARD_SHORTCUTS_MODAL: {
|
|
|
|
key: '?',
|
|
|
|
name: 'Open This Modal'
|
|
|
|
},
|
|
|
|
|
2020-03-02 07:03:38 +02:00
|
|
|
CLOSE_MODAL: {
|
|
|
|
key: 'Esc',
|
|
|
|
name: 'Close Current Modal'
|
|
|
|
},
|
|
|
|
|
|
|
|
ACCEPT_CONFIRM_MODAL: {
|
|
|
|
key: 'Enter',
|
|
|
|
name: 'Accept Confirmation Modal'
|
|
|
|
},
|
|
|
|
|
2018-01-13 04:01:27 +02:00
|
|
|
SERIES_SEARCH_INPUT: {
|
|
|
|
key: 's',
|
|
|
|
name: 'Focus Search Box'
|
|
|
|
},
|
|
|
|
|
|
|
|
SAVE_SETTINGS: {
|
|
|
|
key: 'mod+s',
|
|
|
|
name: 'Save Settings'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function keyboardShortcuts(WrappedComponent) {
|
|
|
|
class KeyboardShortcuts extends Component {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lifecycle
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this._mousetrapBindings = {};
|
|
|
|
this._mousetrap = new Mousetrap();
|
|
|
|
this._mousetrap.stopCallback = this.stopCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.unbindAllShortcuts();
|
|
|
|
this._mousetrap = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Control
|
|
|
|
|
|
|
|
bindShortcut = (key, callback, options = {}) => {
|
|
|
|
this._mousetrap.bind(key, callback);
|
|
|
|
this._mousetrapBindings[key] = options;
|
2022-05-20 06:15:43 +02:00
|
|
|
};
|
2018-01-13 04:01:27 +02:00
|
|
|
|
|
|
|
unbindShortcut = (key) => {
|
2020-09-23 00:12:42 +02:00
|
|
|
if (this._mousetrap != null) {
|
|
|
|
delete this._mousetrapBindings[key];
|
|
|
|
this._mousetrap.unbind(key);
|
|
|
|
}
|
2022-05-20 06:15:43 +02:00
|
|
|
};
|
2018-01-13 04:01:27 +02:00
|
|
|
|
|
|
|
unbindAllShortcuts = () => {
|
|
|
|
const keys = Object.keys(this._mousetrapBindings);
|
|
|
|
|
|
|
|
if (!keys.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.forEach((binding) => {
|
|
|
|
this._mousetrap.unbind(binding);
|
|
|
|
});
|
|
|
|
|
|
|
|
this._mousetrapBindings = {};
|
2022-05-20 06:15:43 +02:00
|
|
|
};
|
2018-01-13 04:01:27 +02:00
|
|
|
|
|
|
|
stopCallback = (event, element, combo) => {
|
|
|
|
const binding = this._mousetrapBindings[combo];
|
|
|
|
|
|
|
|
if (!binding || binding.isGlobal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
element.tagName === 'INPUT' ||
|
|
|
|
element.tagName === 'SELECT' ||
|
|
|
|
element.tagName === 'TEXTAREA' ||
|
|
|
|
(element.contentEditable && element.contentEditable === 'true')
|
|
|
|
);
|
2022-05-20 06:15:43 +02:00
|
|
|
};
|
2018-01-13 04:01:27 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Render
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<WrappedComponent
|
|
|
|
{...this.props}
|
|
|
|
bindShortcut={this.bindShortcut}
|
|
|
|
unbindShortcut={this.unbindShortcut}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardShortcuts.displayName = `KeyboardShortcut(${getDisplayName(WrappedComponent)})`;
|
|
|
|
KeyboardShortcuts.WrappedComponent = WrappedComponent;
|
|
|
|
|
|
|
|
return KeyboardShortcuts;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default keyboardShortcuts;
|