All files / drawing-manager/src useDrawingManager.tsx

57.14% Statements 16/28
43.47% Branches 10/23
25% Functions 2/8
57.14% Lines 16/28

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 86 87 88 89 90 91 92 93 94 95 96                                      2x 2x   2x 2x 2x 2x 2x                         2x   2x               2x                                 2x 1x 1x 1x                     2x                 2x            
import { useState, useMemo } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { requireScript, requireCss, useEventProperties } from '@uiw/react-baidu-map-utils';
import { DrawingManagerProps } from '.';
 
export interface UseDrawingManager extends DrawingManagerProps {}
 
export function useDrawingManager(props = {} as UseDrawingManager) {
  const {
    isOpen,
    drawingMode,
    enableDrawingTool,
    enableCalculate,
    drawingToolOptions,
    markerOptions,
    circleOptions,
    polylineOptions,
    polygonOptions,
    rectangleOptions,
  } = props;
  const { map } = useMapContext();
 
  const [drawingManager, setDrawingManager] = useState<BMapLib.DrawingManager>();
  const libSDK = window.BMapLib;
  const [bMapLib, setBMapLib] = useState<typeof BMapLib>(libSDK);
  const [loadMapLib, setLoadBMapLib] = useState(false || !!libSDK);
  const opts = {
    isOpen,
    drawingMode,
    enableDrawingTool,
    enableCalculate,
    drawingToolOptions,
    markerOptions,
    circleOptions,
    polylineOptions,
    polygonOptions,
    rectangleOptions,
  } as BMapLib.DrawingManagerOptions;
 
  useMemo(() => {
    // 如果第一次加载,会执行下面的
    Iif (map && bMapLib && !drawingManager) {
      if (bMapLib.DrawingManager) {
        const instance = new BMapLib.DrawingManager(map, opts);
        setDrawingManager(instance);
      }
    }
 
    // 如果 bMapLib 已经加载过,会执行下面的
    Iif (map && bMapLib && !bMapLib.DrawingManager) {
      requireCss('//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css').then(() => {});
 
      requireScript('//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js')
        .then(() => {
          if (window.BMapLib) {
            const newMapLib = Object.assign(window.BMapLib, bMapLib);
            setBMapLib(newMapLib);
 
            const instance = new BMapLib.DrawingManager(map, opts);
            setDrawingManager(instance);
          }
        })
        .catch(() => {});
    }
 
    // 如果第一次加载,会执行下面的
    if (!bMapLib && !loadMapLib) {
      setLoadBMapLib(true);
      requireCss('//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css').then(() => {});
      requireScript('//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js')
        .then(() => {
          if (window.BMapLib) {
            setBMapLib(window.BMapLib);
          }
        })
        .catch(() => {});
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map, loadMapLib, bMapLib, drawingManager]);
 
  useEventProperties<BMapLib.DrawingManager, UseDrawingManager>(drawingManager!, props, [
    'CircleComplete',
    'MarkerComplete',
    'OverlayComplete',
    'PolygonComplete',
    'PolylineComplete',
    'RectangleComplete',
  ]);
 
  return {
    drawingManager,
    setDrawingManager,
    BMapLib: bMapLib,
  };
}