|
| 1 | +import { ShapeSizeRestrictions, ShapeType, BASE_ICONS_URL } from '@/core/model'; |
| 2 | +import { forwardRef, useEffect, useState } from 'react'; |
| 3 | +import { ShapeProps } from '../../shape.model'; |
| 4 | +import { loadSvgWithFill } from '@/common/utils/svg.utils'; |
| 5 | +import { Group, Image } from 'react-konva'; |
| 6 | +import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 7 | +import { returnIconSize } from './icon-shape.business'; |
| 8 | +import { useGroupShapeProps } from '../../mock-components.utils'; |
| 9 | +import { useShapeProps } from '../../../shapes/use-shape-props.hook'; |
| 10 | +import { BASIC_SHAPE } from '../../front-components/shape.const'; |
| 11 | + |
| 12 | +const MouseCursorSizeRestrictions: ShapeSizeRestrictions = { |
| 13 | + minWidth: 25, |
| 14 | + minHeight: 25, |
| 15 | + maxWidth: -1, |
| 16 | + maxHeight: -1, |
| 17 | + defaultWidth: 150, |
| 18 | + defaultHeight: 150, |
| 19 | +}; |
| 20 | + |
| 21 | +export const getMouseCursorShapeSizeRestrictions = (): ShapeSizeRestrictions => |
| 22 | + MouseCursorSizeRestrictions; |
| 23 | + |
| 24 | +const shapeType: ShapeType = 'mouseCursor'; |
| 25 | + |
| 26 | +export const MouseCursorShape = forwardRef<any, ShapeProps>((props, ref) => { |
| 27 | + const { |
| 28 | + x, |
| 29 | + y, |
| 30 | + width, |
| 31 | + height, |
| 32 | + id, |
| 33 | + onSelected, |
| 34 | + text, |
| 35 | + iconSize, |
| 36 | + otherProps, |
| 37 | + ...shapeProps |
| 38 | + } = props; |
| 39 | + |
| 40 | + const [iconWidth, iconHeight] = returnIconSize(iconSize); |
| 41 | + const restrictedSize = fitSizeToShapeSizeRestrictions( |
| 42 | + MouseCursorSizeRestrictions, |
| 43 | + iconWidth, |
| 44 | + iconHeight |
| 45 | + ); |
| 46 | + |
| 47 | + const { width: restrictedWidth, height: restrictedHeight } = restrictedSize; |
| 48 | + const { stroke } = useShapeProps(otherProps, BASIC_SHAPE); |
| 49 | + const commonGroupProps = useGroupShapeProps( |
| 50 | + props, |
| 51 | + restrictedSize, |
| 52 | + shapeType, |
| 53 | + ref |
| 54 | + ); |
| 55 | + |
| 56 | + const [image, setImage] = useState<HTMLImageElement | null>(null); |
| 57 | + //const imgRef = useRef(null); |
| 58 | + const fileName = 'cursor.svg'; |
| 59 | + useEffect(() => { |
| 60 | + loadSvgWithFill(`${BASE_ICONS_URL}${fileName}`, `${stroke}`).then(img => { |
| 61 | + setImage(img); |
| 62 | + }); |
| 63 | + }, [stroke]); |
| 64 | + return ( |
| 65 | + <Group {...commonGroupProps} {...shapeProps}> |
| 66 | + {image && ( |
| 67 | + <Image |
| 68 | + image={image} |
| 69 | + x={0} |
| 70 | + y={0} |
| 71 | + width={restrictedWidth} |
| 72 | + height={restrictedHeight} |
| 73 | + //ref={imageRef} |
| 74 | + /> |
| 75 | + )} |
| 76 | + </Group> |
| 77 | + ); |
| 78 | +}); |
0 commit comments