All files / polygon/src usePolygon.tsx

48% Statements 12/25
7.69% Branches 1/13
16.66% Functions 1/6
54.54% Lines 12/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                    1x 1x 1x 1x   1x                       1x                       1x             1x 1x                   1x   1x                 1x              
import { useState, useEffect } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { noop, useEnableProperties, useProperties, useVisiable, useEventProperties } from '@uiw/react-baidu-map-utils';
import { PolygonProps } from './';
 
export interface UsePolygon extends PolygonProps {}
 
export function usePolygon(props = {} as UsePolygon) {
  const {
    strokeColor,
    fillColor,
    strokeWeight,
    strokeOpacity,
    fillOpacity,
    strokeStyle,
    enableMassClear,
    enableEditing,
    enableClicking,
  } = props;
  const { map } = useMapContext();
  const [polygon, setPolygon] = useState<BMap.Polygon>();
  const [path, setPath] = useState(props.path || []);
 
  const opts = {
    strokeColor,
    fillColor,
    strokeWeight,
    strokeOpacity,
    fillOpacity,
    strokeStyle,
    enableMassClear,
    enableEditing,
    enableClicking,
  };
 
  useEffect(() => {
    if (!BMap || !map) return noop;
    const points = (path || []).map((item) => new BMap.Point(item.lng, item.lat));
    const instance = new BMap.Polygon(points, opts);
    map.addOverlay(instance);
    setPolygon(instance);
    return function () {
      map.removeOverlay(instance);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map]);
 
  useEffect(() => {
    if (path && polygon) {
      const points = path.map((item) => new BMap.Point(item.lng, item.lat));
      polygon.setPath(points);
    }
  }, [polygon, path]);
 
  useVisiable(polygon!, props);
  useEventProperties<BMap.Polygon, UsePolygon>(polygon!, props, [
    'Click',
    'DoubleClick',
    'MouseDown',
    'MouseUp',
    'MouseOut',
    'MouseOver',
    'Remove',
    'LineUpdate',
  ]);
  useEnableProperties<BMap.Polygon, UsePolygon>(polygon!, props, ['Editing', 'MassClear']);
  // PositionAt
  useProperties<BMap.Polygon, PolygonProps>(polygon!, props, [
    'StrokeColor',
    'StrokeOpacity',
    'FillColor',
    'FillOpacity',
    'StrokeWeight',
    'StrokeStyle',
  ]);
 
  return {
    polygon,
    setPolygon,
    path,
    setPath,
  };
}