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

functions.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // @flow
  2. import {
  3. isParticipantApproved,
  4. isEnabledFromState,
  5. isLocalParticipantApprovedFromState,
  6. isSupported
  7. } from '../av-moderation/functions';
  8. import { getFeatureFlag, INVITE_ENABLED } from '../base/flags';
  9. import { MEDIA_TYPE, type MediaType } from '../base/media/constants';
  10. import {
  11. getDominantSpeakerParticipant,
  12. isLocalParticipantModerator,
  13. isParticipantModerator,
  14. getLocalParticipant,
  15. getRemoteParticipantsSorted,
  16. getRaiseHandsQueue
  17. } from '../base/participants/functions';
  18. import { toState } from '../base/redux';
  19. import { QUICK_ACTION_BUTTON, REDUCER_KEY, MEDIA_STATE } from './constants';
  20. /**
  21. * Generates a class attribute value.
  22. *
  23. * @param {Iterable<string>} args - String iterable.
  24. * @returns {string} Class attribute value.
  25. */
  26. export const classList = (...args: Array<string | boolean>) => args.filter(Boolean).join(' ');
  27. /**
  28. * Find the first styled ancestor component of an element.
  29. *
  30. * @param {Element} target - Element to look up.
  31. * @param {StyledComponentClass} component - Styled component reference.
  32. * @returns {Element|null} Ancestor.
  33. */
  34. export const findStyledAncestor = (target: Object, component: any) => {
  35. if (!target || target.matches(`.${component.styledComponentId}`)) {
  36. return target;
  37. }
  38. return findStyledAncestor(target.parentElement, component);
  39. };
  40. /**
  41. * Checks if a participant is force muted.
  42. *
  43. * @param {Object} participant - The participant.
  44. * @param {MediaType} mediaType - The media type.
  45. * @param {Object} state - The redux state.
  46. * @returns {MediaState}
  47. */
  48. export function isForceMuted(participant: Object, mediaType: MediaType, state: Object) {
  49. if (isEnabledFromState(mediaType, state)) {
  50. if (participant.local) {
  51. return !isLocalParticipantApprovedFromState(mediaType, state);
  52. }
  53. // moderators cannot be force muted
  54. if (isParticipantModerator(participant)) {
  55. return false;
  56. }
  57. return !isParticipantApproved(participant.id, mediaType)(state);
  58. }
  59. return false;
  60. }
  61. /**
  62. * Determines the audio media state (the mic icon) for a participant.
  63. *
  64. * @param {Object} participant - The participant.
  65. * @param {boolean} muted - The mute state of the participant.
  66. * @param {Object} state - The redux state.
  67. * @returns {MediaState}
  68. */
  69. export function getParticipantAudioMediaState(participant: Object, muted: Boolean, state: Object) {
  70. const dominantSpeaker = getDominantSpeakerParticipant(state);
  71. if (muted) {
  72. if (isForceMuted(participant, MEDIA_TYPE.AUDIO, state)) {
  73. return MEDIA_STATE.FORCE_MUTED;
  74. }
  75. return MEDIA_STATE.MUTED;
  76. }
  77. if (participant === dominantSpeaker) {
  78. return MEDIA_STATE.DOMINANT_SPEAKER;
  79. }
  80. return MEDIA_STATE.UNMUTED;
  81. }
  82. /**
  83. * Determines the video media state (the mic icon) for a participant.
  84. *
  85. * @param {Object} participant - The participant.
  86. * @param {boolean} muted - The mute state of the participant.
  87. * @param {Object} state - The redux state.
  88. * @returns {MediaState}
  89. */
  90. export function getParticipantVideoMediaState(participant: Object, muted: Boolean, state: Object) {
  91. if (muted) {
  92. if (isForceMuted(participant, MEDIA_TYPE.VIDEO, state)) {
  93. return MEDIA_STATE.FORCE_MUTED;
  94. }
  95. return MEDIA_STATE.MUTED;
  96. }
  97. return MEDIA_STATE.UNMUTED;
  98. }
  99. /**
  100. * Get a style property from a style declaration as a float.
  101. *
  102. * @param {CSSStyleDeclaration} styles - Style declaration.
  103. * @param {string} name - Property name.
  104. * @returns {number} Float value.
  105. */
  106. export const getFloatStyleProperty = (styles: Object, name: string) =>
  107. parseFloat(styles.getPropertyValue(name));
  108. /**
  109. * Gets the outer height of an element, including margins.
  110. *
  111. * @param {Element} element - Target element.
  112. * @returns {number} Computed height.
  113. */
  114. export const getComputedOuterHeight = (element: HTMLElement) => {
  115. const computedStyle = getComputedStyle(element);
  116. return element.offsetHeight
  117. + getFloatStyleProperty(computedStyle, 'margin-top')
  118. + getFloatStyleProperty(computedStyle, 'margin-bottom');
  119. };
  120. /**
  121. * Returns this feature's root state.
  122. *
  123. * @param {Object} state - Global state.
  124. * @returns {Object} Feature state.
  125. */
  126. const getState = (state: Object) => state[REDUCER_KEY];
  127. /**
  128. * Is the participants pane open.
  129. *
  130. * @param {Object} state - Global state.
  131. * @returns {boolean} Is the participants pane open.
  132. */
  133. export const getParticipantsPaneOpen = (state: Object) => Boolean(getState(state)?.isOpen);
  134. /**
  135. * Returns the type of quick action button to be displayed for a participant.
  136. * The button is displayed when hovering a participant from the participant list.
  137. *
  138. * @param {Object} participant - The participant.
  139. * @param {boolean} isAudioMuted - If audio is muted for the participant.
  140. * @param {Object} state - The redux state.
  141. * @returns {string} - The type of the quick action button.
  142. */
  143. export function getQuickActionButtonType(participant: Object, isAudioMuted: Boolean, state: Object) {
  144. // handled only by moderators
  145. if (isLocalParticipantModerator(state)) {
  146. if (!isAudioMuted) {
  147. return QUICK_ACTION_BUTTON.MUTE;
  148. }
  149. if (isSupported()(state)) {
  150. return QUICK_ACTION_BUTTON.ASK_TO_UNMUTE;
  151. }
  152. }
  153. return QUICK_ACTION_BUTTON.NONE;
  154. }
  155. /**
  156. * Returns true if the invite button should be rendered.
  157. *
  158. * @param {Object} state - Global state.
  159. * @returns {boolean}
  160. */
  161. export const shouldRenderInviteButton = (state: Object) => {
  162. const { disableInviteFunctions } = toState(state)['features/base/config'];
  163. const flagEnabled = getFeatureFlag(state, INVITE_ENABLED, true);
  164. return flagEnabled && !disableInviteFunctions;
  165. };
  166. /**
  167. * Selector for retrieving ids of participants in the order that they are displayed in the filmstrip (with the
  168. * exception of participants with raised hand). The participants are reordered as follows.
  169. * 1. Dominant speaker.
  170. * 2. Local participant.
  171. * 3. Participants with raised hand.
  172. * 4. Participants with screenshare sorted alphabetically by their display name.
  173. * 5. Shared video participants.
  174. * 6. Recent speakers sorted alphabetically by their display name.
  175. * 7. Rest of the participants sorted alphabetically by their display name.
  176. *
  177. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  178. * {@code getState} function to be used to retrieve the state features/base/participants.
  179. * @returns {Array<string>}
  180. */
  181. export function getSortedParticipantIds(stateful: Object | Function): Array<string> {
  182. const { id } = getLocalParticipant(stateful);
  183. const remoteParticipants = getRemoteParticipantsSorted(stateful);
  184. const reorderedParticipants = new Set(remoteParticipants);
  185. const raisedHandParticipants = getRaiseHandsQueue(stateful);
  186. const remoteRaisedHandParticipants = new Set(raisedHandParticipants || []);
  187. const dominantSpeaker = getDominantSpeakerParticipant(stateful);
  188. for (const participant of remoteRaisedHandParticipants.keys()) {
  189. // Avoid duplicates.
  190. if (reorderedParticipants.has(participant)) {
  191. reorderedParticipants.delete(participant);
  192. } else {
  193. remoteRaisedHandParticipants.delete(participant);
  194. }
  195. }
  196. // Remove self.
  197. remoteRaisedHandParticipants.delete(id);
  198. const dominant = [];
  199. // Remove dominant speaker.
  200. if (dominantSpeaker && dominantSpeaker.id !== id) {
  201. remoteRaisedHandParticipants.delete(dominantSpeaker.id);
  202. reorderedParticipants.delete(dominantSpeaker.id);
  203. dominant.push(dominantSpeaker.id);
  204. }
  205. // Move self and participants with raised hand to the top of the list.
  206. return [
  207. ...dominant,
  208. id,
  209. ...Array.from(remoteRaisedHandParticipants.keys()),
  210. ...Array.from(reorderedParticipants.keys())
  211. ];
  212. }