@panyam/tsappkit-dock 0.7.8 → 0.7.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,14 +6,18 @@ A dock for IDE-like pages, built on [dockview-core](https://www.npmjs.com/packag
6
6
  npm install @panyam/tsappkit-dock dockview-core
7
7
  ```
8
8
 
9
- It needs `dockview-core` 4.13 or later, up to 8.x, and no UI framework. It shares one version with tsappkit and goapplib, so `@panyam/tsappkit-dock@0.7.8` goes with `@panyam/tsappkit@0.7.8` and goapplib `v0.7.8`. Load dockview's stylesheet in your page. dockview-core 4.13 to 7.x ship it as `dockview-core/dist/styles/dockview.css`. 8.x doesn't, and the same file comes in the `dockview` package as `dockview/dist/styles/dockview.css`.
9
+ It needs `dockview-core` 4.13 or later, up to 8.x, and no UI framework. It shares one version with tsappkit and goapplib, so `@panyam/tsappkit-dock@0.7.10` goes with `@panyam/tsappkit@0.7.10` and goapplib `v0.7.10`. Load dockview's stylesheet in your page. dockview-core 4.13 to 7.x ship it as `dockview-core/dist/styles/dockview.css`. 8.x doesn't, and the same file comes in the `dockview` package as `dockview/dist/styles/dockview.css`.
10
10
 
11
11
  ## What's in it
12
12
 
13
- - **`createDock`** builds the dock from a panel registry, a default layout, and `hostFor`, which returns the element each panel shows. `release` gets the element back when its panel leaves the grid. The returned dock has `show`, `toggle`, `reset`, `panelsMenu` (a Panels button with a toggle per panel and Reset layout) and `api`, the dockview api.
14
- - **`saveLayout`, `loadLayout`, `panelsToReconcile` and `panelsToPrune`** are the saved-layout logic `createDock` uses, without dockview, for an app that drives dockview itself. A save is `{v: 1, panels, layout}`: `panels` is the registry when it was saved, which is how a later load tells a new panel from a closed one.
13
+ - **`createDock`** builds the dock from a panel registry, a default layout, and `hostFor`, which returns the element each panel shows. `release` gets the element back when its panel leaves where it was. The returned dock has `show`, `place`, `where`, `toggle`, `toggleDrawer`, `reset`, `panelsMenu` (a Panels button with a toggle per panel and Reset layout) and `api`, the dockview api.
14
+ - **Placement.** A panel lives in the grid, in a floating group over it, or in the drawer on one of the four edges. `dock.place(id, {kind: "drawer", edge: "bottom"})` moves it, and so does the ⋯ menu on each group and each drawer tab. `PanelDef.defaultPlacement` says where a panel starts, and Reset layout puts it back there. `dock.where(id)` also reports `{kind: "popout"}` for a panel popped out through `dock.api`.
15
+ - **`moved`.** Called after a panel ends up somewhere new, with its element in place. Its `documentChanged` flag is true on the way into a popout window and back, which is when a canvas needs rebuilding.
16
+ - **Drawers.** One per edge, laid over the dock with a scrim. A drawer holding several panels shows tabs. Escape or a click on the scrim closes it, and a drag on its inner edge resizes it. The saved layout keeps each drawer's panels, active tab, size and open state.
17
+ - **Panels added at runtime.** `resolve(id)` describes a panel the registry doesn't list, such as one per open document. `dock.show(id)` opens it, it can be placed anywhere, and a saved layout brings it back.
18
+ - **`saveLayout`, `loadLayout`, `panelsToReconcile` and `panelsToPrune`** are the saved-layout logic `createDock` uses, without dockview, for an app that drives dockview itself. A save is `{v: 2, panels, layout, drawers}`: `panels` is the registry when it was saved, which is how a later load tells a new panel from a closed one. A version 1 save, from 0.7.10, loads as grid only.
15
19
  - **`resolveTheme` and `documentTheme`** map `<html data-theme>` and the OS colour scheme to light or dark. The dock follows both by default.
16
- - **`drawerSize`, `clampSize`, `loadDrawerSize` and `saveDrawerSize`** size a drawer on any edge from a drag on its inner edge, clamped to a minimum and a share of the viewport, and save it.
20
+ - **`drawerSize`, `clampSize`, `loadDrawerSize` and `saveDrawerSize`** are the drawers' sizing, for an app that builds its own: a size on any edge from a drag on its inner edge, clamped to a minimum and a share of the viewport.
17
21
 
18
22
  A three-panel page whose content is built before the dock:
19
23
 
@@ -45,11 +49,13 @@ dock.panelsMenu(document.getElementById("toolbar")!);
45
49
 
46
50
  `log` has `defaultOpen: false`, so a reader whose layout was saved before `log` existed finds it in the Panels menu rather than in their grid.
47
51
 
48
- Panel content can be anything that renders into an element: plain DOM, Solid, or another framework. With Solid, mount once into the element `hostFor` returns, and return that same element every time. The dock calls `hostFor` whenever a panel enters the grid, and Solid's `render` appends, so rendering inside `hostFor` adds a second tree on each reopen. A closed panel's tree keeps running, since it's only detached. To tear it down, call `render`'s dispose function in `release` and build a fresh host next time.
52
+ Panel content can be anything that renders into an element: plain DOM, Solid, or another framework. With Solid, mount once into the element `hostFor` returns, and return that same element every time. The dock calls `hostFor` whenever a panel arrives somewhere, and Solid's `render` appends, so rendering inside `hostFor` adds a second tree on each reopen. A closed panel's tree keeps running, since it's only detached. To tear it down, call `render`'s dispose function in `release` and build a fresh host next time.
49
53
 
50
54
  A panel reopened from the Panels menu goes to its `PanelDef.position`, then to its spec in the default layout. The first panel in a default layout usually has no position, so give it one in its `PanelDef` if it should come back to the same place.
51
55
 
52
- Drawers on four edges, floating panels, and moving a panel between them are planned in [#133](https://github.com/panyam/goapplib/issues/133).
56
+ Moving a panel into or out of a drawer moves its element to a new parent, and the browser reloads an iframe when that happens. Moving between the grid and a float, or between tabs in the grid, doesn't, since dockview keeps panel content in a layer of its own. Keep iframes out of panels that go into drawers, or reload what they show.
57
+
58
+ The [dock guide](https://panyam.github.io/goapplib/guide/dock/) has a live demo whose panels are the docsite's own pages.
53
59
 
54
60
  ## License
55
61
 
package/dist/index.d.mts CHANGED
@@ -1,5 +1,48 @@
1
1
  import { DockviewApi } from 'dockview-core';
2
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
+
3
46
  /**
4
47
  * One panel the app can show. The registry is a list of these in a stable order, and it is saved
5
48
  * with every layout so a later load can tell a panel the app added since apart from one the reader
@@ -22,7 +65,34 @@ interface PanelDef {
22
65
  * layout is tried next, and then the grid root.
23
66
  */
24
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;
25
93
  }
94
+ /** The saved drawers, by edge. An edge with no drawer is absent. */
95
+ type SavedDrawers = Partial<Record<DrawerEdge, SavedDrawer>>;
26
96
  /** A dockview `addPanel` position: a direction from a panel that is already open. */
27
97
  interface PanelPosition {
28
98
  direction: "left" | "right" | "above" | "below" | "within";
@@ -51,13 +121,15 @@ interface DockStorage {
51
121
  interface LoadedLayout {
52
122
  layout: unknown;
53
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;
54
126
  }
55
127
  /**
56
128
  * Saves dockview's `toJSON()` blob as `{v: 1, panels, layout}`, where `panels` is the registry's
57
- * ids. A storage that throws (quota, private mode) is ignored, so the page keeps working and the
58
- * layout just isn't saved.
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.
59
131
  */
60
- declare function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown): void;
132
+ declare function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown, drawers?: SavedDrawers): void;
61
133
  /**
62
134
  * Reads a saved layout. It returns null when nothing is saved or the save isn't JSON, and the
63
135
  * caller applies its default layout in both cases. A bare dockview blob, saved before the registry
@@ -87,30 +159,64 @@ declare function resolveTheme(dataTheme: string | null, prefersDark: boolean): "
87
159
  */
88
160
  declare function documentTheme(doc?: Document): "dark" | "light";
89
161
 
162
+ /**
163
+ * Where a panel is: a `Placement`, or a dockview popout window. Popouts come from `dock.api`, since
164
+ * the dock doesn't place panels there itself (see the dock experiments page for why).
165
+ */
166
+ type PanelLocation = Placement | {
167
+ kind: "popout";
168
+ };
169
+ /** What `moved` gets besides the panel and its old and new place. */
170
+ interface MoveInfo {
171
+ /**
172
+ * True when the panel's element is in a different document from the last time the dock saw it,
173
+ * which happens on the way into a popout window and on the way back. A canvas can't draw again
174
+ * once the window it was in has closed, so rebuild canvases (and WebGL contexts) when this is
175
+ * true. Moves within one document leave canvases working.
176
+ */
177
+ documentChanged: boolean;
178
+ }
90
179
  /** What `createDock` needs. */
91
180
  interface DockOptions {
92
181
  /** The element dockview renders into. It needs a size, as dockview fills it. */
93
182
  container: HTMLElement;
94
- /** The panel registry, in a stable order. Only these ids get content. */
183
+ /** The panel registry, in a stable order. These are the panels the Panels menu lists. */
95
184
  panels: readonly PanelDef[];
185
+ /**
186
+ * Describes a panel the registry doesn't list, such as one per document the reader opens.
187
+ * Return undefined for an id that isn't one. A resolvable panel opens with `show(id)`, can be
188
+ * placed anywhere, and is kept when a saved layout names it. The Panels menu lists it while it's
189
+ * open.
190
+ */
191
+ resolve?: (id: string) => PanelDef | undefined;
96
192
  /**
97
193
  * The arrangement used when nothing is saved, after a rejected save, and on `reset`. Either
98
194
  * `addPanel` specs as data, or a function that builds it on the api, for an app that sizes
99
- * groups after adding them.
195
+ * groups after adding them. Registry panels whose `defaultPlacement` is a drawer or a float go
196
+ * there afterwards, whichever form this takes.
100
197
  */
101
198
  defaultLayout: readonly PanelSpec[] | ((api: DockviewApi) => void);
102
199
  /**
103
- * Returns the element a panel shows. The dock calls it each time the panel enters the grid, so
104
- * return the same element every time if its content should survive a close and reopen. It is
105
- * only called with registry ids.
200
+ * Returns the element a panel shows. The dock calls it each time the panel arrives somewhere: in
201
+ * the grid, in a float, or in a drawer. Return the same element every time if its content should
202
+ * survive a move, a close and a reopen. It's only called with ids the registry or `resolve` knows.
106
203
  */
107
204
  hostFor: (id: string) => HTMLElement;
108
205
  /**
109
- * Called when a panel leaves the grid, with the element `hostFor` gave it. dockview has already
110
- * detached it, so this is where an app parks the element somewhere hidden, or tears its content
111
- * down. Clearing the layout, as `reset` does, calls it for every open panel.
206
+ * Called when a panel leaves where it was, with the element `hostFor` gave it, already detached.
207
+ * Closing a panel calls it, and so does moving it between the grid and a drawer, before `hostFor`
208
+ * is called for the new place. Moving it between the grid and a float doesn't, since dockview
209
+ * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
112
210
  */
113
211
  release?: (id: string, el: HTMLElement) => void;
212
+ /**
213
+ * Called after a panel ends up somewhere new, with its element already in place. `to` and
214
+ * `from` are where it is and where it was, and null means closed. It covers every way a panel
215
+ * moves: `place`, `show`, `toggle`, Reset layout, the reader dragging a tab into a float, a popout
216
+ * made through `dock.api`, and a popout window closing. It isn't called for the layout the dock
217
+ * starts with. See `MoveInfo.documentChanged` for the one move that breaks a canvas.
218
+ */
219
+ moved?: (id: string, el: HTMLElement, to: PanelLocation | null, from: PanelLocation | null, info: MoveInfo) => void;
114
220
  /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
115
221
  storage?: DockStorage | null;
116
222
  /** The storage key. Required with `storage`. */
@@ -120,83 +226,74 @@ interface DockOptions {
120
226
  * OS colour scheme changes, and on `applyTheme()`. Default: `documentTheme()`.
121
227
  */
122
228
  theme?: () => "light" | "dark";
229
+ /** Each edge's drawer size and bounds. An edge left out gets the defaults from drawer.ts. */
230
+ drawers?: Partial<Record<DrawerEdge, DrawerConfig>>;
231
+ /**
232
+ * The element drawers are laid over, and the box their sizes are measured against. Default
233
+ * `container`. The dock makes it `position: relative` if it's static.
234
+ */
235
+ drawerHost?: HTMLElement;
236
+ /**
237
+ * Whether each dockview group gets a ⋯ button with Dock, Float and the four drawers for its
238
+ * active panel. Default true. Drawer tabs always have one.
239
+ */
240
+ moveMenu?: boolean;
123
241
  }
124
242
  /** A dock made by `createDock`. */
125
243
  interface Dock {
126
244
  /** The dockview api, for anything the dock doesn't wrap. */
127
245
  readonly api: DockviewApi;
128
246
  /**
129
- * Brings a panel to the front, opening it first if it's closed. It returns false, and does
130
- * nothing, for an id the registry lacks.
247
+ * Brings a panel to the front wherever it is, opening its drawer if it's in one. A closed panel
248
+ * opens at its `defaultPlacement`. It returns false, and does nothing, for an id that neither the
249
+ * registry nor `resolve` knows.
131
250
  */
132
251
  show(id: string): boolean;
133
- /** Opens a closed panel or closes an open one. False for an id the registry lacks. */
252
+ /**
253
+ * Moves a panel to `target` and brings it to the front. A closed panel opens there. False for an
254
+ * unknown id.
255
+ */
256
+ place(id: string, target: Placement): boolean;
257
+ /** Where a panel is now, or null when it's closed. A panel in a dockview popout reports `popout`. */
258
+ where(id: string): PanelLocation | null;
259
+ /** Opens a closed panel or closes an open one. False for an unknown id. */
134
260
  toggle(id: string): boolean;
135
- /** Forgets the saved layout and applies the default one. Panels keep their `hostFor` elements. */
261
+ /**
262
+ * Opens or closes the drawer on `edge`, or flips it when `open` is absent. An empty drawer stays
263
+ * closed. Returns whether it's open afterwards.
264
+ */
265
+ toggleDrawer(edge: DrawerEdge, open?: boolean): boolean;
266
+ /**
267
+ * Forgets the saved layout and applies the default one, drawers included. Panels keep their
268
+ * `hostFor` elements.
269
+ */
136
270
  reset(): void;
137
271
  /** Reads `theme` again and applies it. For a theme setting the dock doesn't watch. */
138
272
  applyTheme(): void;
139
273
  /**
140
- * Adds a Panels menu button to `host`. Its list has one item per registry panel, each toggling
141
- * its panel, with a ✓ on the open ones, and then Reset layout. Returns a function that removes
142
- * it. The elements carry `dock-panels-*` classes and no styles beyond showing and hiding the
143
- * list, so the app styles them.
274
+ * Adds a Panels menu button to `host`. Its list has one item per registry panel and per open
275
+ * resolved panel, each toggling its panel, with a ✓ on the open ones and where a panel is when
276
+ * it's not in the grid, and then Reset layout. Returns a function that removes it. The elements
277
+ * carry `dock-panels-*` classes and no styles beyond showing and hiding the list.
144
278
  */
145
279
  panelsMenu(host: HTMLElement): () => void;
146
280
  /**
147
- * Saves the layout, stops watching the theme, removes any Panels menus, and disposes of
148
- * dockview. Changes are otherwise saved a microtask after dockview reports them. A second call
149
- * does nothing.
281
+ * Saves the layout, stops watching the theme, removes the menus and drawers, and disposes of
282
+ * dockview. Panels in drawers are released. Changes are otherwise saved a microtask after
283
+ * dockview reports them. A second call does nothing.
150
284
  */
151
285
  dispose(): void;
152
286
  }
153
287
  /**
154
288
  * Builds a dockview dock from a panel registry. It restores the saved layout when there is one,
155
- * opens the panels added to the registry since it was saved, and closes the ones the registry
156
- * dropped. A save that dockview rejects is removed and the default layout applied. Every layout
157
- * change after that is saved.
289
+ * drawers included, opens the panels added to the registry since it was saved, and closes the
290
+ * ones nothing knows any more. A save that dockview rejects is removed and the default layout
291
+ * applied. Every change after that is saved.
158
292
  *
159
- * Content lives outside dockview. `hostFor` hands an element to a panel when it enters the grid,
160
- * and `release` hands it back when it leaves, so an app can keep a panel's content alive while
161
- * the panel is closed.
293
+ * Content lives outside dockview. `hostFor` hands an element to a panel when it arrives somewhere,
294
+ * and `release` hands it back when it leaves, so an app can keep a panel's content alive while the
295
+ * panel is closed or moving between the grid and a drawer.
162
296
  */
163
297
  declare function createDock(opts: DockOptions): Dock;
164
298
 
165
- /** The page edge a drawer is anchored to. */
166
- type DrawerEdge = "left" | "right" | "top" | "bottom";
167
- /** The smallest a drawer gets, in px, unless the caller passes its own `min`. */
168
- declare const DRAWER_MIN = 280;
169
- /** The largest share of the viewport a drawer takes, unless the caller passes its own `maxFrac`. */
170
- declare const DRAWER_MAX_FRAC = 0.9;
171
- /** A drawer's size when nothing is saved. */
172
- declare const DRAWER_DEFAULT = 420;
173
- /** Bounds for `clampSize` and `drawerSize`. */
174
- interface DrawerBounds {
175
- min?: number;
176
- maxFrac?: number;
177
- }
178
- /**
179
- * Clamps `px` to `[min, viewport * maxFrac]` and rounds it. A viewport too small for the minimum
180
- * gets the minimum, so the drawer can overflow a tiny window rather than vanish.
181
- */
182
- declare function clampSize(px: number, viewport: number, bounds?: DrawerBounds): number;
183
- /**
184
- * Turns a drag on a drawer's inner edge into its size. The size is the distance from the pointer
185
- * to the anchored edge, then clamped. Pass the pointer's `clientX` and the viewport's width for a
186
- * left or right drawer, and `clientY` and the height for a top or bottom one.
187
- */
188
- declare function drawerSize(edge: DrawerEdge, pointer: number, viewport: number, bounds?: DrawerBounds): number;
189
- /** The part of Web Storage drawer sizes need. A null storage means sizes aren't saved. */
190
- interface DrawerStorage {
191
- getItem(key: string): string | null;
192
- setItem(key: string, value: string): void;
193
- }
194
- /**
195
- * Reads a saved size, or `fallback` when nothing is saved or the value isn't a positive number.
196
- * It doesn't clamp, since the viewport may have changed; clamp against the live one when applying.
197
- */
198
- declare function loadDrawerSize(storage: DrawerStorage | null, key: string, fallback?: number): number;
199
- /** Saves a size, rounded. A storage that throws is ignored. */
200
- declare function saveDrawerSize(storage: DrawerStorage | null, key: string, px: number): void;
201
-
202
- export { DRAWER_DEFAULT, DRAWER_MAX_FRAC, DRAWER_MIN, type Dock, type DockOptions, type DockStorage, type DrawerBounds, type DrawerEdge, type DrawerStorage, type LoadedLayout, type PanelDef, type PanelPosition, type PanelSpec, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
299
+ 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 MoveInfo, type PanelDef, type PanelLocation, type PanelPosition, type PanelSpec, type Placement, type SavedDrawer, type SavedDrawers, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };