@olenbetong/appframe-ds 0.9.0 → 1.0.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/CHANGELOG.md +123 -0
- package/package.json +22 -17
- package/scripts/copyAssets.mjs +3 -0
- package/src/autocomplete/Autocomplete.css +13 -0
- package/src/autocomplete/Autocomplete.tsx +3 -1
- package/src/autocomplete/Combobox.tsx +16 -3
- package/src/binding/BoundTextField.tsx +9 -1
- package/src/filter/FieldFilterPanel.css +25 -2
- package/src/grid/AfGridColumnsPanel.tsx +1 -1
- package/src/grid/AfGridContext.tsx +1 -1
- package/src/grid/AfGridError.tsx +1 -1
- package/src/grid/AfHeaderFilterCell.tsx +1 -1
- package/src/grid/Toolbar.tsx +1 -1
- package/src/grid/editing.ts +17 -7
- package/src/grid/filter.ts +1 -1
- package/src/grid/index.css +59 -1
- package/src/grid/index.tsx +126 -18
- package/src/grid/localization.ts +1 -1
- package/src/grid/slots/index.tsx +27 -2
- package/src/grid/theme.tsx +3 -50
- package/src/grid/useAfColumns.tsx +103 -45
- package/src/grid/useAfCurrentIndex.ts +13 -5
- package/src/grid/useAfGridApi.ts +6 -1
- package/src/grid/useAfKeyBindings.ts +1 -1
- package/src/grid/useAfNewItemRow.ts +258 -0
- package/src/grid/useAfPagination.ts +2 -2
- package/src/grid/useAfPersistedState.ts +9 -1
- package/src/grid/useAfRowEditModel.ts +20 -12
- package/src/grid/useAfRowGrouping.ts +47 -0
- package/src/grid/useAfServerAggregation.ts +145 -0
- package/src/grid/useAfSortModel.ts +15 -3
- package/src/layout/AppMenuDrawer.css +70 -0
- package/src/layout/AppMenuDrawer.tsx +122 -0
- package/src/layout/index.tsx +1 -0
- package/src/mdi/Mdi.css +138 -0
- package/src/mdi/Mdi.test.tsx +87 -0
- package/src/mdi/Mdi.tsx +141 -0
- package/src/mdi/MdiConfirmCloseDialog.tsx +47 -0
- package/src/mdi/MdiContext.tsx +59 -0
- package/src/mdi/MdiDocumentContext.tsx +60 -0
- package/src/mdi/MdiPanel.tsx +61 -0
- package/src/mdi/MdiRestore.test.tsx +82 -0
- package/src/mdi/MdiTab.tsx +30 -0
- package/src/mdi/controller.test.ts +382 -0
- package/src/mdi/controller.ts +656 -0
- package/src/mdi/dockviewHost.ts +132 -0
- package/src/mdi/fakeHost.ts +200 -0
- package/src/mdi/host.ts +44 -0
- package/src/mdi/ids.test.ts +20 -0
- package/src/mdi/ids.ts +18 -0
- package/src/mdi/index.ts +31 -0
- package/src/mdi/layoutRestore.test.ts +156 -0
- package/src/mdi/layoutRestore.ts +311 -0
- package/src/mdi/layoutSpec.test.ts +143 -0
- package/src/mdi/layoutSpec.ts +214 -0
- package/src/mdi/layoutStore.test.ts +93 -0
- package/src/mdi/layoutStore.ts +99 -0
- package/src/mdi/memorySettingsStore.ts +21 -0
- package/src/mdi/registry.ts +34 -0
- package/src/mdi/testTypes.ts +37 -0
- package/src/mdi/theme.ts +22 -0
- package/src/mdi/types.ts +214 -0
- package/src/page/Page.css +10 -2
- package/src/page/PageBlock.css +5 -0
- package/src/page/PageBlock.tsx +14 -0
- package/src/page/PageHeader.css +0 -60
- package/src/page/PageHeader.tsx +6 -74
- package/src/test/happy-dom-document-class.ts +24 -0
- package/src/test/setup.ts +4 -0
- package/src/theme/useDsColorScheme.ts +53 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import type { SerializedDockview, SerializedGridObject } from "dockview-react";
|
|
2
|
+
|
|
3
|
+
import { isGroupSpec, type MdiGroupState, specDocumentId } from "./layoutSpec.js";
|
|
4
|
+
import { resolveClosable, resolveType } from "./registry.js";
|
|
5
|
+
import type {
|
|
6
|
+
MdiDocumentSpec,
|
|
7
|
+
MdiDocumentTypes,
|
|
8
|
+
MdiGroupSpec,
|
|
9
|
+
MdiLayout,
|
|
10
|
+
MdiLayoutNode,
|
|
11
|
+
MdiLayoutSpec,
|
|
12
|
+
MdiPosition,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
type GridNode = SerializedGridObject<MdiGroupState>;
|
|
16
|
+
type LeafNode = GridNode & { type: "leaf"; data: MdiGroupState };
|
|
17
|
+
type BranchNode = GridNode & { type: "branch"; data: GridNode[] };
|
|
18
|
+
|
|
19
|
+
function isLeaf(node: GridNode): node is LeafNode {
|
|
20
|
+
return node.type === "leaf";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isBranch(node: GridNode): node is BranchNode {
|
|
24
|
+
return node.type === "branch";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function collectLeaves(node: GridNode, result: LeafNode[] = []): LeafNode[] {
|
|
28
|
+
if (isLeaf(node)) {
|
|
29
|
+
result.push(node);
|
|
30
|
+
} else if (isBranch(node)) {
|
|
31
|
+
for (let child of node.data) {
|
|
32
|
+
collectLeaves(child, result);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The group states in the grid, in layout order. Popout and floating groups are docked first. */
|
|
40
|
+
export function listGridGroups(dockview: SerializedDockview): MdiGroupState[] {
|
|
41
|
+
return collectLeaves(dockview.grid.root).map((leaf) => leaf.data);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Merges the groups of a floating/popout window into one group state */
|
|
45
|
+
function mergeGroups(groups: MdiGroupState[]): MdiGroupState | undefined {
|
|
46
|
+
let [first, ...rest] = groups;
|
|
47
|
+
|
|
48
|
+
if (!first) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let views = [...first.views];
|
|
53
|
+
for (let group of rest) {
|
|
54
|
+
views.push(...group.views);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { ...first, views, activeView: first.activeView ?? views[0] };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function windowGroups(entry: { data?: MdiGroupState; grid?: { root: GridNode } }): MdiGroupState[] {
|
|
61
|
+
if (entry.data) {
|
|
62
|
+
return [entry.data];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (entry.grid) {
|
|
66
|
+
return collectLeaves(entry.grid.root).map((leaf) => leaf.data);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Moves floating and popout groups back into the grid. Browsers block `window.open`
|
|
74
|
+
* without a user gesture, so popouts cannot be restored as windows on page load anyway;
|
|
75
|
+
* docking them keeps the documents visible instead of relying on dockview's fallback.
|
|
76
|
+
*/
|
|
77
|
+
export function dockPopoutGroups(dockview: SerializedDockview): SerializedDockview {
|
|
78
|
+
let result = structuredClone(dockview);
|
|
79
|
+
let popouts = result.popoutGroups ?? [];
|
|
80
|
+
let floating = result.floatingGroups ?? [];
|
|
81
|
+
delete result.popoutGroups;
|
|
82
|
+
delete result.floatingGroups;
|
|
83
|
+
|
|
84
|
+
if (popouts.length === 0 && floating.length === 0) {
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!isBranch(result.grid.root)) {
|
|
89
|
+
result.grid.root = { type: "branch", size: result.grid.root.size, data: [result.grid.root] };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let root = result.grid.root as BranchNode;
|
|
93
|
+
let leaves = collectLeaves(root);
|
|
94
|
+
let toDock: MdiGroupState[] = [];
|
|
95
|
+
|
|
96
|
+
for (let popout of popouts) {
|
|
97
|
+
let merged = mergeGroups(windowGroups(popout));
|
|
98
|
+
if (!merged) continue;
|
|
99
|
+
|
|
100
|
+
// A popped-out group leaves a hidden placeholder in the grid; put the documents back there.
|
|
101
|
+
let reference = popout.gridReferenceGroup
|
|
102
|
+
? leaves.find((leaf) => leaf.data.id === popout.gridReferenceGroup)
|
|
103
|
+
: undefined;
|
|
104
|
+
|
|
105
|
+
if (reference) {
|
|
106
|
+
reference.data.views.push(...merged.views);
|
|
107
|
+
reference.data.activeView ??= merged.activeView;
|
|
108
|
+
delete reference.visible;
|
|
109
|
+
} else {
|
|
110
|
+
toDock.push(merged);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
for (let entry of floating) {
|
|
115
|
+
let merged = mergeGroups(windowGroups(entry));
|
|
116
|
+
if (merged) toDock.push(merged);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let usedIds = new Set(leaves.map((leaf) => leaf.data.id));
|
|
120
|
+
let averageSize =
|
|
121
|
+
root.data.length > 0 ? root.data.reduce((sum, node) => sum + (node.size ?? 0), 0) / root.data.length : 1;
|
|
122
|
+
|
|
123
|
+
for (let group of toDock) {
|
|
124
|
+
let id = group.id;
|
|
125
|
+
while (usedIds.has(id)) {
|
|
126
|
+
id = `${id}-docked`;
|
|
127
|
+
}
|
|
128
|
+
usedIds.add(id);
|
|
129
|
+
root.data.push({ type: "leaf", data: { ...group, id }, size: averageSize || 1 });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function pruneNode(node: GridNode, keep: Set<string>): GridNode | undefined {
|
|
136
|
+
if (isLeaf(node)) {
|
|
137
|
+
let views = node.data.views.filter((view) => keep.has(view));
|
|
138
|
+
|
|
139
|
+
if (views.length === 0) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let activeView = node.data.activeView && views.includes(node.data.activeView) ? node.data.activeView : views[0];
|
|
144
|
+
return { ...node, data: { ...node.data, views, activeView } };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (isBranch(node)) {
|
|
148
|
+
let children = node.data.map((child) => pruneNode(child, keep)).filter((child) => child !== undefined);
|
|
149
|
+
|
|
150
|
+
if (children.length === 0) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// A branch wrapping a single leaf is redundant. A single nested branch has to stay,
|
|
155
|
+
// since dockview alternates orientation per level.
|
|
156
|
+
if (children.length === 1 && isLeaf(children[0]!)) {
|
|
157
|
+
return { ...children[0]!, size: node.size };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { ...node, data: children };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return node;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Removes every document that is not in `keep` from the layout, dropping groups and splits that become empty. */
|
|
167
|
+
export function pruneLayout(layout: MdiLayout, keep: Set<string>): MdiLayout {
|
|
168
|
+
let documents: MdiLayout["documents"] = {};
|
|
169
|
+
for (let [id, document] of Object.entries(layout.documents)) {
|
|
170
|
+
if (keep.has(id)) documents[id] = document;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let panels: SerializedDockview["panels"] = {};
|
|
174
|
+
for (let [id, panel] of Object.entries(layout.dockview.panels)) {
|
|
175
|
+
if (keep.has(id)) panels[id] = panel;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let root = pruneNode(layout.dockview.grid.root, keep);
|
|
179
|
+
|
|
180
|
+
if (!root) {
|
|
181
|
+
root = { type: "branch", size: layout.dockview.grid.root.size, data: [] };
|
|
182
|
+
} else if (!isBranch(root)) {
|
|
183
|
+
root = { type: "branch", size: root.size, data: [root] };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let remainingGroups = new Set(collectLeaves(root).map((leaf) => leaf.data.id));
|
|
187
|
+
let activeGroup =
|
|
188
|
+
layout.dockview.activeGroup && remainingGroups.has(layout.dockview.activeGroup)
|
|
189
|
+
? layout.dockview.activeGroup
|
|
190
|
+
: undefined;
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
...layout,
|
|
194
|
+
documents,
|
|
195
|
+
dockview: { ...layout.dockview, panels, activeGroup, grid: { ...layout.dockview.grid, root } },
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function isLayoutEmpty(layout: MdiLayout): boolean {
|
|
200
|
+
return Object.keys(layout.documents).length === 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Spec documents that must always be present (not closable) but are missing from the layout */
|
|
204
|
+
export function missingRequiredDocuments(
|
|
205
|
+
spec: MdiLayoutSpec,
|
|
206
|
+
types: MdiDocumentTypes,
|
|
207
|
+
layout: MdiLayout,
|
|
208
|
+
): (MdiDocumentSpec & { id: string })[] {
|
|
209
|
+
let missing: (MdiDocumentSpec & { id: string })[] = [];
|
|
210
|
+
|
|
211
|
+
function walk(node: MdiLayoutNode) {
|
|
212
|
+
if (isGroupSpec(node)) {
|
|
213
|
+
for (let tab of node.tabs) {
|
|
214
|
+
let definition = resolveType(types, tab.type);
|
|
215
|
+
let id = specDocumentId(types, tab);
|
|
216
|
+
|
|
217
|
+
if (!resolveClosable(definition, tab.closable) && !layout.documents[id]) {
|
|
218
|
+
missing.push({ ...tab, id });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
node.children.forEach(walk);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
walk(spec.root);
|
|
227
|
+
return missing;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function firstExistingPanel(node: MdiLayoutNode, types: MdiDocumentTypes, existing: Set<string>): string | undefined {
|
|
231
|
+
if (isGroupSpec(node)) {
|
|
232
|
+
for (let tab of node.tabs) {
|
|
233
|
+
let id = specDocumentId(types, tab);
|
|
234
|
+
if (existing.has(id)) return id;
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
for (let child of node.children) {
|
|
240
|
+
let found = firstExistingPanel(child, types, existing);
|
|
241
|
+
if (found) return found;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
type PlacementResult = { found: boolean; position?: MdiPosition; groupSpec?: MdiGroupSpec };
|
|
248
|
+
|
|
249
|
+
function findPlacement(
|
|
250
|
+
node: MdiLayoutNode,
|
|
251
|
+
id: string,
|
|
252
|
+
types: MdiDocumentTypes,
|
|
253
|
+
existing: Set<string>,
|
|
254
|
+
): PlacementResult {
|
|
255
|
+
if (isGroupSpec(node)) {
|
|
256
|
+
let ids = node.tabs.map((tab) => specDocumentId(types, tab));
|
|
257
|
+
|
|
258
|
+
if (!ids.includes(id)) {
|
|
259
|
+
return { found: false };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
let sibling = ids.find((tabId) => tabId !== id && existing.has(tabId));
|
|
263
|
+
return {
|
|
264
|
+
found: true,
|
|
265
|
+
groupSpec: node,
|
|
266
|
+
position: sibling ? { referencePanel: sibling, direction: "within" } : undefined,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
for (let [index, child] of node.children.entries()) {
|
|
271
|
+
let result = findPlacement(child, id, types, existing);
|
|
272
|
+
if (!result.found) continue;
|
|
273
|
+
if (result.position) return result;
|
|
274
|
+
|
|
275
|
+
// Place it next to the nearest sibling subtree that still has a panel in the layout.
|
|
276
|
+
for (let distance = 1; distance < node.children.length; distance++) {
|
|
277
|
+
for (let siblingIndex of [index - distance, index + distance]) {
|
|
278
|
+
let sibling = node.children[siblingIndex];
|
|
279
|
+
if (!sibling) continue;
|
|
280
|
+
|
|
281
|
+
let referencePanel = firstExistingPanel(sibling, types, existing);
|
|
282
|
+
if (!referencePanel) continue;
|
|
283
|
+
|
|
284
|
+
let before = siblingIndex < index;
|
|
285
|
+
let direction: MdiPosition["direction"] =
|
|
286
|
+
node.direction === "horizontal" ? (before ? "right" : "left") : before ? "below" : "above";
|
|
287
|
+
|
|
288
|
+
return { found: true, groupSpec: result.groupSpec, position: { referencePanel, direction } };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return { found: true, groupSpec: result.groupSpec };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return { found: false };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Where to add a spec document that is missing from a restored layout: in its spec group
|
|
300
|
+
* if a sibling tab is still open, otherwise next to the nearest panel of a neighbouring
|
|
301
|
+
* subtree, otherwise at the right edge.
|
|
302
|
+
*/
|
|
303
|
+
export function placementFor(
|
|
304
|
+
spec: MdiLayoutSpec,
|
|
305
|
+
id: string,
|
|
306
|
+
types: MdiDocumentTypes,
|
|
307
|
+
existing: Set<string>,
|
|
308
|
+
): { position: MdiPosition; groupSpec?: MdiGroupSpec } {
|
|
309
|
+
let result = findPlacement(spec.root, id, types, existing);
|
|
310
|
+
return { position: result.position ?? { direction: "right" }, groupSpec: result.groupSpec };
|
|
311
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { layoutSpecToLayout, listSpecDocuments, specDocumentId } from "./layoutSpec.js";
|
|
4
|
+
import { testSpec, testTypes } from "./testTypes.js";
|
|
5
|
+
import type { MdiLayoutSpec } from "./types.js";
|
|
6
|
+
|
|
7
|
+
describe("layoutSpecToLayout", () => {
|
|
8
|
+
it("builds the documents map with resolved titles and closable flags", () => {
|
|
9
|
+
let layout = layoutSpecToLayout(testSpec, testTypes, 1);
|
|
10
|
+
|
|
11
|
+
expect(layout.format).toBe(1);
|
|
12
|
+
expect(layout.layoutVersion).toBe(1);
|
|
13
|
+
expect(Object.keys(layout.documents)).toEqual(["explorer", "editor:a", "editor:b", "properties"]);
|
|
14
|
+
expect(layout.documents["explorer"]).toEqual({
|
|
15
|
+
type: "explorer",
|
|
16
|
+
key: undefined,
|
|
17
|
+
params: {},
|
|
18
|
+
title: "Explorer",
|
|
19
|
+
closable: false,
|
|
20
|
+
});
|
|
21
|
+
expect(layout.documents["editor:a"]).toMatchObject({
|
|
22
|
+
key: "a",
|
|
23
|
+
params: { name: "A" },
|
|
24
|
+
title: "Editor A",
|
|
25
|
+
closable: true,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("registers every panel on the shared mdi renderer", () => {
|
|
30
|
+
let layout = layoutSpecToLayout(testSpec, testTypes, 1);
|
|
31
|
+
|
|
32
|
+
for (let [id, panel] of Object.entries(layout.dockview.panels)) {
|
|
33
|
+
expect(panel).toEqual({ id, contentComponent: "mdi", tabComponent: "mdi", title: layout.documents[id]!.title });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("mirrors the split tree with proportional sizes and alternating orientation", () => {
|
|
38
|
+
let { dockview } = layoutSpecToLayout(testSpec, testTypes, 1);
|
|
39
|
+
let root = dockview.grid.root;
|
|
40
|
+
|
|
41
|
+
expect(dockview.grid.orientation).toBe("HORIZONTAL");
|
|
42
|
+
expect(root.type).toBe("branch");
|
|
43
|
+
|
|
44
|
+
let [explorer, right] = root.data as { type: string; size?: number; data: unknown }[];
|
|
45
|
+
expect(explorer!.type).toBe("leaf");
|
|
46
|
+
expect(explorer!.size).toBe(250);
|
|
47
|
+
expect(right!.type).toBe("branch");
|
|
48
|
+
expect(right!.size).toBe(750);
|
|
49
|
+
|
|
50
|
+
let [editors, properties] = right!.data as { type: string; size?: number; data: { views: string[] } }[];
|
|
51
|
+
expect(editors!.size).toBe(750);
|
|
52
|
+
expect(properties!.size).toBe(250);
|
|
53
|
+
expect(editors!.data.views).toEqual(["editor:a", "editor:b"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("carries group options and the active tab", () => {
|
|
57
|
+
let { dockview } = layoutSpecToLayout(testSpec, testTypes, 1);
|
|
58
|
+
let [explorer, right] = dockview.grid.root.data as { data: unknown }[];
|
|
59
|
+
let explorerGroup = explorer!.data as { id: string; locked?: unknown; hideHeader?: boolean; activeView?: string };
|
|
60
|
+
let editorsGroup = (right!.data as { data: { activeView?: string } }[])[0]!.data;
|
|
61
|
+
|
|
62
|
+
expect(explorerGroup).toMatchObject({
|
|
63
|
+
id: "mdi-g1",
|
|
64
|
+
locked: true,
|
|
65
|
+
hideHeader: true,
|
|
66
|
+
activeView: "explorer",
|
|
67
|
+
});
|
|
68
|
+
expect(editorsGroup.activeView).toBe("editor:b");
|
|
69
|
+
expect(dockview.activeGroup).toBe("mdi-g1");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("locks a group without a tab bar unless the spec says otherwise", () => {
|
|
73
|
+
let spec: MdiLayoutSpec = {
|
|
74
|
+
root: {
|
|
75
|
+
direction: "horizontal",
|
|
76
|
+
children: [
|
|
77
|
+
{ tabs: [{ type: "explorer" }], hideHeader: true },
|
|
78
|
+
{ tabs: [{ type: "properties" }], hideHeader: true, locked: false },
|
|
79
|
+
{ tabs: [{ type: "settings" }], locked: "no-drop-target" },
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
let { dockview } = layoutSpecToLayout(spec, testTypes, 1);
|
|
84
|
+
let groups = (dockview.grid.root.data as { data: { locked?: unknown } }[]).map((leaf) => leaf.data.locked);
|
|
85
|
+
|
|
86
|
+
expect(groups).toEqual([true, undefined, "no-drop-target"]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("wraps a root group in a branch", () => {
|
|
90
|
+
let layout = layoutSpecToLayout({ root: { tabs: [{ type: "settings" }] } }, testTypes, 0);
|
|
91
|
+
|
|
92
|
+
expect(layout.dockview.grid.root.type).toBe("branch");
|
|
93
|
+
expect((layout.dockview.grid.root.data as unknown[]).length).toBe(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("inlines nested splits with the same direction", () => {
|
|
97
|
+
let spec: MdiLayoutSpec = {
|
|
98
|
+
root: {
|
|
99
|
+
direction: "horizontal",
|
|
100
|
+
children: [
|
|
101
|
+
{ tabs: [{ type: "explorer" }], size: 2 },
|
|
102
|
+
{
|
|
103
|
+
direction: "horizontal",
|
|
104
|
+
size: 2,
|
|
105
|
+
children: [{ tabs: [{ type: "settings" }] }, { tabs: [{ type: "properties" }] }],
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
let { dockview } = layoutSpecToLayout(spec, testTypes, 0);
|
|
111
|
+
let children = dockview.grid.root.data as { type: string; size?: number }[];
|
|
112
|
+
|
|
113
|
+
expect(children.map((child) => child.type)).toEqual(["leaf", "leaf", "leaf"]);
|
|
114
|
+
expect(children.map((child) => child.size)).toEqual([500, 250, 250]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("rejects duplicate documents, unknown types and empty groups", () => {
|
|
118
|
+
expect(() =>
|
|
119
|
+
layoutSpecToLayout({ root: { tabs: [{ type: "settings" }, { type: "settings" }] } }, testTypes, 0),
|
|
120
|
+
).toThrow(/more than once/);
|
|
121
|
+
expect(() => layoutSpecToLayout({ root: { tabs: [{ type: "nope" }] } }, testTypes, 0)).toThrow(
|
|
122
|
+
/unknown document type/,
|
|
123
|
+
);
|
|
124
|
+
expect(() => layoutSpecToLayout({ root: { tabs: [] } }, testTypes, 0)).toThrow(/at least one tab/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("rejects an active tab that is not in the group", () => {
|
|
128
|
+
expect(() =>
|
|
129
|
+
layoutSpecToLayout({ root: { tabs: [{ type: "settings" }], active: "editor:a" } }, testTypes, 0),
|
|
130
|
+
).toThrow(/not part of its group/);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("spec helpers", () => {
|
|
135
|
+
it("ignores the key of singleton types", () => {
|
|
136
|
+
expect(specDocumentId(testTypes, { type: "explorer", key: "ignored" })).toBe("explorer");
|
|
137
|
+
expect(specDocumentId(testTypes, { type: "editor", key: "x" })).toBe("editor:x");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("lists every document in the spec", () => {
|
|
141
|
+
expect(listSpecDocuments(testSpec).map((tab) => tab.type)).toEqual(["explorer", "editor", "editor", "properties"]);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { GroupviewPanelState, SerializedDockview, SerializedGridObject } from "dockview-react";
|
|
2
|
+
import { Orientation } from "dockview-react";
|
|
3
|
+
|
|
4
|
+
import { panelId } from "./ids.js";
|
|
5
|
+
import { resolveClosable, resolveTitle, resolveType } from "./registry.js";
|
|
6
|
+
import type {
|
|
7
|
+
MdiDocumentSpec,
|
|
8
|
+
MdiDocumentState,
|
|
9
|
+
MdiDocumentTypes,
|
|
10
|
+
MdiGroupSpec,
|
|
11
|
+
MdiLayout,
|
|
12
|
+
MdiLayoutNode,
|
|
13
|
+
MdiLayoutSpec,
|
|
14
|
+
MdiLocked,
|
|
15
|
+
MdiSplitSpec,
|
|
16
|
+
} from "./types.js";
|
|
17
|
+
|
|
18
|
+
/** dockview does not export its group state type; derive it from the serialized layout. */
|
|
19
|
+
export type MdiGroupState = SerializedDockview["grid"]["root"] extends SerializedGridObject<infer T> ? T : never;
|
|
20
|
+
|
|
21
|
+
/** The single dockview component/tab renderer every Mdi panel uses */
|
|
22
|
+
export const MDI_COMPONENT = "mdi";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* dockview scales the serialized sizes proportionally to the real container, so the
|
|
26
|
+
* numbers only need to be consistent relative to each other.
|
|
27
|
+
*/
|
|
28
|
+
const NOMINAL_SIZE = 1000;
|
|
29
|
+
|
|
30
|
+
export function isGroupSpec(node: MdiLayoutNode): node is MdiGroupSpec {
|
|
31
|
+
return "tabs" in node;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A group without a tab bar refuses drops into it unless the spec says otherwise. */
|
|
35
|
+
export function resolveLocked(group: MdiGroupSpec): MdiLocked {
|
|
36
|
+
if (group.locked !== undefined) return group.locked;
|
|
37
|
+
return group.hideHeader ? true : false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function specDocumentId(types: MdiDocumentTypes, tab: MdiDocumentSpec): string {
|
|
41
|
+
let definition = resolveType(types, tab.type);
|
|
42
|
+
return panelId(tab.type, definition.singleton ? undefined : tab.key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function listSpecDocuments(spec: MdiLayoutSpec): MdiDocumentSpec[] {
|
|
46
|
+
let result: MdiDocumentSpec[] = [];
|
|
47
|
+
|
|
48
|
+
function walk(node: MdiLayoutNode) {
|
|
49
|
+
if (isGroupSpec(node)) {
|
|
50
|
+
result.push(...node.tabs);
|
|
51
|
+
} else {
|
|
52
|
+
node.children.forEach(walk);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
walk(spec.root);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type BuildContext = {
|
|
61
|
+
types: MdiDocumentTypes;
|
|
62
|
+
documents: Record<string, MdiDocumentState>;
|
|
63
|
+
panels: Record<string, GroupviewPanelState>;
|
|
64
|
+
groupCount: number;
|
|
65
|
+
firstGroup?: string;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function toOrientation(direction: MdiSplitSpec["direction"]): Orientation {
|
|
69
|
+
return direction === "horizontal" ? Orientation.HORIZONTAL : Orientation.VERTICAL;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function addDocument(tab: MdiDocumentSpec, context: BuildContext): string {
|
|
73
|
+
let definition = resolveType(context.types, tab.type);
|
|
74
|
+
let key = definition.singleton ? undefined : tab.key;
|
|
75
|
+
let id = panelId(tab.type, key);
|
|
76
|
+
|
|
77
|
+
if (context.documents[id]) {
|
|
78
|
+
throw new Error(`Mdi: the layout contains the document "${id}" more than once.`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let params = { ...tab.params };
|
|
82
|
+
let title = tab.title ?? resolveTitle(definition, tab.type, params, key);
|
|
83
|
+
|
|
84
|
+
context.documents[id] = {
|
|
85
|
+
type: tab.type,
|
|
86
|
+
key,
|
|
87
|
+
params,
|
|
88
|
+
title,
|
|
89
|
+
closable: resolveClosable(definition, tab.closable),
|
|
90
|
+
};
|
|
91
|
+
context.panels[id] = {
|
|
92
|
+
id,
|
|
93
|
+
contentComponent: MDI_COMPONENT,
|
|
94
|
+
tabComponent: MDI_COMPONENT,
|
|
95
|
+
title,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function serializeGroup(group: MdiGroupSpec, context: BuildContext): MdiGroupState {
|
|
102
|
+
if (group.tabs.length === 0) {
|
|
103
|
+
throw new Error("Mdi: every group in the layout needs at least one tab.");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
context.groupCount += 1;
|
|
107
|
+
let id = `mdi-g${context.groupCount}`;
|
|
108
|
+
let views = group.tabs.map((tab) => addDocument(tab, context));
|
|
109
|
+
|
|
110
|
+
if (group.active !== undefined && !views.includes(group.active)) {
|
|
111
|
+
throw new Error(`Mdi: active tab "${group.active}" is not part of its group.`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
context.firstGroup ??= id;
|
|
115
|
+
|
|
116
|
+
let state: MdiGroupState = {
|
|
117
|
+
id,
|
|
118
|
+
views,
|
|
119
|
+
activeView: group.active ?? views[0],
|
|
120
|
+
};
|
|
121
|
+
let locked = resolveLocked(group);
|
|
122
|
+
|
|
123
|
+
if (locked) {
|
|
124
|
+
state.locked = locked;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (group.hideHeader) {
|
|
128
|
+
state.hideHeader = true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return state;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The children of a split with their normalised weights. dockview alternates the
|
|
136
|
+
* orientation on every level of the tree, so a nested split with the same direction as
|
|
137
|
+
* its parent is inlined into the parent instead.
|
|
138
|
+
*/
|
|
139
|
+
function flattenChildren(split: MdiSplitSpec): { node: MdiLayoutNode; weight: number }[] {
|
|
140
|
+
let result: { node: MdiLayoutNode; weight: number }[] = [];
|
|
141
|
+
let total = split.children.reduce((sum, child) => sum + (child.size ?? 1), 0) || 1;
|
|
142
|
+
|
|
143
|
+
for (let child of split.children) {
|
|
144
|
+
let weight = (child.size ?? 1) / total;
|
|
145
|
+
|
|
146
|
+
if (!isGroupSpec(child) && child.direction === split.direction) {
|
|
147
|
+
for (let inner of flattenChildren(child)) {
|
|
148
|
+
result.push({ node: inner.node, weight: weight * inner.weight });
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
result.push({ node: child, weight });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A node's `size` is its extent along the parent's axis. A branch's children share the
|
|
160
|
+
* branch's extent along its own axis, which equals the parent's `size`.
|
|
161
|
+
*/
|
|
162
|
+
function serializeNode(
|
|
163
|
+
node: MdiLayoutNode,
|
|
164
|
+
size: number,
|
|
165
|
+
axisExtent: number,
|
|
166
|
+
context: BuildContext,
|
|
167
|
+
): SerializedGridObject<MdiGroupState> {
|
|
168
|
+
if (isGroupSpec(node)) {
|
|
169
|
+
return { type: "leaf", data: serializeGroup(node, context), size };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let children = flattenChildren(node);
|
|
173
|
+
|
|
174
|
+
if (children.length === 0) {
|
|
175
|
+
throw new Error("Mdi: every split in the layout needs at least one child.");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
type: "branch",
|
|
180
|
+
size,
|
|
181
|
+
data: children.map((child) => serializeNode(child.node, child.weight * axisExtent, size, context)),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Builds the persisted layout for a layout spec. */
|
|
186
|
+
export function layoutSpecToLayout(
|
|
187
|
+
spec: MdiLayoutSpec,
|
|
188
|
+
types: MdiDocumentTypes,
|
|
189
|
+
layoutVersion: string | number,
|
|
190
|
+
): MdiLayout {
|
|
191
|
+
let context: BuildContext = { types, documents: {}, panels: {}, groupCount: 0 };
|
|
192
|
+
let root: SerializedGridObject<MdiGroupState>;
|
|
193
|
+
let orientation: Orientation;
|
|
194
|
+
|
|
195
|
+
if (isGroupSpec(spec.root)) {
|
|
196
|
+
orientation = Orientation.HORIZONTAL;
|
|
197
|
+
root = {
|
|
198
|
+
type: "branch",
|
|
199
|
+
size: NOMINAL_SIZE,
|
|
200
|
+
data: [serializeNode(spec.root, NOMINAL_SIZE, NOMINAL_SIZE, context)],
|
|
201
|
+
};
|
|
202
|
+
} else {
|
|
203
|
+
orientation = toOrientation(spec.root.direction);
|
|
204
|
+
root = serializeNode(spec.root, NOMINAL_SIZE, NOMINAL_SIZE, context);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let dockview: SerializedDockview = {
|
|
208
|
+
grid: { root, width: NOMINAL_SIZE, height: NOMINAL_SIZE, orientation },
|
|
209
|
+
panels: context.panels,
|
|
210
|
+
activeGroup: context.firstGroup,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return { format: 1, layoutVersion, documents: context.documents, dockview };
|
|
214
|
+
}
|