-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcalculations.ts
More file actions
76 lines (58 loc) · 2.1 KB
/
calculations.ts
File metadata and controls
76 lines (58 loc) · 2.1 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
import {Percent, percent} from '../includes/sizes.js';
export function calculateColumnWidths(
widths: Percent[],
splitterIndex: number,
delta: Percent,
minWidths: Map<number, Percent>
): Percent[] {
const result = [...widths];
// No-op for invalid splitter position or zero delta
if (delta === 0 || splitterIndex < 0 || splitterIndex >= widths.length - 1) {
return result;
}
const absDelta = Math.abs(delta);
let remaining: Percent = percent(absDelta);
const leftIndices: number[] = [];
const rightIndices: number[] = [];
// Collect column indices to the left of the splitter (inclusive)
for (let i = splitterIndex; i >= 0; i--) {
leftIndices.push(i);
}
// Collect column indices to the right of the splitter
for (let i = splitterIndex + 1; i < widths.length; i++) {
rightIndices.push(i);
}
// One side shrinks, the other grows depending on drag direction
const shrinkingSide = delta > 0 ? rightIndices : leftIndices;
const growingSide = delta > 0 ? leftIndices : rightIndices;
// Calculate total shrinkable space respecting minWidth
let totalAvailable: Percent = percent(0);
for (const i of shrinkingSide) {
const min = minWidths.get(i) ?? percent(0);
const available = Math.max(0, result[i] - min);
totalAvailable = percent(totalAvailable + available);
}
const effectiveDelta =
totalAvailable < remaining ? totalAvailable : remaining;
remaining = percent(effectiveDelta);
// Shrink columns sequentially until the delta is fully consumed
for (const i of shrinkingSide) {
if (remaining === 0) {
break;
}
const available = Math.max(0, result[i] - (minWidths.get(i) ?? 0));
const take = Math.min(available, remaining);
result[i] = percent(result[i] - take);
remaining = percent(remaining - take);
}
// Apply the exact opposite delta to the growing side
let toAdd: Percent = percent(effectiveDelta);
for (const i of growingSide) {
if (toAdd === 0) {
break;
}
result[i] = percent(result[i] + toAdd);
toAdd = percent(0); // all growth is applied to the nearest column
}
return result;
}