-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfile-tree-resize.hook.ts
More file actions
132 lines (118 loc) · 3.2 KB
/
file-tree-resize.hook.ts
File metadata and controls
132 lines (118 loc) · 3.2 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { ElementSize, Size } from '@/core/model';
import { useCanvasContext } from '@/core/providers';
import { useEffect, useRef } from 'react';
import { FileTreeItem, FileTreeSizeValues } from './file-tree.model';
// Hook to resize edition text area based on content
const useFileTreeResizeOnContentChange = (
id: string,
coords: { x: number; y: number },
text: string,
currentSize: Size,
calculatedSize: Size,
minHeight: number
) => {
const previousText = useRef(text);
const { updateShapeSizeAndPosition } = useCanvasContext();
useEffect(() => {
const textChanged = previousText.current !== text;
const finalHeight = Math.max(calculatedSize.height, minHeight);
const finalSize = { ...calculatedSize, height: finalHeight };
const sizeChanged =
finalHeight !== currentSize.height ||
calculatedSize.width !== currentSize.width;
if (textChanged && sizeChanged) {
previousText.current = text;
updateShapeSizeAndPosition(id, coords, finalSize, false);
} else if (sizeChanged) {
// If only the size has changed, also resize
updateShapeSizeAndPosition(id, coords, finalSize, false);
}
}, [
text,
calculatedSize.height,
calculatedSize.width,
currentSize.height,
currentSize.width,
id,
coords.x,
coords.y,
updateShapeSizeAndPosition,
]);
};
// Hook to force width change when ElementSize changes (XS ↔ S)
// This ensures that when dropping a component and changing from S to XS (or vice versa),
// the component doesn't maintain the previous width but forces the correct one:
// - XS: 150px width
// - S: 230px width
const useFileTreeResizeOnSizeChange = (
id: string,
coords: { x: number; y: number },
currentSize: Size,
treeItems: FileTreeItem[],
sizeValues: FileTreeSizeValues,
size?: ElementSize
) => {
const previousSize = useRef(size);
const { updateShapeSizeAndPosition } = useCanvasContext();
useEffect(() => {
// Only update if the size has changed
if (previousSize.current !== size) {
previousSize.current = size;
const newWidth = size === 'XS' ? 150 : 230;
const minContentHeight =
treeItems && sizeValues
? treeItems.length * sizeValues.elementHeight +
sizeValues.paddingY * 2
: currentSize.height;
if (
currentSize.width !== newWidth ||
currentSize.height !== minContentHeight
) {
updateShapeSizeAndPosition(
id,
coords,
{ width: newWidth, height: minContentHeight },
false
);
}
}
}, [
size,
currentSize.width,
currentSize.height,
id,
coords.x,
coords.y,
updateShapeSizeAndPosition,
treeItems,
sizeValues,
]);
};
export const useFileTreeResize = (
id: string,
coords: { x: number; y: number },
text: string,
currentSize: Size,
calculatedSize: Size,
minHeight: number,
treeItems: FileTreeItem[],
sizeValues: FileTreeSizeValues,
size?: ElementSize
) => {
useFileTreeResizeOnContentChange(
id,
coords,
text,
currentSize,
calculatedSize,
minHeight
);
useFileTreeResizeOnSizeChange(
id,
coords,
currentSize,
treeItems,
sizeValues,
size
);
};