export default function workspaceReducer()

in src/state/workspace/workspaceReducer.ts [209:303]


export default function workspaceReducer<NodeType, EdgeType>(
  state: DiagramMakerWorkspace | undefined, action: DiagramMakerAction<NodeType, EdgeType>
): DiagramMakerWorkspace {
  if (state === undefined) {
    return getDefaultWorkspaceState();
  }
  switch (action.type) {
    case WorkspaceActionsType.WORKSPACE_DRAG:
      return produce(state, (draftState) => {
        dragReducer(draftState, action);
      });
    case WorkspaceActionsType.WORKSPACE_ZOOM:
      return produce(state, (draftState) => {
        zoomReducer(draftState, action);
      });
    case WorkspaceActionsType.WORKSPACE_RESIZE:
      return produce(state, (draftState) => {
        resizeReducer(draftState, action);
      });
    case EditorActionsType.FOCUS_NODE:
      return produce(state, (draftState) => {
        focusReducer(draftState, action);
      });
    case EditorActionsType.FIT:
      return produce(state, (draftState) => {
        fitReducer(draftState, action);
      });
    case WorkspaceActionsType.WORKSPACE_RESET_ZOOM:
      return produce(state, (draftState) => {
        resetZoomReducer(draftState);
      });
    case NodeActionsType.NODE_DRAG:
      return produce(state, (draftState) => {
        const canvasSize =  state.canvasSize;
        const workspacePos = state.position;
        const nodePos = action.payload.position;
        const nodeSize = action.payload.size;

        // Resize and move workspace when node is dragged outside right boundary
        if (nodePos.x + nodeSize.width > canvasSize.width) {
          const incrementWidth = nodePos.x + nodeSize.width - canvasSize.width;
          const newCanvasSize = {
            height: canvasSize.height,
            width: canvasSize.width + incrementWidth
          };
          const newWokspacePos = {
            x: workspacePos.x - incrementWidth,
            y: workspacePos.y
          };
          const resizeAction = createResizeWorkspaceCanvasAction(newCanvasSize);
          const dragAction = createDragWorkspaceAction(newWokspacePos);
          canvasResizeReducer(draftState, resizeAction);
          dragReducer(draftState, dragAction);
        }
        // Resize and move workspace when node is dragged outside bottom boundary
        if (nodePos.y + nodeSize.height > canvasSize.height) {
          const incrementHeight = nodePos.y + nodeSize.height - canvasSize.height;
          const newCanvasSize = {
            height: canvasSize.height + incrementHeight,
            width: canvasSize.width
          };
          const newWokspacePos = {
            x: workspacePos.x,
            y: workspacePos.y - incrementHeight
          };
          const resizeAction = createResizeWorkspaceCanvasAction(newCanvasSize);
          const dragAction = createDragWorkspaceAction(newWokspacePos);
          canvasResizeReducer(draftState, resizeAction);
          dragReducer(draftState, dragAction);
        }
        // Resize workspace when node is dragged outside top boundary
        if (nodePos.y < 0) {
          const incrementHeight = - nodePos.y;
          const newCanvasSize = {
            height: canvasSize.height + incrementHeight,
            width: canvasSize.width
          };
          const resizeAction = createResizeWorkspaceCanvasAction(newCanvasSize);
          canvasResizeReducer(draftState, resizeAction);
        }
        // Resize workspace when node is dragged outside left boundary
        if (nodePos.x < 0) {
          const incrementWidth = - nodePos.x;
          const newCanvasSize = {
            height: canvasSize.height,
            width: canvasSize.width + incrementWidth
          };
          const resizeAction = createResizeWorkspaceCanvasAction(newCanvasSize);
          canvasResizeReducer(draftState, resizeAction);
        }
      });
    default:
      return state;
  }
}