/** * * AnnotationLayer * */ import { LayerGroup as LeafletLayerGroup, withLeaflet, FeatureGroup } from 'react-leaflet'; import React, { useCallback, useRef } from 'react'; import { SupportedShapes, Annotations, SureContextProps } from 'types'; import { DomEvent } from 'leaflet'; import EditControl from './EditControl'; import { useDispatch } from 'react-redux'; import './styles.css'; import Annotation from 'types/Annotation'; import AnnotationPolygon from './AnnotationPolygon'; import AnnotationHighlight from './AnnotationHighlight'; import AnnotationCircle from './AnnotationCircle'; import { AnnotationShapes } from './types'; import { editAnnotationAction } from 'containers/Editor/actions'; import AnnotationRectangle from './AnnotationRectangle'; import { fromJS } from 'utils/geo'; interface AnnotationLayerProps { data: Annotations; selectedAnnotations: Annotations; leaflet; onLayerClick?: (annotation: Annotation) => any; onCreated?: any; editable?: boolean; } const GuessComponent: React.SFC = (props) => { const {annotation, onClick} = props; const isInvisible = annotation.properties.type === SupportedShapes.invisible; const onLayerClick = useCallback((event) => { DomEvent.stopPropagation(event); return onClick && onClick(annotation); }, [onClick, annotation]); // readonly mode, we display a "reverted" shape to highlight the zone of interest if (!props.editable && props.selected && !isInvisible) { return ; } switch (annotation.geometry.type) { case 'Point': return ; case 'Polygon': case 'MultiPolygon': if (annotation.properties.type === SupportedShapes.rectangle) { return ; } return ; } return ; }; const AnnotationLayer = withLeaflet((props) => { const map = props.leaflet.map; const dispatch = useDispatch(); const containerRef = useRef(null); const onEdit = useCallback(() => { if (props.selectedAnnotations && containerRef.current && props.onCreated) { containerRef.current.leafletElement.getLayers().forEach((layer: any) => { const annotation = props.selectedAnnotations.find( (annotation) => annotation.properties.id === layer.options.properties.id, ); if (annotation) { const feature = layer.toGeoJSON(); if (annotation.properties.type === SupportedShapes.circle) { dispatch(editAnnotationAction( annotation, annotation.set( 'geometry', fromJS(feature.geometry), ).setIn( ['properties', 'radius'], (layer as L.CircleMarker).getRadius(), ), )); } return dispatch(editAnnotationAction( annotation, fromJS(feature).set('properties', annotation.properties), )); } return; }); return; } }, [props.selectedAnnotations, props.onCreated, containerRef.current]); const onCreate = useCallback((event) => { if (event.layerType === SupportedShapes.circle) { const feature = event.layer.toGeoJSON(); feature.properties.radius = (event.layer as L.CircleMarker).getRadius(); event.layer.remove(map); return props.onCreated(feature); } const feature = event.layer.toGeoJSON(); feature.properties.type = event.layerType; event.layer.remove(map); return props.onCreated(feature); }, []); return ( {props.editable && } {props.data.map((annotation) => { const selected = props.selectedAnnotations.contains(annotation); return ( ); })} ); }); export default AnnotationLayer;