Skip to content

Commit 7fded32

Browse files
Externalize paragraph paths calculation to business file
1 parent d4c3c71 commit 7fded32

2 files changed

Lines changed: 39 additions & 31 deletions

File tree

src/common/components/mock-components/front-low-wireframes-components/paragraph-scribbled-shape/paragraph-scribbled-shape.tsx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { ShapeProps } from '../../shape.model';
55
import { useShapeProps } from '../../../shapes/use-shape-props.hook';
66
import { BASIC_SHAPE } from '../../front-components/shape.const';
77
import { useGroupShapeProps } from '../../mock-components.utils';
8-
import { calculatePath } from '../text-scribbled-shape/text-scribbled.business';
98
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes';
10-
11-
const MIN_LINE_HEIGHT = 25;
9+
import { MIN_LINE_HEIGHT } from './paragraph-scribbled.const';
10+
import { calculateParagraphPaths } from './paragraph-scribbled.business';
1211

1312
const paragraphScribbledShapeRestrictions: ShapeSizeRestrictions = {
1413
minWidth: 100,
@@ -43,35 +42,8 @@ export const ParagraphScribbled = forwardRef<any, ShapeProps>((props, ref) => {
4342

4443
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
4544

46-
// Calculate how many lines fit based on the height
47-
const numLines = Math.max(1, Math.trunc(restrictedHeight / MIN_LINE_HEIGHT));
48-
49-
// Generate one path per line
5045
const paths = useMemo(() => {
51-
return Array.from({ length: numLines }).map((_, i) => {
52-
const lineY = i * MIN_LINE_HEIGHT;
53-
const lineId = `${id}-${i}`;
54-
const rawPath = calculatePath(restrictedWidth, MIN_LINE_HEIGHT, lineId);
55-
56-
// Adjust the path to shift Y coordinate for each line
57-
// 🔍 Step by step:
58-
// The path assumes the text is vertically centered in a block of given height (e.g., 25px).
59-
// If you just drew this path multiple times, all lines would overlap.
60-
// To fix that, we shift the Y coordinate for each point in the path.
61-
//
62-
// Regular expression: /\d+,\d+/g
63-
// Finds all x,y coordinates in the path string (e.g., "10,12", "15,11").
64-
// We split each coordinate, convert y to number, add vertical offset (lineY),
65-
// then reassemble the coordinate string.
66-
const shiftedPath = rawPath.replace(/\d+,\d+/g, match => {
67-
const [xStr, yStr] = match.split(',');
68-
const x = parseFloat(xStr);
69-
const y = parseFloat(yStr) + lineY;
70-
return `${x},${y}`;
71-
});
72-
73-
return shiftedPath;
74-
});
46+
return calculateParagraphPaths(restrictedWidth, restrictedHeight, id);
7547
}, [restrictedWidth, restrictedHeight, id]);
7648

7749
return (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { calculatePath } from '../text-scribbled-shape/text-scribbled.business';
2+
import { MIN_LINE_HEIGHT } from './paragraph-scribbled.const';
3+
4+
export const calculateParagraphPaths = (
5+
restrictedWidth: number,
6+
restrictedHeight: number,
7+
id: string
8+
): string[] => {
9+
// Calculate how many lines fit based on the height
10+
const numLines = Math.max(1, Math.trunc(restrictedHeight / MIN_LINE_HEIGHT));
11+
12+
return Array.from({ length: numLines }).map((_, i) => {
13+
const lineY = i * MIN_LINE_HEIGHT;
14+
const lineId = `${id}-${i}`;
15+
const rawPath = calculatePath(restrictedWidth, MIN_LINE_HEIGHT, lineId);
16+
17+
// Adjust the path to shift Y coordinate for each line
18+
// 🔍 Step by step:
19+
// The path assumes the text is vertically centered in a block of given height (e.g., 25px).
20+
// If you just drew this path multiple times, all lines would overlap.
21+
// To fix that, we shift the Y coordinate for each point in the path.
22+
//
23+
// Regular expression: /\d+,\d+/g
24+
// Finds all x,y coordinates in the path string (e.g., "10,12", "15,11").
25+
// We split each coordinate, convert y to number, add vertical offset (lineY),
26+
// then reassemble the coordinate string.
27+
const shiftedPath = rawPath.replace(/\d+,\d+/g, match => {
28+
const [xStr, yStr] = match.split(',');
29+
const x = parseFloat(xStr);
30+
const y = parseFloat(yStr) + lineY;
31+
return `${x},${y}`;
32+
});
33+
34+
return shiftedPath;
35+
});
36+
};

0 commit comments

Comments
 (0)