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.

KeyboardShortcutsDialog.web.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* @flow */
  2. import { withStyles } from '@material-ui/core/styles';
  3. import clsx from 'clsx';
  4. import React, { Component } from 'react';
  5. import { Dialog } from '../../base/dialog';
  6. import { translate } from '../../base/i18n';
  7. /**
  8. * The type of the React {@code Component} props of
  9. * {@link KeyboardShortcutsDialog}.
  10. */
  11. type Props = {
  12. /**
  13. * An object containing the CSS classes.
  14. */
  15. classes: Object,
  16. /**
  17. * A Map with keyboard keys as keys and translation keys as values.
  18. */
  19. shortcutDescriptions: Object,
  20. /**
  21. * Invoked to obtain translated strings.
  22. */
  23. t: Function
  24. };
  25. /**
  26. * Creates the styles for the component.
  27. *
  28. * @param {Object} theme - The current UI theme.
  29. *
  30. * @returns {Object}
  31. */
  32. const styles = theme => {
  33. return {
  34. list: {
  35. listStyleType: 'none',
  36. padding: 0,
  37. '& .shortcuts-list__item': {
  38. display: 'flex',
  39. justifyContent: 'space-between',
  40. marginBottom: theme.spacing(2)
  41. },
  42. '& .item-action': {
  43. backgroundColor: theme.palette.ui04,
  44. fontWeight: 'bold',
  45. padding: '1px 4px',
  46. borderRadius: '4px'
  47. }
  48. }
  49. };
  50. };
  51. /**
  52. * Implements a React {@link Component} which displays a dialog describing
  53. * registered keyboard shortcuts.
  54. *
  55. * @augments Component
  56. */
  57. class KeyboardShortcutsDialog extends Component<Props> {
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. const shortcuts = Array.from(this.props.shortcutDescriptions)
  66. .map(description => this._renderShortcutsListItem(...description));
  67. return (
  68. <Dialog
  69. cancelKey = { 'dialog.close' }
  70. submitDisabled = { true }
  71. titleKey = 'keyboardShortcuts.keyboardShortcuts'
  72. width = 'small'>
  73. <div
  74. id = 'keyboard-shortcuts'>
  75. <ul
  76. className = { clsx('shortcuts-list', this.props.classes.list) }
  77. id = 'keyboard-shortcuts-list'>
  78. { shortcuts }
  79. </ul>
  80. </div>
  81. </Dialog>
  82. );
  83. }
  84. /**
  85. * Creates a {@code ReactElement} for describing a single keyboard shortcut.
  86. *
  87. * @param {string} keyboardKey - The keyboard key that triggers an action.
  88. * @param {string} translationKey - A description of what the action does.
  89. * @private
  90. * @returns {ReactElement}
  91. */
  92. _renderShortcutsListItem(keyboardKey, translationKey) {
  93. let modifierKey = 'Alt';
  94. if (window.navigator?.platform) {
  95. if (window.navigator.platform.indexOf('Mac') !== -1) {
  96. modifierKey = '⌥';
  97. }
  98. }
  99. return (
  100. <li
  101. className = 'shortcuts-list__item'
  102. key = { keyboardKey }>
  103. <span
  104. aria-label = { this.props.t(translationKey) }
  105. className = 'shortcuts-list__description'>
  106. { this.props.t(translationKey) }
  107. </span>
  108. <span className = 'item-action'>
  109. { keyboardKey.startsWith(':')
  110. ? `${modifierKey} + ${keyboardKey.slice(1)}`
  111. : keyboardKey }
  112. </span>
  113. </li>
  114. );
  115. }
  116. }
  117. export default translate(withStyles(styles)(KeyboardShortcutsDialog));