@panyam/tsappkit-dock 0.0.0-stage → 0.7.9
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 +60 -2
- package/dist/index.d.mts +274 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.js +659 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +644 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -4
- package/src/dock.ts +566 -0
- package/src/drawer.ts +68 -0
- package/src/drawerView.ts +233 -0
- package/src/index.ts +30 -0
- package/src/layout.ts +174 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// The drawer element: one per edge, laid over the dock with a scrim behind it. It holds the panels
|
|
2
|
+
// placed in it as tabs, and dock.ts moves their elements in and out. Styles are inline and minimal
|
|
3
|
+
// (position, size, the scrim), so a drawer works with no CSS; everything else hangs off the
|
|
4
|
+
// dock-drawer-* classes for the app to style.
|
|
5
|
+
import { clampSize, drawerSize, DRAWER_DEFAULT, type DrawerBounds, type DrawerEdge } from "./drawer.js";
|
|
6
|
+
|
|
7
|
+
/** What one drawer saves: its panels in tab order, the active one, its size, and whether it's open. */
|
|
8
|
+
export interface DrawerState {
|
|
9
|
+
panels: string[];
|
|
10
|
+
active?: string;
|
|
11
|
+
size?: number;
|
|
12
|
+
open?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Per-edge drawer settings for `createDock`. */
|
|
16
|
+
export interface DrawerConfig extends DrawerBounds {
|
|
17
|
+
/** The size, in px, when nothing is saved. Default `DRAWER_DEFAULT`. */
|
|
18
|
+
size?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Tab {
|
|
22
|
+
id: string;
|
|
23
|
+
el: HTMLElement;
|
|
24
|
+
button: HTMLButtonElement;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// dockview stacks floating groups and always-rendered panel content at --dv-overlay-z-index (999)
|
|
28
|
+
// and up to 100 above it, so a drawer and its scrim go above all of that.
|
|
29
|
+
const Z_SCRIM = "calc(var(--dv-overlay-z-index, 999) + 200)";
|
|
30
|
+
const Z_DRAWER = "calc(var(--dv-overlay-z-index, 999) + 201)";
|
|
31
|
+
|
|
32
|
+
const HORIZONTAL: Record<DrawerEdge, boolean> = { left: true, right: true, top: false, bottom: false };
|
|
33
|
+
|
|
34
|
+
export class DrawerView {
|
|
35
|
+
readonly root: HTMLElement;
|
|
36
|
+
readonly scrim: HTMLElement;
|
|
37
|
+
readonly tabStrip: HTMLElement;
|
|
38
|
+
readonly body: HTMLElement;
|
|
39
|
+
readonly handle: HTMLElement;
|
|
40
|
+
private tabs: Tab[] = [];
|
|
41
|
+
private activeId?: string;
|
|
42
|
+
private isOpen = false;
|
|
43
|
+
private px: number;
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
readonly edge: DrawerEdge,
|
|
47
|
+
private readonly host: HTMLElement,
|
|
48
|
+
private readonly config: DrawerConfig,
|
|
49
|
+
private readonly hooks: {
|
|
50
|
+
changed: () => void;
|
|
51
|
+
menu: (anchor: HTMLElement, id: string) => void;
|
|
52
|
+
},
|
|
53
|
+
) {
|
|
54
|
+
const doc = host.ownerDocument;
|
|
55
|
+
this.px = config.size ?? DRAWER_DEFAULT;
|
|
56
|
+
|
|
57
|
+
this.scrim = doc.createElement("div");
|
|
58
|
+
this.scrim.className = `dock-drawer-scrim dock-drawer-scrim-${edge}`;
|
|
59
|
+
Object.assign(this.scrim.style, { position: "absolute", inset: "0", background: "rgba(0,0,0,.25)", zIndex: Z_SCRIM, display: "none" });
|
|
60
|
+
this.scrim.addEventListener("click", () => this.setOpen(false));
|
|
61
|
+
|
|
62
|
+
this.root = doc.createElement("aside");
|
|
63
|
+
this.root.className = `dock-drawer dock-drawer-${edge}`;
|
|
64
|
+
this.root.dataset.edge = edge;
|
|
65
|
+
Object.assign(this.root.style, {
|
|
66
|
+
position: "absolute",
|
|
67
|
+
zIndex: Z_DRAWER,
|
|
68
|
+
display: "none",
|
|
69
|
+
flexDirection: "column",
|
|
70
|
+
background: "var(--dv-group-view-background-color, Canvas)",
|
|
71
|
+
color: "var(--dv-activegroup-visiblepanel-tab-color, CanvasText)",
|
|
72
|
+
boxShadow: "0 0 16px rgba(0,0,0,.35)",
|
|
73
|
+
[edge]: "0",
|
|
74
|
+
...(HORIZONTAL[edge] ? { top: "0", bottom: "0" } : { left: "0", right: "0" }),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.tabStrip = doc.createElement("div");
|
|
78
|
+
this.tabStrip.className = "dock-drawer-tabs";
|
|
79
|
+
this.tabStrip.setAttribute("role", "tablist");
|
|
80
|
+
Object.assign(this.tabStrip.style, { display: "flex", gap: "2px", alignItems: "center", flex: "none" });
|
|
81
|
+
|
|
82
|
+
this.body = doc.createElement("div");
|
|
83
|
+
this.body.className = "dock-drawer-body";
|
|
84
|
+
Object.assign(this.body.style, { flex: "1", minHeight: "0", overflow: "auto", position: "relative" });
|
|
85
|
+
|
|
86
|
+
this.handle = doc.createElement("div");
|
|
87
|
+
this.handle.className = "dock-drawer-handle";
|
|
88
|
+
const inner = { left: "right", right: "left", top: "bottom", bottom: "top" }[edge];
|
|
89
|
+
Object.assign(this.handle.style, {
|
|
90
|
+
position: "absolute",
|
|
91
|
+
[inner]: "-3px",
|
|
92
|
+
...(HORIZONTAL[edge] ? { top: "0", bottom: "0", width: "6px", cursor: "col-resize" } : { left: "0", right: "0", height: "6px", cursor: "row-resize" }),
|
|
93
|
+
touchAction: "none",
|
|
94
|
+
});
|
|
95
|
+
this.handle.addEventListener("pointerdown", (e) => this.drag(e));
|
|
96
|
+
|
|
97
|
+
this.root.append(this.tabStrip, this.body, this.handle);
|
|
98
|
+
host.append(this.scrim, this.root);
|
|
99
|
+
this.applySize();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get ids(): string[] {
|
|
103
|
+
return this.tabs.map((t) => t.id);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get active(): string | undefined {
|
|
107
|
+
return this.activeId;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get open(): boolean {
|
|
111
|
+
return this.isOpen;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get size(): number {
|
|
115
|
+
return this.px;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
has(id: string): boolean {
|
|
119
|
+
return this.tabs.some((t) => t.id === id);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Adds a panel as a tab, or does nothing if it's already here. It doesn't open the drawer. */
|
|
123
|
+
add(id: string, title: string, el: HTMLElement): void {
|
|
124
|
+
if (this.has(id)) return;
|
|
125
|
+
const doc = this.host.ownerDocument;
|
|
126
|
+
const button = doc.createElement("button");
|
|
127
|
+
button.type = "button";
|
|
128
|
+
button.className = "dock-drawer-tab";
|
|
129
|
+
button.setAttribute("role", "tab");
|
|
130
|
+
button.dataset.panel = id;
|
|
131
|
+
const label = doc.createElement("span");
|
|
132
|
+
label.textContent = title;
|
|
133
|
+
const more = doc.createElement("span");
|
|
134
|
+
more.className = "dock-drawer-tab-menu";
|
|
135
|
+
more.textContent = "⋯";
|
|
136
|
+
more.title = "Move this panel";
|
|
137
|
+
more.addEventListener("click", (e) => {
|
|
138
|
+
e.stopPropagation();
|
|
139
|
+
this.hooks.menu(more, id);
|
|
140
|
+
});
|
|
141
|
+
button.append(label, " ", more);
|
|
142
|
+
button.addEventListener("click", () => this.activate(id));
|
|
143
|
+
this.tabStrip.appendChild(button);
|
|
144
|
+
el.style.display = "none";
|
|
145
|
+
this.body.appendChild(el);
|
|
146
|
+
this.tabs.push({ id, el, button });
|
|
147
|
+
if (!this.activeId) this.activate(id, false);
|
|
148
|
+
this.hooks.changed();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Takes a panel out and returns its element, detached. The next tab becomes active. */
|
|
152
|
+
remove(id: string): HTMLElement | undefined {
|
|
153
|
+
const i = this.tabs.findIndex((t) => t.id === id);
|
|
154
|
+
if (i < 0) return undefined;
|
|
155
|
+
const [tab] = this.tabs.splice(i, 1);
|
|
156
|
+
tab.button.remove();
|
|
157
|
+
tab.el.remove();
|
|
158
|
+
tab.el.style.display = "";
|
|
159
|
+
if (this.activeId === id) {
|
|
160
|
+
this.activeId = undefined;
|
|
161
|
+
const next = this.tabs[Math.min(i, this.tabs.length - 1)];
|
|
162
|
+
if (next) this.activate(next.id, false);
|
|
163
|
+
}
|
|
164
|
+
if (this.tabs.length === 0) this.setOpen(false, false);
|
|
165
|
+
this.hooks.changed();
|
|
166
|
+
return tab.el;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
activate(id: string, notify = true): void {
|
|
170
|
+
if (!this.has(id)) return;
|
|
171
|
+
this.activeId = id;
|
|
172
|
+
for (const t of this.tabs) {
|
|
173
|
+
const on = t.id === id;
|
|
174
|
+
t.el.style.display = on ? "" : "none";
|
|
175
|
+
t.button.classList.toggle("dock-drawer-tab-active", on);
|
|
176
|
+
t.button.setAttribute("aria-selected", String(on));
|
|
177
|
+
}
|
|
178
|
+
if (notify) this.hooks.changed();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
setOpen(open: boolean, notify = true): void {
|
|
182
|
+
const show = open && this.tabs.length > 0;
|
|
183
|
+
if (show === this.isOpen) return;
|
|
184
|
+
this.isOpen = show;
|
|
185
|
+
this.root.style.display = show ? "flex" : "none";
|
|
186
|
+
this.scrim.style.display = show ? "block" : "none";
|
|
187
|
+
if (show) this.applySize();
|
|
188
|
+
if (notify) this.hooks.changed();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
setSize(px: number, notify = true): void {
|
|
192
|
+
this.px = Math.round(px);
|
|
193
|
+
this.applySize();
|
|
194
|
+
if (notify) this.hooks.changed();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
state(): DrawerState {
|
|
198
|
+
return { panels: this.ids, active: this.activeId, size: this.px, open: this.isOpen };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
dispose(): void {
|
|
202
|
+
this.scrim.remove();
|
|
203
|
+
this.root.remove();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
|
|
207
|
+
private applySize(): void {
|
|
208
|
+
const rect = this.host.getBoundingClientRect();
|
|
209
|
+
const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
|
|
210
|
+
const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
|
|
211
|
+
this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = `${px}px`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private drag(e: PointerEvent): void {
|
|
215
|
+
e.preventDefault();
|
|
216
|
+
const move = (ev: PointerEvent): void => {
|
|
217
|
+
const rect = this.host.getBoundingClientRect();
|
|
218
|
+
const horizontal = HORIZONTAL[this.edge];
|
|
219
|
+
const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
|
|
220
|
+
const span = horizontal ? rect.width : rect.height;
|
|
221
|
+
this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
|
|
222
|
+
};
|
|
223
|
+
const up = (): void => {
|
|
224
|
+
this.handle.ownerDocument.removeEventListener("pointermove", move);
|
|
225
|
+
this.handle.ownerDocument.removeEventListener("pointerup", up);
|
|
226
|
+
this.root.classList.remove("dock-drawer-resizing");
|
|
227
|
+
this.hooks.changed();
|
|
228
|
+
};
|
|
229
|
+
this.root.classList.add("dock-drawer-resizing");
|
|
230
|
+
this.handle.ownerDocument.addEventListener("pointermove", move);
|
|
231
|
+
this.handle.ownerDocument.addEventListener("pointerup", up);
|
|
232
|
+
}
|
|
233
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
type Placement,
|
|
15
|
+
type SavedDrawer,
|
|
16
|
+
type SavedDrawers,
|
|
17
|
+
} from "./layout.js";
|
|
18
|
+
export type { DrawerConfig } from "./drawerView.js";
|
|
19
|
+
export {
|
|
20
|
+
clampSize,
|
|
21
|
+
drawerSize,
|
|
22
|
+
loadDrawerSize,
|
|
23
|
+
saveDrawerSize,
|
|
24
|
+
DRAWER_DEFAULT,
|
|
25
|
+
DRAWER_MAX_FRAC,
|
|
26
|
+
DRAWER_MIN,
|
|
27
|
+
type DrawerBounds,
|
|
28
|
+
type DrawerEdge,
|
|
29
|
+
type DrawerStorage,
|
|
30
|
+
} from "./drawer.js";
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { DrawerEdge } from "./drawer.js";
|
|
2
|
+
|
|
3
|
+
// The DOM-free half of the dock: the panel registry's types, the saved layout, reconcile and prune,
|
|
4
|
+
// and theme resolution. It needs neither dockview nor a browser, so it unit-tests on its own.
|
|
5
|
+
// dock.ts feeds these to dockview-core.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* One panel the app can show. The registry is a list of these in a stable order, and it is saved
|
|
9
|
+
* with every layout so a later load can tell a panel the app added since apart from one the reader
|
|
10
|
+
* closed.
|
|
11
|
+
*/
|
|
12
|
+
export interface PanelDef {
|
|
13
|
+
/** The panel's dockview id. It is also its component name and its saved name, so keep it stable. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** The tab label. */
|
|
16
|
+
title: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a panel the registry gained after a layout was saved opens on that layout's next load.
|
|
19
|
+
* Default `true`. Set `false` for a panel that should appear only in the Panels menu, so a new
|
|
20
|
+
* feature doesn't crowd an existing reader's arrangement.
|
|
21
|
+
*/
|
|
22
|
+
defaultOpen?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Where the panel opens when it isn't in the layout yet: from reconcile, `show`, or the Panels
|
|
25
|
+
* menu. When the reference panel isn't open, or this is absent, the panel's spec in the default
|
|
26
|
+
* layout is tried next, and then the grid root.
|
|
27
|
+
*/
|
|
28
|
+
position?: PanelPosition;
|
|
29
|
+
/**
|
|
30
|
+
* Where the panel goes when it opens on its own: on first run, on reconcile, from `show`, and on
|
|
31
|
+
* Reset layout. Default `{kind: "grid"}`. A panel in the default layout's specs starts in the grid
|
|
32
|
+
* unless this says a drawer or float.
|
|
33
|
+
*/
|
|
34
|
+
defaultPlacement?: Placement;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Where a panel lives. `grid` is dockview's grid, `float` a dockview floating group over it, and
|
|
39
|
+
* `drawer` the drawer on that edge.
|
|
40
|
+
*/
|
|
41
|
+
export type Placement = { kind: "grid" } | { kind: "float" } | { kind: "drawer"; edge: DrawerEdge };
|
|
42
|
+
|
|
43
|
+
/** One drawer's saved state: its panels in tab order, the active one, its size, and whether it's open. */
|
|
44
|
+
export interface SavedDrawer {
|
|
45
|
+
panels: string[];
|
|
46
|
+
active?: string;
|
|
47
|
+
size?: number;
|
|
48
|
+
open?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The saved drawers, by edge. An edge with no drawer is absent. */
|
|
52
|
+
export type SavedDrawers = Partial<Record<DrawerEdge, SavedDrawer>>;
|
|
53
|
+
|
|
54
|
+
/** A dockview `addPanel` position: a direction from a panel that is already open. */
|
|
55
|
+
export interface PanelPosition {
|
|
56
|
+
direction: "left" | "right" | "above" | "below" | "within";
|
|
57
|
+
referencePanel?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* One `addPanel` call written as data. A default layout is a list of these, ordered so every
|
|
62
|
+
* `referencePanel` is added before something refers to it. A spec without a position anchors its
|
|
63
|
+
* panel at the grid root. `title` defaults to the registry's.
|
|
64
|
+
*/
|
|
65
|
+
export interface PanelSpec {
|
|
66
|
+
id: string;
|
|
67
|
+
title?: string;
|
|
68
|
+
position?: PanelPosition;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The part of Web Storage the dock needs. `localStorage` satisfies it, and tests pass a fake. */
|
|
72
|
+
export interface DockStorage {
|
|
73
|
+
getItem(key: string): string | null;
|
|
74
|
+
setItem(key: string, value: string): void;
|
|
75
|
+
removeItem(key: string): void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface SavedLayout {
|
|
79
|
+
v: 1 | 2;
|
|
80
|
+
panels: string[];
|
|
81
|
+
layout: unknown;
|
|
82
|
+
drawers?: SavedDrawers;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* What `loadLayout` returns. `layout` is dockview's `toJSON()` blob. `savedPanels` is the registry
|
|
87
|
+
* when it was saved, and it is empty for a bare blob from before saves carried one.
|
|
88
|
+
*/
|
|
89
|
+
export interface LoadedLayout {
|
|
90
|
+
layout: unknown;
|
|
91
|
+
savedPanels: string[];
|
|
92
|
+
/** The drawers from a version 2 save. A version 1 or bare save has none, so it loads as grid only. */
|
|
93
|
+
drawers: SavedDrawers;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Saves dockview's `toJSON()` blob as `{v: 1, panels, layout}`, where `panels` is the registry's
|
|
98
|
+
* ids. With `drawers` it saves `{v: 2, panels, layout, drawers}`. A storage that throws (quota,
|
|
99
|
+
* private mode) is ignored, so the page keeps working and the layout just isn't saved.
|
|
100
|
+
*/
|
|
101
|
+
export function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown, drawers?: SavedDrawers): void {
|
|
102
|
+
try {
|
|
103
|
+
const panels = registry.map((p) => p.id);
|
|
104
|
+
const saved: SavedLayout = drawers ? { v: 2, panels, layout, drawers } : { v: 1, panels, layout };
|
|
105
|
+
storage.setItem(key, JSON.stringify(saved));
|
|
106
|
+
} catch {
|
|
107
|
+
// best effort
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Reads a saved layout. It returns null when nothing is saved or the save isn't JSON, and the
|
|
113
|
+
* caller applies its default layout in both cases. A bare dockview blob, saved before the registry
|
|
114
|
+
* was saved with it, loads with an empty `savedPanels`.
|
|
115
|
+
*/
|
|
116
|
+
export function loadLayout(storage: DockStorage, key: string): LoadedLayout | null {
|
|
117
|
+
let raw: string | null;
|
|
118
|
+
try {
|
|
119
|
+
raw = storage.getItem(key);
|
|
120
|
+
} catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (!raw) return null;
|
|
124
|
+
try {
|
|
125
|
+
const parsed: unknown = JSON.parse(raw);
|
|
126
|
+
if (parsed && typeof parsed === "object" && "v" in parsed && "layout" in parsed) {
|
|
127
|
+
const s = parsed as SavedLayout;
|
|
128
|
+
const drawers = s.v === 2 && s.drawers && typeof s.drawers === "object" ? s.drawers : {};
|
|
129
|
+
return { layout: s.layout, savedPanels: Array.isArray(s.panels) ? s.panels : [], drawers };
|
|
130
|
+
}
|
|
131
|
+
return { layout: parsed, savedPanels: [], drawers: {} };
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Returns the ids of panels to open on a restored layout. These are the registry's panels that
|
|
139
|
+
* the saved registry lacks and that don't have `defaultOpen: false`. A panel the reader closed was
|
|
140
|
+
* in the saved registry, so it stays closed. A bare save has an empty registry, so every panel
|
|
141
|
+
* counts as new once; the caller skips the ones the layout already has open.
|
|
142
|
+
*/
|
|
143
|
+
export function panelsToReconcile(savedPanels: readonly string[], registry: readonly PanelDef[]): string[] {
|
|
144
|
+
return registry.filter((p) => p.defaultOpen !== false && !savedPanels.includes(p.id)).map((p) => p.id);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Returns the ids in `open` that the registry no longer has. A restored layout can name a panel a
|
|
149
|
+
* later release removed, and without pruning it shows as an empty tab.
|
|
150
|
+
*/
|
|
151
|
+
export function panelsToPrune(open: readonly string[], registry: readonly PanelDef[]): string[] {
|
|
152
|
+
const known = new Set(registry.map((p) => p.id));
|
|
153
|
+
return open.filter((id) => !known.has(id));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Maps a page's theme setting to light or dark. A `data-theme` of `light` or `dark` wins, and
|
|
158
|
+
* anything else follows the OS, which the caller passes as `prefersDark`.
|
|
159
|
+
*/
|
|
160
|
+
export function resolveTheme(dataTheme: string | null, prefersDark: boolean): "dark" | "light" {
|
|
161
|
+
if (dataTheme === "dark") return "dark";
|
|
162
|
+
if (dataTheme === "light") return "light";
|
|
163
|
+
return prefersDark ? "dark" : "light";
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Reads `resolveTheme` off the document: `data-theme` on `<html>`, then `prefers-color-scheme`.
|
|
168
|
+
* It is `createDock`'s default `theme`.
|
|
169
|
+
*/
|
|
170
|
+
export function documentTheme(doc: Document = document): "dark" | "light" {
|
|
171
|
+
const forced = doc.documentElement.getAttribute("data-theme");
|
|
172
|
+
const prefersDark = doc.defaultView?.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
|
|
173
|
+
return resolveTheme(forced, prefersDark);
|
|
174
|
+
}
|