@andrewcyuan/focusgrid 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -9
- package/dist/core/index.cjs +194 -299
- package/dist/core/index.d.cts +18 -11
- package/dist/core/index.d.ts +18 -11
- package/dist/core/index.js +195 -299
- package/dist/dom/index.cjs +59 -145
- package/dist/dom/index.d.cts +0 -2
- package/dist/dom/index.d.ts +0 -2
- package/dist/dom/index.js +59 -145
- package/dist/react/index.cjs +52 -53
- package/dist/react/index.d.cts +22 -21
- package/dist/react/index.d.ts +22 -21
- package/dist/react/index.js +53 -54
- package/dist/react/styles.css +9 -9
- package/package.json +18 -24
package/dist/core/index.cjs
CHANGED
|
@@ -9,37 +9,35 @@ function createId(prefix = "node") {
|
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region core/src/layout/geometry.ts
|
|
12
|
+
const HANDLE_SIZE = 3;
|
|
12
13
|
function computeLayout(state) {
|
|
13
|
-
const
|
|
14
|
+
const collector = {
|
|
14
15
|
panes: [],
|
|
15
16
|
handles: [],
|
|
16
17
|
rectByNodeId: /* @__PURE__ */ new Map()
|
|
17
18
|
};
|
|
18
|
-
const
|
|
19
|
+
const rect = {
|
|
19
20
|
x: 0,
|
|
20
21
|
y: 0,
|
|
21
22
|
width: Math.max(0, state.container.width),
|
|
22
23
|
height: Math.max(0, state.container.height)
|
|
23
|
-
}
|
|
24
|
+
};
|
|
25
|
+
const root = computeNode(state.root, rect, collector, state.activePaneId);
|
|
24
26
|
return {
|
|
25
|
-
...
|
|
27
|
+
...collector,
|
|
26
28
|
root
|
|
27
29
|
};
|
|
28
30
|
}
|
|
29
31
|
function normalizeSplitSizes(sizes, expectedLength) {
|
|
30
|
-
const fallback = () => Array.from({ length: expectedLength }, () => 1 / expectedLength);
|
|
31
|
-
if (sizes.length !== expectedLength) return fallback();
|
|
32
32
|
const total = sizes.reduce((sum, size) => sum + Math.max(0, size), 0);
|
|
33
|
-
if (total <= 0) return
|
|
33
|
+
if (sizes.length !== expectedLength || total <= 0) return Array.from({ length: expectedLength }, () => 1 / expectedLength);
|
|
34
34
|
return sizes.map((size) => Math.max(0, size) / total);
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
if (node.orientation === direction) return node.children.reduce((sum, child) => sum + getMinimumSize(child, direction, minWidth, minHeight), 0) + Math.max(0, node.children.length - 1) * 3;
|
|
39
|
-
return Math.max(0, ...node.children.map((child) => getMinimumSize(child, direction, minWidth, minHeight)));
|
|
36
|
+
function getHandlesSize(split) {
|
|
37
|
+
return Math.max(0, split.children.length - 1) * HANDLE_SIZE;
|
|
40
38
|
}
|
|
41
|
-
function computeNode(node, rect,
|
|
42
|
-
|
|
39
|
+
function computeNode(node, rect, collector, activePaneId) {
|
|
40
|
+
collector.rectByNodeId.set(node.id, rect);
|
|
43
41
|
if (node.kind === "pane") {
|
|
44
42
|
const pane = {
|
|
45
43
|
paneId: node.paneId,
|
|
@@ -47,7 +45,7 @@ function computeNode(node, rect, geometry, activePaneId) {
|
|
|
47
45
|
rect,
|
|
48
46
|
active: node.paneId === activePaneId
|
|
49
47
|
};
|
|
50
|
-
|
|
48
|
+
collector.panes.push(pane);
|
|
51
49
|
return {
|
|
52
50
|
kind: "pane",
|
|
53
51
|
pane
|
|
@@ -57,14 +55,13 @@ function computeNode(node, rect, geometry, activePaneId) {
|
|
|
57
55
|
const horizontal = node.orientation === "horizontal";
|
|
58
56
|
const axisSize = horizontal ? rect.width : rect.height;
|
|
59
57
|
const axisStart = horizontal ? rect.x : rect.y;
|
|
60
|
-
const
|
|
61
|
-
const contentSize = Math.max(0, axisSize - handleTotal);
|
|
58
|
+
const contentSize = Math.max(0, axisSize - getHandlesSize(node));
|
|
62
59
|
let cursor = axisStart;
|
|
63
60
|
const handles = [];
|
|
64
61
|
const children = node.children.map((child, index) => {
|
|
65
62
|
const isLast = index === node.children.length - 1;
|
|
66
63
|
const childSize = isLast ? axisStart + axisSize - cursor : Math.floor(contentSize * (sizes[index] ?? 0));
|
|
67
|
-
const computedChild = computeNode(child, createChildRect(node.orientation, rect, cursor, childSize),
|
|
64
|
+
const computedChild = computeNode(child, createChildRect(node.orientation, rect, cursor, childSize), collector, activePaneId);
|
|
68
65
|
cursor += childSize;
|
|
69
66
|
if (!isLast) {
|
|
70
67
|
const handle = {
|
|
@@ -72,11 +69,11 @@ function computeNode(node, rect, geometry, activePaneId) {
|
|
|
72
69
|
splitId: node.id,
|
|
73
70
|
index,
|
|
74
71
|
direction: node.orientation,
|
|
75
|
-
rect: createChildRect(node.orientation, rect, cursor,
|
|
72
|
+
rect: createChildRect(node.orientation, rect, cursor, HANDLE_SIZE)
|
|
76
73
|
};
|
|
77
74
|
handles.push(handle);
|
|
78
|
-
|
|
79
|
-
cursor +=
|
|
75
|
+
collector.handles.push(handle);
|
|
76
|
+
cursor += HANDLE_SIZE;
|
|
80
77
|
}
|
|
81
78
|
return computedChild;
|
|
82
79
|
});
|
|
@@ -102,139 +99,91 @@ function createChildRect(direction, rect, cursor, size) {
|
|
|
102
99
|
}
|
|
103
100
|
//#endregion
|
|
104
101
|
//#region core/src/layout/resize.ts
|
|
105
|
-
function
|
|
102
|
+
function getMinimumSize(node, direction, paneMinSize) {
|
|
103
|
+
if (node.kind === "pane") return paneMinSize;
|
|
104
|
+
const childMinimums = node.children.map((child) => getMinimumSize(child, direction, paneMinSize));
|
|
105
|
+
if (node.orientation !== direction) return Math.max(0, ...childMinimums);
|
|
106
|
+
return childMinimums.reduce((sum, size) => sum + size, 0) + getHandlesSize(node);
|
|
107
|
+
}
|
|
108
|
+
function resizeSplit(split, totalPx, index, deltaPx, snapshotSizes, paneMinSize) {
|
|
106
109
|
if (totalPx <= 0 || index < 0 || index >= split.children.length - 1) return split;
|
|
107
|
-
const
|
|
110
|
+
const nextSizes = normalizeSplitSizes(snapshotSizes ?? split.sizes, split.children.length);
|
|
108
111
|
const deltaRatio = deltaPx / totalPx;
|
|
109
|
-
const nextSizes = [...baseSizes];
|
|
110
112
|
nextSizes[index] += deltaRatio;
|
|
111
113
|
nextSizes[index + 1] -= deltaRatio;
|
|
112
|
-
const minSizes = split.children.map((child) => getMinimumSize(child, split.orientation,
|
|
113
|
-
const
|
|
114
|
-
const clamped = clampAdjacentPair(nextSizes, minSizes, index);
|
|
115
|
-
const fitted = fitNodeToAxisSize({
|
|
114
|
+
const minSizes = split.children.map((child) => getMinimumSize(child, split.orientation, paneMinSize) / totalPx);
|
|
115
|
+
const fitted = fitSplitToAxisSize({
|
|
116
116
|
...split,
|
|
117
|
-
sizes:
|
|
118
|
-
}, split.orientation, totalPx,
|
|
119
|
-
|
|
120
|
-
return fitted;
|
|
117
|
+
sizes: clampAdjacentPair(nextSizes, minSizes, index)
|
|
118
|
+
}, split.orientation, totalPx, paneMinSize);
|
|
119
|
+
return sizesEqual(fitted.sizes, normalizeSplitSizes(split.sizes, split.children.length)) ? split : fitted;
|
|
121
120
|
}
|
|
122
121
|
function sizesEqual(a, b) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
});
|
|
135
|
-
return changed ? {
|
|
136
|
-
...node,
|
|
137
|
-
children
|
|
138
|
-
} : node;
|
|
139
|
-
}
|
|
140
|
-
const sizes = normalizeSplitSizes(node.sizes, node.children.length);
|
|
141
|
-
const handleTotal = Math.max(0, node.children.length - 1) * 3;
|
|
142
|
-
const contentSize = Math.max(0, axisSize - handleTotal);
|
|
143
|
-
const fittedSizes = fitSizesToMinimums(sizes, node.children.map((child) => getMinimumSize(child, direction, minWidth, minHeight)), contentSize);
|
|
144
|
-
let changed = !sizesEqual(fittedSizes, sizes);
|
|
145
|
-
const children = node.children.map((child, index) => {
|
|
146
|
-
const nextChild = fitNodeToAxisSize(child, direction, contentSize * (fittedSizes[index] ?? 0), minWidth, minHeight);
|
|
122
|
+
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
123
|
+
}
|
|
124
|
+
function fitSplitToAxisSize(split, direction, axisSize, paneMinSize) {
|
|
125
|
+
const sameAxis = split.orientation === direction;
|
|
126
|
+
const contentSize = Math.max(0, axisSize - getHandlesSize(split));
|
|
127
|
+
const normalized = normalizeSplitSizes(split.sizes, split.children.length);
|
|
128
|
+
const sizes = sameAxis ? fitSizesToMinimums(normalized, split.children.map((child) => getMinimumSize(child, direction, paneMinSize)), contentSize) : split.sizes;
|
|
129
|
+
let changed = sameAxis && !sizesEqual(sizes, normalized);
|
|
130
|
+
const children = split.children.map((child, index) => {
|
|
131
|
+
if (child.kind === "pane") return child;
|
|
132
|
+
const nextChild = fitSplitToAxisSize(child, direction, sameAxis ? contentSize * (sizes[index] ?? 0) : axisSize, paneMinSize);
|
|
147
133
|
changed ||= nextChild !== child;
|
|
148
134
|
return nextChild;
|
|
149
135
|
});
|
|
150
136
|
return changed ? {
|
|
151
|
-
...
|
|
137
|
+
...split,
|
|
152
138
|
children,
|
|
153
|
-
sizes
|
|
154
|
-
} :
|
|
139
|
+
sizes
|
|
140
|
+
} : split;
|
|
155
141
|
}
|
|
156
142
|
function fitSizesToMinimums(sizes, minSizes, contentSize) {
|
|
157
143
|
const normalized = normalizeSplitSizes(sizes, sizes.length);
|
|
158
144
|
if (contentSize <= 0) return normalized;
|
|
159
|
-
if (normalized.
|
|
145
|
+
if (normalized.every((size, index) => size * contentSize >= (minSizes[index] ?? 0))) return normalized;
|
|
160
146
|
const out = Array.from({ length: normalized.length }, () => 0);
|
|
161
147
|
const fixed = Array.from({ length: normalized.length }, () => false);
|
|
162
148
|
let remainingSize = contentSize;
|
|
163
149
|
let remainingWeight = normalized.reduce((sum, size) => sum + size, 0);
|
|
150
|
+
const shareOf = (index) => remainingSize * (remainingWeight > 0 ? normalized[index] / remainingWeight : 0);
|
|
164
151
|
let changed = true;
|
|
165
152
|
while (changed) {
|
|
166
153
|
changed = false;
|
|
167
154
|
for (let index = 0; index < normalized.length; index += 1) {
|
|
168
|
-
if (fixed[index]) continue;
|
|
169
|
-
const weight = remainingWeight > 0 ? normalized[index] / remainingWeight : 0;
|
|
170
|
-
const allocated = remainingSize * weight;
|
|
171
155
|
const minSize = minSizes[index] ?? 0;
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
156
|
+
if (fixed[index] || shareOf(index) >= minSize) continue;
|
|
157
|
+
out[index] = minSize;
|
|
158
|
+
fixed[index] = true;
|
|
159
|
+
remainingSize -= minSize;
|
|
160
|
+
remainingWeight -= normalized[index];
|
|
161
|
+
changed = true;
|
|
179
162
|
}
|
|
180
163
|
}
|
|
181
|
-
for (let index = 0; index < normalized.length; index += 1)
|
|
182
|
-
if (fixed[index]) continue;
|
|
183
|
-
const weight = remainingWeight > 0 ? normalized[index] / remainingWeight : 0;
|
|
184
|
-
out[index] = Math.max(0, remainingSize * weight);
|
|
185
|
-
}
|
|
164
|
+
for (let index = 0; index < normalized.length; index += 1) if (!fixed[index]) out[index] = Math.max(0, shareOf(index));
|
|
186
165
|
const total = out.reduce((sum, size) => sum + size, 0);
|
|
187
|
-
|
|
188
|
-
return out.map((size) => size / total);
|
|
166
|
+
return total <= 0 ? normalized : out.map((size) => size / total);
|
|
189
167
|
}
|
|
190
168
|
function clampAdjacentPair(sizes, minSizes, index) {
|
|
191
169
|
const out = [...sizes];
|
|
192
170
|
const pairTotal = out[index] + out[index + 1];
|
|
193
171
|
const leftMin = minSizes[index] ?? 0;
|
|
194
|
-
const
|
|
195
|
-
const maxLeft = Math.max(leftMin, pairTotal - rightMin);
|
|
172
|
+
const maxLeft = Math.max(leftMin, pairTotal - (minSizes[index + 1] ?? 0));
|
|
196
173
|
out[index] = Math.min(Math.max(out[index], leftMin), maxLeft);
|
|
197
174
|
out[index + 1] = pairTotal - out[index];
|
|
198
175
|
return normalizeSplitSizes(out, out.length);
|
|
199
176
|
}
|
|
200
177
|
//#endregion
|
|
201
|
-
//#region core/src/layout/types.ts
|
|
202
|
-
const cardinalDirections = [
|
|
203
|
-
"left",
|
|
204
|
-
"right",
|
|
205
|
-
"up",
|
|
206
|
-
"down"
|
|
207
|
-
];
|
|
208
|
-
let PaneCommandCapability = /* @__PURE__ */ function(PaneCommandCapability) {
|
|
209
|
-
PaneCommandCapability["ResizeX"] = "canResizeX";
|
|
210
|
-
PaneCommandCapability["ResizeY"] = "canResizeY";
|
|
211
|
-
PaneCommandCapability["Remove"] = "canRemove";
|
|
212
|
-
PaneCommandCapability["SplitHorizontal"] = "canSplitHorizontal";
|
|
213
|
-
PaneCommandCapability["SplitVertical"] = "canSplitVertical";
|
|
214
|
-
PaneCommandCapability["SwapX"] = "canSwapX";
|
|
215
|
-
PaneCommandCapability["SwapY"] = "canSwapY";
|
|
216
|
-
PaneCommandCapability["Focus"] = "canFocus";
|
|
217
|
-
return PaneCommandCapability;
|
|
218
|
-
}({});
|
|
219
|
-
//#endregion
|
|
220
178
|
//#region core/src/layout/tree.ts
|
|
221
|
-
function
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
visit(root, null);
|
|
228
|
-
return index;
|
|
229
|
-
function visit(node, parent) {
|
|
230
|
-
index.nodeById.set(node.id, node);
|
|
231
|
-
index.parentByNodeId.set(node.id, parent);
|
|
232
|
-
if (node.kind === "pane") {
|
|
233
|
-
index.paneNodeByPaneId.set(node.paneId, node);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
for (const child of node.children) visit(child, node);
|
|
179
|
+
function findNode(root, nodeId) {
|
|
180
|
+
if (root.id === nodeId) return root;
|
|
181
|
+
if (root.kind === "pane") return null;
|
|
182
|
+
for (const child of root.children) {
|
|
183
|
+
const node = findNode(child, nodeId);
|
|
184
|
+
if (node) return node;
|
|
237
185
|
}
|
|
186
|
+
return null;
|
|
238
187
|
}
|
|
239
188
|
function findPaneNode(root, paneId) {
|
|
240
189
|
if (!paneId) return null;
|
|
@@ -245,12 +194,14 @@ function findPaneNode(root, paneId) {
|
|
|
245
194
|
}
|
|
246
195
|
return null;
|
|
247
196
|
}
|
|
248
|
-
function
|
|
249
|
-
if (root.kind === "pane") return null;
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
197
|
+
function findPanePath(root, paneId) {
|
|
198
|
+
if (root.kind === "pane") return root.paneId === paneId ? [] : null;
|
|
199
|
+
for (const [childIndex, child] of root.children.entries()) {
|
|
200
|
+
const path = findPanePath(child, paneId);
|
|
201
|
+
if (path) return [{
|
|
202
|
+
split: root,
|
|
203
|
+
childIndex
|
|
204
|
+
}, ...path];
|
|
254
205
|
}
|
|
255
206
|
return null;
|
|
256
207
|
}
|
|
@@ -275,19 +226,11 @@ function updatePane(root, paneId, update) {
|
|
|
275
226
|
return transformLayout(root, (node) => node.kind === "pane" && node.paneId === paneId ? update(node) : node);
|
|
276
227
|
}
|
|
277
228
|
function markFocusedPanePath(root, paneId) {
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const path = /* @__PURE__ */ new Map();
|
|
282
|
-
let childId = pane.id;
|
|
283
|
-
let parent = index.parentByNodeId.get(childId);
|
|
284
|
-
while (parent) {
|
|
285
|
-
path.set(parent.id, childId);
|
|
286
|
-
childId = parent.id;
|
|
287
|
-
parent = index.parentByNodeId.get(childId);
|
|
288
|
-
}
|
|
229
|
+
const path = paneId ? findPanePath(root, paneId) : null;
|
|
230
|
+
if (!path) return root;
|
|
231
|
+
const focusedChildIds = new Map(path.map(({ split, childIndex }) => [split.id, split.children[childIndex].id]));
|
|
289
232
|
return transformLayout(root, (node) => {
|
|
290
|
-
const focusedChildId =
|
|
233
|
+
const focusedChildId = focusedChildIds.get(node.id);
|
|
291
234
|
return node.kind === "split" && focusedChildId !== void 0 && node.lastFocusedChildId !== focusedChildId ? {
|
|
292
235
|
...node,
|
|
293
236
|
lastFocusedChildId: focusedChildId
|
|
@@ -298,15 +241,14 @@ function removePaneNode(node, paneId) {
|
|
|
298
241
|
if (node.kind === "pane") return node.paneId === paneId ? null : node;
|
|
299
242
|
const children = [];
|
|
300
243
|
const sizes = [];
|
|
301
|
-
|
|
302
|
-
const
|
|
303
|
-
if (
|
|
304
|
-
children.push(
|
|
305
|
-
sizes.push(node.sizes[
|
|
244
|
+
node.children.forEach((child, index) => {
|
|
245
|
+
const nextChild = removePaneNode(child, paneId);
|
|
246
|
+
if (nextChild) {
|
|
247
|
+
children.push(nextChild);
|
|
248
|
+
sizes.push(node.sizes[index] ?? 1);
|
|
306
249
|
}
|
|
307
|
-
}
|
|
308
|
-
if (children.length
|
|
309
|
-
if (children.length === 1) return children[0];
|
|
250
|
+
});
|
|
251
|
+
if (children.length <= 1) return children[0] ?? null;
|
|
310
252
|
return {
|
|
311
253
|
...node,
|
|
312
254
|
children,
|
|
@@ -314,112 +256,66 @@ function removePaneNode(node, paneId) {
|
|
|
314
256
|
};
|
|
315
257
|
}
|
|
316
258
|
//#endregion
|
|
317
|
-
//#region core/src/layout/
|
|
259
|
+
//#region core/src/layout/types.ts
|
|
260
|
+
let PaneCommandCapability = /* @__PURE__ */ function(PaneCommandCapability) {
|
|
261
|
+
PaneCommandCapability["ResizeX"] = "canResizeX";
|
|
262
|
+
PaneCommandCapability["ResizeY"] = "canResizeY";
|
|
263
|
+
PaneCommandCapability["Remove"] = "canRemove";
|
|
264
|
+
PaneCommandCapability["SplitHorizontal"] = "canSplitHorizontal";
|
|
265
|
+
PaneCommandCapability["SplitVertical"] = "canSplitVertical";
|
|
266
|
+
PaneCommandCapability["SwapX"] = "canSwapX";
|
|
267
|
+
PaneCommandCapability["SwapY"] = "canSwapY";
|
|
268
|
+
PaneCommandCapability["Focus"] = "canFocus";
|
|
269
|
+
return PaneCommandCapability;
|
|
270
|
+
}({});
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region core/src/layout/navigation.ts
|
|
318
273
|
function isHorizontalDirection(direction) {
|
|
319
274
|
return direction === "left" || direction === "right";
|
|
320
275
|
}
|
|
321
|
-
function
|
|
322
|
-
|
|
323
|
-
if (direction === "right") return rect.x >= activeRect.x + activeRect.width - epsilon;
|
|
324
|
-
if (direction === "left") return rect.x + rect.width <= activeRect.x + epsilon;
|
|
325
|
-
if (direction === "down") return rect.y >= activeRect.y + activeRect.height - epsilon;
|
|
326
|
-
return rect.y + rect.height <= activeRect.y + epsilon;
|
|
327
|
-
}
|
|
328
|
-
function getEnteringEdge(rect, direction) {
|
|
329
|
-
if (direction === "right") return rect.x;
|
|
330
|
-
if (direction === "left") return rect.x + rect.width;
|
|
331
|
-
if (direction === "down") return rect.y;
|
|
332
|
-
return rect.y + rect.height;
|
|
333
|
-
}
|
|
334
|
-
function compareEnteringEdge(first, second, direction) {
|
|
335
|
-
return direction === "left" || direction === "up" ? second - first : first - second;
|
|
336
|
-
}
|
|
337
|
-
function getPerpendicularCenter(rect, direction) {
|
|
338
|
-
const center = getRectCenter(rect);
|
|
339
|
-
return isHorizontalDirection(direction) ? center.y : center.x;
|
|
340
|
-
}
|
|
341
|
-
function getRectCenter(rect) {
|
|
342
|
-
return {
|
|
343
|
-
x: rect.x + rect.width / 2,
|
|
344
|
-
y: rect.y + rect.height / 2
|
|
345
|
-
};
|
|
276
|
+
function isForwardDirection(direction) {
|
|
277
|
+
return direction === "right" || direction === "down";
|
|
346
278
|
}
|
|
347
|
-
function
|
|
348
|
-
const
|
|
349
|
-
return
|
|
279
|
+
function nearestSplitsOnAxis(path, direction) {
|
|
280
|
+
const horizontal = isHorizontalDirection(direction);
|
|
281
|
+
return path.filter(({ split }) => split.orientation === "horizontal" === horizontal).toReversed();
|
|
350
282
|
}
|
|
351
|
-
//#endregion
|
|
352
|
-
//#region core/src/layout/navigation.ts
|
|
353
283
|
function findPaneInDirection(root, layout, paneId, direction) {
|
|
354
|
-
const index = buildLayoutIndex(root);
|
|
355
|
-
const paneNode = index.paneNodeByPaneId.get(paneId);
|
|
356
|
-
if (!paneNode) return null;
|
|
357
284
|
const activePane = layout.panes.find((pane) => pane.paneId === paneId);
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
parent = index.parentByNodeId.get(currentId) ?? null;
|
|
285
|
+
const path = findPanePath(root, paneId);
|
|
286
|
+
if (!activePane || !path) return null;
|
|
287
|
+
for (const { split, childIndex } of nearestSplitsOnAxis(path, direction)) {
|
|
288
|
+
const sibling = split.children[childIndex + (isForwardDirection(direction) ? 1 : -1)];
|
|
289
|
+
if (!sibling) continue;
|
|
290
|
+
const siblingPaneIds = new Set(collectPaneIds(sibling));
|
|
291
|
+
return findClosestPane(layout.panes.filter((pane) => siblingPaneIds.has(pane.paneId)), activePane, direction, (candidateId) => getFocusMemoryRank(sibling, candidateId));
|
|
366
292
|
}
|
|
367
293
|
return null;
|
|
368
294
|
}
|
|
369
295
|
function findPaneForFocusCommand(root, layout, paneId, direction, overflow) {
|
|
370
296
|
const activePane = layout.panes.find((pane) => pane.paneId === paneId);
|
|
371
297
|
if (!activePane) return null;
|
|
372
|
-
const
|
|
373
|
-
const
|
|
374
|
-
const target = sortFocusCandidates(candidates.filter((pane) => isRectInDirection(pane.rect, activePane.rect, direction)), activePane, direction)[0]?.paneId ?? null;
|
|
298
|
+
const candidates = layout.panes.filter((pane) => pane.paneId !== paneId && findPaneNode(root, pane.paneId)?.["canFocus"] !== false);
|
|
299
|
+
const target = findClosestPane(candidates.filter((pane) => isRectInDirection(pane.rect, activePane.rect, direction)), activePane, direction, () => 0);
|
|
375
300
|
if (target || !overflow) return target;
|
|
376
|
-
return
|
|
377
|
-
}
|
|
378
|
-
function
|
|
379
|
-
const activeCenter =
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (direction === "right") return first.rect.x - second.rect.x;
|
|
392
|
-
if (direction === "left") return second.rect.x + second.rect.width - (first.rect.x + first.rect.width);
|
|
393
|
-
if (direction === "down") return first.rect.y - second.rect.y;
|
|
394
|
-
return second.rect.y + second.rect.height - (first.rect.y + first.rect.height);
|
|
301
|
+
return candidates.toSorted((first, second) => enteringEdgeOrder(first.rect, direction) - enteringEdgeOrder(second.rect, direction))[0]?.paneId ?? null;
|
|
302
|
+
}
|
|
303
|
+
function findClosestPane(candidates, activePane, direction, focusMemoryRank) {
|
|
304
|
+
const activeCenter = rectCenter(activePane.rect);
|
|
305
|
+
const ranked = candidates.map((pane) => {
|
|
306
|
+
const center = rectCenter(pane.rect);
|
|
307
|
+
const dx = Math.abs(center.x - activeCenter.x);
|
|
308
|
+
const dy = Math.abs(center.y - activeCenter.y);
|
|
309
|
+
return {
|
|
310
|
+
paneId: pane.paneId,
|
|
311
|
+
edge: enteringEdgeOrder(pane.rect, direction),
|
|
312
|
+
memory: focusMemoryRank(pane.paneId),
|
|
313
|
+
perpendicular: isHorizontalDirection(direction) ? dy : dx,
|
|
314
|
+
centerDistance: dx + dy
|
|
315
|
+
};
|
|
395
316
|
});
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
const childIndex = parent.children.findIndex((child) => child.id === childId);
|
|
399
|
-
if (childIndex === -1) return null;
|
|
400
|
-
const horizontal = direction === "left" || direction === "right";
|
|
401
|
-
if (horizontal && parent.orientation === "horizontal") {
|
|
402
|
-
if (direction === "right") return parent.children[childIndex + 1] ?? null;
|
|
403
|
-
return parent.children[childIndex - 1] ?? null;
|
|
404
|
-
}
|
|
405
|
-
if (!horizontal && parent.orientation === "vertical") {
|
|
406
|
-
if (direction === "down") return parent.children[childIndex + 1] ?? null;
|
|
407
|
-
return parent.children[childIndex - 1] ?? null;
|
|
408
|
-
}
|
|
409
|
-
return null;
|
|
410
|
-
}
|
|
411
|
-
function findTargetPaneInSubtree(subtree, panes, activePane, direction) {
|
|
412
|
-
const subtreePaneIds = new Set(collectPaneIds(subtree));
|
|
413
|
-
const candidates = panes.filter((pane) => subtreePaneIds.has(pane.paneId));
|
|
414
|
-
if (candidates.length === 0) return null;
|
|
415
|
-
const activeCenter = getRectCenter(activePane.rect);
|
|
416
|
-
return candidates.map((pane) => ({
|
|
417
|
-
pane,
|
|
418
|
-
edge: getEnteringEdge(pane.rect, direction),
|
|
419
|
-
perpendicularDistance: Math.abs(getPerpendicularCenter(pane.rect, direction) - getPerpendicularCenter(activePane.rect, direction)),
|
|
420
|
-
focusMemoryRank: getFocusMemoryRank(subtree, pane.paneId),
|
|
421
|
-
centerDistance: getCenterDistance(pane.rect, activeCenter)
|
|
422
|
-
})).sort((first, second) => compareEnteringEdge(first.edge, second.edge, direction) || first.focusMemoryRank - second.focusMemoryRank || first.perpendicularDistance - second.perpendicularDistance || first.centerDistance - second.centerDistance)[0].pane.paneId;
|
|
317
|
+
ranked.sort((first, second) => first.edge - second.edge || first.memory - second.memory || first.perpendicular - second.perpendicular || first.centerDistance - second.centerDistance);
|
|
318
|
+
return ranked[0]?.paneId ?? null;
|
|
423
319
|
}
|
|
424
320
|
function getFocusMemoryRank(subtree, paneId) {
|
|
425
321
|
if (subtree.kind === "pane") return Number.POSITIVE_INFINITY;
|
|
@@ -429,24 +325,24 @@ function getFocusMemoryRank(subtree, paneId) {
|
|
|
429
325
|
const nestedRank = getFocusMemoryRank(rememberedChild, paneId);
|
|
430
326
|
return Number.isFinite(nestedRank) ? nestedRank : 1;
|
|
431
327
|
}
|
|
432
|
-
function
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
328
|
+
function isRectInDirection(rect, activeRect, direction) {
|
|
329
|
+
const epsilon = .001;
|
|
330
|
+
if (direction === "right") return rect.x >= activeRect.x + activeRect.width - epsilon;
|
|
331
|
+
if (direction === "left") return rect.x + rect.width <= activeRect.x + epsilon;
|
|
332
|
+
if (direction === "down") return rect.y >= activeRect.y + activeRect.height - epsilon;
|
|
333
|
+
return rect.y + rect.height <= activeRect.y + epsilon;
|
|
334
|
+
}
|
|
335
|
+
function enteringEdgeOrder(rect, direction) {
|
|
336
|
+
if (direction === "right") return rect.x;
|
|
337
|
+
if (direction === "left") return -(rect.x + rect.width);
|
|
338
|
+
if (direction === "down") return rect.y;
|
|
339
|
+
return -(rect.y + rect.height);
|
|
340
|
+
}
|
|
341
|
+
function rectCenter(rect) {
|
|
342
|
+
return {
|
|
343
|
+
x: rect.x + rect.width / 2,
|
|
344
|
+
y: rect.y + rect.height / 2
|
|
345
|
+
};
|
|
450
346
|
}
|
|
451
347
|
//#endregion
|
|
452
348
|
//#region core/src/validation.ts
|
|
@@ -460,10 +356,6 @@ var FocusGridStateValidationException = class extends Error {
|
|
|
460
356
|
this.name = "FocusGridStateValidationException";
|
|
461
357
|
}
|
|
462
358
|
};
|
|
463
|
-
function assertValidFocusGridControllerState(input) {
|
|
464
|
-
const result = validateFocusGridControllerState(input);
|
|
465
|
-
if (!result.ok) throw new FocusGridStateValidationException(result.errors);
|
|
466
|
-
}
|
|
467
359
|
function validateFocusGridControllerState(input) {
|
|
468
360
|
const errors = [];
|
|
469
361
|
const nodeIds = /* @__PURE__ */ new Set();
|
|
@@ -479,9 +371,6 @@ function validateFocusGridControllerState(input) {
|
|
|
479
371
|
function fields(value, path, allowed, meta = {}) {
|
|
480
372
|
for (const field of Object.keys(value)) if (!allowed.has(field)) add("unknown-field", `${path}.${field}`, `Unknown field "${field}".`, meta);
|
|
481
373
|
}
|
|
482
|
-
function size(value, path, label, meta = {}) {
|
|
483
|
-
if (!isSize(value)) add("invalid-number", path, `${label} must be a finite, non-negative number.`, meta);
|
|
484
|
-
}
|
|
485
374
|
function id(value, seen, kind, path, meta) {
|
|
486
375
|
const label = kind === "pane" ? "Pane" : "Node";
|
|
487
376
|
if (typeof value !== "string" || value.length === 0) add(`invalid-${kind}-id`, path, `${label} id must be a non-empty string.`, meta);
|
|
@@ -525,7 +414,7 @@ function validateFocusGridControllerState(input) {
|
|
|
525
414
|
});
|
|
526
415
|
}
|
|
527
416
|
if (value.lastFocusedChildId !== void 0 && typeof value.lastFocusedChildId !== "string") add("invalid-last-focused-child", `${path}.lastFocusedChildId`, "lastFocusedChildId must be a node id string when provided.", meta);
|
|
528
|
-
if (typeof value.lastFocusedChildId === "string" && Array.isArray(value.children) && !value.children.some((child) => isRecord(child) && child.id === value.lastFocusedChildId)) add("invalid-last-focused-child", `${path}.lastFocusedChildId`, "lastFocusedChildId must reference one of the split's direct children.", {
|
|
417
|
+
else if (typeof value.lastFocusedChildId === "string" && Array.isArray(value.children) && !value.children.some((child) => isRecord(child) && child.id === value.lastFocusedChildId)) add("invalid-last-focused-child", `${path}.lastFocusedChildId`, "lastFocusedChildId must reference one of the split's direct children.", {
|
|
529
418
|
...meta,
|
|
530
419
|
nodeId: value.lastFocusedChildId
|
|
531
420
|
});
|
|
@@ -543,7 +432,7 @@ function validateFocusGridControllerState(input) {
|
|
|
543
432
|
else {
|
|
544
433
|
fields(input.container, "$.container", CONTAINER_FIELDS);
|
|
545
434
|
for (const key of CONTAINER_FIELDS) if (!(key in input.container)) add("missing-field", `$.container.${key}`, `${key} is required.`);
|
|
546
|
-
else
|
|
435
|
+
else if (!isSize(input.container[key])) add("invalid-number", `$.container.${key}`, `container ${key} must be a finite, non-negative number.`);
|
|
547
436
|
}
|
|
548
437
|
}
|
|
549
438
|
return errors.length ? {
|
|
@@ -594,7 +483,8 @@ var FocusGridController = class {
|
|
|
594
483
|
listeners = /* @__PURE__ */ new Set();
|
|
595
484
|
constructor({ paneDefaults = {}, minWidth = 0, minHeight = 0, directionalFocusOverflow = false, ...state }, domController) {
|
|
596
485
|
this.domController = domController;
|
|
597
|
-
|
|
486
|
+
const validation = validateFocusGridControllerState(state);
|
|
487
|
+
if (!validation.ok) throw new FocusGridStateValidationException(validation.errors);
|
|
598
488
|
if (![minWidth, minHeight].every((value) => Number.isFinite(value) && value >= 0)) throw new RangeError("Minimum pane dimensions must be finite, non-negative numbers.");
|
|
599
489
|
this.minWidth = minWidth;
|
|
600
490
|
this.minHeight = minHeight;
|
|
@@ -606,7 +496,6 @@ var FocusGridController = class {
|
|
|
606
496
|
root
|
|
607
497
|
};
|
|
608
498
|
}
|
|
609
|
-
/** Computed render data; stable until the next committed change. */
|
|
610
499
|
getLayout() {
|
|
611
500
|
return this.cachedLayout ??= computeLayout(this.currentState);
|
|
612
501
|
}
|
|
@@ -618,8 +507,8 @@ var FocusGridController = class {
|
|
|
618
507
|
return { ...this.currentState.container };
|
|
619
508
|
}
|
|
620
509
|
getSplitSizes(splitId) {
|
|
621
|
-
const split =
|
|
622
|
-
return split ? [...split.sizes] : null;
|
|
510
|
+
const split = findNode(this.currentState.root, splitId);
|
|
511
|
+
return split?.kind === "split" ? [...split.sizes] : null;
|
|
623
512
|
}
|
|
624
513
|
splitActive(side) {
|
|
625
514
|
const pane = findPaneNode(this.currentState.root, this.currentState.activePaneId);
|
|
@@ -655,32 +544,36 @@ var FocusGridController = class {
|
|
|
655
544
|
deltaPx
|
|
656
545
|
}) : false;
|
|
657
546
|
}
|
|
658
|
-
split(paneNodeId,
|
|
547
|
+
split(paneNodeId, { side, newPaneId, newPaneNodeId, splitId: requestedSplitId, preserveActivePane, ...capabilities }) {
|
|
659
548
|
const state = this.currentState;
|
|
660
|
-
const
|
|
661
|
-
const target = index.nodeById.get(paneNodeId);
|
|
549
|
+
const target = findNode(state.root, paneNodeId);
|
|
662
550
|
if (target?.kind !== "pane") return null;
|
|
663
|
-
const paneId =
|
|
664
|
-
const id =
|
|
665
|
-
const splitId =
|
|
666
|
-
if (
|
|
667
|
-
const { side, newPaneId, newPaneNodeId, splitId: suppliedSplitId, preserveActivePane, ...paneProps } = props;
|
|
551
|
+
const paneId = newPaneId ?? createId("pane");
|
|
552
|
+
const id = newPaneNodeId ?? createId("node");
|
|
553
|
+
const splitId = requestedSplitId ?? createId("split");
|
|
554
|
+
if (findPaneNode(state.root, paneId) || findNode(state.root, id) || findNode(state.root, splitId) || id === splitId) return null;
|
|
668
555
|
const pane = withPaneDefaults({
|
|
669
556
|
kind: "pane",
|
|
670
557
|
id,
|
|
671
558
|
paneId,
|
|
672
|
-
...
|
|
559
|
+
...capabilities
|
|
673
560
|
}, this.paneDefaults);
|
|
674
|
-
const
|
|
561
|
+
const horizontal = isHorizontalDirection(side);
|
|
562
|
+
const split = {
|
|
675
563
|
kind: "split",
|
|
676
564
|
id: splitId,
|
|
677
|
-
orientation:
|
|
678
|
-
children: side
|
|
565
|
+
orientation: horizontal ? "horizontal" : "vertical",
|
|
566
|
+
children: isForwardDirection(side) ? [target, pane] : [pane, target],
|
|
679
567
|
sizes: [.5, .5]
|
|
680
|
-
}
|
|
568
|
+
};
|
|
569
|
+
const paneMinSize = horizontal ? this.minWidth : this.minHeight;
|
|
570
|
+
const rect = this.getLayout().rectByNodeId.get(target.id);
|
|
571
|
+
const available = horizontal ? rect.width : rect.height;
|
|
572
|
+
if (paneMinSize > 0 && available < getMinimumSize(split, split.orientation, paneMinSize)) return null;
|
|
573
|
+
const root = updatePane(state.root, target.paneId, () => split);
|
|
681
574
|
const activePaneId = preserveActivePane ? state.activePaneId : paneId;
|
|
682
575
|
this.commit({
|
|
683
|
-
root:
|
|
576
|
+
root: markFocusedPanePath(root, activePaneId),
|
|
684
577
|
activePaneId
|
|
685
578
|
});
|
|
686
579
|
return paneId;
|
|
@@ -692,16 +585,15 @@ var FocusGridController = class {
|
|
|
692
585
|
const root = removePaneNode(state.root, paneId);
|
|
693
586
|
const activePaneId = state.activePaneId === paneId ? collectPaneIds(root)[0] : state.activePaneId;
|
|
694
587
|
return this.commit({
|
|
695
|
-
root:
|
|
588
|
+
root: markFocusedPanePath(root, activePaneId),
|
|
696
589
|
activePaneId
|
|
697
590
|
});
|
|
698
591
|
}
|
|
699
592
|
swap(firstPaneId, secondPaneId) {
|
|
700
593
|
if (firstPaneId === secondPaneId) return false;
|
|
701
594
|
const state = this.currentState;
|
|
702
|
-
const
|
|
703
|
-
const
|
|
704
|
-
const second = index.paneNodeByPaneId.get(secondPaneId);
|
|
595
|
+
const first = findPaneNode(state.root, firstPaneId);
|
|
596
|
+
const second = findPaneNode(state.root, secondPaneId);
|
|
705
597
|
if (!first || !second) return false;
|
|
706
598
|
const root = transformLayout(state.root, (node) => {
|
|
707
599
|
if (node.id === first.id) return {
|
|
@@ -714,23 +606,22 @@ var FocusGridController = class {
|
|
|
714
606
|
};
|
|
715
607
|
return node;
|
|
716
608
|
});
|
|
717
|
-
return this.commit({ root:
|
|
718
|
-
}
|
|
719
|
-
resize(paneId,
|
|
720
|
-
const
|
|
721
|
-
const
|
|
722
|
-
if (!
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}) : false;
|
|
609
|
+
return this.commit({ root: markFocusedPanePath(root, state.activePaneId) });
|
|
610
|
+
}
|
|
611
|
+
resize(paneId, { direction, deltaPx }) {
|
|
612
|
+
const path = findPanePath(this.currentState.root, paneId);
|
|
613
|
+
const nearest = path ? nearestSplitsOnAxis(path, direction)[0] : void 0;
|
|
614
|
+
if (!nearest) return false;
|
|
615
|
+
return this.resizeHandle(nearest.split.id, {
|
|
616
|
+
index: Math.max(0, nearest.childIndex - 1),
|
|
617
|
+
deltaPx: isForwardDirection(direction) ? deltaPx : -deltaPx
|
|
618
|
+
});
|
|
728
619
|
}
|
|
729
620
|
resizeHandle(splitId, props) {
|
|
730
621
|
const state = this.currentState;
|
|
731
622
|
const rect = this.getLayout().rectByNodeId.get(splitId);
|
|
732
623
|
if (!rect) return false;
|
|
733
|
-
const root = transformLayout(state.root, (node) => node.kind === "split" && node.id === splitId ? resizeSplit(node, node.orientation === "horizontal" ? rect.width : rect.height, props.index, props.deltaPx, props.snapshotSizes, this.minWidth
|
|
624
|
+
const root = transformLayout(state.root, (node) => node.kind === "split" && node.id === splitId ? resizeSplit(node, node.orientation === "horizontal" ? rect.width : rect.height, props.index, props.deltaPx, props.snapshotSizes, node.orientation === "horizontal" ? this.minWidth : this.minHeight) : node);
|
|
734
625
|
return this.commit({ root });
|
|
735
626
|
}
|
|
736
627
|
focus(paneId) {
|
|
@@ -743,10 +634,10 @@ var FocusGridController = class {
|
|
|
743
634
|
});
|
|
744
635
|
}
|
|
745
636
|
updatePane(paneId, patch) {
|
|
746
|
-
const root = updatePane(this.currentState.root, paneId, (pane) =>
|
|
637
|
+
const root = updatePane(this.currentState.root, paneId, (pane) => keepPaneIfUnchanged(pane, withoutUndefined({
|
|
747
638
|
...pane,
|
|
748
639
|
...patch
|
|
749
|
-
})
|
|
640
|
+
})));
|
|
750
641
|
return this.commit({ root });
|
|
751
642
|
}
|
|
752
643
|
setContainerSize(width, height) {
|
|
@@ -797,16 +688,20 @@ var FocusGridController = class {
|
|
|
797
688
|
}
|
|
798
689
|
};
|
|
799
690
|
function withPaneDefaults(pane, defaults) {
|
|
800
|
-
|
|
801
|
-
const next = Object.fromEntries(Object.entries({
|
|
691
|
+
return keepPaneIfUnchanged(pane, withoutUndefined({
|
|
802
692
|
...defaults,
|
|
803
|
-
...
|
|
804
|
-
})
|
|
805
|
-
|
|
693
|
+
...withoutUndefined(pane)
|
|
694
|
+
}));
|
|
695
|
+
}
|
|
696
|
+
function withoutUndefined(value) {
|
|
697
|
+
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== void 0));
|
|
698
|
+
}
|
|
699
|
+
function keepPaneIfUnchanged(pane, next) {
|
|
700
|
+
const keys = Object.keys(next);
|
|
701
|
+
return keys.length === Object.keys(pane).length && keys.every((key) => Object.is(pane[key], next[key])) ? pane : next;
|
|
806
702
|
}
|
|
807
703
|
//#endregion
|
|
808
704
|
exports.FocusGridController = FocusGridController;
|
|
809
705
|
exports.FocusGridStateValidationException = FocusGridStateValidationException;
|
|
810
706
|
exports.PaneCommandCapability = PaneCommandCapability;
|
|
811
|
-
exports.cardinalDirections = cardinalDirections;
|
|
812
707
|
exports.validateFocusGridControllerState = validateFocusGridControllerState;
|