|
| 1 | +import { forwardRef, useMemo } from 'react'; |
| 2 | +import { Group, Path, Rect } from 'react-konva'; |
| 3 | +import { ShapeSizeRestrictions, ShapeType } from '@/core/model'; |
| 4 | +import { ShapeProps } from '../../shape.model'; |
| 5 | +import { useShapeProps } from '../../../shapes/use-shape-props.hook'; |
| 6 | +import { BASIC_SHAPE } from '../../front-components/shape.const'; |
| 7 | +import { useGroupShapeProps } from '../../mock-components.utils'; |
| 8 | +import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes'; |
| 9 | +import { MIN_LINE_HEIGHT } from './paragraph-scribbled.const'; |
| 10 | +import { calculateParagraphPaths } from './paragraph-scribbled.business'; |
| 11 | + |
| 12 | +const paragraphScribbledShapeRestrictions: ShapeSizeRestrictions = { |
| 13 | + minWidth: 100, |
| 14 | + minHeight: MIN_LINE_HEIGHT, |
| 15 | + maxWidth: -1, |
| 16 | + maxHeight: -1, |
| 17 | + defaultWidth: 300, |
| 18 | + defaultHeight: 150, |
| 19 | +}; |
| 20 | + |
| 21 | +export const getParagraphScribbledShapeRestrictions = |
| 22 | + (): ShapeSizeRestrictions => paragraphScribbledShapeRestrictions; |
| 23 | + |
| 24 | +const shapeType: ShapeType = 'paragraphScribbled'; |
| 25 | + |
| 26 | +export const ParagraphScribbled = forwardRef<any, ShapeProps>((props, ref) => { |
| 27 | + const { width, height, id, otherProps, ...shapeProps } = props; |
| 28 | + |
| 29 | + const { stroke } = useShapeProps(otherProps, BASIC_SHAPE); |
| 30 | + const commonGroupProps = useGroupShapeProps( |
| 31 | + props, |
| 32 | + { width, height }, |
| 33 | + shapeType, |
| 34 | + ref |
| 35 | + ); |
| 36 | + |
| 37 | + const restrictedSize = fitSizeToShapeSizeRestrictions( |
| 38 | + paragraphScribbledShapeRestrictions, |
| 39 | + width, |
| 40 | + height |
| 41 | + ); |
| 42 | + |
| 43 | + const { width: restrictedWidth, height: restrictedHeight } = restrictedSize; |
| 44 | + |
| 45 | + const paths = useMemo(() => { |
| 46 | + return calculateParagraphPaths(restrictedWidth, restrictedHeight, id); |
| 47 | + }, [restrictedWidth, restrictedHeight, id]); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Group {...commonGroupProps} {...shapeProps}> |
| 51 | + {paths.map((path, idx) => ( |
| 52 | + <Path |
| 53 | + key={idx} |
| 54 | + data={path} |
| 55 | + stroke={stroke} |
| 56 | + strokeWidth={3} |
| 57 | + lineCap="round" |
| 58 | + lineJoin="round" |
| 59 | + /> |
| 60 | + ))} |
| 61 | + <Rect |
| 62 | + width={restrictedSize.width} |
| 63 | + height={restrictedSize.height} |
| 64 | + stroke={stroke} |
| 65 | + strokeWidth={0} |
| 66 | + /> |
| 67 | + </Group> |
| 68 | + ); |
| 69 | +}); |
0 commit comments