File size: 2,128 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as React from 'react';
import isVisible from 'rc-util/lib/Dom/isVisible';

import Marker from './Marker';

export interface MarkersProps {
  targetClassName: string | null;
  containerRef: React.RefObject<HTMLDivElement | null>;
}

interface RectType {
  left: number;
  top: number;
  width: number;
  height: number;
  visible: boolean;
}

const Markers: React.FC<MarkersProps> = (props) => {
  const { targetClassName, containerRef } = props;

  const [rectList, setRectList] = React.useState<RectType[]>([]);

  // ======================== Effect =========================
  React.useEffect(() => {
    const targetElements = targetClassName
      ? Array.from(containerRef.current?.querySelectorAll<HTMLElement>(`.${targetClassName}`) || [])
      : [];

    const containerRect = containerRef.current?.getBoundingClientRect() || ({} as DOMRect);

    const targetRectList = targetElements.map<RectType>((targetElement) => {
      const rect = targetElement.getBoundingClientRect();
      return {
        left: rect.left - (containerRect.left || 0),
        top: rect.top - (containerRect.top || 0),
        width: rect.width,
        height: rect.height,
        visible: isVisible(targetElement),
      };
    });

    setRectList((prev) => {
      return Array.from({ length: Math.max(prev.length, targetRectList.length) }).map<RectType>(
        (_, index) => {
          const prevRect = prev[index] || {};
          const nextRect = targetRectList[index] || {};
          return {
            left: nextRect.left ?? prevRect.left ?? 0,
            top: nextRect.top ?? prevRect.top ?? 0,
            width: nextRect.width ?? prevRect.width ?? 0,
            height: nextRect.height ?? prevRect.height ?? 0,
            visible: !!nextRect.visible,
          };
        },
      );
    });
  }, [targetClassName]);

  // ======================== Render =========================
  return (
    <>
      {rectList.map((rect, index) => {
        const key = `key-${index}`;
        return <Marker rect={rect} key={key} data-id={key} primary={index === 0} />;
      })}
    </>
  );
};

export default Markers;