您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

KeyboardShortcutsDialog.tsx 3.6KB

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