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