-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
84 lines (72 loc) · 1.98 KB
/
utils.js
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
export const autoincrement = state => { return (state.next || 0) + 1 };
function compact(layout, vertical, horizontal) {
if(!vertical && !horizontal) {
return layout;
}
const max_x = layout.reduce((max_x, item) => Math.max(max_x, item.x + item.w), 0);
const max_y = layout.reduce((max_y, item) => Math.max(max_y, item.y + item.h), 0);
const matrix = [];
const updated = new Array(layout.length).fill(false);
for(let y = 0; y < max_y; y++) {
matrix.push(new Array(max_x).fill(undefined));
}
//fill layout matrix
layout.forEach(function(item) {
for(let i_y = item.y, i_y_max = item.y + item.h; i_y < i_y_max; i_y++) {
for(let i_x = item.x, i_x_max = item.x + item.w; i_x < i_x_max; i_x++) {
matrix[i_y][i_x] = item.i;
}
}
});
//compact vertical
if(vertical) {
let compressed = 0;
for (let y = 0; y < max_y; y++) {
let is_empty_row = true;
for (let x = 0; x < max_x; x++) {
if (matrix[y][x] !== undefined) {
is_empty_row = false;
break;
}
}
if (is_empty_row) {
const compressed_y = y - compressed;
layout.forEach((item, i) => {
if (item.y > compressed_y) {
item.y--;
updated[i] = true;
}
});
compressed++;
}
}
}
//compact horizontal
if(horizontal) {
let compressed = 0;
for (let x = 0; x < max_x; x++) {
let is_empty_col = true;
for (let y = 0; y < max_y; y++) {
if (matrix[y][x] !== undefined) {
is_empty_col = false;
break;
}
}
if (is_empty_col) {
const compressed_x = x - compressed;
layout.forEach((item, i) => {
if (item.x > compressed_x) {
item.x--;
updated[i] = true;
}
});
compressed++;
}
}
}
return layout.map((item, i) => updated[i] ? { ...item } : item);
}
export const compact_vertical = layout => compact(layout, true, false);
export const compact_horizontal = layout => compact(layout, false, true);
export const compact_full = layout => compact(layout, true, true);
export const compact_none = layout => layout;