import { Feature, FeatureCollection, MultiPolygon, union } from "@turf/turf"; import EventEmitter from "eventemitter3"; export type PolygonFeatureType = Feature< MultiPolygon, { id: string; name: string; color?: string; active?: boolean; [key: string]: any; } >; class PolygonStore extends EventEmitter { features: PolygonFeatureType[] = []; constructor() { super(); } updateFeature(feature: PolygonFeatureType) { this.features.forEach((f, i) => { if (f.properties.id == feature.properties.id) { this.features[i] = feature; } }); this.emit("updateFeature"); } toggleActiveFeature(feature: PolygonFeatureType) { this.features.forEach((f) => { if (f.properties.id == feature.properties.id) { f.properties.active = !f.properties.active; } }); this.emit("updateFeature"); } unactiveAllFeature() { this.features.forEach((f) => { f.properties.active = false; }); this.emit("updateFeature"); } getComboedMultiPolygon() { return this.features.reduce((result: PolygonFeatureType, f: PolygonFeatureType) => { return union(result, f) as PolygonFeatureType; }); // const multipolygon: FeatureCollection = combine(featureCollection(this.features)); // return multipolygon.features as PolygonFeatureType; } reset() { this.features = []; } } export default PolygonStore;