Cmen
3 years ago
8 changed files with 200 additions and 49 deletions
@ -0,0 +1,110 @@
|
||||
import { IStore } from "./type"; |
||||
import { registerHotkey } from "@utils"; |
||||
import { getGeoJSON } from "./utils/getGeoJSON"; |
||||
import { OverlayTypes } from "../types/enum"; |
||||
import { EditorAction } from "./actions"; |
||||
interface IGlobalController { |
||||
saveGeoJSON: () => void; |
||||
saveProject: () => void; |
||||
saveTemp: () => void; |
||||
clearEditor: () => void; |
||||
openGeoJSON: () => void; |
||||
openProject: () => void; |
||||
showCommand: () => void; |
||||
createRectangle: () => void; |
||||
createPolygon: () => void; |
||||
createPolyline: () => void; |
||||
createCircle: () => void; |
||||
} |
||||
|
||||
// 全局控制器, 用以复用一些快捷方法.
|
||||
export class GlobelController implements IGlobalController { |
||||
_store: IStore; |
||||
constructor(store: IStore) { |
||||
this._store = store; |
||||
|
||||
this._bindOperations(); |
||||
this._registerHotkeys(); |
||||
} |
||||
|
||||
get mapState() { |
||||
return this._store.getState().map; |
||||
} |
||||
|
||||
saveGeoJSON() { |
||||
const geojson = getGeoJSON(this.mapState); |
||||
console.log(geojson); |
||||
} |
||||
|
||||
saveProject() { |
||||
//
|
||||
} |
||||
saveTemp() { |
||||
//
|
||||
} |
||||
clearEditor() { |
||||
//
|
||||
} |
||||
openGeoJSON() { |
||||
//
|
||||
} |
||||
openProject() { |
||||
//
|
||||
} |
||||
showCommand() { |
||||
//
|
||||
} |
||||
createRectangle() { |
||||
this._createOverlay(OverlayTypes.Rectangle); |
||||
} |
||||
createPolygon() { |
||||
this._createOverlay(OverlayTypes.Polygon); |
||||
} |
||||
createPolyline() { |
||||
this._createOverlay(OverlayTypes.Polyline); |
||||
} |
||||
createCircle() { |
||||
this._createOverlay(OverlayTypes.Circle); |
||||
} |
||||
|
||||
private _createOverlay(type: OverlayTypes) { |
||||
this._store.dispatch(EditorAction.createOverlay(type)); |
||||
} |
||||
|
||||
private _bindOperations() { |
||||
this.saveGeoJSON = this.saveGeoJSON.bind(this); |
||||
this.createRectangle = this.createRectangle.bind(this); |
||||
this.createPolygon = this.createPolygon.bind(this); |
||||
this.createPolyline = this.createPolyline.bind(this); |
||||
this.createCircle = this.createCircle.bind(this); |
||||
} |
||||
|
||||
private _registerHotkeys() { |
||||
registerHotkey("s", { callback: this.saveGeoJSON, ctrl: true }); |
||||
registerHotkey("s", { |
||||
callback: this.saveProject, |
||||
ctrl: true, |
||||
alt: true, |
||||
}); |
||||
registerHotkey("s", { callback: this.saveTemp, alt: true }); |
||||
registerHotkey("c", { callback: this.clearEditor, alt: true }); |
||||
registerHotkey("o", { callback: this.openGeoJSON, alt: true }); |
||||
registerHotkey("o", { |
||||
callback: this.openProject, |
||||
alt: true, |
||||
ctrl: true, |
||||
}); |
||||
registerHotkey("p", { callback: this.showCommand, alt: true }); |
||||
registerHotkey("1", { |
||||
callback: this.createRectangle, |
||||
alt: true, |
||||
}); |
||||
registerHotkey("2", { callback: this.createPolygon, alt: true }); |
||||
registerHotkey("3", { |
||||
callback: this.createPolyline, |
||||
alt: true, |
||||
}); |
||||
registerHotkey("4", { callback: this.createCircle, alt: true }); |
||||
// registerHotkey("5", { callback: () => {}, alt: true });
|
||||
} |
||||
} |
@ -1,22 +1,28 @@
|
||||
import { createStore, Store } from "redux"; |
||||
import { createContext, Context } from "react"; |
||||
import { reducer, Action } from "./reducers"; |
||||
import { IEditorState } from "./type"; |
||||
import { createStore } from "redux"; |
||||
// import { createContext, Context } from "react";
|
||||
import { reducer } from "./reducers"; |
||||
import { IStore } from "./type"; |
||||
import { EditorAction } from "./actions"; |
||||
import { GlobelController } from "./GlobalController"; |
||||
|
||||
export * from "./type"; |
||||
export * from "./selectors"; |
||||
export * from "./constants"; |
||||
|
||||
type IStore = Store<IEditorState, Action>; |
||||
const store: IStore = createStore(reducer); |
||||
export { EditorAction }; |
||||
|
||||
(window as any).store = store; |
||||
declare global { |
||||
interface Window { |
||||
store: IStore; |
||||
} |
||||
} |
||||
|
||||
export { store }; |
||||
export const store: IStore = createStore(reducer); |
||||
|
||||
export { EditorAction }; |
||||
export const globalController = new GlobelController(store); |
||||
|
||||
window.store = store; |
||||
|
||||
export const StoreContext = createContext<IStore | null>( |
||||
null |
||||
) as Context<IStore>; |
||||
// export const StoreContext = createContext<IStore | null>(
|
||||
// null
|
||||
// ) as Context<IStore>;
|
||||
|
@ -0,0 +1,25 @@
|
||||
import { IMapState } from "../type"; |
||||
|
||||
export function getGeoJSON(state: IMapState): GeoJSON.FeatureCollection { |
||||
//
|
||||
const features: GeoJSON.Feature[] = []; |
||||
const { rectangles, polygons, polylines, circles } = state; |
||||
|
||||
rectangles.forEach((rect) => { |
||||
const path = rect.path!; |
||||
|
||||
features.push({ |
||||
type: "Feature", |
||||
geometry: { |
||||
type: "MultiPolygon", |
||||
coordinates: [[path]], |
||||
}, |
||||
properties: {}, |
||||
}); |
||||
}); |
||||
|
||||
return { |
||||
type: "FeatureCollection", |
||||
features, |
||||
}; |
||||
} |
@ -0,0 +1,36 @@
|
||||
import "@amap/amap-jsapi-types"; |
||||
declare global { |
||||
namespace AMapLoader { |
||||
const load: (config: any) => Promise<AMap.Map>; |
||||
} |
||||
namespace AMap { |
||||
interface IBaseEditor { |
||||
// new (map: AMap.Map): any;
|
||||
} |
||||
type MapOverlay = |
||||
| AMap.Rectangle |
||||
| AMap.Polygon |
||||
| AMap.Polyline |
||||
| AMap.Circle; |
||||
class BaseEditor implements IBaseEditor { |
||||
constructor(map: AMap.Map); |
||||
setTarget(target: any): void; |
||||
getTarget(): MapOverlay; |
||||
open(): void; |
||||
close(): void; |
||||
destroy(): void; |
||||
} |
||||
class PolygonEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class RectangleEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class PolylineEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class CircleEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
} |
||||
} |
@ -1,36 +1 @@
|
||||
declare global { |
||||
namespace AMapLoader { |
||||
const load: (config: any) => Promise<AMap.Map>; |
||||
} |
||||
namespace AMap { |
||||
interface IBaseEditor { |
||||
// new (map: AMap.Map): any;
|
||||
} |
||||
type MapOverlay = |
||||
| AMap.Rectangle |
||||
| AMap.Polygon |
||||
| AMap.Polyline |
||||
| AMap.Circle; |
||||
class BaseEditor implements IBaseEditor { |
||||
constructor(map: AMap.Map); |
||||
setTarget(target: any): void; |
||||
getTarget(): MapOverlay; |
||||
open(): void; |
||||
close(): void; |
||||
destroy(): void; |
||||
} |
||||
class PolygonEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class RectangleEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class PolylineEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
class CircleEditor extends BaseEditor { |
||||
//
|
||||
} |
||||
} |
||||
} |
||||
export * from "./enum"; |
||||
|
Loading…
Reference in new issue