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.

functions.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React from 'react';
  2. // @ts-expect-error
  3. import VideoLayout from '../../../modules/UI/videolayout/VideoLayout';
  4. import { IReduxState, IStore } from '../app/types';
  5. import { IJitsiConference } from '../base/conference/reducer';
  6. import JitsiMeetJS from '../base/lib-jitsi-meet';
  7. import { enableReceiver, stopReceiver } from './actions';
  8. import { EVENTS, REMOTE_CONTROL_MESSAGE_NAME } from './constants';
  9. import { keyboardEventToKey } from './keycodes';
  10. import logger from './logger';
  11. /**
  12. * Checks if the remote control is enabled.
  13. *
  14. * @param {*} state - The redux state.
  15. * @returns {boolean} - True if the remote control is enabled and false otherwise.
  16. */
  17. export function isRemoteControlEnabled(state: IReduxState) {
  18. return !state['features/base/config'].disableRemoteControl && JitsiMeetJS.isDesktopSharingEnabled();
  19. }
  20. /**
  21. * Sends remote control message to other participant through data channel.
  22. *
  23. * @param {JitsiConference} conference - The JitsiConference object.
  24. * @param {string} to - The participant who will receive the event.
  25. * @param {RemoteControlEvent} event - The remote control event.
  26. * @returns {boolean} - True if the message was sent successfully and false otherwise.
  27. */
  28. export function sendRemoteControlEndpointMessage(
  29. conference: IJitsiConference | undefined,
  30. to: string | undefined,
  31. event: Object) {
  32. if (!to) {
  33. logger.warn('Remote control: Skip sending remote control event. Params:', to);
  34. return false;
  35. }
  36. try {
  37. conference?.sendEndpointMessage(to, {
  38. name: REMOTE_CONTROL_MESSAGE_NAME,
  39. ...event
  40. });
  41. return true;
  42. } catch (error) {
  43. logger.error('Failed to send EndpointMessage via the datachannels', error);
  44. return false;
  45. }
  46. }
  47. /**
  48. * Handles remote control events from the external app. Currently only
  49. * events with type EVENTS.supported and EVENTS.stop are
  50. * supported.
  51. *
  52. * @param {RemoteControlEvent} event - The remote control event.
  53. * @param {Store} store - The redux store.
  54. * @returns {void}
  55. */
  56. export function onRemoteControlAPIEvent(event: { type: string; }, { getState, dispatch }: IStore) {
  57. switch (event.type) {
  58. case EVENTS.supported:
  59. logger.log('Remote Control supported.');
  60. if (isRemoteControlEnabled(getState())) {
  61. dispatch(enableReceiver());
  62. } else {
  63. logger.log('Remote Control disabled.');
  64. }
  65. break;
  66. case EVENTS.stop: {
  67. dispatch(stopReceiver());
  68. break;
  69. }
  70. }
  71. }
  72. /**
  73. * Returns the area used for capturing mouse and key events.
  74. *
  75. * @returns {JQuery} - A JQuery selector.
  76. */
  77. export function getRemoteConrolEventCaptureArea() {
  78. return VideoLayout.getLargeVideoWrapper();
  79. }
  80. /**
  81. * Extract the keyboard key from the keyboard event.
  82. *
  83. * @param {KeyboardEvent} event - The event.
  84. * @returns {KEYS} The key that is pressed or undefined.
  85. */
  86. export function getKey(event: React.KeyboardEvent) {
  87. return keyboardEventToKey(event);
  88. }
  89. /**
  90. * Extract the modifiers from the keyboard event.
  91. *
  92. * @param {KeyboardEvent} event - The event.
  93. * @returns {Array} With possible values: "shift", "control", "alt", "command".
  94. */
  95. export function getModifiers(event: React.KeyboardEvent) {
  96. const modifiers = [];
  97. if (event.shiftKey) {
  98. modifiers.push('shift');
  99. }
  100. if (event.ctrlKey) {
  101. modifiers.push('control');
  102. }
  103. if (event.altKey) {
  104. modifiers.push('alt');
  105. }
  106. if (event.metaKey) {
  107. modifiers.push('command');
  108. }
  109. return modifiers;
  110. }