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.

middleware.web.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { AnyAction } from 'redux';
  2. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  3. import {
  4. CLEAR_TOOLBOX_TIMEOUT,
  5. SET_FULL_SCREEN,
  6. SET_TOOLBOX_TIMEOUT
  7. } from './actionTypes';
  8. import './subscriber.web';
  9. /**
  10. * Middleware which intercepts Toolbox actions to handle changes to the
  11. * visibility timeout of the Toolbox.
  12. *
  13. * @param {Store} store - The redux store.
  14. * @returns {Function}
  15. */
  16. MiddlewareRegistry.register(store => next => action => {
  17. switch (action.type) {
  18. case CLEAR_TOOLBOX_TIMEOUT: {
  19. const { timeoutID } = store.getState()['features/toolbox'];
  20. clearTimeout(timeoutID ?? undefined);
  21. break;
  22. }
  23. case SET_FULL_SCREEN:
  24. return _setFullScreen(next, action);
  25. case SET_TOOLBOX_TIMEOUT: {
  26. const { timeoutID } = store.getState()['features/toolbox'];
  27. const { handler, timeoutMS }: { handler: Function; timeoutMS: number; } = action;
  28. clearTimeout(timeoutID ?? undefined);
  29. action.timeoutID = setTimeout(handler, timeoutMS);
  30. break;
  31. }
  32. }
  33. return next(action);
  34. });
  35. type DocumentElement = {
  36. requestFullscreen?: Function;
  37. webkitRequestFullscreen?: Function;
  38. };
  39. /**
  40. * Makes an external request to enter or exit full screen mode.
  41. *
  42. * @param {Dispatch} next - The redux dispatch function to dispatch the
  43. * specified action to the specified store.
  44. * @param {Action} action - The redux action SET_FULL_SCREEN which is being
  45. * dispatched in the specified store.
  46. * @private
  47. * @returns {Object} The value returned by {@code next(action)}.
  48. */
  49. function _setFullScreen(next: Function, action: AnyAction) {
  50. const result = next(action);
  51. const { fullScreen } = action;
  52. if (fullScreen) {
  53. const documentElement: DocumentElement
  54. = document.documentElement || {};
  55. if (typeof documentElement.requestFullscreen === 'function') {
  56. documentElement.requestFullscreen();
  57. } else if (
  58. typeof documentElement.webkitRequestFullscreen === 'function') {
  59. documentElement.webkitRequestFullscreen();
  60. }
  61. return result;
  62. }
  63. if (typeof document.exitFullscreen === 'function') {
  64. document.exitFullscreen();
  65. } else if (typeof document.webkitExitFullscreen === 'function') {
  66. document.webkitExitFullscreen();
  67. }
  68. return result;
  69. }