@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.
@@ -0,0 +1,274 @@
1
+ import { DockviewApi } from 'dockview-core';
2
+
3
+ /** The page edge a drawer is anchored to. */
4
+ type DrawerEdge = "left" | "right" | "top" | "bottom";
5
+ /** The smallest a drawer gets, in px, unless the caller passes its own `min`. */
6
+ declare const DRAWER_MIN = 280;
7
+ /** The largest share of the viewport a drawer takes, unless the caller passes its own `maxFrac`. */
8
+ declare const DRAWER_MAX_FRAC = 0.9;
9
+ /** A drawer's size when nothing is saved. */
10
+ declare const DRAWER_DEFAULT = 420;
11
+ /** Bounds for `clampSize` and `drawerSize`. */
12
+ interface DrawerBounds {
13
+ min?: number;
14
+ maxFrac?: number;
15
+ }
16
+ /**
17
+ * Clamps `px` to `[min, viewport * maxFrac]` and rounds it. A viewport too small for the minimum
18
+ * gets the minimum, so the drawer can overflow a tiny window rather than vanish.
19
+ */
20
+ declare function clampSize(px: number, viewport: number, bounds?: DrawerBounds): number;
21
+ /**
22
+ * Turns a drag on a drawer's inner edge into its size. The size is the distance from the pointer
23
+ * to the anchored edge, then clamped. Pass the pointer's `clientX` and the viewport's width for a
24
+ * left or right drawer, and `clientY` and the height for a top or bottom one.
25
+ */
26
+ declare function drawerSize(edge: DrawerEdge, pointer: number, viewport: number, bounds?: DrawerBounds): number;
27
+ /** The part of Web Storage drawer sizes need. A null storage means sizes aren't saved. */
28
+ interface DrawerStorage {
29
+ getItem(key: string): string | null;
30
+ setItem(key: string, value: string): void;
31
+ }
32
+ /**
33
+ * Reads a saved size, or `fallback` when nothing is saved or the value isn't a positive number.
34
+ * It doesn't clamp, since the viewport may have changed; clamp against the live one when applying.
35
+ */
36
+ declare function loadDrawerSize(storage: DrawerStorage | null, key: string, fallback?: number): number;
37
+ /** Saves a size, rounded. A storage that throws is ignored. */
38
+ declare function saveDrawerSize(storage: DrawerStorage | null, key: string, px: number): void;
39
+
40
+ /** Per-edge drawer settings for `createDock`. */
41
+ interface DrawerConfig extends DrawerBounds {
42
+ /** The size, in px, when nothing is saved. Default `DRAWER_DEFAULT`. */
43
+ size?: number;
44
+ }
45
+
46
+ /**
47
+ * One panel the app can show. The registry is a list of these in a stable order, and it is saved
48
+ * with every layout so a later load can tell a panel the app added since apart from one the reader
49
+ * closed.
50
+ */
51
+ interface PanelDef {
52
+ /** The panel's dockview id. It is also its component name and its saved name, so keep it stable. */
53
+ id: string;
54
+ /** The tab label. */
55
+ title: string;
56
+ /**
57
+ * Whether a panel the registry gained after a layout was saved opens on that layout's next load.
58
+ * Default `true`. Set `false` for a panel that should appear only in the Panels menu, so a new
59
+ * feature doesn't crowd an existing reader's arrangement.
60
+ */
61
+ defaultOpen?: boolean;
62
+ /**
63
+ * Where the panel opens when it isn't in the layout yet: from reconcile, `show`, or the Panels
64
+ * menu. When the reference panel isn't open, or this is absent, the panel's spec in the default
65
+ * layout is tried next, and then the grid root.
66
+ */
67
+ position?: PanelPosition;
68
+ /**
69
+ * Where the panel goes when it opens on its own: on first run, on reconcile, from `show`, and on
70
+ * Reset layout. Default `{kind: "grid"}`. A panel in the default layout's specs starts in the grid
71
+ * unless this says a drawer or float.
72
+ */
73
+ defaultPlacement?: Placement;
74
+ }
75
+ /**
76
+ * Where a panel lives. `grid` is dockview's grid, `float` a dockview floating group over it, and
77
+ * `drawer` the drawer on that edge.
78
+ */
79
+ type Placement = {
80
+ kind: "grid";
81
+ } | {
82
+ kind: "float";
83
+ } | {
84
+ kind: "drawer";
85
+ edge: DrawerEdge;
86
+ };
87
+ /** One drawer's saved state: its panels in tab order, the active one, its size, and whether it's open. */
88
+ interface SavedDrawer {
89
+ panels: string[];
90
+ active?: string;
91
+ size?: number;
92
+ open?: boolean;
93
+ }
94
+ /** The saved drawers, by edge. An edge with no drawer is absent. */
95
+ type SavedDrawers = Partial<Record<DrawerEdge, SavedDrawer>>;
96
+ /** A dockview `addPanel` position: a direction from a panel that is already open. */
97
+ interface PanelPosition {
98
+ direction: "left" | "right" | "above" | "below" | "within";
99
+ referencePanel?: string;
100
+ }
101
+ /**
102
+ * One `addPanel` call written as data. A default layout is a list of these, ordered so every
103
+ * `referencePanel` is added before something refers to it. A spec without a position anchors its
104
+ * panel at the grid root. `title` defaults to the registry's.
105
+ */
106
+ interface PanelSpec {
107
+ id: string;
108
+ title?: string;
109
+ position?: PanelPosition;
110
+ }
111
+ /** The part of Web Storage the dock needs. `localStorage` satisfies it, and tests pass a fake. */
112
+ interface DockStorage {
113
+ getItem(key: string): string | null;
114
+ setItem(key: string, value: string): void;
115
+ removeItem(key: string): void;
116
+ }
117
+ /**
118
+ * What `loadLayout` returns. `layout` is dockview's `toJSON()` blob. `savedPanels` is the registry
119
+ * when it was saved, and it is empty for a bare blob from before saves carried one.
120
+ */
121
+ interface LoadedLayout {
122
+ layout: unknown;
123
+ savedPanels: string[];
124
+ /** The drawers from a version 2 save. A version 1 or bare save has none, so it loads as grid only. */
125
+ drawers: SavedDrawers;
126
+ }
127
+ /**
128
+ * Saves dockview's `toJSON()` blob as `{v: 1, panels, layout}`, where `panels` is the registry's
129
+ * ids. With `drawers` it saves `{v: 2, panels, layout, drawers}`. A storage that throws (quota,
130
+ * private mode) is ignored, so the page keeps working and the layout just isn't saved.
131
+ */
132
+ declare function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown, drawers?: SavedDrawers): void;
133
+ /**
134
+ * Reads a saved layout. It returns null when nothing is saved or the save isn't JSON, and the
135
+ * caller applies its default layout in both cases. A bare dockview blob, saved before the registry
136
+ * was saved with it, loads with an empty `savedPanels`.
137
+ */
138
+ declare function loadLayout(storage: DockStorage, key: string): LoadedLayout | null;
139
+ /**
140
+ * Returns the ids of panels to open on a restored layout. These are the registry's panels that
141
+ * the saved registry lacks and that don't have `defaultOpen: false`. A panel the reader closed was
142
+ * in the saved registry, so it stays closed. A bare save has an empty registry, so every panel
143
+ * counts as new once; the caller skips the ones the layout already has open.
144
+ */
145
+ declare function panelsToReconcile(savedPanels: readonly string[], registry: readonly PanelDef[]): string[];
146
+ /**
147
+ * Returns the ids in `open` that the registry no longer has. A restored layout can name a panel a
148
+ * later release removed, and without pruning it shows as an empty tab.
149
+ */
150
+ declare function panelsToPrune(open: readonly string[], registry: readonly PanelDef[]): string[];
151
+ /**
152
+ * Maps a page's theme setting to light or dark. A `data-theme` of `light` or `dark` wins, and
153
+ * anything else follows the OS, which the caller passes as `prefersDark`.
154
+ */
155
+ declare function resolveTheme(dataTheme: string | null, prefersDark: boolean): "dark" | "light";
156
+ /**
157
+ * Reads `resolveTheme` off the document: `data-theme` on `<html>`, then `prefers-color-scheme`.
158
+ * It is `createDock`'s default `theme`.
159
+ */
160
+ declare function documentTheme(doc?: Document): "dark" | "light";
161
+
162
+ /** What `createDock` needs. */
163
+ interface DockOptions {
164
+ /** The element dockview renders into. It needs a size, as dockview fills it. */
165
+ container: HTMLElement;
166
+ /** The panel registry, in a stable order. These are the panels the Panels menu lists. */
167
+ panels: readonly PanelDef[];
168
+ /**
169
+ * Describes a panel the registry doesn't list, such as one per document the reader opens.
170
+ * Return undefined for an id that isn't one. A resolvable panel opens with `show(id)`, can be
171
+ * placed anywhere, and is kept when a saved layout names it. The Panels menu lists it while it's
172
+ * open.
173
+ */
174
+ resolve?: (id: string) => PanelDef | undefined;
175
+ /**
176
+ * The arrangement used when nothing is saved, after a rejected save, and on `reset`. Either
177
+ * `addPanel` specs as data, or a function that builds it on the api, for an app that sizes
178
+ * groups after adding them. Registry panels whose `defaultPlacement` is a drawer or a float go
179
+ * there afterwards, whichever form this takes.
180
+ */
181
+ defaultLayout: readonly PanelSpec[] | ((api: DockviewApi) => void);
182
+ /**
183
+ * Returns the element a panel shows. The dock calls it each time the panel arrives somewhere: in
184
+ * the grid, in a float, or in a drawer. Return the same element every time if its content should
185
+ * survive a move, a close and a reopen. It's only called with ids the registry or `resolve` knows.
186
+ */
187
+ hostFor: (id: string) => HTMLElement;
188
+ /**
189
+ * Called when a panel leaves where it was, with the element `hostFor` gave it, already detached.
190
+ * Closing a panel calls it, and so does moving it between the grid and a drawer, before `hostFor`
191
+ * is called for the new place. Moving it between the grid and a float doesn't, since dockview
192
+ * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
193
+ */
194
+ release?: (id: string, el: HTMLElement) => void;
195
+ /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
196
+ storage?: DockStorage | null;
197
+ /** The storage key. Required with `storage`. */
198
+ key?: string;
199
+ /**
200
+ * Returns the dock's theme. It is read at creation, when `<html data-theme>` changes, when the
201
+ * OS colour scheme changes, and on `applyTheme()`. Default: `documentTheme()`.
202
+ */
203
+ theme?: () => "light" | "dark";
204
+ /** Each edge's drawer size and bounds. An edge left out gets the defaults from drawer.ts. */
205
+ drawers?: Partial<Record<DrawerEdge, DrawerConfig>>;
206
+ /**
207
+ * The element drawers are laid over, and the box their sizes are measured against. Default
208
+ * `container`. The dock makes it `position: relative` if it's static.
209
+ */
210
+ drawerHost?: HTMLElement;
211
+ /**
212
+ * Whether each dockview group gets a ⋯ button with Dock, Float and the four drawers for its
213
+ * active panel. Default true. Drawer tabs always have one.
214
+ */
215
+ moveMenu?: boolean;
216
+ }
217
+ /** A dock made by `createDock`. */
218
+ interface Dock {
219
+ /** The dockview api, for anything the dock doesn't wrap. */
220
+ readonly api: DockviewApi;
221
+ /**
222
+ * Brings a panel to the front wherever it is, opening its drawer if it's in one. A closed panel
223
+ * opens at its `defaultPlacement`. It returns false, and does nothing, for an id that neither the
224
+ * registry nor `resolve` knows.
225
+ */
226
+ show(id: string): boolean;
227
+ /**
228
+ * Moves a panel to `target` and brings it to the front. A closed panel opens there. False for an
229
+ * unknown id.
230
+ */
231
+ place(id: string, target: Placement): boolean;
232
+ /** Where a panel is now, or null when it's closed. */
233
+ where(id: string): Placement | null;
234
+ /** Opens a closed panel or closes an open one. False for an unknown id. */
235
+ toggle(id: string): boolean;
236
+ /**
237
+ * Opens or closes the drawer on `edge`, or flips it when `open` is absent. An empty drawer stays
238
+ * closed. Returns whether it's open afterwards.
239
+ */
240
+ toggleDrawer(edge: DrawerEdge, open?: boolean): boolean;
241
+ /**
242
+ * Forgets the saved layout and applies the default one, drawers included. Panels keep their
243
+ * `hostFor` elements.
244
+ */
245
+ reset(): void;
246
+ /** Reads `theme` again and applies it. For a theme setting the dock doesn't watch. */
247
+ applyTheme(): void;
248
+ /**
249
+ * Adds a Panels menu button to `host`. Its list has one item per registry panel and per open
250
+ * resolved panel, each toggling its panel, with a ✓ on the open ones and where a panel is when
251
+ * it's not in the grid, and then Reset layout. Returns a function that removes it. The elements
252
+ * carry `dock-panels-*` classes and no styles beyond showing and hiding the list.
253
+ */
254
+ panelsMenu(host: HTMLElement): () => void;
255
+ /**
256
+ * Saves the layout, stops watching the theme, removes the menus and drawers, and disposes of
257
+ * dockview. Panels in drawers are released. Changes are otherwise saved a microtask after
258
+ * dockview reports them. A second call does nothing.
259
+ */
260
+ dispose(): void;
261
+ }
262
+ /**
263
+ * Builds a dockview dock from a panel registry. It restores the saved layout when there is one,
264
+ * drawers included, opens the panels added to the registry since it was saved, and closes the
265
+ * ones nothing knows any more. A save that dockview rejects is removed and the default layout
266
+ * applied. Every change after that is saved.
267
+ *
268
+ * Content lives outside dockview. `hostFor` hands an element to a panel when it arrives somewhere,
269
+ * and `release` hands it back when it leaves, so an app can keep a panel's content alive while the
270
+ * panel is closed or moving between the grid and a drawer.
271
+ */
272
+ declare function createDock(opts: DockOptions): Dock;
273
+
274
+ export { DRAWER_DEFAULT, DRAWER_MAX_FRAC, DRAWER_MIN, type Dock, type DockOptions, type DockStorage, type DrawerBounds, type DrawerConfig, type DrawerEdge, type DrawerStorage, type LoadedLayout, type PanelDef, type PanelPosition, type PanelSpec, type Placement, type SavedDrawer, type SavedDrawers, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };