@panyam/tsappkit-dock 0.7.9 → 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,15 +6,16 @@ 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.9` goes with `@panyam/tsappkit@0.7.9` and goapplib `v0.7.9`. 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
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.
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.
15
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.
16
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.
17
- - **`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.9, loads as grid only.
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.
18
19
  - **`resolveTheme` and `documentTheme`** map `<html data-theme>` and the OS colour scheme to light or dark. The dock follows both by default.
19
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.
20
21
 
package/dist/index.d.mts CHANGED
@@ -159,6 +159,23 @@ declare function resolveTheme(dataTheme: string | null, prefersDark: boolean): "
159
159
  */
160
160
  declare function documentTheme(doc?: Document): "dark" | "light";
161
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
+ }
162
179
  /** What `createDock` needs. */
163
180
  interface DockOptions {
164
181
  /** The element dockview renders into. It needs a size, as dockview fills it. */
@@ -192,6 +209,14 @@ interface DockOptions {
192
209
  * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
193
210
  */
194
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;
195
220
  /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
196
221
  storage?: DockStorage | null;
197
222
  /** The storage key. Required with `storage`. */
@@ -229,8 +254,8 @@ interface Dock {
229
254
  * unknown id.
230
255
  */
231
256
  place(id: string, target: Placement): boolean;
232
- /** Where a panel is now, or null when it's closed. */
233
- where(id: string): Placement | null;
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;
234
259
  /** Opens a closed panel or closes an open one. False for an unknown id. */
235
260
  toggle(id: string): boolean;
236
261
  /**
@@ -271,4 +296,4 @@ interface Dock {
271
296
  */
272
297
  declare function createDock(opts: DockOptions): Dock;
273
298
 
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 };
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 };
package/dist/index.d.ts CHANGED
@@ -159,6 +159,23 @@ declare function resolveTheme(dataTheme: string | null, prefersDark: boolean): "
159
159
  */
160
160
  declare function documentTheme(doc?: Document): "dark" | "light";
161
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
+ }
162
179
  /** What `createDock` needs. */
163
180
  interface DockOptions {
164
181
  /** The element dockview renders into. It needs a size, as dockview fills it. */
@@ -192,6 +209,14 @@ interface DockOptions {
192
209
  * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
193
210
  */
194
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;
195
220
  /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
196
221
  storage?: DockStorage | null;
197
222
  /** The storage key. Required with `storage`. */
@@ -229,8 +254,8 @@ interface Dock {
229
254
  * unknown id.
230
255
  */
231
256
  place(id: string, target: Placement): boolean;
232
- /** Where a panel is now, or null when it's closed. */
233
- where(id: string): Placement | null;
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;
234
259
  /** Opens a closed panel or closes an open one. False for an unknown id. */
235
260
  toggle(id: string): boolean;
236
261
  /**
@@ -271,4 +296,4 @@ interface Dock {
271
296
  */
272
297
  declare function createDock(opts: DockOptions): Dock;
273
298
 
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 };
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 };
package/dist/index.js CHANGED
@@ -99,6 +99,10 @@ var DrawerView = class {
99
99
  get size() {
100
100
  return this.px;
101
101
  }
102
+ /** The element a panel in this drawer shows, or undefined if it isn't here. */
103
+ elementOf(id) {
104
+ return this.tabs.find((t) => t.id === id)?.el;
105
+ }
102
106
  has(id) {
103
107
  return this.tabs.some((t) => t.id === id);
104
108
  }
@@ -256,7 +260,8 @@ function documentTheme(doc = document) {
256
260
 
257
261
  // src/dock.ts
258
262
  var EDGES = ["left", "right", "top", "bottom"];
259
- var placementLabel = (p) => !p || p.kind === "grid" ? "" : p.kind === "float" ? " \xB7 float" : ` \xB7 ${p.edge} drawer`;
263
+ var placementLabel = (p) => !p || p.kind === "grid" ? "" : p.kind === "drawer" ? ` \xB7 ${p.edge} drawer` : ` \xB7 ${p.kind}`;
264
+ var TOWARD_EDGE = { left: "left", right: "right", top: "above", bottom: "below" };
260
265
  function createDock(opts) {
261
266
  const { container, panels, hostFor, release } = opts;
262
267
  const storage = opts.storage ?? null;
@@ -301,14 +306,16 @@ function createDock(opts) {
301
306
  if (d) return { kind: "drawer", edge: d.edge };
302
307
  const panel = api.getPanel(id);
303
308
  if (!panel) return null;
304
- return panel.group.api.location.type === "floating" ? { kind: "float" } : { kind: "grid" };
309
+ const type = panel.group.api.location.type;
310
+ return type === "floating" ? { kind: "float" } : type === "popout" ? { kind: "popout" } : { kind: "grid" };
305
311
  };
312
+ const elementOf = (id) => drawerWith(id)?.elementOf(id) ?? api.getPanel(id)?.view.content.element;
306
313
  const same = (a, b) => !!a && a.kind === b.kind && (a.kind !== "drawer" || b.kind === "drawer" && a.edge === b.edge);
307
314
  const drawer = (edge) => {
308
315
  let d = drawers.get(edge);
309
316
  if (!d) {
310
317
  if (doc.defaultView?.getComputedStyle(drawerHost).position === "static") drawerHost.style.position = "relative";
311
- d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => save(), menu: (anchor, id) => moveMenu(anchor, id) });
318
+ d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => changed(), menu: (anchor, id) => moveMenu(anchor, id) });
312
319
  drawers.set(edge, d);
313
320
  }
314
321
  return d;
@@ -319,11 +326,24 @@ function createDock(opts) {
319
326
  return out;
320
327
  };
321
328
  const usable = (p) => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
322
- const addToGrid = (id) => {
329
+ const inGrid = (p) => !!p && (!p.referencePanel || api.getPanel(p.referencePanel)?.group.api.location.type === "grid" && api.getPanel(p.referencePanel).group.api.isVisible);
330
+ const addToGrid = (id, prefer) => {
323
331
  const def = defOf(id);
324
332
  const spec = specs?.find((s) => s.id === id);
325
- const position = [def.position, spec?.position].find(usable);
326
- api.addPanel({ id, component: id, title: def.title, ...position ? { position } : {} });
333
+ const position = [prefer, def.position, spec?.position].find(inGrid);
334
+ if (position) {
335
+ api.addPanel({ id, component: id, title: def.title, position });
336
+ return;
337
+ }
338
+ const showing = (g) => !!g && g.api.location.type === "grid" && g.api.isVisible;
339
+ const active = api.activeGroup;
340
+ const group = showing(active) ? active : api.groups.find(showing);
341
+ api.addPanel({
342
+ id,
343
+ component: id,
344
+ title: def.title,
345
+ position: group ? { referenceGroup: group, direction: "within" } : { direction: "right" }
346
+ });
327
347
  };
328
348
  const enter = (id, target) => {
329
349
  if (target.kind === "drawer") {
@@ -353,33 +373,50 @@ function createDock(opts) {
353
373
  }
354
374
  api.getPanel(id)?.api.setActive();
355
375
  };
376
+ const batch = (fn) => {
377
+ quiet++;
378
+ try {
379
+ fn();
380
+ } finally {
381
+ quiet--;
382
+ }
383
+ changed();
384
+ };
356
385
  const place = (id, target) => {
357
386
  if (!defOf(id)) return false;
387
+ batch(() => placeNow(id, target));
388
+ return true;
389
+ };
390
+ const placeNow = (id, target) => {
358
391
  const now = where(id);
359
392
  if (!same(now, target)) {
360
- if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
361
- enter(id, target);
393
+ if (now?.kind === "drawer" && target.kind === "grid") {
394
+ const size = drawer(now.edge).size;
395
+ leave(id);
396
+ addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
397
+ const horizontal = now.edge === "left" || now.edge === "right";
398
+ api.getPanel(id)?.api.setSize(horizontal ? { width: size } : { height: size });
399
+ } else {
400
+ if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
401
+ enter(id, target);
402
+ }
362
403
  }
363
404
  reveal(id);
364
- save();
365
- return true;
366
405
  };
367
406
  const show = (id) => {
368
407
  const def = defOf(id);
369
408
  if (!def) return false;
370
- if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
371
- reveal(id);
372
- save();
409
+ batch(() => {
410
+ if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
411
+ reveal(id);
412
+ });
373
413
  return true;
374
414
  };
375
415
  const toggle = (id) => {
376
416
  if (!defOf(id)) return false;
377
- if (where(id)) {
378
- leave(id);
379
- save();
380
- return true;
381
- }
382
- return show(id);
417
+ if (!where(id)) return show(id);
418
+ batch(() => leave(id));
419
+ return true;
383
420
  };
384
421
  const applyDefault = () => {
385
422
  const elsewhere = (def) => !!def?.defaultPlacement && def.defaultPlacement.kind === "drawer";
@@ -446,6 +483,27 @@ function createDock(opts) {
446
483
  if (quiet || disposed || !storage) return;
447
484
  saveLayout(storage, key, panels, api.toJSON(), drawerStates());
448
485
  };
486
+ const seen = /* @__PURE__ */ new Map();
487
+ const key0 = (p) => JSON.stringify(p);
488
+ const sync = (notify = true) => {
489
+ if (disposed || quiet) return;
490
+ const ids = /* @__PURE__ */ new Set([...panels.map((p) => p.id), ...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids), ...seen.keys()]);
491
+ for (const id of ids) {
492
+ if (!defOf(id)) continue;
493
+ const at = where(id);
494
+ const prev = seen.get(id);
495
+ const el = at ? elementOf(id) : prev?.el;
496
+ const doc2 = at && el ? el.ownerDocument : prev?.doc;
497
+ seen.set(id, { at, el, doc: doc2 });
498
+ if (!notify || !opts.moved || !el) continue;
499
+ if (key0(at) === key0(prev?.at ?? null) && doc2 === prev?.doc) continue;
500
+ opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc2 !== prev.doc });
501
+ }
502
+ };
503
+ const changed = () => {
504
+ save();
505
+ sync();
506
+ };
449
507
  const reset = () => {
450
508
  quiet++;
451
509
  try {
@@ -456,7 +514,7 @@ function createDock(opts) {
456
514
  } finally {
457
515
  quiet--;
458
516
  }
459
- save();
517
+ changed();
460
518
  };
461
519
  quiet++;
462
520
  try {
@@ -464,11 +522,10 @@ function createDock(opts) {
464
522
  } finally {
465
523
  quiet--;
466
524
  }
467
- if (storage) {
468
- save();
469
- const sub = api.onDidLayoutChange(() => save());
470
- cleanups.push(() => sub.dispose());
471
- }
525
+ save();
526
+ sync(false);
527
+ const sub = api.onDidLayoutChange(() => changed());
528
+ cleanups.push(() => sub.dispose());
472
529
  const applyTheme = () => component.updateOptions({ theme: theme() === "dark" ? dockviewCore.themeDark : dockviewCore.themeLight });
473
530
  const view = doc.defaultView;
474
531
  if (view?.MutationObserver) {