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