@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/src/dock.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DockviewComponent, themeDark, themeLight } from "dockview-core";
2
- import type { DockviewApi, IContentRenderer, IHeaderActionsRenderer } from "dockview-core";
2
+ import type { DockviewApi, DockviewGroupPanel, IContentRenderer, IHeaderActionsRenderer } from "dockview-core";
3
3
  import { DRAWER_DEFAULT, type DrawerEdge } from "./drawer.js";
4
4
  import { DrawerView, type DrawerConfig } from "./drawerView.js";
5
5
  import {
@@ -17,6 +17,23 @@ import {
17
17
 
18
18
  const EDGES: readonly DrawerEdge[] = ["left", "right", "top", "bottom"];
19
19
 
20
+ /**
21
+ * Where a panel is: a `Placement`, or a dockview popout window. Popouts come from `dock.api`, since
22
+ * the dock doesn't place panels there itself (see the dock experiments page for why).
23
+ */
24
+ export type PanelLocation = Placement | { kind: "popout" };
25
+
26
+ /** What `moved` gets besides the panel and its old and new place. */
27
+ export interface MoveInfo {
28
+ /**
29
+ * True when the panel's element is in a different document from the last time the dock saw it,
30
+ * which happens on the way into a popout window and on the way back. A canvas can't draw again
31
+ * once the window it was in has closed, so rebuild canvases (and WebGL contexts) when this is
32
+ * true. Moves within one document leave canvases working.
33
+ */
34
+ documentChanged: boolean;
35
+ }
36
+
20
37
  /** What `createDock` needs. */
21
38
  export interface DockOptions {
22
39
  /** The element dockview renders into. It needs a size, as dockview fills it. */
@@ -50,6 +67,14 @@ export interface DockOptions {
50
67
  * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
51
68
  */
52
69
  release?: (id: string, el: HTMLElement) => void;
70
+ /**
71
+ * Called after a panel ends up somewhere new, with its element already in place. `to` and
72
+ * `from` are where it is and where it was, and null means closed. It covers every way a panel
73
+ * moves: `place`, `show`, `toggle`, Reset layout, the reader dragging a tab into a float, a popout
74
+ * made through `dock.api`, and a popout window closing. It isn't called for the layout the dock
75
+ * starts with. See `MoveInfo.documentChanged` for the one move that breaks a canvas.
76
+ */
77
+ moved?: (id: string, el: HTMLElement, to: PanelLocation | null, from: PanelLocation | null, info: MoveInfo) => void;
53
78
  /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
54
79
  storage?: DockStorage | null;
55
80
  /** The storage key. Required with `storage`. */
@@ -88,8 +113,8 @@ export interface Dock {
88
113
  * unknown id.
89
114
  */
90
115
  place(id: string, target: Placement): boolean;
91
- /** Where a panel is now, or null when it's closed. */
92
- where(id: string): Placement | null;
116
+ /** Where a panel is now, or null when it's closed. A panel in a dockview popout reports `popout`. */
117
+ where(id: string): PanelLocation | null;
93
118
  /** Opens a closed panel or closes an open one. False for an unknown id. */
94
119
  toggle(id: string): boolean;
95
120
  /**
@@ -119,8 +144,10 @@ export interface Dock {
119
144
  dispose(): void;
120
145
  }
121
146
 
122
- const placementLabel = (p: Placement | null): string =>
123
- !p || p.kind === "grid" ? "" : p.kind === "float" ? " · float" : ` · ${p.edge} drawer`;
147
+ const placementLabel = (p: PanelLocation | null): string =>
148
+ !p || p.kind === "grid" ? "" : p.kind === "drawer" ? ` · ${p.edge} drawer` : ` · ${p.kind}`;
149
+
150
+ const TOWARD_EDGE: Record<DrawerEdge, PanelPosition["direction"]> = { left: "left", right: "right", top: "above", bottom: "below" };
124
151
 
125
152
  /**
126
153
  * Builds a dockview dock from a panel registry. It restores the saved layout when there is one,
@@ -177,15 +204,19 @@ export function createDock(opts: DockOptions): Dock {
177
204
 
178
205
  const drawerWith = (id: string): DrawerView | undefined => [...drawers.values()].find((d) => d.has(id));
179
206
 
180
- const where = (id: string): Placement | null => {
207
+ const where = (id: string): PanelLocation | null => {
181
208
  const d = drawerWith(id);
182
209
  if (d) return { kind: "drawer", edge: d.edge };
183
210
  const panel = api.getPanel(id);
184
211
  if (!panel) return null;
185
- return panel.group.api.location.type === "floating" ? { kind: "float" } : { kind: "grid" };
212
+ const type = panel.group.api.location.type;
213
+ return type === "floating" ? { kind: "float" } : type === "popout" ? { kind: "popout" } : { kind: "grid" };
186
214
  };
187
215
 
188
- const same = (a: Placement | null, b: Placement): boolean =>
216
+ const elementOf = (id: string): HTMLElement | undefined =>
217
+ drawerWith(id)?.elementOf(id) ?? (api.getPanel(id)?.view.content.element as HTMLElement | undefined);
218
+
219
+ const same = (a: PanelLocation | null, b: Placement): boolean =>
189
220
  !!a && a.kind === b.kind && (a.kind !== "drawer" || (b.kind === "drawer" && a.edge === b.edge));
190
221
 
191
222
  // ---- drawers ----
@@ -194,7 +225,7 @@ export function createDock(opts: DockOptions): Dock {
194
225
  let d = drawers.get(edge);
195
226
  if (!d) {
196
227
  if (doc.defaultView?.getComputedStyle(drawerHost).position === "static") drawerHost.style.position = "relative";
197
- d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => save(), menu: (anchor, id) => moveMenu(anchor, id) });
228
+ d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => changed(), menu: (anchor, id) => moveMenu(anchor, id) });
198
229
  drawers.set(edge, d);
199
230
  }
200
231
  return d;
@@ -210,12 +241,31 @@ export function createDock(opts: DockOptions): Dock {
210
241
  // ---- moving panels ----
211
242
 
212
243
  const usable = (p?: PanelPosition): p is PanelPosition => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
244
+ const inGrid = (p?: PanelPosition): p is PanelPosition =>
245
+ !!p && (!p.referencePanel || (api.getPanel(p.referencePanel)?.group.api.location.type === "grid" && api.getPanel(p.referencePanel)!.group.api.isVisible));
213
246
 
214
- const addToGrid = (id: string): void => {
247
+ // Puts a panel in the grid, never in a float, a popout, or a popout's hidden placeholder. A position
248
+ // counts only if its reference panel is in the grid. Without one the panel tabs into the active group, or the first grid group
249
+ // when the active one is a float or a popout, and goes to the grid's edge when the grid is empty.
250
+ const addToGrid = (id: string, prefer?: PanelPosition): void => {
215
251
  const def = defOf(id)!;
216
252
  const spec = specs?.find((s) => s.id === id);
217
- const position = [def.position, spec?.position].find(usable);
218
- api.addPanel({ id, component: id, title: def.title, ...(position ? { position } : {}) });
253
+ const position = [prefer, def.position, spec?.position].find(inGrid);
254
+ if (position) {
255
+ api.addPanel({ id, component: id, title: def.title, position });
256
+ return;
257
+ }
258
+ // While a group is popped out, dockview keeps a hidden placeholder for it in the grid, and a
259
+ // panel tabbed into that would be invisible.
260
+ const showing = (g?: DockviewGroupPanel) => !!g && g.api.location.type === "grid" && g.api.isVisible;
261
+ const active = api.activeGroup;
262
+ const group = showing(active) ? active : api.groups.find(showing);
263
+ api.addPanel({
264
+ id,
265
+ component: id,
266
+ title: def.title,
267
+ position: group ? { referenceGroup: group, direction: "within" } : { direction: "right" },
268
+ });
219
269
  };
220
270
 
221
271
  const enter = (id: string, target: Placement): void => {
@@ -249,37 +299,59 @@ export function createDock(opts: DockOptions): Dock {
249
299
  api.getPanel(id)?.api.setActive();
250
300
  };
251
301
 
302
+ // Runs a move with saving and moved held back, then does both once, so a move that passes through
303
+ // a closed state on its way (leave, then enter) reports only where it ended up.
304
+ const batch = (fn: () => void): void => {
305
+ quiet++;
306
+ try {
307
+ fn();
308
+ } finally {
309
+ quiet--;
310
+ }
311
+ changed();
312
+ };
313
+
252
314
  const place = (id: string, target: Placement): boolean => {
253
315
  if (!defOf(id)) return false;
316
+ batch(() => placeNow(id, target));
317
+ return true;
318
+ };
319
+
320
+ const placeNow = (id: string, target: Placement): void => {
254
321
  const now = where(id);
255
322
  if (!same(now, target)) {
256
- // Grid to float stays inside dockview, which moves the group and keeps the element. Every
257
- // other move goes out through release and back in through hostFor.
258
- if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
259
- enter(id, target);
323
+ if (now?.kind === "drawer" && target.kind === "grid") {
324
+ // Docking from a drawer puts the panel where the drawer was: on its edge, at its size.
325
+ const size = drawer(now.edge).size;
326
+ leave(id);
327
+ addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
328
+ const horizontal = now.edge === "left" || now.edge === "right";
329
+ api.getPanel(id)?.api.setSize(horizontal ? { width: size } : { height: size });
330
+ } else {
331
+ // Grid to float stays inside dockview, which moves the group and keeps the element. Every
332
+ // other move goes out through release and back in through hostFor.
333
+ if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
334
+ enter(id, target);
335
+ }
260
336
  }
261
337
  reveal(id);
262
- save();
263
- return true;
264
338
  };
265
339
 
266
340
  const show = (id: string): boolean => {
267
341
  const def = defOf(id);
268
342
  if (!def) return false;
269
- if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
270
- reveal(id);
271
- save();
343
+ batch(() => {
344
+ if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
345
+ reveal(id);
346
+ });
272
347
  return true;
273
348
  };
274
349
 
275
350
  const toggle = (id: string): boolean => {
276
351
  if (!defOf(id)) return false;
277
- if (where(id)) {
278
- leave(id);
279
- save();
280
- return true;
281
- }
282
- return show(id);
352
+ if (!where(id)) return show(id);
353
+ batch(() => leave(id));
354
+ return true;
283
355
  };
284
356
 
285
357
  // ---- default layout, restore, reset ----
@@ -354,6 +426,34 @@ export function createDock(opts: DockOptions): Dock {
354
426
  saveLayout(storage, key, panels, api.toJSON(), drawerStates());
355
427
  };
356
428
 
429
+ // What `moved` compares against: each panel's last place, element and document.
430
+ const seen = new Map<string, { at: PanelLocation | null; el?: HTMLElement; doc?: Document }>();
431
+ const key0 = (p: PanelLocation | null) => JSON.stringify(p);
432
+
433
+ // Compares every panel's place and document with the last ones seen, and calls `moved` for each
434
+ // that differs. Running it after every change, whatever made it, catches moves made through
435
+ // dock.api and by dockview itself as well as the dock's own.
436
+ const sync = (notify = true): void => {
437
+ if (disposed || quiet) return;
438
+ const ids = new Set([...panels.map((p) => p.id), ...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids), ...seen.keys()]);
439
+ for (const id of ids) {
440
+ if (!defOf(id)) continue;
441
+ const at = where(id);
442
+ const prev = seen.get(id);
443
+ const el = at ? elementOf(id) : prev?.el;
444
+ const doc = at && el ? el.ownerDocument : prev?.doc;
445
+ seen.set(id, { at, el, doc });
446
+ if (!notify || !opts.moved || !el) continue;
447
+ if (key0(at) === key0(prev?.at ?? null) && doc === prev?.doc) continue;
448
+ opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc !== prev.doc });
449
+ }
450
+ };
451
+
452
+ const changed = (): void => {
453
+ save();
454
+ sync();
455
+ };
456
+
357
457
  const reset = (): void => {
358
458
  quiet++;
359
459
  try {
@@ -364,7 +464,7 @@ export function createDock(opts: DockOptions): Dock {
364
464
  } finally {
365
465
  quiet--;
366
466
  }
367
- save();
467
+ changed();
368
468
  };
369
469
 
370
470
  quiet++;
@@ -373,12 +473,12 @@ export function createDock(opts: DockOptions): Dock {
373
473
  } finally {
374
474
  quiet--;
375
475
  }
376
- if (storage) {
377
- // Save what restore built, so the save names today's registry and a rejected save is replaced.
378
- save();
379
- const sub = api.onDidLayoutChange(() => save());
380
- cleanups.push(() => sub.dispose());
381
- }
476
+ // Save what restore built, so the save names today's registry and a rejected save is replaced,
477
+ // and note where everything starts without calling moved for it.
478
+ save();
479
+ sync(false);
480
+ const sub = api.onDidLayoutChange(() => changed());
481
+ cleanups.push(() => sub.dispose());
382
482
 
383
483
  // ---- theme ----
384
484
 
package/src/drawerView.ts CHANGED
@@ -115,6 +115,11 @@ export class DrawerView {
115
115
  return this.px;
116
116
  }
117
117
 
118
+ /** The element a panel in this drawer shows, or undefined if it isn't here. */
119
+ elementOf(id: string): HTMLElement | undefined {
120
+ return this.tabs.find((t) => t.id === id)?.el;
121
+ }
122
+
118
123
  has(id: string): boolean {
119
124
  return this.tabs.some((t) => t.id === id);
120
125
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createDock, type Dock, type DockOptions } from "./dock.js";
1
+ export { createDock, type Dock, type DockOptions, type MoveInfo, type PanelLocation } from "./dock.js";
2
2
  export {
3
3
  documentTheme,
4
4
  loadLayout,