index.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Dispatch } from 'react';
  2. import {
  3. CHANGE_SCALE,
  4. TOGGLE_DISPLAY_MODE,
  5. SET_SIDEBAR,
  6. SET_MARKUP_TOOL,
  7. } from '../../constants/actionTypes';
  8. const applyMiddleware = (state: any, dispatch: Dispatch<any>) => (action: {
  9. type: string;
  10. payload?: any;
  11. }): void => {
  12. dispatch(action);
  13. switch (action.type) {
  14. case TOGGLE_DISPLAY_MODE: {
  15. if (action.payload === 'full') {
  16. const screenWidth = window.document.body.offsetWidth - 5;
  17. const originPdfWidth = state.viewport.width / state.scale;
  18. const rate = (screenWidth / originPdfWidth).toFixed(2);
  19. dispatch({ type: CHANGE_SCALE, payload: rate });
  20. } else {
  21. const screenWidth = window.document.body.offsetWidth - 288;
  22. const originPdfWidth = state.viewport.width / state.scale;
  23. const rate = screenWidth / originPdfWidth;
  24. dispatch({ type: CHANGE_SCALE, payload: rate });
  25. }
  26. break;
  27. }
  28. case SET_SIDEBAR: {
  29. if (action.payload === '') {
  30. dispatch({ type: SET_MARKUP_TOOL, payload: '' });
  31. }
  32. break;
  33. }
  34. default:
  35. break;
  36. }
  37. };
  38. export default applyMiddleware;