2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
|
|
|
import { withStyles } from '@mui/styles';
|
2022-02-26 21:45:32 +00:00
|
|
|
import clsx from 'clsx';
|
2017-11-13 22:58:00 +00:00
|
|
|
import React, { Component } from 'react';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { WithTranslation } from 'react-i18next';
|
2022-09-29 10:26:34 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-11-13 22:58:00 +00:00
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
import { IStore } from '../../../app/types';
|
|
|
|
import { hideDialog } from '../../../base/dialog/actions';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { translate } from '../../../base/i18n/functions';
|
2022-09-29 10:26:34 +00:00
|
|
|
import Dialog from '../../../base/ui/components/web/Dialog';
|
2017-11-13 22:58:00 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 05:02:23 +00:00
|
|
|
* The type of the React {@code Component} props of
|
|
|
|
* {@link KeyboardShortcutsDialog}.
|
2017-11-13 22:58:00 +00:00
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
interface Props extends WithTranslation {
|
2018-10-30 05:02:23 +00:00
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
/**
|
|
|
|
* Dispatches close dialog.
|
|
|
|
*/
|
|
|
|
_onCloseDialog: () => void;
|
|
|
|
|
2022-02-26 21:45:32 +00:00
|
|
|
/**
|
|
|
|
* An object containing the CSS classes.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
classes: any;
|
2022-02-26 21:45:32 +00:00
|
|
|
|
2017-11-13 22:58:00 +00:00
|
|
|
/**
|
2018-10-30 05:02:23 +00:00
|
|
|
* A Map with keyboard keys as keys and translation keys as values.
|
2017-11-13 22:58:00 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
shortcutDescriptions: Map<string, string>;
|
2022-08-25 11:35:19 +00:00
|
|
|
}
|
2017-11-13 22:58:00 +00:00
|
|
|
|
2022-02-26 21:45:32 +00:00
|
|
|
/**
|
|
|
|
* Creates the styles for the component.
|
|
|
|
*
|
|
|
|
* @param {Object} theme - The current UI theme.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-09-13 07:36:00 +00:00
|
|
|
const styles = (theme: Theme) => {
|
2022-02-26 21:45:32 +00:00
|
|
|
return {
|
|
|
|
list: {
|
|
|
|
listStyleType: 'none',
|
|
|
|
padding: 0,
|
|
|
|
|
|
|
|
'& .shortcuts-list__item': {
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
marginBottom: theme.spacing(2)
|
2022-08-04 10:39:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'& .item-action': {
|
|
|
|
backgroundColor: theme.palette.ui04,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
padding: '1px 4px',
|
|
|
|
borderRadius: '4px'
|
2022-02-26 21:45:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* Implements a React {@link Component} which displays a dialog describing
|
|
|
|
* registered keyboard shortcuts.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments Component
|
2018-10-30 05:02:23 +00:00
|
|
|
*/
|
|
|
|
class KeyboardShortcutsDialog extends Component<Props> {
|
2017-11-13 22:58:00 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const shortcuts = Array.from(this.props.shortcutDescriptions)
|
|
|
|
.map(description => this._renderShortcutsListItem(...description));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog
|
2022-09-29 10:26:34 +00:00
|
|
|
onCancel = { this.props._onCloseDialog }
|
|
|
|
titleKey = 'keyboardShortcuts.keyboardShortcuts'>
|
2017-11-13 22:58:00 +00:00
|
|
|
<div
|
|
|
|
id = 'keyboard-shortcuts'>
|
|
|
|
<ul
|
2022-02-26 21:45:32 +00:00
|
|
|
className = { clsx('shortcuts-list', this.props.classes.list) }
|
2017-11-13 22:58:00 +00:00
|
|
|
id = 'keyboard-shortcuts-list'>
|
2022-08-25 11:35:19 +00:00
|
|
|
{shortcuts}
|
2017-11-13 22:58:00 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a {@code ReactElement} for describing a single keyboard shortcut.
|
|
|
|
*
|
|
|
|
* @param {string} keyboardKey - The keyboard key that triggers an action.
|
|
|
|
* @param {string} translationKey - A description of what the action does.
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
_renderShortcutsListItem(keyboardKey: string, translationKey: string) {
|
2021-07-13 06:50:08 +00:00
|
|
|
let modifierKey = 'Alt';
|
|
|
|
|
|
|
|
if (window.navigator?.platform) {
|
|
|
|
if (window.navigator.platform.indexOf('Mac') !== -1) {
|
|
|
|
modifierKey = '⌥';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:58:00 +00:00
|
|
|
return (
|
|
|
|
<li
|
|
|
|
className = 'shortcuts-list__item'
|
|
|
|
key = { keyboardKey }>
|
2021-06-10 12:48:44 +00:00
|
|
|
<span
|
|
|
|
aria-label = { this.props.t(translationKey) }
|
|
|
|
className = 'shortcuts-list__description'>
|
2017-11-13 22:58:00 +00:00
|
|
|
{ this.props.t(translationKey) }
|
|
|
|
</span>
|
|
|
|
<span className = 'item-action'>
|
2022-08-04 10:39:22 +00:00
|
|
|
{ keyboardKey.startsWith(':')
|
|
|
|
? `${modifierKey} + ${keyboardKey.slice(1)}`
|
|
|
|
: keyboardKey }
|
2017-11-13 22:58:00 +00:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
/**
|
|
|
|
* Function that maps parts of Redux actions into component props.
|
|
|
|
*
|
|
|
|
* @param {Object} dispatch - Redux dispatch.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function mapDispatchToProps(dispatch: IStore['dispatch']) {
|
|
|
|
return {
|
|
|
|
_onCloseDialog: () => dispatch(hideDialog())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(null, mapDispatchToProps)(translate(withStyles(styles)(KeyboardShortcutsDialog)));
|