You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShortcutsTab.tsx 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Theme } from '@mui/material';
  2. import React from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { withStyles } from 'tss-react/mui';
  5. import AbstractDialogTab, {
  6. IProps as AbstractDialogTabProps } from '../../../base/dialog/components/web/AbstractDialogTab';
  7. import { translate } from '../../../base/i18n/functions';
  8. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  9. import Checkbox from '../../../base/ui/components/web/Checkbox';
  10. /**
  11. * The type of the React {@code Component} props of {@link ShortcutsTab}.
  12. */
  13. export interface IProps extends AbstractDialogTabProps, WithTranslation {
  14. /**
  15. * CSS classes object.
  16. */
  17. classes?: Partial<Record<keyof ReturnType<typeof styles>, string>>;
  18. /**
  19. * Whether to display the shortcuts or not.
  20. */
  21. displayShortcuts: boolean;
  22. /**
  23. * Whether the keyboard shortcuts are enabled or not.
  24. */
  25. keyboardShortcutsEnabled: boolean;
  26. /**
  27. * The keyboard shortcuts descriptions.
  28. */
  29. keyboardShortcutsHelpDescriptions: Map<string, string>;
  30. }
  31. const styles = (theme: Theme) => {
  32. return {
  33. container: {
  34. display: 'flex',
  35. flexDirection: 'column' as const,
  36. width: '100%',
  37. paddingBottom: theme.spacing(3)
  38. },
  39. checkbox: {
  40. marginBottom: theme.spacing(3)
  41. },
  42. listContainer: {
  43. listStyleType: 'none',
  44. padding: 0,
  45. margin: 0
  46. },
  47. listItem: {
  48. display: 'flex',
  49. justifyContent: 'space-between',
  50. alignItems: 'center',
  51. padding: `${theme.spacing(1)} 0`,
  52. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  53. color: theme.palette.text01
  54. },
  55. listItemKey: {
  56. backgroundColor: theme.palette.ui04,
  57. ...withPixelLineHeight(theme.typography.labelBold),
  58. padding: `${theme.spacing(1)} ${theme.spacing(2)}`,
  59. borderRadius: `${Number(theme.shape.borderRadius) / 2}px`
  60. }
  61. };
  62. };
  63. /**
  64. * React {@code Component} for modifying the local user's profile.
  65. *
  66. * @augments Component
  67. */
  68. class ShortcutsTab extends AbstractDialogTab<IProps, any> {
  69. /**
  70. * Initializes a new {@code MoreTab} instance.
  71. *
  72. * @param {Object} props - The read-only properties with which the new
  73. * instance is to be initialized.
  74. */
  75. constructor(props: IProps) {
  76. super(props);
  77. // Bind event handler so it is only bound once for every instance.
  78. this._onKeyboardShortcutEnableChanged = this._onKeyboardShortcutEnableChanged.bind(this);
  79. this._renderShortcutsListItem = this._renderShortcutsListItem.bind(this);
  80. }
  81. /**
  82. * Callback invoked to select if global keyboard shortcuts
  83. * should be enabled.
  84. *
  85. * @param {Object} e - The key event to handle.
  86. *
  87. * @returns {void}
  88. */
  89. _onKeyboardShortcutEnableChanged({ target: { checked } }: React.ChangeEvent<HTMLInputElement>) {
  90. super._onChange({ keyboardShortcutsEnabled: checked });
  91. }
  92. /**
  93. * Render a keyboard shortcut with key and description.
  94. *
  95. * @param {string} keyboardKey - The keyboard key for the shortcut.
  96. * @param {string} translationKey - The translation key for the shortcut description.
  97. * @returns {JSX}
  98. */
  99. _renderShortcutsListItem(keyboardKey: string, translationKey: string) {
  100. const { t } = this.props;
  101. const classes = withStyles.getClasses(this.props);
  102. let modifierKey = 'Alt';
  103. if (window.navigator?.platform) {
  104. if (window.navigator.platform.indexOf('Mac') !== -1) {
  105. modifierKey = '⌥';
  106. }
  107. }
  108. return (
  109. <li
  110. className = { classes.listItem }
  111. key = { keyboardKey }>
  112. <span
  113. aria-label = { t(translationKey) }>
  114. {t(translationKey)}
  115. </span>
  116. <span className = { classes.listItemKey }>
  117. {keyboardKey.startsWith(':')
  118. ? `${modifierKey} + ${keyboardKey.slice(1)}`
  119. : keyboardKey}
  120. </span>
  121. </li>
  122. );
  123. }
  124. /**
  125. * Implements React's {@link Component#render()}.
  126. *
  127. * @inheritdoc
  128. * @returns {ReactElement}
  129. */
  130. render() {
  131. const {
  132. displayShortcuts,
  133. keyboardShortcutsHelpDescriptions,
  134. keyboardShortcutsEnabled,
  135. t
  136. } = this.props;
  137. const classes = withStyles.getClasses(this.props);
  138. const shortcutDescriptions: Map<string, string> = displayShortcuts
  139. ? keyboardShortcutsHelpDescriptions
  140. : new Map();
  141. return (
  142. <div className = { classes.container }>
  143. <Checkbox
  144. checked = { keyboardShortcutsEnabled }
  145. className = { classes.checkbox }
  146. label = { t('prejoin.keyboardShortcuts') }
  147. name = 'enable-keyboard-shortcuts'
  148. onChange = { this._onKeyboardShortcutEnableChanged } />
  149. {displayShortcuts && (
  150. <ul className = { classes.listContainer }>
  151. {Array.from(shortcutDescriptions)
  152. .map(description => this._renderShortcutsListItem(...description))}
  153. </ul>
  154. )}
  155. </div>
  156. );
  157. }
  158. }
  159. export default withStyles(translate(ShortcutsTab), styles);