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

functions.js 8.7KB

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