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.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // @flow
  2. import UIEvents from '../../../service/UI/UIEvents';
  3. import { getCurrentConference } from '../base/conference';
  4. import { setActiveModalId } from '../base/modal';
  5. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  6. import { TOGGLE_DOCUMENT_EDITING } from './actionTypes';
  7. import { setDocumentEditingState, setDocumentUrl } from './actions';
  8. import { SHARE_DOCUMENT_VIEW_ID } from './constants';
  9. declare var APP: Object;
  10. const ETHERPAD_COMMAND = 'etherpad';
  11. /**
  12. * Middleware that captures actions related to collaborative document editing
  13. * and notifies components not hooked into redux.
  14. *
  15. * @param {Store} store - The redux store.
  16. * @returns {Function}
  17. */
  18. // eslint-disable-next-line no-unused-vars
  19. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  20. switch (action.type) {
  21. case TOGGLE_DOCUMENT_EDITING: {
  22. if (typeof APP === 'undefined') {
  23. const editing = !getState()['features/etherpad'].editing;
  24. dispatch(setDocumentEditingState(editing));
  25. if (editing) {
  26. dispatch(setActiveModalId(SHARE_DOCUMENT_VIEW_ID));
  27. } else if (getState()['features/base/modal'].activeModalId === SHARE_DOCUMENT_VIEW_ID) {
  28. dispatch(setActiveModalId(undefined));
  29. }
  30. } else {
  31. APP.UI.emitEvent(UIEvents.ETHERPAD_CLICKED);
  32. }
  33. break;
  34. }
  35. }
  36. return next(action);
  37. });
  38. /**
  39. * Set up state change listener to perform maintenance tasks when the conference
  40. * is left or failed, e.g. clear messages or close the chat modal if it's left
  41. * open.
  42. */
  43. StateListenerRegistry.register(
  44. state => getCurrentConference(state),
  45. (conference, { dispatch, getState }, previousConference) => {
  46. if (conference) {
  47. conference.addCommandListener(ETHERPAD_COMMAND,
  48. ({ value }) => {
  49. let url;
  50. const { etherpad_base: etherpadBase } = getState()['features/base/config'];
  51. if (etherpadBase) {
  52. const u = new URL(value, etherpadBase);
  53. url = u.toString();
  54. }
  55. dispatch(setDocumentUrl(url));
  56. }
  57. );
  58. }
  59. if (previousConference) {
  60. dispatch(setDocumentUrl(undefined));
  61. }
  62. });