diff --git a/src/map/MapEditor.ts b/src/map/MapEditor.ts index aa8c949..1228cfe 100644 --- a/src/map/MapEditor.ts +++ b/src/map/MapEditor.ts @@ -152,11 +152,11 @@ export class MapEditor extends Emitter implements IMapEditor { } selectOverlays(ids?: string[]) { - if (!ids?.length) return; - this.selectedIds.forEach((id) => { + this.selectedIds?.forEach((id) => { const { target, type } = this.overlayMap[id]; target.setOptions(getOverlayOptions(type)); }); + if (ids == null) return; this.selectedIds = ids; ids.forEach((id) => { const { target } = this.overlayMap[id]; diff --git a/src/map/type.ts b/src/map/type.ts index 7f3e095..0b5c122 100644 --- a/src/map/type.ts +++ b/src/map/type.ts @@ -6,7 +6,7 @@ export interface IMapEditor { createOverlay(type: OverlayTypes): void; importProject(options: IMapOptions): void; importGeoJSON(geojson: GeoJSON.FeatureCollection): void; - selectOverlays(ids: string[]): void; + selectOverlays(ids?: string[]): void; } export interface IOverlay { diff --git a/src/store/actions/StoreAction.ts b/src/store/actions/StoreAction.ts index a1f30e6..c3180b3 100644 --- a/src/store/actions/StoreAction.ts +++ b/src/store/actions/StoreAction.ts @@ -22,7 +22,7 @@ export const StoreAction = { payload: overlay, }; }, - selectOverlay(id: string) { + selectOverlay(id?: string) { return { type: ActionTypes.SelectOverlay, payload: id, diff --git a/src/store/actions/index.ts b/src/store/actions/index.ts index c7b000e..4a747d9 100644 --- a/src/store/actions/index.ts +++ b/src/store/actions/index.ts @@ -7,7 +7,7 @@ import { getGeoJSON } from "../utils"; import { OverlayTypes, EventTypes } from "@types"; import { IOverlay, IMapOptions } from "@map"; import { StoreAction } from "./StoreAction"; -import { IStore, IEditorState } from "../type"; +import { IStore } from "../type"; import { initState } from "../initState"; export { ActionTypes } from "./StoreAction"; @@ -89,8 +89,14 @@ export class EditorAction { } selectOverlay(id: string) { - this.mapEditor?.selectOverlays([id]); - this.dispatch(StoreAction.selectOverlay(id)); + const { selectedIds } = this.store.getState(); + if (selectedIds.indexOf(id) >= 0) { + this.mapEditor?.selectOverlays(); + this.dispatch(StoreAction.selectOverlay()); + } else { + this.mapEditor?.selectOverlays([id]); + this.dispatch(StoreAction.selectOverlay(id)); + } } saveProject() { diff --git a/src/store/reducers/index.ts b/src/store/reducers/index.ts index aab50b9..f2c2175 100644 --- a/src/store/reducers/index.ts +++ b/src/store/reducers/index.ts @@ -23,10 +23,12 @@ function replaceState(state = initState, payload: IEditorState) { return payload; } -export function selectOverlay(state = initState, payload: any) { - const id = payload as string; +export function selectOverlay(state = initState, payload: string | undefined) { + // const id = payload as string; + // todo: 考虑支持多选 + const ids = payload ? [payload] : []; return produce(state, (draft) => { - draft.selectedIds = [id]; + draft.selectedIds = ids; }); }