Skip to content

Commit 4a020ad

Browse files
feat(file-tree): Implement dynamic height adaptation based on content
1 parent fffa03c commit 4a020ad

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

src/common/components/mock-components/front-rich-components/file-tree/file-tree.business.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes';
2+
import { ShapeSizeRestrictions, Size } from '@/core/model';
3+
14
export const joinTextContent = (text: string): string[] => {
25
return text.split(', ');
36
};
@@ -8,6 +11,14 @@ export interface FileTreeItem {
811
text: string;
912
}
1013

14+
interface FileTreeDynamicSizeParams {
15+
width: number;
16+
height: number;
17+
elementHeight: number;
18+
paddingY: number;
19+
baseRestrictions: ShapeSizeRestrictions;
20+
}
21+
1122
export const parseFileTreeText = (text: string): FileTreeItem[] => {
1223
return text
1324
.split(',')
@@ -46,3 +57,32 @@ export const parseFileTreeText = (text: string): FileTreeItem[] => {
4657
})
4758
.filter((item): item is FileTreeItem => item !== null);
4859
};
60+
61+
export const calculateFileTreeDynamicSize = (
62+
treeItems: FileTreeItem[],
63+
params: FileTreeDynamicSizeParams
64+
): Size => {
65+
const { width, height, elementHeight, paddingY, baseRestrictions } = params;
66+
67+
// Calculate minimum height required based on content
68+
const minContentHeight = treeItems.length * elementHeight + paddingY * 2;
69+
70+
// Create dynamic constraints with adaptive minimum height
71+
const dynamicRestrictions: ShapeSizeRestrictions = {
72+
...baseRestrictions,
73+
minHeight: minContentHeight,
74+
defaultHeight: Math.max(
75+
baseRestrictions.defaultHeight || 280,
76+
minContentHeight
77+
),
78+
};
79+
80+
// Use the user's current height, but ensure that it is not less than minContentHeight.
81+
const finalHeight = Math.max(height, minContentHeight);
82+
83+
return fitSizeToShapeSizeRestrictions(
84+
dynamicRestrictions,
85+
width,
86+
finalHeight
87+
);
88+
};

src/common/components/mock-components/front-rich-components/file-tree/file-tree.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { ShapeSizeRestrictions, ShapeType } from '@/core/model';
22
import { forwardRef, useEffect, useState } from 'react';
33
import { Group, Image, Rect, Text } from 'react-konva';
44
import { ShapeProps } from '../../shape.model';
5-
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes';
65
import { useGroupShapeProps } from '../../mock-components.utils';
76
import { loadSvgWithFill } from '@/common/utils/svg.utils';
87
import { useShapeProps } from '@/common/components/shapes/use-shape-props.hook';
98
import { BASIC_SHAPE } from '../../front-components/shape.const';
10-
import { parseFileTreeText } from './file-tree.business';
9+
import {
10+
calculateFileTreeDynamicSize,
11+
parseFileTreeText,
12+
} from './file-tree.business';
1113

1214
const fileTreeShapeRestrictions: ShapeSizeRestrictions = {
1315
minWidth: 220,
@@ -51,20 +53,23 @@ export const FileTreeShape = forwardRef<any, FileTreeShapeProps>(
5153
}
5254
);
5355

54-
const restrictedSize = fitSizeToShapeSizeRestrictions(
55-
fileTreeShapeRestrictions,
56-
width,
57-
height
58-
);
59-
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
60-
6156
const iconWidth = 50;
6257
const elementHeight = 60;
6358
const paddingX = 40;
64-
const paddingTop = 30;
59+
const paddingY = 30;
6560
const fileX = 50 + paddingX;
6661
const iconTextSpacing = 10;
6762

63+
const restrictedSize = calculateFileTreeDynamicSize(treeItems, {
64+
width,
65+
height,
66+
elementHeight,
67+
paddingY,
68+
baseRestrictions: fileTreeShapeRestrictions,
69+
});
70+
71+
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
72+
6873
const folderTextX = iconWidth + iconTextSpacing + paddingX;
6974
const fileTextX = fileX + iconWidth + iconTextSpacing;
7075

@@ -102,7 +107,7 @@ export const FileTreeShape = forwardRef<any, FileTreeShapeProps>(
102107
x={0}
103108
y={0}
104109
width={restrictedWidth}
105-
height={restrictedHeight + 20}
110+
height={restrictedHeight}
106111
stroke={stroke}
107112
strokeWidth={2}
108113
fill={fill}
@@ -116,14 +121,14 @@ export const FileTreeShape = forwardRef<any, FileTreeShapeProps>(
116121
<Image
117122
image={icons[item.type]!}
118123
x={item.type === 'file' ? fileX : paddingX}
119-
y={paddingTop + elementHeight * index}
124+
y={paddingY + elementHeight * index}
120125
width={iconWidth}
121126
height={iconWidth}
122127
/>
123128
)}
124129
<Text
125130
x={item.type === 'file' ? fileTextX : folderTextX}
126-
y={paddingTop + elementHeight * index + 20}
131+
y={paddingY + elementHeight * index + 20}
127132
text={item.text}
128133
width={
129134
item.type === 'file' ? fileAvailableWidth : folderAvailableWidth

0 commit comments

Comments
 (0)