Skip to content

Commit bac0766

Browse files
committed
updated width calc
1 parent b5f6722 commit bac0766

2 files changed

Lines changed: 52 additions & 15 deletions

File tree

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

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

1518
const horizontalMenuShapeSizeRestrictions: ShapeSizeRestrictions = {
1619
minWidth: 200,
@@ -42,7 +45,7 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
4245
const csvData = splitCSVContentIntoRows(text);
4346
const headers = extractCSVHeaders(csvData[0]);
4447
const itemLabels = headers.map(header => header.text);
45-
const verticalPadding = 8;
48+
const totalVerticalPadding = 8;
4649
const numberOfItems = itemLabels.length;
4750
const itemSpacing = 10;
4851

@@ -52,8 +55,9 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
5255
height
5356
);
5457
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
55-
const totalMargins = restrictedWidth - itemSpacing * (numberOfItems + 1);
56-
const itemWidth = totalMargins / numberOfItems;
58+
const totalHorizontalMargins =
59+
restrictedWidth - itemSpacing * (numberOfItems + 1);
60+
const itemWidth = totalHorizontalMargins / numberOfItems;
5761

5862
const {
5963
stroke,
@@ -65,7 +69,7 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
6569
fontVariant,
6670
} = useShapeProps(otherProps, BASIC_SHAPE);
6771

68-
const itemVerticalPadding = 4;
72+
const singleVerticalPadding = totalVerticalPadding / 2;
6973

7074
const activeSelected = otherProps?.activeElement ?? 0;
7175

@@ -75,9 +79,25 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
7579
shapeType,
7680
ref
7781
);
78-
useResizeOnFontSizeChange(id, { x, y }, text, fontSize, fontVariant);
82+
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+
7998
return (
8099
<Group {...commonGroupProps} {...shapeProps}>
100+
{/* Main Rectangle*/}
81101
<Rect
82102
x={0}
83103
y={0}
@@ -92,11 +112,12 @@ export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
92112

93113
{itemLabels.map((header, index) => (
94114
<Group key={index}>
115+
{/* Blue selected rectangle */}
95116
<Rect
96117
x={itemSpacing * (index + 1) + itemWidth * index}
97-
y={itemVerticalPadding}
118+
y={singleVerticalPadding}
98119
width={itemWidth}
99-
height={restrictedHeight - verticalPadding}
120+
height={restrictedHeight - totalVerticalPadding}
100121
fill={index === activeSelected ? 'lightblue' : fill}
101122
/>
102123
<Text

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,

0 commit comments

Comments
 (0)