@panyam/tsappkit-dock 0.0.0-stage → 0.7.8
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/LICENSE +202 -0
- package/README.md +55 -2
- package/dist/index.d.mts +202 -0
- package/dist/index.d.ts +202 -0
- package/dist/index.js +273 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +258 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -4
- package/src/dock.ts +275 -0
- package/src/drawer.ts +68 -0
- package/src/index.ts +26 -0
- package/src/layout.ts +144 -0
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@panyam/tsappkit-dock",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.8",
|
|
4
|
+
"description": "A dock for IDE-like pages on dockview-core: a panel registry, a default layout, a saved layout that picks up new panels, theme switching, and drawer sizing. It moves DOM elements and needs no UI framework.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"!src/**/*.test.ts",
|
|
19
|
+
"!src/**/*.test.tsx"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "tsc --noEmit && vitest run",
|
|
28
|
+
"test:watch": "vitest"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"dockview",
|
|
32
|
+
"dock",
|
|
33
|
+
"drawer",
|
|
34
|
+
"panels",
|
|
35
|
+
"layout",
|
|
36
|
+
"tsappkit"
|
|
37
|
+
],
|
|
38
|
+
"author": "panyam",
|
|
39
|
+
"license": "Apache-2.0",
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"dockview-core": ">=4.13.0 <9.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"dockview-core": "8.8.3",
|
|
45
|
+
"jsdom": "^29.1.1",
|
|
46
|
+
"solid-js": "^1.9.19",
|
|
47
|
+
"tsup": "^8.5.1",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vite-plugin-solid": "^2.11.16",
|
|
50
|
+
"vitest": "^5.0.3"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/panyam/goapplib.git",
|
|
55
|
+
"directory": "tsappkit-dock"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/dock.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { DockviewComponent, themeDark, themeLight } from "dockview-core";
|
|
2
|
+
import type { DockviewApi, IContentRenderer } from "dockview-core";
|
|
3
|
+
import {
|
|
4
|
+
documentTheme,
|
|
5
|
+
loadLayout,
|
|
6
|
+
panelsToPrune,
|
|
7
|
+
panelsToReconcile,
|
|
8
|
+
saveLayout,
|
|
9
|
+
type DockStorage,
|
|
10
|
+
type PanelDef,
|
|
11
|
+
type PanelPosition,
|
|
12
|
+
type PanelSpec,
|
|
13
|
+
} from "./layout.js";
|
|
14
|
+
|
|
15
|
+
/** What `createDock` needs. */
|
|
16
|
+
export interface DockOptions {
|
|
17
|
+
/** The element dockview renders into. It needs a size, as dockview fills it. */
|
|
18
|
+
container: HTMLElement;
|
|
19
|
+
/** The panel registry, in a stable order. Only these ids get content. */
|
|
20
|
+
panels: readonly PanelDef[];
|
|
21
|
+
/**
|
|
22
|
+
* The arrangement used when nothing is saved, after a rejected save, and on `reset`. Either
|
|
23
|
+
* `addPanel` specs as data, or a function that builds it on the api, for an app that sizes
|
|
24
|
+
* groups after adding them.
|
|
25
|
+
*/
|
|
26
|
+
defaultLayout: readonly PanelSpec[] | ((api: DockviewApi) => void);
|
|
27
|
+
/**
|
|
28
|
+
* Returns the element a panel shows. The dock calls it each time the panel enters the grid, so
|
|
29
|
+
* return the same element every time if its content should survive a close and reopen. It is
|
|
30
|
+
* only called with registry ids.
|
|
31
|
+
*/
|
|
32
|
+
hostFor: (id: string) => HTMLElement;
|
|
33
|
+
/**
|
|
34
|
+
* Called when a panel leaves the grid, with the element `hostFor` gave it. dockview has already
|
|
35
|
+
* detached it, so this is where an app parks the element somewhere hidden, or tears its content
|
|
36
|
+
* down. Clearing the layout, as `reset` does, calls it for every open panel.
|
|
37
|
+
*/
|
|
38
|
+
release?: (id: string, el: HTMLElement) => void;
|
|
39
|
+
/** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
|
|
40
|
+
storage?: DockStorage | null;
|
|
41
|
+
/** The storage key. Required with `storage`. */
|
|
42
|
+
key?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the dock's theme. It is read at creation, when `<html data-theme>` changes, when the
|
|
45
|
+
* OS colour scheme changes, and on `applyTheme()`. Default: `documentTheme()`.
|
|
46
|
+
*/
|
|
47
|
+
theme?: () => "light" | "dark";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A dock made by `createDock`. */
|
|
51
|
+
export interface Dock {
|
|
52
|
+
/** The dockview api, for anything the dock doesn't wrap. */
|
|
53
|
+
readonly api: DockviewApi;
|
|
54
|
+
/**
|
|
55
|
+
* Brings a panel to the front, opening it first if it's closed. It returns false, and does
|
|
56
|
+
* nothing, for an id the registry lacks.
|
|
57
|
+
*/
|
|
58
|
+
show(id: string): boolean;
|
|
59
|
+
/** Opens a closed panel or closes an open one. False for an id the registry lacks. */
|
|
60
|
+
toggle(id: string): boolean;
|
|
61
|
+
/** Forgets the saved layout and applies the default one. Panels keep their `hostFor` elements. */
|
|
62
|
+
reset(): void;
|
|
63
|
+
/** Reads `theme` again and applies it. For a theme setting the dock doesn't watch. */
|
|
64
|
+
applyTheme(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Adds a Panels menu button to `host`. Its list has one item per registry panel, each toggling
|
|
67
|
+
* its panel, with a ✓ on the open ones, and then Reset layout. Returns a function that removes
|
|
68
|
+
* it. The elements carry `dock-panels-*` classes and no styles beyond showing and hiding the
|
|
69
|
+
* list, so the app styles them.
|
|
70
|
+
*/
|
|
71
|
+
panelsMenu(host: HTMLElement): () => void;
|
|
72
|
+
/**
|
|
73
|
+
* Saves the layout, stops watching the theme, removes any Panels menus, and disposes of
|
|
74
|
+
* dockview. Changes are otherwise saved a microtask after dockview reports them. A second call
|
|
75
|
+
* does nothing.
|
|
76
|
+
*/
|
|
77
|
+
dispose(): void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Builds a dockview dock from a panel registry. It restores the saved layout when there is one,
|
|
82
|
+
* opens the panels added to the registry since it was saved, and closes the ones the registry
|
|
83
|
+
* dropped. A save that dockview rejects is removed and the default layout applied. Every layout
|
|
84
|
+
* change after that is saved.
|
|
85
|
+
*
|
|
86
|
+
* Content lives outside dockview. `hostFor` hands an element to a panel when it enters the grid,
|
|
87
|
+
* and `release` hands it back when it leaves, so an app can keep a panel's content alive while
|
|
88
|
+
* the panel is closed.
|
|
89
|
+
*/
|
|
90
|
+
export function createDock(opts: DockOptions): Dock {
|
|
91
|
+
const { container, panels, hostFor, release } = opts;
|
|
92
|
+
const storage = opts.storage ?? null;
|
|
93
|
+
const key = opts.key ?? "";
|
|
94
|
+
if (storage && !key) throw new Error("createDock: storage needs a key");
|
|
95
|
+
const theme = opts.theme ?? (() => documentTheme(container.ownerDocument));
|
|
96
|
+
const doc = container.ownerDocument;
|
|
97
|
+
const byId = new Map(panels.map((p) => [p.id, p]));
|
|
98
|
+
const specs = Array.isArray(opts.defaultLayout) ? (opts.defaultLayout as readonly PanelSpec[]) : null;
|
|
99
|
+
const cleanups: (() => void)[] = [];
|
|
100
|
+
let disposed = false;
|
|
101
|
+
|
|
102
|
+
const renderer = (id: string): IContentRenderer => {
|
|
103
|
+
if (!byId.has(id)) return { element: doc.createElement("div"), init: () => {} };
|
|
104
|
+
const element = hostFor(id);
|
|
105
|
+
return { element, init: () => {}, dispose: () => release?.(id, element) };
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const component = new DockviewComponent(container, {
|
|
109
|
+
createComponent: (o) => renderer(o.id),
|
|
110
|
+
// "always" keeps a background tab's content attached, only hidden. The default detaches it,
|
|
111
|
+
// and an app that finds its content by id at startup would miss the panels behind a tab.
|
|
112
|
+
defaultRenderer: "always",
|
|
113
|
+
theme: theme() === "dark" ? themeDark : themeLight,
|
|
114
|
+
});
|
|
115
|
+
const api = component.api;
|
|
116
|
+
|
|
117
|
+
const usable = (p?: PanelPosition): p is PanelPosition => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
|
|
118
|
+
|
|
119
|
+
const open = (id: string): void => {
|
|
120
|
+
const def = byId.get(id)!;
|
|
121
|
+
const spec = specs?.find((s) => s.id === id);
|
|
122
|
+
const position = [def.position, spec?.position].find(usable);
|
|
123
|
+
api.addPanel({ id, component: id, title: def.title, ...(position ? { position } : {}) });
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const applyDefault = (): void => {
|
|
127
|
+
if (!specs) {
|
|
128
|
+
(opts.defaultLayout as (api: DockviewApi) => void)(api);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
for (const spec of specs) {
|
|
132
|
+
const def = byId.get(spec.id);
|
|
133
|
+
if (!def || api.getPanel(spec.id)) continue;
|
|
134
|
+
const position = usable(spec.position) ? spec.position : undefined;
|
|
135
|
+
api.addPanel({ id: spec.id, component: spec.id, title: spec.title ?? def.title, ...(position ? { position } : {}) });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const restore = (): void => {
|
|
140
|
+
const saved = storage ? loadLayout(storage, key) : null;
|
|
141
|
+
if (!saved) {
|
|
142
|
+
applyDefault();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
api.fromJSON(saved.layout as Parameters<DockviewApi["fromJSON"]>[0]);
|
|
147
|
+
for (const id of panelsToPrune(api.panels.map((p) => p.id), panels)) {
|
|
148
|
+
const panel = api.getPanel(id);
|
|
149
|
+
if (panel) api.removePanel(panel);
|
|
150
|
+
}
|
|
151
|
+
for (const id of panelsToReconcile(saved.savedPanels, panels)) {
|
|
152
|
+
if (!api.getPanel(id)) open(id);
|
|
153
|
+
}
|
|
154
|
+
} catch (err) {
|
|
155
|
+
console.warn("tsappkit-dock: saved layout rejected, using the default", err);
|
|
156
|
+
storage?.removeItem(key);
|
|
157
|
+
api.clear();
|
|
158
|
+
applyDefault();
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
restore();
|
|
163
|
+
if (storage) {
|
|
164
|
+
// Save what restore built, so the save names today's registry and a rejected save is replaced.
|
|
165
|
+
saveLayout(storage, key, panels, api.toJSON());
|
|
166
|
+
const sub = api.onDidLayoutChange(() => saveLayout(storage, key, panels, api.toJSON()));
|
|
167
|
+
cleanups.push(() => sub.dispose());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const applyTheme = (): void => component.updateOptions({ theme: theme() === "dark" ? themeDark : themeLight });
|
|
171
|
+
const view = doc.defaultView;
|
|
172
|
+
if (view?.MutationObserver) {
|
|
173
|
+
const mo = new view.MutationObserver(applyTheme);
|
|
174
|
+
mo.observe(doc.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
|
|
175
|
+
cleanups.push(() => mo.disconnect());
|
|
176
|
+
}
|
|
177
|
+
const mq = view?.matchMedia?.("(prefers-color-scheme: dark)");
|
|
178
|
+
if (mq?.addEventListener) {
|
|
179
|
+
mq.addEventListener("change", applyTheme);
|
|
180
|
+
cleanups.push(() => mq.removeEventListener("change", applyTheme));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const reset = (): void => {
|
|
184
|
+
storage?.removeItem(key);
|
|
185
|
+
api.clear();
|
|
186
|
+
applyDefault();
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const toggle = (id: string): boolean => {
|
|
190
|
+
if (!byId.has(id)) return false;
|
|
191
|
+
const panel = api.getPanel(id);
|
|
192
|
+
if (panel) api.removePanel(panel);
|
|
193
|
+
else open(id);
|
|
194
|
+
return true;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const panelsMenu = (host: HTMLElement): (() => void) => {
|
|
198
|
+
const btn = doc.createElement("button");
|
|
199
|
+
btn.type = "button";
|
|
200
|
+
btn.className = "dock-panels-btn";
|
|
201
|
+
btn.textContent = "Panels ▾";
|
|
202
|
+
btn.setAttribute("aria-haspopup", "true");
|
|
203
|
+
btn.setAttribute("aria-expanded", "false");
|
|
204
|
+
const list = doc.createElement("div");
|
|
205
|
+
list.className = "dock-panels-list";
|
|
206
|
+
list.style.display = "none";
|
|
207
|
+
const setOpen = (show: boolean): void => {
|
|
208
|
+
list.style.display = show ? "block" : "none";
|
|
209
|
+
btn.setAttribute("aria-expanded", String(show));
|
|
210
|
+
};
|
|
211
|
+
const item = (label: string, cls: string, act: () => void): HTMLButtonElement => {
|
|
212
|
+
const b = doc.createElement("button");
|
|
213
|
+
b.type = "button";
|
|
214
|
+
b.className = cls;
|
|
215
|
+
b.textContent = label;
|
|
216
|
+
b.addEventListener("click", () => {
|
|
217
|
+
act();
|
|
218
|
+
setOpen(false);
|
|
219
|
+
});
|
|
220
|
+
return b;
|
|
221
|
+
};
|
|
222
|
+
const rebuild = (): void => {
|
|
223
|
+
list.replaceChildren(
|
|
224
|
+
...panels.map((def) =>
|
|
225
|
+
item(`${api.getPanel(def.id) ? "✓" : " "} ${def.title}`, "dock-panels-item", () => toggle(def.id)),
|
|
226
|
+
),
|
|
227
|
+
item(" Reset layout", "dock-panels-item dock-panels-reset", reset),
|
|
228
|
+
);
|
|
229
|
+
};
|
|
230
|
+
btn.addEventListener("click", () => {
|
|
231
|
+
const show = list.style.display === "none";
|
|
232
|
+
if (show) rebuild();
|
|
233
|
+
setOpen(show);
|
|
234
|
+
});
|
|
235
|
+
const outside = (e: Event): void => {
|
|
236
|
+
if (!host.contains(e.target as Node)) setOpen(false);
|
|
237
|
+
};
|
|
238
|
+
const escape = (e: KeyboardEvent): void => {
|
|
239
|
+
if (e.key === "Escape") setOpen(false);
|
|
240
|
+
};
|
|
241
|
+
doc.addEventListener("click", outside);
|
|
242
|
+
doc.addEventListener("keydown", escape);
|
|
243
|
+
host.append(btn, list);
|
|
244
|
+
const remove = (): void => {
|
|
245
|
+
doc.removeEventListener("click", outside);
|
|
246
|
+
doc.removeEventListener("keydown", escape);
|
|
247
|
+
btn.remove();
|
|
248
|
+
list.remove();
|
|
249
|
+
};
|
|
250
|
+
cleanups.push(remove);
|
|
251
|
+
return remove;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
api,
|
|
256
|
+
show(id) {
|
|
257
|
+
if (!byId.has(id)) return false;
|
|
258
|
+
if (!api.getPanel(id)) open(id);
|
|
259
|
+
api.getPanel(id)?.api.setActive();
|
|
260
|
+
return true;
|
|
261
|
+
},
|
|
262
|
+
toggle,
|
|
263
|
+
reset,
|
|
264
|
+
applyTheme,
|
|
265
|
+
panelsMenu,
|
|
266
|
+
dispose() {
|
|
267
|
+
if (disposed) return;
|
|
268
|
+
disposed = true;
|
|
269
|
+
for (const c of cleanups.splice(0)) c();
|
|
270
|
+
// dockview reports layout changes a microtask late, so save now or lose the last change.
|
|
271
|
+
if (storage) saveLayout(storage, key, panels, api.toJSON());
|
|
272
|
+
component.dispose();
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
}
|
package/src/drawer.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The DOM-free half of a drawer: its size from a drag on any of the four edges, clamped, and saved.
|
|
2
|
+
// The drawer element itself comes in a later release (#133).
|
|
3
|
+
|
|
4
|
+
/** The page edge a drawer is anchored to. */
|
|
5
|
+
export type DrawerEdge = "left" | "right" | "top" | "bottom";
|
|
6
|
+
|
|
7
|
+
/** The smallest a drawer gets, in px, unless the caller passes its own `min`. */
|
|
8
|
+
export const DRAWER_MIN = 280;
|
|
9
|
+
/** The largest share of the viewport a drawer takes, unless the caller passes its own `maxFrac`. */
|
|
10
|
+
export const DRAWER_MAX_FRAC = 0.9;
|
|
11
|
+
/** A drawer's size when nothing is saved. */
|
|
12
|
+
export const DRAWER_DEFAULT = 420;
|
|
13
|
+
|
|
14
|
+
/** Bounds for `clampSize` and `drawerSize`. */
|
|
15
|
+
export interface DrawerBounds {
|
|
16
|
+
min?: number;
|
|
17
|
+
maxFrac?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Clamps `px` to `[min, viewport * maxFrac]` and rounds it. A viewport too small for the minimum
|
|
22
|
+
* gets the minimum, so the drawer can overflow a tiny window rather than vanish.
|
|
23
|
+
*/
|
|
24
|
+
export function clampSize(px: number, viewport: number, bounds: DrawerBounds = {}): number {
|
|
25
|
+
const min = bounds.min ?? DRAWER_MIN;
|
|
26
|
+
const max = Math.max(min, Math.floor(viewport * (bounds.maxFrac ?? DRAWER_MAX_FRAC)));
|
|
27
|
+
return Math.min(max, Math.max(min, Math.round(px)));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Turns a drag on a drawer's inner edge into its size. The size is the distance from the pointer
|
|
32
|
+
* to the anchored edge, then clamped. Pass the pointer's `clientX` and the viewport's width for a
|
|
33
|
+
* left or right drawer, and `clientY` and the height for a top or bottom one.
|
|
34
|
+
*/
|
|
35
|
+
export function drawerSize(edge: DrawerEdge, pointer: number, viewport: number, bounds?: DrawerBounds): number {
|
|
36
|
+
const fromEdge = edge === "left" || edge === "top" ? pointer : viewport - pointer;
|
|
37
|
+
return clampSize(fromEdge, viewport, bounds);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The part of Web Storage drawer sizes need. A null storage means sizes aren't saved. */
|
|
41
|
+
export interface DrawerStorage {
|
|
42
|
+
getItem(key: string): string | null;
|
|
43
|
+
setItem(key: string, value: string): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reads a saved size, or `fallback` when nothing is saved or the value isn't a positive number.
|
|
48
|
+
* It doesn't clamp, since the viewport may have changed; clamp against the live one when applying.
|
|
49
|
+
*/
|
|
50
|
+
export function loadDrawerSize(storage: DrawerStorage | null, key: string, fallback = DRAWER_DEFAULT): number {
|
|
51
|
+
let raw: string | null = null;
|
|
52
|
+
try {
|
|
53
|
+
raw = storage?.getItem(key) ?? null;
|
|
54
|
+
} catch {
|
|
55
|
+
return fallback;
|
|
56
|
+
}
|
|
57
|
+
const n = raw == null ? NaN : parseInt(raw, 10);
|
|
58
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Saves a size, rounded. A storage that throws is ignored. */
|
|
62
|
+
export function saveDrawerSize(storage: DrawerStorage | null, key: string, px: number): void {
|
|
63
|
+
try {
|
|
64
|
+
storage?.setItem(key, String(Math.round(px)));
|
|
65
|
+
} catch {
|
|
66
|
+
// best effort
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { createDock, type Dock, type DockOptions } from "./dock.js";
|
|
2
|
+
export {
|
|
3
|
+
documentTheme,
|
|
4
|
+
loadLayout,
|
|
5
|
+
panelsToPrune,
|
|
6
|
+
panelsToReconcile,
|
|
7
|
+
resolveTheme,
|
|
8
|
+
saveLayout,
|
|
9
|
+
type DockStorage,
|
|
10
|
+
type LoadedLayout,
|
|
11
|
+
type PanelDef,
|
|
12
|
+
type PanelPosition,
|
|
13
|
+
type PanelSpec,
|
|
14
|
+
} from "./layout.js";
|
|
15
|
+
export {
|
|
16
|
+
clampSize,
|
|
17
|
+
drawerSize,
|
|
18
|
+
loadDrawerSize,
|
|
19
|
+
saveDrawerSize,
|
|
20
|
+
DRAWER_DEFAULT,
|
|
21
|
+
DRAWER_MAX_FRAC,
|
|
22
|
+
DRAWER_MIN,
|
|
23
|
+
type DrawerBounds,
|
|
24
|
+
type DrawerEdge,
|
|
25
|
+
type DrawerStorage,
|
|
26
|
+
} from "./drawer.js";
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// The DOM-free half of the dock: the panel registry's types, the saved layout, reconcile and prune,
|
|
2
|
+
// and theme resolution. It needs neither dockview nor a browser, so it unit-tests on its own.
|
|
3
|
+
// dock.ts feeds these to dockview-core.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One panel the app can show. The registry is a list of these in a stable order, and it is saved
|
|
7
|
+
* with every layout so a later load can tell a panel the app added since apart from one the reader
|
|
8
|
+
* closed.
|
|
9
|
+
*/
|
|
10
|
+
export interface PanelDef {
|
|
11
|
+
/** The panel's dockview id. It is also its component name and its saved name, so keep it stable. */
|
|
12
|
+
id: string;
|
|
13
|
+
/** The tab label. */
|
|
14
|
+
title: string;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a panel the registry gained after a layout was saved opens on that layout's next load.
|
|
17
|
+
* Default `true`. Set `false` for a panel that should appear only in the Panels menu, so a new
|
|
18
|
+
* feature doesn't crowd an existing reader's arrangement.
|
|
19
|
+
*/
|
|
20
|
+
defaultOpen?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Where the panel opens when it isn't in the layout yet: from reconcile, `show`, or the Panels
|
|
23
|
+
* menu. When the reference panel isn't open, or this is absent, the panel's spec in the default
|
|
24
|
+
* layout is tried next, and then the grid root.
|
|
25
|
+
*/
|
|
26
|
+
position?: PanelPosition;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A dockview `addPanel` position: a direction from a panel that is already open. */
|
|
30
|
+
export interface PanelPosition {
|
|
31
|
+
direction: "left" | "right" | "above" | "below" | "within";
|
|
32
|
+
referencePanel?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One `addPanel` call written as data. A default layout is a list of these, ordered so every
|
|
37
|
+
* `referencePanel` is added before something refers to it. A spec without a position anchors its
|
|
38
|
+
* panel at the grid root. `title` defaults to the registry's.
|
|
39
|
+
*/
|
|
40
|
+
export interface PanelSpec {
|
|
41
|
+
id: string;
|
|
42
|
+
title?: string;
|
|
43
|
+
position?: PanelPosition;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The part of Web Storage the dock needs. `localStorage` satisfies it, and tests pass a fake. */
|
|
47
|
+
export interface DockStorage {
|
|
48
|
+
getItem(key: string): string | null;
|
|
49
|
+
setItem(key: string, value: string): void;
|
|
50
|
+
removeItem(key: string): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface SavedLayout {
|
|
54
|
+
v: 1;
|
|
55
|
+
panels: string[];
|
|
56
|
+
layout: unknown;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* What `loadLayout` returns. `layout` is dockview's `toJSON()` blob. `savedPanels` is the registry
|
|
61
|
+
* when it was saved, and it is empty for a bare blob from before saves carried one.
|
|
62
|
+
*/
|
|
63
|
+
export interface LoadedLayout {
|
|
64
|
+
layout: unknown;
|
|
65
|
+
savedPanels: string[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Saves dockview's `toJSON()` blob as `{v: 1, panels, layout}`, where `panels` is the registry's
|
|
70
|
+
* ids. A storage that throws (quota, private mode) is ignored, so the page keeps working and the
|
|
71
|
+
* layout just isn't saved.
|
|
72
|
+
*/
|
|
73
|
+
export function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown): void {
|
|
74
|
+
try {
|
|
75
|
+
const saved: SavedLayout = { v: 1, panels: registry.map((p) => p.id), layout };
|
|
76
|
+
storage.setItem(key, JSON.stringify(saved));
|
|
77
|
+
} catch {
|
|
78
|
+
// best effort
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Reads a saved layout. It returns null when nothing is saved or the save isn't JSON, and the
|
|
84
|
+
* caller applies its default layout in both cases. A bare dockview blob, saved before the registry
|
|
85
|
+
* was saved with it, loads with an empty `savedPanels`.
|
|
86
|
+
*/
|
|
87
|
+
export function loadLayout(storage: DockStorage, key: string): LoadedLayout | null {
|
|
88
|
+
let raw: string | null;
|
|
89
|
+
try {
|
|
90
|
+
raw = storage.getItem(key);
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
if (!raw) return null;
|
|
95
|
+
try {
|
|
96
|
+
const parsed: unknown = JSON.parse(raw);
|
|
97
|
+
if (parsed && typeof parsed === "object" && "v" in parsed && "layout" in parsed) {
|
|
98
|
+
const s = parsed as SavedLayout;
|
|
99
|
+
return { layout: s.layout, savedPanels: Array.isArray(s.panels) ? s.panels : [] };
|
|
100
|
+
}
|
|
101
|
+
return { layout: parsed, savedPanels: [] };
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns the ids of panels to open on a restored layout. These are the registry's panels that
|
|
109
|
+
* the saved registry lacks and that don't have `defaultOpen: false`. A panel the reader closed was
|
|
110
|
+
* in the saved registry, so it stays closed. A bare save has an empty registry, so every panel
|
|
111
|
+
* counts as new once; the caller skips the ones the layout already has open.
|
|
112
|
+
*/
|
|
113
|
+
export function panelsToReconcile(savedPanels: readonly string[], registry: readonly PanelDef[]): string[] {
|
|
114
|
+
return registry.filter((p) => p.defaultOpen !== false && !savedPanels.includes(p.id)).map((p) => p.id);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns the ids in `open` that the registry no longer has. A restored layout can name a panel a
|
|
119
|
+
* later release removed, and without pruning it shows as an empty tab.
|
|
120
|
+
*/
|
|
121
|
+
export function panelsToPrune(open: readonly string[], registry: readonly PanelDef[]): string[] {
|
|
122
|
+
const known = new Set(registry.map((p) => p.id));
|
|
123
|
+
return open.filter((id) => !known.has(id));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Maps a page's theme setting to light or dark. A `data-theme` of `light` or `dark` wins, and
|
|
128
|
+
* anything else follows the OS, which the caller passes as `prefersDark`.
|
|
129
|
+
*/
|
|
130
|
+
export function resolveTheme(dataTheme: string | null, prefersDark: boolean): "dark" | "light" {
|
|
131
|
+
if (dataTheme === "dark") return "dark";
|
|
132
|
+
if (dataTheme === "light") return "light";
|
|
133
|
+
return prefersDark ? "dark" : "light";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Reads `resolveTheme` off the document: `data-theme` on `<html>`, then `prefers-color-scheme`.
|
|
138
|
+
* It is `createDock`'s default `theme`.
|
|
139
|
+
*/
|
|
140
|
+
export function documentTheme(doc: Document = document): "dark" | "light" {
|
|
141
|
+
const forced = doc.documentElement.getAttribute("data-theme");
|
|
142
|
+
const prefersDark = doc.defaultView?.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
|
|
143
|
+
return resolveTheme(forced, prefersDark);
|
|
144
|
+
}
|