All files / polyline/src usePolyline.tsx

53.84% Statements 14/26
17.64% Branches 3/17
28.57% Functions 2/7
58.33% Lines 14/24

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 85                                  1x 1x 1x 1x     1x                   1x               1x 1x               1x             1x 1x                   1x   1x             1x              
import { useState, useEffect, useMemo } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { useEnableProperties, useProperties, useVisiable, useEventProperties } from '@uiw/react-baidu-map-utils';
import { PolylineProps } from './';
 
export interface UsePolyline extends PolylineProps {}
 
export function usePolyline(props = {} as UsePolyline) {
  const {
    strokeColor,
    strokeWeight,
    strokeOpacity,
    strokeStyle,
    enableMassClear,
    enableEditing,
    enableClicking,
    icons,
  } = props;
  const { map } = useMapContext();
  const [polyline, setPolyline] = useState<BMap.Polyline>();
  const [path, setPath] = useState(props.path || []);
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const opts = {
    strokeColor,
    strokeWeight,
    strokeOpacity,
    strokeStyle,
    enableMassClear,
    enableEditing,
    enableClicking,
    icons,
  };
  useEffect(() => {
    return () => {
      if (polyline && map) {
        map.removeOverlay(polyline);
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map]);
  useMemo(() => {
    Iif (map && !polyline) {
      const points = (props.path || []).map((item) => new BMap.Point(item.lng, item.lat));
      const instance = new BMap.Polyline(points, opts);
      map.addOverlay(instance);
      setPolyline(instance);
    }
  }, [map, opts, polyline, props.path]);
 
  useEffect(() => {
    if (path && polyline) {
      const points = path.map((item) => new BMap.Point(item.lng, item.lat));
      polyline.setPath(points);
    }
  }, [polyline, path]);
 
  useVisiable(polyline!, props);
  useEventProperties<BMap.Polyline, UsePolyline>(polyline!, props, [
    'Click',
    'DblClick',
    'MouseDown',
    'MouseUp',
    'MouseOut',
    'MouseOver',
    'Remove',
    'LineUpdate',
  ]);
  useEnableProperties<BMap.Polyline, UsePolyline>(polyline!, props, ['Editing', 'MassClear']);
  // PositionAt
  useProperties<BMap.Polyline, UsePolyline>(polyline!, props, [
    'StrokeColor',
    'StrokeOpacity',
    'StrokeWeight',
    'StrokeStyle',
  ]);
 
  return {
    polyline,
    setPolyline,
    path,
    setPath,
  };
}