forked from runjuu/column-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizer.ts
92 lines (73 loc) · 2.44 KB
/
resizer.ts
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
import { SizeRelatedInfo } from './types';
import { getNextSizeRelatedInfo, BarActionScanResult } from './utils';
type ResizeResult = SizeRelatedInfo | BarActionScanResult;
function getBarIndex(indexOfBar: number): number {
return indexOfBar * 2 + 1;
}
function getSectionIndex(indexOfSection: number): number {
return indexOfSection * 2;
}
export class Resizer {
private isDiscarded = false;
constructor(private resizeResult: ResizeResult) {}
resizeSection(indexOfSection: number, config: { toSize: number; preferMoveLeftBar?: boolean }) {
if (this.isDiscarded) {
return;
}
const sectionID = getSectionIndex(indexOfSection);
const currentSize = this.getSize(sectionID);
if (currentSize >= 0 && config.toSize >= 0) {
const offset = config.toSize - currentSize;
if (sectionID === this.resizeResult.sizeInfoArray.length - 1 || config.preferMoveLeftBar) {
this.moveBar(indexOfSection - 1, { withOffset: -offset });
} else {
this.moveBar(indexOfSection, { withOffset: offset });
}
}
}
moveBar(indexOfBar: number, config: { withOffset: number }) {
if (this.isDiscarded) {
return;
}
this.resizeResult = getNextSizeRelatedInfo(
getBarIndex(indexOfBar),
config.withOffset,
this.resizeResult.sizeInfoArray,
);
}
discard() {
this.isDiscarded = true;
}
isSectionResized(indexOfSection: number): boolean {
const sectionID = getSectionIndex(indexOfSection);
if ('defaultSizeInfoArray' in this.resizeResult) {
return (
this.getSize(sectionID) !== this.resizeResult.defaultSizeInfoArray[sectionID].currentSize
);
} else {
return false;
}
}
isBarActivated(indexOfBar: number): boolean {
if ('barIndex' in this.resizeResult) {
return this.resizeResult.barIndex === getBarIndex(indexOfBar);
} else {
return false;
}
}
getSectionSize(indexOfSection: number) {
return this.getSize(getSectionIndex(indexOfSection));
}
getResult(): SizeRelatedInfo {
return { ...this.resizeResult, discard: this.isDiscarded };
}
getTotalSize(): number {
return this.resizeResult.sizeInfoArray
.filter((sizeInfo, index) => sizeInfo && index % 2 === 0)
.reduce((total, { currentSize }) => total + currentSize, 0);
}
private getSize(index: number): number | -1 {
const sizeInfo = this.resizeResult.sizeInfoArray[index];
return sizeInfo ? sizeInfo.currentSize : -1;
}
}