File size: 4,911 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
import { useMemo } from 'react';
import { useCanvasStore } from '@/lib/store';
import type { PPTShapeElement } from '@/lib/types/slides';
import type { OperateResizeHandlers } from '@/lib/types/edit';
import { SHAPE_PATH_FORMULAS } from '@/configs/shapes';
import { useCommonOperate } from '../hooks/useCommonOperate';
import { RotateHandler } from './RotateHandler';
import { ResizeHandler } from './ResizeHandler';
import { BorderLine } from './BorderLine';

interface ShapeElementOperateProps {
  readonly elementInfo: PPTShapeElement;
  readonly handlerVisible: boolean;
  readonly rotateElement: (e: React.MouseEvent, element: PPTShapeElement) => void;
  readonly scaleElement: (
    e: React.MouseEvent,
    element: PPTShapeElement,
    command: OperateResizeHandlers,
  ) => void;
  readonly moveShapeKeypoint: (
    e: React.MouseEvent,
    element: PPTShapeElement,
    index: number,
  ) => void;
}

export function ShapeElementOperate({
  elementInfo,
  handlerVisible,
  rotateElement,
  scaleElement,
  moveShapeKeypoint,
}: ShapeElementOperateProps) {
  const canvasScale = useCanvasStore.use.canvasScale();

  const scaleWidth = useMemo(
    () => elementInfo.width * canvasScale,
    [elementInfo.width, canvasScale],
  );
  const scaleHeight = useMemo(
    () => elementInfo.height * canvasScale,
    [elementInfo.height, canvasScale],
  );
  const { resizeHandlers, borderLines } = useCommonOperate(scaleWidth, scaleHeight);

  const keypoints = useMemo(() => {
    if (!elementInfo.pathFormula || elementInfo.keypoints === undefined) return [];
    const pathFormula = SHAPE_PATH_FORMULAS[elementInfo.pathFormula];

    return elementInfo.keypoints.map((keypoint, index) => {
      const getBaseSize = pathFormula.getBaseSize![index];
      const relative = pathFormula.relative![index];
      const keypointPos = getBaseSize(elementInfo.width, elementInfo.height) * keypoint;

      let styles: React.CSSProperties = {};
      if (relative === 'left') styles = { left: keypointPos * canvasScale + 'px' };
      else if (relative === 'right')
        styles = {
          left: (elementInfo.width - keypointPos) * canvasScale + 'px',
        };
      else if (relative === 'center')
        styles = {
          left: ((elementInfo.width - keypointPos) / 2) * canvasScale + 'px',
        };
      else if (relative === 'top') styles = { top: keypointPos * canvasScale + 'px' };
      else if (relative === 'bottom')
        styles = {
          top: (elementInfo.height - keypointPos) * canvasScale + 'px',
        };
      else if (relative === 'left_bottom')
        styles = {
          left: keypointPos * canvasScale + 'px',
          top: elementInfo.height * canvasScale + 'px',
        };
      else if (relative === 'right_bottom')
        styles = {
          left: (elementInfo.width - keypointPos) * canvasScale + 'px',
          top: elementInfo.height * canvasScale + 'px',
        };
      else if (relative === 'top_right')
        styles = {
          left: elementInfo.width * canvasScale + 'px',
          top: keypointPos * canvasScale + 'px',
        };
      else if (relative === 'bottom_right')
        styles = {
          left: elementInfo.width * canvasScale + 'px',
          top: (elementInfo.height - keypointPos) * canvasScale + 'px',
        };

      return {
        keypoint,
        styles,
      };
    });
  }, [elementInfo, canvasScale]);

  return (
    <div className="shape-element-operate">
      {borderLines.map((line) => (
        <BorderLine
          key={line.type}
          type={line.type}
          style={line.style}
          className="operate-border-line"
        />
      ))}
      {handlerVisible && (
        <>
          {resizeHandlers.map((point) => (
            <ResizeHandler
              key={point.direction}
              type={point.direction}
              rotate={elementInfo.rotate}
              style={point.style}
              className="operate-resize-handler"
              onMouseDown={(e) => {
                e.stopPropagation();
                scaleElement(e, elementInfo, point.direction);
              }}
            />
          ))}
          <RotateHandler
            className="operate-rotate-handler"
            style={{ left: scaleWidth / 2 + 'px' }}
            onMouseDown={(e) => {
              e.stopPropagation();
              rotateElement(e, elementInfo);
            }}
          />
          {keypoints.map((keypoint, index) => (
            <div
              key={index}
              className="operate-keypoint-handler absolute w-[10px] h-[10px] left-0 top-0 m-[-5px_0_0_-5px] border border-primary bg-[#ffe873] rounded-[1px]"
              style={keypoint.styles}
              onMouseDown={(e) => {
                e.stopPropagation();
                moveShapeKeypoint(e, elementInfo, index);
              }}
            />
          ))}
        </>
      )}
    </div>
  );
}