reducer.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from "react";
  2. import { getNextLayout } from "src/containers/LiveEditor/helpers";
  3. import { AppConfig, initialStates } from "../hocs/config";
  4. export enum ConfigActionType {
  5. SET_CONFIG,
  6. TOGGLE_LAYOUT,
  7. TOGGLE_EXPAND,
  8. TOGGLE_AUTOFORMAT,
  9. TOGGLE_DOCK,
  10. ZOOM_IN,
  11. ZOOM_OUT,
  12. CENTER_VIEW,
  13. SET_JSON,
  14. }
  15. export type ReducerAction = {
  16. type: ConfigActionType;
  17. payload?: any;
  18. };
  19. export const useConfigReducer: React.Reducer<AppConfig, ReducerAction> = (
  20. state = initialStates,
  21. action
  22. ) => {
  23. switch (action.type) {
  24. case ConfigActionType.SET_CONFIG:
  25. return { ...state, settings: action.payload };
  26. case ConfigActionType.CENTER_VIEW:
  27. return {
  28. ...state,
  29. settings: { ...state.settings, transform: Math.random() },
  30. };
  31. case ConfigActionType.ZOOM_IN:
  32. return {
  33. ...state,
  34. settings: {
  35. ...state.settings,
  36. zoomScale: state.settings.zoomScale + 0.2,
  37. },
  38. };
  39. case ConfigActionType.ZOOM_OUT:
  40. return {
  41. ...state,
  42. settings: {
  43. ...state.settings,
  44. zoomScale: state.settings.zoomScale - 0.2,
  45. },
  46. };
  47. case ConfigActionType.TOGGLE_AUTOFORMAT:
  48. return {
  49. ...state,
  50. settings: {
  51. ...state.settings,
  52. autoformat: !state.settings.autoformat,
  53. },
  54. };
  55. case ConfigActionType.TOGGLE_DOCK:
  56. return {
  57. ...state,
  58. settings: {
  59. ...state.settings,
  60. hideEditor: !state.settings.hideEditor,
  61. },
  62. };
  63. case ConfigActionType.TOGGLE_EXPAND:
  64. return {
  65. ...state,
  66. settings: {
  67. ...state.settings,
  68. expand: !state.settings.expand,
  69. },
  70. };
  71. case ConfigActionType.TOGGLE_LAYOUT:
  72. return {
  73. ...state,
  74. settings: {
  75. ...state.settings,
  76. layout: getNextLayout(state.settings.layout),
  77. },
  78. };
  79. case ConfigActionType.SET_JSON:
  80. return {
  81. ...state,
  82. json: action.payload,
  83. };
  84. default:
  85. return state;
  86. }
  87. };