2017-09-17 14:04:56 +00:00
|
|
|
// @flow
|
2017-09-25 21:32:58 +00:00
|
|
|
import * as React from 'react';
|
2017-09-17 14:04:56 +00:00
|
|
|
import classnames from 'classnames';
|
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
|
|
|
|
|
|
|
type Props = {
|
2019-02-08 18:20:24 +00:00
|
|
|
// One of these two must be given.
|
|
|
|
// If both is given, keyCombination will be used.
|
|
|
|
keyCombination?: KeyCombination,
|
|
|
|
keyBindings?: KeyBindings,
|
2017-09-17 14:04:56 +00:00
|
|
|
|
|
|
|
// Optional
|
2018-12-12 17:36:11 +00:00
|
|
|
className?: string,
|
2019-02-08 18:20:24 +00:00
|
|
|
// Show fallback message if keyCombination is not given,
|
|
|
|
// but keyBindings has no key combinations.
|
|
|
|
useFallbackMessage?: boolean,
|
2017-09-17 14:04:56 +00:00
|
|
|
};
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
class Hotkey extends React.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let keyComb = null;
|
|
|
|
if (keyCombination != null) {
|
|
|
|
keyComb = keyCombination;
|
|
|
|
} else if (keyBindings != null) {
|
|
|
|
const keyCombs = getPlatformKeyCombinations(keyBindings);
|
|
|
|
// Only take the first key combination if there is a mapping.
|
|
|
|
if (keyCombs.length > 0) {
|
|
|
|
keyComb = keyCombs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let display = '';
|
|
|
|
if (keyComb != null) {
|
|
|
|
display = constructKeyCombinationDisplay(keyComb, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isFallback = display.length === 0 && useFallbackMessage;
|
|
|
|
if (isFallback) {
|
|
|
|
display = 'Not defined';
|
|
|
|
}
|
|
|
|
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;
|