Browse Source

使用overlays不进行分类

master
Cmen 3 years ago
parent
commit
33fdd4f40e
  1. 25
      src/editor/Catalog/index.tsx
  2. 5
      src/editor/Property/index.tsx
  3. 50
      src/map/MapEditor.ts
  4. 9
      src/map/type.ts
  5. 11
      src/store/actions/index.ts
  6. 5
      src/store/initState.ts
  7. 17
      src/store/reducers/map/index.ts

25
src/editor/Catalog/index.tsx

@ -6,6 +6,7 @@ import { IOverlay } from "@map";
import "./style.less"; import "./style.less";
import classNames from "classnames"; import classNames from "classnames";
import { OverlayTypes } from "../../types/enum";
const { Panel } = Collapse; const { Panel } = Collapse;
@ -36,8 +37,28 @@ const OverlayList = (props: OverlayListProps) => {
}; };
const Catalog = () => { const Catalog = () => {
const { rectangles, polygons, polylines, circles } = const { overlays } = useSelector(mapOptionsSelector);
useSelector(mapOptionsSelector); const rectangles: IOverlay[] = [],
polygons: IOverlay[] = [],
polylines: IOverlay[] = [],
circles: IOverlay[] = [];
overlays.forEach((overlay) => {
switch (overlay.type) {
case OverlayTypes.Rectangle:
rectangles.push(overlay);
break;
case OverlayTypes.Polygon:
polygons.push(overlay);
break;
case OverlayTypes.Polyline:
polylines.push(overlay);
break;
default:
circles.push(overlay);
}
});
return ( return (
<Collapse <Collapse
defaultActiveKey={["1", "2", "3", "4"]} defaultActiveKey={["1", "2", "3", "4"]}

5
src/editor/Property/index.tsx

@ -1,4 +1,9 @@
import { useSelector } from "react-redux";
import { selectedIdsSelector, mapOptionsSelector } from "@store";
const Property = () => { const Property = () => {
const selectedIds = useSelector(selectedIdsSelector);
// const { } = useSelector(mapOptionsSelector);
return <h1></h1>; return <h1></h1>;
}; };

50
src/map/MapEditor.ts

@ -16,7 +16,6 @@ import { IMapOptions, IMapEditor } from "./type";
import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants"; import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants";
import { EventTypes, OverlayTypes } from "../types/enum"; import { EventTypes, OverlayTypes } from "../types/enum";
import { Position } from "geojson";
type AMapOverlayEditor = type AMapOverlayEditor =
| AMap.RectangleEditor | AMap.RectangleEditor
@ -95,16 +94,22 @@ export class MapEditor extends Emitter implements IMapEditor {
getEditorByType(overlayType: OverlayTypes) { getEditorByType(overlayType: OverlayTypes) {
return this.overlayEditors.find( return this.overlayEditors.find(
(editor) => editor.getType() === overlayType (editor) => editor.getType() === overlayType
); )!;
} }
importProject(options: IMapOptions) { importProject(options: IMapOptions) {
this.clearOverlays(); this.clearOverlays();
const { rectangles, polygons, polylines, circles } = options; const { overlays } = options;
const rectangleEditor = this.getEditorByType(OverlayTypes.Rectangle);
const polygonEditor = this.getEditorByType(OverlayTypes.Polygon); const overlayEditors: Record<
const polylineEditor = this.getEditorByType(OverlayTypes.Polyline); OverlayTypes,
const circleEditor = this.getEditorByType(OverlayTypes.Circle); BaseOverlayEditor<AMapOverlayEditor>
> = {
[OverlayTypes.Rectangle]: this.getEditorByType(OverlayTypes.Rectangle),
[OverlayTypes.Polygon]: this.getEditorByType(OverlayTypes.Polygon),
[OverlayTypes.Polyline]: this.getEditorByType(OverlayTypes.Polyline),
[OverlayTypes.Circle]: this.getEditorByType(OverlayTypes.Circle),
};
if (options.center) { if (options.center) {
const [lng, lat] = options.center; const [lng, lat] = options.center;
@ -114,11 +119,9 @@ export class MapEditor extends Emitter implements IMapEditor {
this.map.setZoom(options.zoom); this.map.setZoom(options.zoom);
} }
const addOverlay = ( overlays.forEach((overlay) => {
id: string, const { type, id } = overlay;
target: AMap.MapOverlay, const target = overlayEditors[type].build(overlay);
type: OverlayTypes
) => {
this.overlayMap[id] = { this.overlayMap[id] = {
type, type,
target, target,
@ -127,28 +130,7 @@ export class MapEditor extends Emitter implements IMapEditor {
type === OverlayTypes.Polyline ? PolylineOptions : PolygonOptions type === OverlayTypes.Polyline ? PolylineOptions : PolygonOptions
); );
this.map.add(target); this.map.add(target);
}; });
rectangles.forEach((rect) =>
addOverlay(rect.id, rectangleEditor!.build(rect), OverlayTypes.Rectangle)
);
polygons.forEach((polygon) =>
addOverlay(
polygon.id,
polygonEditor!.build(polygon),
OverlayTypes.Polygon
)
);
polylines.forEach((polyline) =>
addOverlay(
polyline.id,
polylineEditor!.build(polyline),
OverlayTypes.Polyline
)
);
circles.forEach((circle) =>
addOverlay(circle.id, circleEditor!.build(circle), OverlayTypes.Circle)
);
} }
importGeoJSON(geojson: GeoJSON.FeatureCollection): void { importGeoJSON(geojson: GeoJSON.FeatureCollection): void {

9
src/map/type.ts

@ -20,13 +20,12 @@ export interface IOverlay {
lngLat?: GeoJSON.Position; lngLat?: GeoJSON.Position;
path?: GeoJSON.Position[]; path?: GeoJSON.Position[];
radius?: number; radius?: number;
backgroundImage?: string;
category?: string;
height?: string;
} }
export interface IMapOptions { export interface IMapOptions {
polygons: IOverlay[]; overlays: IOverlay[];
polylines: IOverlay[];
circles: IOverlay[];
rectangles: IOverlay[];
selectedIds?: string[];
center?: GeoJSON.Position; center?: GeoJSON.Position;
zoom?: number; zoom?: number;
} }

11
src/store/actions/index.ts

@ -50,9 +50,14 @@ export class EditorAction {
setInterval(() => { setInterval(() => {
const { mapOptions } = this; const { mapOptions } = this;
mapOptions.center = this.mapEditor?.getCenter(); sessionStorage.setItem(
mapOptions.zoom = this.mapEditor?.getZoom(); "fine-geojson",
sessionStorage.setItem("fine-geojson", JSON.stringify(mapOptions)); JSON.stringify({
...mapOptions,
center: this.mapEditor?.getCenter(),
zoom: this.mapEditor?.getZoom(),
})
);
}, 10000); }, 10000);
} }
// ---------- public methods ------------- // ---------- public methods -------------

5
src/store/initState.ts

@ -5,9 +5,6 @@ export const initState: IEditorState = {
overlayType: null, overlayType: null,
selectedIds: [], selectedIds: [],
map: { map: {
polygons: [], overlays: [],
polylines: [],
circles: [],
rectangles: [],
}, },
}; };

17
src/store/reducers/map/index.ts

@ -18,12 +18,10 @@ export function clearOverlays(state = initState) {
export function deleteOverlays(state = initState, payload: any) { export function deleteOverlays(state = initState, payload: any) {
const { selectedIds } = state; const { selectedIds } = state;
const [id] = selectedIds!; const [id] = selectedIds!;
const filterFunc = (overlay: IOverlay) => overlay.id !== id;
return produce(state, (draft) => { return produce(state, (draft) => {
draft.map.rectangles = draft.map.rectangles.filter(filterFunc); draft.map.overlays = draft.map.overlays.filter(
draft.map.polygons = draft.map.polygons.filter(filterFunc); (overlay: IOverlay) => overlay.id !== id
draft.map.polylines = draft.map.polylines.filter(filterFunc); );
draft.map.circles = draft.map.circles.filter(filterFunc);
draft.selectedIds = []; draft.selectedIds = [];
}); });
} }
@ -34,14 +32,7 @@ export function finishEditOverlay(state = initState, payload: any) {
const { type, id } = overlay; const { type, id } = overlay;
// todo: uniqueName. // todo: uniqueName.
overlay.name = OverlayNamePrefixs[type] + overlay.id; overlay.name = OverlayNamePrefixs[type] + overlay.id;
const overlays = const { overlays } = draft.map;
type === OverlayTypes.Rectangle
? draft.map.rectangles
: type === OverlayTypes.Circle
? draft.map.circles
: type === OverlayTypes.Polygon
? draft.map.polygons
: draft.map.polylines;
const existed = overlays.find((overlay) => overlay.id === id); const existed = overlays.find((overlay) => overlay.id === id);
if (existed) { if (existed) {
overlays[overlays.indexOf(existed)] = overlay; overlays[overlays.indexOf(existed)] = overlay;

Loading…
Cancel
Save