File size: 8,115 Bytes
f56a29b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { useState, useCallback, type RefObject } from 'react';
import { useKeyboardStore } from '@/lib/store/keyboard';
import { useCanvasStore } from '@/lib/store';
import type { PPTElement } from '@/lib/types/slides';
import { getElementRange } from '@/lib/utils/element';

export function useMouseSelection(
  elementListRef: React.RefObject<PPTElement[]>,
  viewportRef: RefObject<HTMLElement | null>,
) {
  const [mouseSelectionVisible, setMouseSelectionVisible] = useState(false);
  const [mouseSelectionQuadrant, setMouseSelectionQuadrant] = useState(1);
  const [mouseSelection, setMouseSelection] = useState({
    top: 0,
    left: 0,
    width: 0,
    height: 0,
  });

  const canvasScale = useCanvasStore.use.canvasScale();
  const hiddenElementIdList = useCanvasStore.use.hiddenElementIdList();
  const setActiveElementIdList = useCanvasStore.use.setActiveElementIdList();
  const ctrlOrShiftKeyActive = useKeyboardStore((state) => state.ctrlOrShiftKeyActive());

  // Update mouse selection range
  const updateMouseSelection = useCallback(
    (e: React.MouseEvent) => {
      if (!viewportRef.current) return;

      let isMouseDown = true;
      const viewportRect = viewportRef.current.getBoundingClientRect();

      const minSelectionRange = 5;

      const startPageX = e.pageX;
      const startPageY = e.pageY;

      const left = (startPageX - viewportRect.x) / canvasScale;
      const top = (startPageY - viewportRect.y) / canvasScale;

      // Initialize selection start position and defaults
      setMouseSelection({
        top: top,
        left: left,
        width: 0,
        height: 0,
      });
      setMouseSelectionVisible(false);
      setMouseSelectionQuadrant(4);

      const handleMouseMove = (e: MouseEvent) => {
        if (!isMouseDown) return;

        const currentPageX = e.pageX;
        const currentPageY = e.pageY;

        const offsetWidth = (currentPageX - startPageX) / canvasScale;
        const offsetHeight = (currentPageY - startPageY) / canvasScale;

        const width = Math.abs(offsetWidth);
        const height = Math.abs(offsetHeight);

        if (width < minSelectionRange || height < minSelectionRange) return;

        // Determine mouse selection (movement) direction
        // Classified by quadrant position, e.g. bottom-right is quadrant 4
        let quadrant = 0;
        if (offsetWidth > 0 && offsetHeight > 0) quadrant = 4;
        else if (offsetWidth < 0 && offsetHeight < 0) quadrant = 2;
        else if (offsetWidth > 0 && offsetHeight < 0) quadrant = 1;
        else if (offsetWidth < 0 && offsetHeight > 0) quadrant = 3;

        // Update selection range
        setMouseSelection((prev) => ({
          ...prev,
          width: width,
          height: height,
        }));
        setMouseSelectionVisible(true);
        setMouseSelectionQuadrant(quadrant);
      };

      const handleMouseUp = () => {
        document.onmousemove = null;
        document.onmouseup = null;
        isMouseDown = false;

        // Check which canvas elements are within the mouse selection range and set them as selected
        let inRangeElementList: PPTElement[] = [];
        for (const element of elementListRef.current) {
          const mouseSelectionLeft = mouseSelection.left;
          const mouseSelectionTop = mouseSelection.top;
          const mouseSelectionWidth = mouseSelection.width;
          const mouseSelectionHeight = mouseSelection.height;

          const { minX, maxX, minY, maxY } = getElementRange(element);

          // Inclusion check differs for each quadrant direction
          let isInclude = false;
          if (ctrlOrShiftKeyActive) {
            if (mouseSelectionQuadrant === 4) {
              isInclude =
                maxX > mouseSelectionLeft &&
                minX < mouseSelectionLeft + mouseSelectionWidth &&
                maxY > mouseSelectionTop &&
                minY < mouseSelectionTop + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 2) {
              isInclude =
                maxX > mouseSelectionLeft - mouseSelectionWidth &&
                minX < mouseSelectionLeft - mouseSelectionWidth + mouseSelectionWidth &&
                maxY > mouseSelectionTop - mouseSelectionHeight &&
                minY < mouseSelectionTop - mouseSelectionHeight + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 1) {
              isInclude =
                maxX > mouseSelectionLeft &&
                minX < mouseSelectionLeft + mouseSelectionWidth &&
                maxY > mouseSelectionTop - mouseSelectionHeight &&
                minY < mouseSelectionTop - mouseSelectionHeight + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 3) {
              isInclude =
                maxX > mouseSelectionLeft - mouseSelectionWidth &&
                minX < mouseSelectionLeft - mouseSelectionWidth + mouseSelectionWidth &&
                maxY > mouseSelectionTop &&
                minY < mouseSelectionTop + mouseSelectionHeight;
            }
          } else {
            if (mouseSelectionQuadrant === 4) {
              isInclude =
                minX > mouseSelectionLeft &&
                maxX < mouseSelectionLeft + mouseSelectionWidth &&
                minY > mouseSelectionTop &&
                maxY < mouseSelectionTop + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 2) {
              isInclude =
                minX > mouseSelectionLeft - mouseSelectionWidth &&
                maxX < mouseSelectionLeft - mouseSelectionWidth + mouseSelectionWidth &&
                minY > mouseSelectionTop - mouseSelectionHeight &&
                maxY < mouseSelectionTop - mouseSelectionHeight + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 1) {
              isInclude =
                minX > mouseSelectionLeft &&
                maxX < mouseSelectionLeft + mouseSelectionWidth &&
                minY > mouseSelectionTop - mouseSelectionHeight &&
                maxY < mouseSelectionTop - mouseSelectionHeight + mouseSelectionHeight;
            } else if (mouseSelectionQuadrant === 3) {
              isInclude =
                minX > mouseSelectionLeft - mouseSelectionWidth &&
                maxX < mouseSelectionLeft - mouseSelectionWidth + mouseSelectionWidth &&
                minY > mouseSelectionTop &&
                maxY < mouseSelectionTop + mouseSelectionHeight;
            }
          }

          // Locked or hidden elements should not be selected even if within range
          if (isInclude && !element.lock && !hiddenElementIdList.includes(element.id))
            inRangeElementList.push(element);
        }

        // If grouped elements are in range, all members of the group must be in range to be selected
        inRangeElementList = inRangeElementList.filter((inRangeElement) => {
          if (inRangeElement.groupId) {
            const inRangeElementIdList = inRangeElementList.map(
              (inRangeElement) => inRangeElement.id,
            );
            const groupElementList = elementListRef.current.filter(
              (element) => element.groupId === inRangeElement.groupId,
            );
            return groupElementList.every((groupElement) =>
              inRangeElementIdList.includes(groupElement.id),
            );
          }
          return true;
        });
        const inRangeElementIdList = inRangeElementList.map((inRangeElement) => inRangeElement.id);
        setActiveElementIdList(inRangeElementIdList);

        setMouseSelectionVisible(false);
      };

      document.onmousemove = handleMouseMove;
      document.onmouseup = handleMouseUp;
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps -- Intentionally excludes mouseSelection state to avoid infinite re-creation
    [
      viewportRef,
      canvasScale,
      ctrlOrShiftKeyActive,
      hiddenElementIdList,
      elementListRef,
      setActiveElementIdList,
    ],
  );

  return {
    mouseSelection,
    mouseSelectionVisible,
    mouseSelectionQuadrant,
    updateMouseSelection,
  };
}