File size: 2,623 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 | import { useMemo } from 'react';
import { useCanvasStore } from '@/lib/store';
import type {
PPTVideoElement,
PPTLatexElement,
PPTAudioElement,
PPTChartElement,
} from '@/lib/types/slides';
import type { OperateResizeHandlers } from '@/lib/types/edit';
import { useCommonOperate } from '../hooks/useCommonOperate';
import { RotateHandler } from './RotateHandler';
import { ResizeHandler } from './ResizeHandler';
import { BorderLine } from './BorderLine';
type PPTElement = PPTVideoElement | PPTLatexElement | PPTAudioElement | PPTChartElement;
interface CommonElementOperateProps {
readonly elementInfo: PPTElement;
readonly handlerVisible: boolean;
readonly rotateElement: (e: React.MouseEvent, element: PPTElement) => void;
readonly scaleElement: (
e: React.MouseEvent,
element: PPTElement,
command: OperateResizeHandlers,
) => void;
}
export function CommonElementOperate({
elementInfo,
handlerVisible,
rotateElement,
scaleElement,
}: CommonElementOperateProps) {
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 cannotRotate = useMemo(
() => ['chart', 'video', 'audio'].includes(elementInfo.type),
[elementInfo.type],
);
return (
<div className="common-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);
}}
/>
))}
{!cannotRotate && (
<RotateHandler
className="operate-rotate-handler"
style={{ left: scaleWidth / 2 + 'px' }}
onMouseDown={(e) => {
e.stopPropagation();
rotateElement(e, elementInfo);
}}
/>
)}
</>
)}
</div>
);
}
|