Browse Source

走通创建图层流程

master
Cmen 3 years ago
parent
commit
f86829c9d8
  1. 4
      src/editor/Plot/mapHooks.ts
  2. 4
      src/map/editors/BaseLayerEditor.ts
  3. 16
      src/map/index.ts
  4. 11
      src/store/actions.ts
  5. 2
      src/store/index.ts
  6. 1
      src/store/initState.ts
  7. 12
      src/store/reducers.ts
  8. 6
      src/store/type.ts
  9. 2
      src/types/index.ts

4
src/editor/Plot/mapHooks.ts

@ -1,8 +1,12 @@
import { Dispatch } from "react"; import { Dispatch } from "react";
import { editorAction, IEditorState } from "@store"; import { editorAction, IEditorState } from "@store";
import { MapEditor } from "@map"; import { MapEditor } from "@map";
import { EventTypes } from "../../types/enum";
// 注册地图的事件钩子 // 注册地图的事件钩子
export function registerMapEventHooks(map: MapEditor, dispatch: Dispatch<any>) { export function registerMapEventHooks(map: MapEditor, dispatch: Dispatch<any>) {
// //
map.on(EventTypes.FinishCreateOverlay, (evt: any) =>
dispatch(editorAction.finishCreateOverlay(evt))
);
} }

4
src/map/editors/BaseLayerEditor.ts

@ -14,7 +14,9 @@ export abstract class BaseLayerEditor<T extends AMap.BaseEditor> {
} }
finish() { finish() {
// const target = this.editor.getT const target = this.editor.getTarget();
this.editor.setTarget(null); this.editor.setTarget(null);
return target;
} }
} }

16
src/map/index.ts

@ -7,10 +7,13 @@ import "@amap/amap-jsapi-types";
import { registerHotkey } from "../utils/hotkeys"; import { registerHotkey } from "../utils/hotkeys";
import { EventTypes } from "../types/enum"; import { EventTypes } from "../types/enum";
let uuid = 0;
const getUuid = () => ++uuid;
export class MapEditor extends Emitter { export class MapEditor extends Emitter {
dom: HTMLDivElement; dom: HTMLDivElement;
private _map: AMap.Map | undefined; private _map: AMap.Map | undefined;
private rectangleEditor: RectangleEditor | undefined; private rectangleEditor: RectangleEditor | undefined;
private overlayMap: Record<number, AMap.MapOverlay> = {};
constructor(dom: HTMLDivElement) { constructor(dom: HTMLDivElement) {
super(); super();
this.dom = dom; this.dom = dom;
@ -45,8 +48,17 @@ export class MapEditor extends Emitter {
} }
finishCreateOverlay() { finishCreateOverlay() {
this.rectangleEditor?.finish(); const target = this.rectangleEditor?.finish();
this.emit(EventTypes.FinishCreateOverlay); let evt: any = null;
if (target != null) {
const id = getUuid();
this.overlayMap[id] = target;
evt = {
id,
type: "rectangle",
};
}
this.emit(EventTypes.FinishCreateOverlay, evt);
} }
initEditors() { initEditors() {

11
src/store/actions.ts

@ -1,8 +1,13 @@
export enum ActionTypes { export enum ActionTypes {
AddRect = "addRect", AddRect = "addRect",
CreateOverlay = "createOverlay", CreateOverlay = "createOverlay",
FinishCreateOverlay = "finishCreateOverlay",
} }
export type CreatedOverlay = {
id: string;
type: string;
};
export class EditorAction { export class EditorAction {
addRect() { addRect() {
return { return {
@ -15,4 +20,10 @@ export class EditorAction {
payload: type, payload: type,
}; };
} }
finishCreateOverlay(overlay: any) {
return {
type: ActionTypes.FinishCreateOverlay,
payload: overlay as CreatedOverlay,
};
}
} }

2
src/store/index.ts

@ -9,6 +9,8 @@ export * from "./type";
type IStore = Store<IEditorState, Action>; type IStore = Store<IEditorState, Action>;
const store: IStore = createStore(reducer); const store: IStore = createStore(reducer);
(window as any).store = store;
export { store }; export { store };
// export class Store { // export class Store {

1
src/store/initState.ts

@ -5,5 +5,6 @@ export const initState: IEditorState = {
status: "", status: "",
overlayType: "", overlayType: "",
polygons: [], polygons: [],
rectangles: [],
}, },
}; };

12
src/store/reducers.ts

@ -1,7 +1,7 @@
import { createStore } from "redux"; import { createStore } from "redux";
import { initState } from "./initState"; import { initState } from "./initState";
import { ActionTypes } from "./actions"; import { ActionTypes } from "./actions";
import { IEditorState } from "./type"; import { IEditorState, IOverlay } from "./type";
export type Action = { export type Action = {
type: ActionTypes; type: ActionTypes;
@ -21,6 +21,16 @@ export function reducer(state = initState, action: Action): IEditorState {
}, },
// //
}; };
case ActionTypes.FinishCreateOverlay:
return {
...state,
map: {
...state.map,
status: "",
overlayType: "",
rectangles: [payload as IOverlay].concat(state.map.rectangles),
},
};
default: default:
return state; return state;
} }

6
src/store/type.ts

@ -1,12 +1,14 @@
export interface IPolygon { export interface IOverlay {
id: string; id: string;
name: string; name: string;
type: string;
} }
export interface IMapState { export interface IMapState {
status: string; status: string;
overlayType: string; overlayType: string;
polygons: IPolygon[]; polygons: IOverlay[];
rectangles: IOverlay[];
} }
export interface IEditorState { export interface IEditorState {

2
src/types/index.ts

@ -6,9 +6,11 @@ declare global {
interface IBaseEditor { interface IBaseEditor {
// new (map: AMap.Map): any; // new (map: AMap.Map): any;
} }
type MapOverlay = AMap.Rectangle | AMap.Polygon | AMap.Polyline;
class BaseEditor implements IBaseEditor { class BaseEditor implements IBaseEditor {
constructor(map: AMap.Map); constructor(map: AMap.Map);
setTarget(target: any): void; setTarget(target: any): void;
getTarget(): MapOverlay;
open(): void; open(): void;
} }
class PolygonEditor extends BaseEditor { class PolygonEditor extends BaseEditor {

Loading…
Cancel
Save