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

middleware.web.ts 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { batch } from 'react-redux';
  2. import { AnyAction } from 'redux';
  3. import { OVERWRITE_CONFIG, SET_CONFIG, UPDATE_CONFIG } from '../base/config/actionTypes';
  4. import { NotifyClickButton } from '../base/config/configType';
  5. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  6. import { I_AM_VISITOR_MODE } from '../visitors/actionTypes';
  7. import {
  8. CLEAR_TOOLBOX_TIMEOUT,
  9. SET_BUTTONS_WITH_NOTIFY_CLICK,
  10. SET_FULL_SCREEN,
  11. SET_PARTICIPANT_MENU_BUTTONS_WITH_NOTIFY_CLICK,
  12. SET_TOOLBAR_BUTTONS,
  13. SET_TOOLBOX_TIMEOUT
  14. } from './actionTypes';
  15. import { setMainToolbarThresholds } from './actions.web';
  16. import { THRESHOLDS, TOOLBAR_BUTTONS } from './constants';
  17. import { getToolbarButtons } from './functions.web';
  18. import { NOTIFY_CLICK_MODE } from './types';
  19. import './subscriber.web';
  20. /**
  21. * Middleware which intercepts Toolbox actions to handle changes to the
  22. * visibility timeout of the Toolbox.
  23. *
  24. * @param {Store} store - The redux store.
  25. * @returns {Function}
  26. */
  27. MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case CLEAR_TOOLBOX_TIMEOUT: {
  30. const { timeoutID } = store.getState()['features/toolbox'];
  31. clearTimeout(timeoutID ?? undefined);
  32. break;
  33. }
  34. case UPDATE_CONFIG:
  35. case OVERWRITE_CONFIG:
  36. case I_AM_VISITOR_MODE:
  37. case SET_CONFIG: {
  38. const result = next(action);
  39. const { dispatch, getState } = store;
  40. const state = getState();
  41. if (action.type !== I_AM_VISITOR_MODE) {
  42. const {
  43. customToolbarButtons,
  44. buttonsWithNotifyClick,
  45. participantMenuButtonsWithNotifyClick,
  46. customParticipantMenuButtons
  47. } = state['features/base/config'];
  48. batch(() => {
  49. if (action.type !== I_AM_VISITOR_MODE) {
  50. dispatch(setMainToolbarThresholds(THRESHOLDS));
  51. }
  52. dispatch({
  53. type: SET_BUTTONS_WITH_NOTIFY_CLICK,
  54. buttonsWithNotifyClick: _buildButtonsArray(buttonsWithNotifyClick, customToolbarButtons)
  55. });
  56. dispatch({
  57. type: SET_PARTICIPANT_MENU_BUTTONS_WITH_NOTIFY_CLICK,
  58. participantMenuButtonsWithNotifyClick:
  59. _buildButtonsArray(participantMenuButtonsWithNotifyClick, customParticipantMenuButtons)
  60. });
  61. });
  62. }
  63. const toolbarButtons = getToolbarButtons(state, TOOLBAR_BUTTONS);
  64. dispatch({
  65. type: SET_TOOLBAR_BUTTONS,
  66. toolbarButtons
  67. });
  68. return result;
  69. }
  70. case SET_FULL_SCREEN:
  71. return _setFullScreen(next, action);
  72. case SET_TOOLBOX_TIMEOUT: {
  73. const { timeoutID } = store.getState()['features/toolbox'];
  74. const { handler, timeoutMS }: { handler: Function; timeoutMS: number; } = action;
  75. clearTimeout(timeoutID ?? undefined);
  76. action.timeoutID = setTimeout(handler, timeoutMS);
  77. break;
  78. }
  79. }
  80. return next(action);
  81. });
  82. type DocumentElement = {
  83. requestFullscreen?: Function;
  84. webkitRequestFullscreen?: Function;
  85. };
  86. /**
  87. * Makes an external request to enter or exit full screen mode.
  88. *
  89. * @param {Dispatch} next - The redux dispatch function to dispatch the
  90. * specified action to the specified store.
  91. * @param {Action} action - The redux action SET_FULL_SCREEN which is being
  92. * dispatched in the specified store.
  93. * @private
  94. * @returns {Object} The value returned by {@code next(action)}.
  95. */
  96. function _setFullScreen(next: Function, action: AnyAction) {
  97. const result = next(action);
  98. const { fullScreen } = action;
  99. if (fullScreen) {
  100. const documentElement: DocumentElement
  101. = document.documentElement || {};
  102. if (typeof documentElement.requestFullscreen === 'function') {
  103. documentElement.requestFullscreen();
  104. } else if (
  105. typeof documentElement.webkitRequestFullscreen === 'function') {
  106. documentElement.webkitRequestFullscreen();
  107. }
  108. return result;
  109. }
  110. if (typeof document.exitFullscreen === 'function') {
  111. document.exitFullscreen();
  112. } else if (typeof document.webkitExitFullscreen === 'function') {
  113. document.webkitExitFullscreen();
  114. }
  115. return result;
  116. }
  117. /**
  118. * Common logic to gather buttons that have to notify the api when clicked.
  119. *
  120. * @param {Array} buttonsWithNotifyClick - The array of system buttons that need to notify the api.
  121. * @param {Array} customButtons - The custom buttons.
  122. * @returns {Array}
  123. */
  124. function _buildButtonsArray(
  125. buttonsWithNotifyClick?: NotifyClickButton[],
  126. customButtons?: {
  127. icon: string;
  128. id: string;
  129. text: string;
  130. }[]
  131. ): Map<string, NOTIFY_CLICK_MODE> {
  132. const customButtonsWithNotifyClick = customButtons?.map(
  133. ({ id }) => ([ id, NOTIFY_CLICK_MODE.ONLY_NOTIFY ]) as [string, NOTIFY_CLICK_MODE]) ?? [];
  134. const buttons = (Array.isArray(buttonsWithNotifyClick) ? buttonsWithNotifyClick : [])
  135. .filter(button => typeof button === 'string' || (typeof button === 'object' && typeof button.key === 'string'))
  136. .map(button => {
  137. if (typeof button === 'string') {
  138. return [ button, NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY ] as [string, NOTIFY_CLICK_MODE];
  139. }
  140. return [
  141. button.key,
  142. button.preventExecution ? NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY : NOTIFY_CLICK_MODE.ONLY_NOTIFY
  143. ] as [string, NOTIFY_CLICK_MODE];
  144. });
  145. return new Map([ ...customButtonsWithNotifyClick, ...buttons ]);
  146. }