-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-tree.ts
More file actions
224 lines (207 loc) · 6.54 KB
/
split-tree.ts
File metadata and controls
224 lines (207 loc) · 6.54 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import type { LayoutSplitTreeNode } from "../types";
import { createId } from "./app-id";
/** Limits for Layout Command Center custom grid UI. */
export const LAYOUT_GRID_MAX_ROWS = 12;
export const LAYOUT_GRID_MAX_COLS = 12;
export const LAYOUT_GRID_MAX_PANES = 48;
export type SplitAxis = "horizontal" | "vertical";
export type SplitLeafNode = { id: string; type: "leaf"; paneIndex: number };
export type SplitContainerNode = {
id: string;
type: "split";
axis: SplitAxis;
ratio: number;
first: SplitTreeNode;
second: SplitTreeNode;
};
export type SplitTreeNode = SplitLeafNode | SplitContainerNode;
export type SplitResizeState = { splitId: string; axis: SplitAxis };
export const DEFAULT_SPLIT_RATIO = 0.6;
export const MIN_SPLIT_RATIO = 0.2;
export const MAX_SPLIT_RATIO = 0.8;
export const createLeafNode = (paneIndex: number): SplitLeafNode => ({
id: `leaf-${paneIndex}`,
type: "leaf",
paneIndex,
});
export const cloneSplitTree = (node: SplitTreeNode): SplitTreeNode =>
node.type === "leaf"
? { ...node }
: {
...node,
first: cloneSplitTree(node.first),
second: cloneSplitTree(node.second),
};
export const rebalanceSplitTree = (node: SplitTreeNode): SplitTreeNode => {
if (node.type === "leaf") {
return node;
}
const nextFirst = rebalanceSplitTree(node.first);
const nextSecond = rebalanceSplitTree(node.second);
const nextRatio = 0.5;
if (nextFirst === node.first && nextSecond === node.second && node.ratio === nextRatio) {
return node;
}
return {
...node,
ratio: nextRatio,
first: nextFirst,
second: nextSecond,
};
};
export const replacePaneInTree = (
node: SplitTreeNode,
targetPaneIndex: number,
createReplacement: (leaf: SplitLeafNode) => SplitTreeNode,
): SplitTreeNode => {
if (node.type === "leaf") {
return node.paneIndex === targetPaneIndex ? createReplacement(node) : node;
}
return {
...node,
first: replacePaneInTree(node.first, targetPaneIndex, createReplacement),
second: replacePaneInTree(node.second, targetPaneIndex, createReplacement),
};
};
export const updateSplitRatioInTree = (node: SplitTreeNode, splitId: string, ratio: number): SplitTreeNode => {
if (node.type === "leaf") {
return node;
}
if (node.id === splitId) {
return { ...node, ratio: Math.min(MAX_SPLIT_RATIO, Math.max(MIN_SPLIT_RATIO, ratio)) };
}
return {
...node,
first: updateSplitRatioInTree(node.first, splitId, ratio),
second: updateSplitRatioInTree(node.second, splitId, ratio),
};
};
export const removePaneFromTree = (node: SplitTreeNode, targetPane: number): SplitTreeNode | null => {
if (node.type === "leaf") {
return node.paneIndex === targetPane ? null : node;
}
const nextFirst = removePaneFromTree(node.first, targetPane);
const nextSecond = removePaneFromTree(node.second, targetPane);
if (!nextFirst && !nextSecond) {
return null;
}
if (!nextFirst) {
return nextSecond;
}
if (!nextSecond) {
return nextFirst;
}
return {
...node,
first: nextFirst,
second: nextSecond,
};
};
export const createTreeFromPaneCount = (paneCount: number): SplitTreeNode => {
const count = Math.max(1, paneCount);
let tree: SplitTreeNode = createLeafNode(0);
for (let paneIndex = 1; paneIndex < count; paneIndex += 1) {
tree = {
id: `split-${paneIndex}`,
type: "split",
axis: "vertical",
ratio: DEFAULT_SPLIT_RATIO,
first: tree,
second: createLeafNode(paneIndex),
};
}
return tree;
};
const buildEqualGridRow = (cols: number, startPane: number, idPrefix: string): SplitTreeNode => {
if (cols <= 1) {
return createLeafNode(startPane);
}
return {
id: `${idPrefix}-h-${startPane}`,
type: "split",
axis: "horizontal",
ratio: 1 / cols,
first: createLeafNode(startPane),
second: buildEqualGridRow(cols - 1, startPane + 1, idPrefix),
};
};
const buildEqualGrid = (rows: number, cols: number, startPane: number, idPrefix: string): SplitTreeNode => {
if (rows <= 1) {
return buildEqualGridRow(cols, startPane, idPrefix);
}
return {
id: `${idPrefix}-v-${startPane}`,
type: "split",
axis: "vertical",
ratio: 1 / rows,
first: buildEqualGridRow(cols, startPane, idPrefix),
second: buildEqualGrid(rows - 1, cols, startPane + cols, idPrefix),
};
};
/** Equal M×N pane grid as a binary split tree (row-major pane indices). Ratios may be below MIN_SPLIT_RATIO for many columns/rows. */
export const createEqualGridSplitTree = (rows: number, cols: number): SplitTreeNode => {
const r = Math.max(1, Math.floor(rows));
const c = Math.max(1, Math.floor(cols));
const idPrefix = `grid-${createId()}`;
return buildEqualGrid(r, c, 0, idPrefix);
};
export const isLayoutGridDimensionsValid = (rows: number, cols: number): boolean => {
const r = Math.floor(rows);
const c = Math.floor(cols);
if (!Number.isFinite(r) || !Number.isFinite(c)) {
return false;
}
if (r < 1 || c < 1 || r > LAYOUT_GRID_MAX_ROWS || c > LAYOUT_GRID_MAX_COLS) {
return false;
}
return r * c <= LAYOUT_GRID_MAX_PANES;
};
export const serializeSplitTree = (node: SplitTreeNode): LayoutSplitTreeNode => {
if (node.type === "leaf") {
return {
id: node.id,
type: "leaf",
paneIndex: node.paneIndex,
};
}
return {
id: node.id,
type: "split",
axis: node.axis,
ratio: node.ratio,
first: serializeSplitTree(node.first),
second: serializeSplitTree(node.second),
};
};
export const parseSplitTree = (raw: LayoutSplitTreeNode | null | undefined): SplitTreeNode | null => {
if (!raw || typeof raw !== "object") {
return null;
}
if (raw.type === "leaf") {
if (typeof raw.paneIndex !== "number" || !Number.isInteger(raw.paneIndex) || raw.paneIndex < 0) {
return null;
}
return createLeafNode(raw.paneIndex);
}
if (raw.type === "split") {
const first = parseSplitTree(raw.first);
const second = parseSplitTree(raw.second);
if (!first || !second) {
return null;
}
return {
id: typeof raw.id === "string" && raw.id.length > 0 ? raw.id : `split-fallback`,
type: "split",
axis: raw.axis === "horizontal" ? "horizontal" : "vertical",
ratio:
typeof raw.ratio === "number" && Number.isFinite(raw.ratio)
? Math.min(MAX_SPLIT_RATIO, Math.max(MIN_SPLIT_RATIO, raw.ratio))
: DEFAULT_SPLIT_RATIO,
first,
second,
};
}
return null;
};
export const collectPaneOrder = (node: SplitTreeNode): number[] =>
node.type === "leaf" ? [node.paneIndex] : [...collectPaneOrder(node.first), ...collectPaneOrder(node.second)];