-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfab-button.tsx
More file actions
96 lines (83 loc) · 2.96 KB
/
fab-button.tsx
File metadata and controls
96 lines (83 loc) · 2.96 KB
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
import { BASE_ICONS_URL, ShapeSizeRestrictions, ShapeType } from '@/core/model';
import { forwardRef, useEffect, useState } from 'react';
import { Circle, Group, Image } from 'react-konva';
import { ShapeProps } from '../../shape.model';
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes';
import { useShapeProps } from '@/common/components/shapes/use-shape-props.hook';
import { useGroupShapeProps } from '../../mock-components.utils';
import { BASIC_SHAPE } from '../../front-components/shape.const';
import { IconModal } from '@/pods/properties/components/icon-selector/modal';
import { useModalDialogContext } from '@/core/providers/model-dialog-providers/model-dialog.provider';
import { useCanvasContext } from '@/core/providers';
import { loadSvgWithFill } from '@/common/utils/svg.utils';
const fabButtonShapeRestrictions: ShapeSizeRestrictions = {
minWidth: 25,
minHeight: 25,
maxWidth: -1,
maxHeight: -1,
defaultWidth: 85,
defaultHeight: 85,
};
const shapeType: ShapeType = 'fabButton';
export const getFabButtonShapeSizeRestrictions = (): ShapeSizeRestrictions =>
fabButtonShapeRestrictions;
export const FabButtonShape = forwardRef<any, ShapeProps>((props, ref) => {
const { x, y, width, height, id, onSelected, otherProps, ...shapeProps } =
props;
const [iconImage, setIconImage] = useState<HTMLImageElement | null>(null);
const { openModal } = useModalDialogContext();
const { selectionInfo } = useCanvasContext();
const { updateOtherPropsOnSelected } = selectionInfo;
const restrictedSize = fitSizeToShapeSizeRestrictions(
fabButtonShapeRestrictions,
width,
height
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
const radius = Math.min(restrictedWidth, restrictedHeight) / 2;
const center = radius;
const iconInfo = otherProps?.icon;
const iconSize = radius * 1.2;
const iconStroke = otherProps?.stroke || '#ffffff';
const { fill } = useShapeProps(otherProps, BASIC_SHAPE);
const commonGroupProps = useGroupShapeProps(
props,
restrictedSize,
shapeType,
ref
);
const handleDoubleClick = () => {
if (iconInfo) {
openModal(
<IconModal
actualIcon={iconInfo}
onChange={icon => updateOtherPropsOnSelected('icon', icon)}
/>,
'Choose Icon'
);
}
};
useEffect(() => {
if (iconInfo?.filename) {
loadSvgWithFill(`${BASE_ICONS_URL}${iconInfo.filename}`, iconStroke).then(
img => setIconImage(img)
);
}
}, [iconInfo?.filename, iconStroke]);
return (
<Group {...commonGroupProps} {...shapeProps} onDblClick={handleDoubleClick}>
{/* Background Circle */}
<Circle x={center} y={center} radius={radius} fill={fill} />
{/* Icon */}
{iconImage && (
<Image
image={iconImage}
x={center - iconSize / 2}
y={center - iconSize / 2}
width={iconSize}
height={iconSize}
/>
)}
</Group>
);
});