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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* @flow */
  2. import Lozenge from '@atlaskit/lozenge';
  3. import React, { Component } from 'react';
  4. import { Dialog } from '../../base/dialog';
  5. import { translate } from '../../base/i18n';
  6. /**
  7. * The type of the React {@code Component} props of
  8. * {@link KeyboardShortcutsDialog}.
  9. */
  10. type Props = {
  11. /**
  12. * A Map with keyboard keys as keys and translation keys as values.
  13. */
  14. shortcutDescriptions: Object,
  15. /**
  16. * Invoked to obtain translated strings.
  17. */
  18. t: Function
  19. };
  20. /**
  21. * Implements a React {@link Component} which displays a dialog describing
  22. * registered keyboard shortcuts.
  23. *
  24. * @extends Component
  25. */
  26. class KeyboardShortcutsDialog extends Component<Props> {
  27. /**
  28. * Implements React's {@link Component#render()}.
  29. *
  30. * @inheritdoc
  31. * @returns {ReactElement}
  32. */
  33. render() {
  34. const shortcuts = Array.from(this.props.shortcutDescriptions)
  35. .map(description => this._renderShortcutsListItem(...description));
  36. return (
  37. <Dialog
  38. cancelKey = { 'dialog.close' }
  39. submitDisabled = { true }
  40. titleKey = 'keyboardShortcuts.keyboardShortcuts'
  41. width = 'small'>
  42. <div
  43. id = 'keyboard-shortcuts'>
  44. <ul
  45. className = 'shortcuts-list'
  46. id = 'keyboard-shortcuts-list'>
  47. { shortcuts }
  48. </ul>
  49. </div>
  50. </Dialog>
  51. );
  52. }
  53. /**
  54. * Creates a {@code ReactElement} for describing a single keyboard shortcut.
  55. *
  56. * @param {string} keyboardKey - The keyboard key that triggers an action.
  57. * @param {string} translationKey - A description of what the action does.
  58. * @private
  59. * @returns {ReactElement}
  60. */
  61. _renderShortcutsListItem(keyboardKey, translationKey) {
  62. let modifierKey = 'Alt';
  63. if (window.navigator?.platform) {
  64. if (window.navigator.platform.indexOf('Mac') !== -1) {
  65. modifierKey = '⌥';
  66. }
  67. }
  68. return (
  69. <li
  70. className = 'shortcuts-list__item'
  71. key = { keyboardKey }>
  72. <span
  73. aria-label = { this.props.t(translationKey) }
  74. className = 'shortcuts-list__description'>
  75. { this.props.t(translationKey) }
  76. </span>
  77. <span className = 'item-action'>
  78. <Lozenge isBold = { true }>
  79. { keyboardKey.startsWith(':')
  80. ? `${modifierKey} + ${keyboardKey.slice(1)}`
  81. : keyboardKey }
  82. </Lozenge>
  83. </span>
  84. </li>
  85. );
  86. }
  87. }
  88. export default translate(KeyboardShortcutsDialog);