From 6114b2aa5ae9136b107a26d2c18cf126490958e0 Mon Sep 17 00:00:00 2001 From: Cmen <1176967590@qq.com> Date: Sun, 6 Mar 2022 09:43:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=96=E6=B6=88=E9=80=89?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/map/MapEditor.ts | 4 ++-- src/map/type.ts | 2 +- src/store/actions/StoreAction.ts | 2 +- src/store/actions/index.ts | 12 +++++++++--- src/store/reducers/index.ts | 8 +++++--- 5 files changed, 18 insertions(+), 10 deletions(-) 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; }); }