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.

actions.web.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { batch } from 'react-redux';
  2. import { ACTION_SHORTCUT_PRESSED, ACTION_SHORTCUT_RELEASED, createShortcutEvent } from '../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../analytics/functions';
  4. import { IStore } from '../app/types';
  5. import { clickOnVideo } from '../filmstrip/actions.web';
  6. import { openSettingsDialog } from '../settings/actions.web';
  7. import { SETTINGS_TABS } from '../settings/constants';
  8. import { registerShortcut } from './actions.any';
  9. import { areKeyboardShortcutsEnabled, getKeyboardShortcuts } from './functions';
  10. import logger from './logger';
  11. import { getKeyboardKey, getPriorityFocusedElement } from './utils';
  12. export * from './actions.any';
  13. /**
  14. * Initialise global shortcuts.
  15. * Global shortcuts are shortcuts for features that don't have a button or
  16. * link associated with the action. In other words they represent actions
  17. * triggered _only_ with a shortcut.
  18. *
  19. * @returns {Function}
  20. */
  21. const initGlobalKeyboardShortcuts = () =>
  22. (dispatch: IStore['dispatch']) => {
  23. batch(() => {
  24. dispatch(registerShortcut({
  25. character: '?',
  26. helpDescription: 'keyboardShortcuts.toggleShortcuts',
  27. handler: () => {
  28. sendAnalytics(createShortcutEvent('help'));
  29. dispatch(openSettingsDialog(SETTINGS_TABS.SHORTCUTS, false));
  30. }
  31. }));
  32. // register SPACE shortcut in two steps to insure visibility of help message
  33. dispatch(registerShortcut({
  34. character: ' ',
  35. helpCharacter: 'SPACE',
  36. helpDescription: 'keyboardShortcuts.pushToTalk',
  37. handler: () => {
  38. sendAnalytics(createShortcutEvent('push.to.talk', ACTION_SHORTCUT_RELEASED));
  39. logger.log('Talk shortcut released');
  40. APP.conference.muteAudio(true);
  41. }
  42. }));
  43. dispatch(registerShortcut({
  44. character: '0',
  45. helpDescription: 'keyboardShortcuts.focusLocal',
  46. handler: () => {
  47. dispatch(clickOnVideo(0));
  48. }
  49. }));
  50. Array(9).fill(1)
  51. .forEach((_, index) => {
  52. const num = index + 1;
  53. dispatch(registerShortcut({
  54. character: `${num}`,
  55. // only show help hint for the first shortcut
  56. helpCharacter: num === 1 ? '1-9' : undefined,
  57. helpDescription: num === 1 ? 'keyboardShortcuts.focusRemote' : undefined,
  58. handler: () => {
  59. dispatch(clickOnVideo(num));
  60. }
  61. }));
  62. });
  63. });
  64. };
  65. /**
  66. * Initializes keyboard shortcuts.
  67. *
  68. * @returns {Function}
  69. */
  70. export const initKeyboardShortcuts = () =>
  71. (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  72. dispatch(initGlobalKeyboardShortcuts());
  73. window.onkeyup = (e: KeyboardEvent) => {
  74. const state = getState();
  75. const enabled = areKeyboardShortcutsEnabled(state);
  76. const shortcuts = getKeyboardShortcuts(state);
  77. if (!enabled || getPriorityFocusedElement()) {
  78. return;
  79. }
  80. const key = getKeyboardKey(e).toUpperCase();
  81. if (shortcuts.has(key)) {
  82. shortcuts.get(key)?.handler(e);
  83. }
  84. };
  85. window.onkeydown = (e: KeyboardEvent) => {
  86. const state = getState();
  87. const enabled = areKeyboardShortcutsEnabled(state);
  88. if (!enabled) {
  89. return;
  90. }
  91. const focusedElement = getPriorityFocusedElement();
  92. const key = getKeyboardKey(e).toUpperCase();
  93. if (key === ' ' && !focusedElement) {
  94. sendAnalytics(createShortcutEvent('push.to.talk', ACTION_SHORTCUT_PRESSED));
  95. logger.log('Talk shortcut pressed');
  96. APP.conference.muteAudio(false);
  97. } else if (key === 'ESCAPE') {
  98. focusedElement?.blur();
  99. }
  100. };
  101. };