|
|
|
@ -18,7 +18,7 @@ import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants";
|
|
|
|
|
import { Command, EventTypes, OverlayTypes } from "../types/enum"; |
|
|
|
|
|
|
|
|
|
let uuid = 0; |
|
|
|
|
const getUuid = () => ++uuid; |
|
|
|
|
const getUuid = () => ++uuid + ""; |
|
|
|
|
|
|
|
|
|
type AMapOverlayEditor = |
|
|
|
|
| AMap.RectangleEditor |
|
|
|
@ -39,6 +39,8 @@ export class MapEditor extends Emitter {
|
|
|
|
|
private _currentOverlayEditor: |
|
|
|
|
| BaseOverlayEditor<AMapOverlayEditor> |
|
|
|
|
| undefined; |
|
|
|
|
private _selectedIds: string[] = []; |
|
|
|
|
private _editorStatus: "editing" | "creating" | null = null; |
|
|
|
|
constructor(dom: HTMLDivElement) { |
|
|
|
|
super(); |
|
|
|
|
this.dom = dom; |
|
|
|
@ -64,10 +66,17 @@ export class MapEditor extends Emitter {
|
|
|
|
|
this._map = new AMap.Map(this.dom); |
|
|
|
|
this.initEditors(); |
|
|
|
|
// Space的key是空字符串, 这就离谱.
|
|
|
|
|
registerHotkey(" ", { callback: this.finishCreateOverlay.bind(this) }); |
|
|
|
|
registerHotkey(" ", { callback: this._finishEditOverlay.bind(this) }); |
|
|
|
|
registerHotkey("e", { |
|
|
|
|
callback: this._editSelectedOverlay.bind(this), |
|
|
|
|
alt: true, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
update(mapState: IMapState) { |
|
|
|
|
if (this._editorStatus != null) { |
|
|
|
|
this._finishEditOverlay(); |
|
|
|
|
} |
|
|
|
|
const { command } = mapState; |
|
|
|
|
switch (command) { |
|
|
|
|
case Command.CreateOverlay: |
|
|
|
@ -78,30 +87,50 @@ export class MapEditor extends Emitter {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_createOverlay(mapState: IMapState) { |
|
|
|
|
const { overlayType } = mapState; |
|
|
|
|
this.finishCreateOverlay(); |
|
|
|
|
this._currentOverlayEditor = this.overlayEditors.find( |
|
|
|
|
initEditors() { |
|
|
|
|
const { map } = this; |
|
|
|
|
this.overlayEditors = [ |
|
|
|
|
new RectangleEditor(map), |
|
|
|
|
new PolygonEditor(map), |
|
|
|
|
new PolylineEditor(map), |
|
|
|
|
new CircleEditor(map), |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getEditorByType(overlayType: OverlayTypes) { |
|
|
|
|
return this.overlayEditors.find( |
|
|
|
|
(editor) => editor.getType() === overlayType |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_createOverlay(mapState: IMapState) { |
|
|
|
|
const { overlayType } = mapState; |
|
|
|
|
this._currentOverlayEditor = this.getEditorByType(overlayType!); |
|
|
|
|
this._currentOverlayEditor?.create(); |
|
|
|
|
this._editorStatus = "creating"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_selectOverlays(ids?: string[]) { |
|
|
|
|
if (!ids?.length) return; |
|
|
|
|
this._selectedIds = ids; |
|
|
|
|
ids.forEach((id) => { |
|
|
|
|
const { target } = this.overlayMap[id]; |
|
|
|
|
target.setOptions(SelectedOptions); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
finishCreateOverlay() { |
|
|
|
|
_finishEditOverlay() { |
|
|
|
|
if (this._currentOverlayEditor == null) return; |
|
|
|
|
const target = this._currentOverlayEditor.finish(); |
|
|
|
|
if (target == null) return; |
|
|
|
|
const id = getUuid(); |
|
|
|
|
const type = this._currentOverlayEditor.getType(); |
|
|
|
|
const isCreatingOverlay = this._editorStatus === "creating"; |
|
|
|
|
let id = getUuid(); |
|
|
|
|
if (isCreatingOverlay) { |
|
|
|
|
this.overlayMap[id] = { type, target }; |
|
|
|
|
} else { |
|
|
|
|
id = this._selectedIds[0]; |
|
|
|
|
} |
|
|
|
|
const evt: any = { id, type }; |
|
|
|
|
if (type === OverlayTypes.Circle) { |
|
|
|
|
const circle = target as AMap.Circle; |
|
|
|
@ -113,16 +142,16 @@ export class MapEditor extends Emitter {
|
|
|
|
|
target.setOptions( |
|
|
|
|
type === OverlayTypes.Polyline ? PolylineOptions : PolygonOptions |
|
|
|
|
); |
|
|
|
|
this.emit(EventTypes.FinishCreateOverlay, evt); |
|
|
|
|
this.emit(EventTypes.FinishEditOverlay, evt); |
|
|
|
|
this._editorStatus = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
initEditors() { |
|
|
|
|
const { map } = this; |
|
|
|
|
this.overlayEditors = [ |
|
|
|
|
new RectangleEditor(map), |
|
|
|
|
new PolygonEditor(map), |
|
|
|
|
new PolylineEditor(map), |
|
|
|
|
new CircleEditor(map), |
|
|
|
|
]; |
|
|
|
|
_editSelectedOverlay() { |
|
|
|
|
if (this._selectedIds.length !== 1) return; |
|
|
|
|
const [id] = this._selectedIds; |
|
|
|
|
const { target, type } = this.overlayMap[id]; |
|
|
|
|
this._currentOverlayEditor = this.getEditorByType(type); |
|
|
|
|
this._currentOverlayEditor?.edit(target); |
|
|
|
|
this._editorStatus = "editing"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|