diff --git a/src/store/actions.ts b/src/store/actions.ts index 677273e..768ed1a 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -1,5 +1,5 @@ export enum ActionTypes { - AddRect = "addRect", + // AddRect = "addRect", CreateOverlay = "createOverlay", FinishCreateOverlay = "finishCreateOverlay", } @@ -9,11 +9,11 @@ export type CreatedOverlay = { type: string; }; export class EditorAction { - addRect() { - return { - type: ActionTypes.AddRect, - }; - } + // addRect() { + // return { + // type: ActionTypes.AddRect, + // }; + // } createOverlay(type: string) { return { type: ActionTypes.CreateOverlay, diff --git a/src/store/reducers.ts b/src/store/reducers.ts deleted file mode 100644 index 92ad219..0000000 --- a/src/store/reducers.ts +++ /dev/null @@ -1,30 +0,0 @@ -import produce from "immer"; -import { initState } from "./initState"; -import { ActionTypes } from "./actions"; -import { IEditorState, IOverlay } from "./type"; - -export type Action = { - type: ActionTypes; - payload?: any; -}; - -export function reducer(state = initState, action: Action): IEditorState { - const { type, payload } = action; - switch (type) { - case ActionTypes.CreateOverlay: - return produce(state, (draft) => { - draft.map.status = "createOverlay"; - draft.map.overlayType = payload as string; - }); - case ActionTypes.FinishCreateOverlay: - return produce(state, (draft) => { - const overlay = payload as IOverlay; - overlay.name = "测试矩形"; - draft.map.status = ""; - draft.map.overlayType = ""; - draft.map.rectangles = state.map.rectangles.concat([overlay]); - }); - default: - return state; - } -} diff --git a/src/store/reducers/index.ts b/src/store/reducers/index.ts new file mode 100644 index 0000000..1d236cf --- /dev/null +++ b/src/store/reducers/index.ts @@ -0,0 +1,26 @@ +import { initState } from "../initState"; +import { ActionTypes } from "../actions"; +import { createOverlay, finishCreateOverlay } from "./map"; +import { IEditorState } from "@store"; + +export type Action = { + type: ActionTypes; + payload?: any; +}; + +type ActionReducer = (state: IEditorState, payload: any) => IEditorState; + +const actionReducers: Record = { + [ActionTypes.CreateOverlay]: createOverlay, + [ActionTypes.FinishCreateOverlay]: finishCreateOverlay, + // addRect: undefined +}; + +export function reducer(state = initState, action: Action) { + const { payload, type } = action; + if (type in actionReducers) { + return actionReducers[type](state, payload); + } + + return state; +} diff --git a/src/store/reducers/map/index.ts b/src/store/reducers/map/index.ts new file mode 100644 index 0000000..b6eac66 --- /dev/null +++ b/src/store/reducers/map/index.ts @@ -0,0 +1,21 @@ +import produce from "immer"; +import { initState } from "../../initState"; +import { IOverlay } from "@store"; + +export function createOverlay(state = initState, payload: any) { + return produce(state, (draft) => { + draft.map.status = "createOverlay"; + draft.map.overlayType = payload as string; + }); +} + +export function finishCreateOverlay(state = initState, payload: any) { + return produce(state, (draft) => { + const overlay = payload as IOverlay; + // todo: uniqueName. + overlay.name = "矩形" + overlay.id; + draft.map.status = ""; + draft.map.overlayType = ""; + draft.map.rectangles = state.map.rectangles.concat([overlay]); + }); +}