Skip to content

Commit b9b3a62

Browse files
authored
Merge pull request #804 from Lemoncode/feature/#793-fix-horizontal-menu-add-font-size-property
Feature/#793 fix horizontal menu add font size property
2 parents ef590f4 + 2e64edb commit b9b3a62

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

src/common/components/mock-components/front-rich-components/horizontal-menu/horizontal-menu.tsx

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import {
1010
splitCSVContentIntoRows,
1111
} from '@/common/utils/active-element-selector.utils';
1212
import { useGroupShapeProps } from '../../mock-components.utils';
13+
import {
14+
MultipleItemsInfo,
15+
useResizeOnFontSizeChange,
16+
} from '../../front-text-components/front-text-hooks/resize-fontsize-change.hook';
1317

1418
const horizontalMenuShapeSizeRestrictions: ShapeSizeRestrictions = {
1519
minWidth: 200,
16-
minHeight: 25,
20+
minHeight: 50,
1721
maxWidth: -1,
1822
maxHeight: 100,
1923
defaultWidth: 500,
@@ -41,7 +45,7 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
4145
const csvData = splitCSVContentIntoRows(text);
4246
const headers = extractCSVHeaders(csvData[0]);
4347
const itemLabels = headers.map(header => header.text);
44-
48+
const totalVerticalPadding = 8;
4549
const numberOfItems = itemLabels.length;
4650
const itemSpacing = 10;
4751

@@ -51,15 +55,21 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
5155
height
5256
);
5357
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
54-
const totalMargins = restrictedWidth - itemSpacing * (numberOfItems + 1);
55-
const itemWidth = totalMargins / numberOfItems;
58+
const totalHorizontalMargins =
59+
restrictedWidth - itemSpacing * (numberOfItems + 1);
60+
const itemWidth = totalHorizontalMargins / numberOfItems;
5661

57-
const { stroke, strokeStyle, fill, textColor, borderRadius } = useShapeProps(
58-
otherProps,
59-
BASIC_SHAPE
60-
);
62+
const {
63+
stroke,
64+
strokeStyle,
65+
fill,
66+
textColor,
67+
borderRadius,
68+
fontSize,
69+
fontVariant,
70+
} = useShapeProps(otherProps, BASIC_SHAPE);
6171

62-
const itemVerticalPadding = 4;
72+
const singleVerticalPadding = totalVerticalPadding / 2;
6373

6474
const activeSelected = otherProps?.activeElement ?? 0;
6575

@@ -70,8 +80,24 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
7080
ref
7181
);
7282

83+
const multiplesItemsInfo: MultipleItemsInfo = {
84+
numberOfItems: numberOfItems,
85+
horizontalSpacing: itemSpacing,
86+
};
87+
88+
useResizeOnFontSizeChange(
89+
id,
90+
{ x, y },
91+
text,
92+
fontSize,
93+
fontVariant,
94+
false,
95+
multiplesItemsInfo
96+
);
97+
7398
return (
7499
<Group {...commonGroupProps} {...shapeProps}>
100+
{/* Main Rectangle*/}
75101
<Rect
76102
x={0}
77103
y={0}
@@ -86,24 +112,26 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
86112

87113
{itemLabels.map((header, index) => (
88114
<Group key={index}>
115+
{/* Blue selected rectangle */}
89116
<Rect
90117
x={itemSpacing * (index + 1) + itemWidth * index}
91-
y={itemVerticalPadding}
118+
y={singleVerticalPadding}
92119
width={itemWidth}
93-
height={restrictedHeight - 2 * itemVerticalPadding}
120+
height={restrictedHeight - totalVerticalPadding}
94121
fill={index === activeSelected ? 'lightblue' : fill}
95122
/>
96123
<Text
97124
x={itemSpacing * (index + 1) + itemWidth * index}
98-
y={restrictedHeight / 2 - 8}
125+
y={restrictedHeight / 2 - fontSize / 2}
99126
text={header}
100127
fontFamily="Arial"
101-
fontSize={16}
128+
fontSize={fontSize}
102129
fill={textColor}
103130
width={itemWidth}
104131
align="center"
105132
wrap="none"
106133
ellipsis={true}
134+
fontVariant={fontVariant}
107135
/>
108136
</Group>
109137
))}

src/common/components/mock-components/front-text-components/front-text-hooks/resize-fontsize-change.hook.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { calcTextDimensions } from '@/common/utils/calc-text-dimensions';
22
import { useCanvasContext } from '@/core/providers';
33
import { useEffect, useRef } from 'react';
44

5+
export interface MultipleItemsInfo {
6+
numberOfItems: number;
7+
horizontalSpacing: number;
8+
}
9+
510
export const useResizeOnFontSizeChange = (
611
id: string,
712
coords: { x: number; y: number },
813
text: string,
914
fontSize: number,
1015
fontVariant: string,
11-
multiline: boolean = false
16+
multiline: boolean = false,
17+
multipleItemsInfo?: MultipleItemsInfo // Just in case we have a list of items (horizontally), e.g horizontal menu
1218
) => {
1319
const previousFontSize = useRef(fontSize);
1420
const { updateShapeSizeAndPosition, stageRef } = useCanvasContext();
@@ -21,12 +27,22 @@ export const useResizeOnFontSizeChange = (
2127
const paragraphLines = _extractParagraphLines(text);
2228
const longestLine = _findLongestString(paragraphLines);
2329

24-
const { width, height } = calcTextDimensions(
25-
multiline ? longestLine : text,
26-
fontSize,
27-
fontVariant,
28-
konvaLayer
29-
);
30+
const { width: longestLineWidth, height: longestLineHeight } =
31+
calcTextDimensions(
32+
multiline ? longestLine : text,
33+
fontSize,
34+
fontVariant,
35+
konvaLayer
36+
);
37+
38+
// We add to the longest line width the spacing between items if multiple items
39+
const width =
40+
longestLineWidth +
41+
(multipleItemsInfo
42+
? multipleItemsInfo.horizontalSpacing *
43+
multipleItemsInfo.numberOfItems
44+
: 0);
45+
const height = longestLineHeight;
3046

3147
updateShapeSizeAndPosition(
3248
id,

src/pods/canvas/model/shape-other-props.utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export const generateDefaultOtherProps = (
4848
strokeStyle: [],
4949
borderRadius: `${BASIC_SHAPE.DEFAULT_CORNER_RADIUS}`,
5050
activeElement: 0,
51+
fontSize: FONT_SIZE_VALUES.NORMALTEXT,
52+
fontVariant: `${INPUT_SHAPE.DEFAULT_FONT_VARIANT}`,
5153
};
5254
case 'input-stepper':
5355
return {

0 commit comments

Comments
 (0)