2017-09-17 14:04:56 +00:00
|
|
|
import classnames from 'classnames';
|
2021-07-22 23:04:56 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
import { isMac } from '../../common/constants';
|
|
|
|
import type { KeyBindings, KeyCombination } from '../../common/hotkeys';
|
|
|
|
import { constructKeyCombinationDisplay, getPlatformKeyCombinations } from '../../common/hotkeys';
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
|
|
|
/** One of these two must be given. If both is given, keyCombination will be used. */
|
|
|
|
keyCombination?: KeyCombination;
|
|
|
|
keyBindings?: KeyBindings;
|
|
|
|
className?: string;
|
|
|
|
/** Show fallback message if keyCombination is not given, but keyBindings has no key combinations. */
|
|
|
|
useFallbackMessage?: boolean;
|
|
|
|
}
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
class Hotkey extends PureComponent<Props> {
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2019-02-08 18:20:24 +00:00
|
|
|
const { keyCombination, keyBindings, className, useFallbackMessage } = this.props;
|
|
|
|
|
|
|
|
if (keyCombination == null && keyBindings == null) {
|
2020-04-09 17:32:19 +00:00
|
|
|
console.error('Hotkey needs one of keyCombination or keyBindings!');
|
2019-02-08 18:20:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
let keyComb: KeyCombination | null = null;
|
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
if (keyCombination != null) {
|
|
|
|
keyComb = keyCombination;
|
|
|
|
} else if (keyBindings != null) {
|
|
|
|
const keyCombs = getPlatformKeyCombinations(keyBindings);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
// Only take the first key combination if there is a mapping.
|
|
|
|
if (keyCombs.length > 0) {
|
|
|
|
keyComb = keyCombs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let display = '';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
if (keyComb != null) {
|
|
|
|
display = constructKeyCombinationDisplay(keyComb, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isFallback = display.length === 0 && useFallbackMessage;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
if (isFallback) {
|
|
|
|
display = 'Not defined';
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
const classes = {
|
|
|
|
'font-normal': isMac(),
|
|
|
|
italic: isFallback,
|
|
|
|
};
|
|
|
|
return <span className={classnames(className, classes)}>{display}</span>;
|
2017-03-28 22:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Hotkey;
|