@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.
@@ -0,0 +1,238 @@
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
+ /** 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
+
123
+ has(id: string): boolean {
124
+ return this.tabs.some((t) => t.id === id);
125
+ }
126
+
127
+ /** Adds a panel as a tab, or does nothing if it's already here. It doesn't open the drawer. */
128
+ add(id: string, title: string, el: HTMLElement): void {
129
+ if (this.has(id)) return;
130
+ const doc = this.host.ownerDocument;
131
+ const button = doc.createElement("button");
132
+ button.type = "button";
133
+ button.className = "dock-drawer-tab";
134
+ button.setAttribute("role", "tab");
135
+ button.dataset.panel = id;
136
+ const label = doc.createElement("span");
137
+ label.textContent = title;
138
+ const more = doc.createElement("span");
139
+ more.className = "dock-drawer-tab-menu";
140
+ more.textContent = "⋯";
141
+ more.title = "Move this panel";
142
+ more.addEventListener("click", (e) => {
143
+ e.stopPropagation();
144
+ this.hooks.menu(more, id);
145
+ });
146
+ button.append(label, " ", more);
147
+ button.addEventListener("click", () => this.activate(id));
148
+ this.tabStrip.appendChild(button);
149
+ el.style.display = "none";
150
+ this.body.appendChild(el);
151
+ this.tabs.push({ id, el, button });
152
+ if (!this.activeId) this.activate(id, false);
153
+ this.hooks.changed();
154
+ }
155
+
156
+ /** Takes a panel out and returns its element, detached. The next tab becomes active. */
157
+ remove(id: string): HTMLElement | undefined {
158
+ const i = this.tabs.findIndex((t) => t.id === id);
159
+ if (i < 0) return undefined;
160
+ const [tab] = this.tabs.splice(i, 1);
161
+ tab.button.remove();
162
+ tab.el.remove();
163
+ tab.el.style.display = "";
164
+ if (this.activeId === id) {
165
+ this.activeId = undefined;
166
+ const next = this.tabs[Math.min(i, this.tabs.length - 1)];
167
+ if (next) this.activate(next.id, false);
168
+ }
169
+ if (this.tabs.length === 0) this.setOpen(false, false);
170
+ this.hooks.changed();
171
+ return tab.el;
172
+ }
173
+
174
+ activate(id: string, notify = true): void {
175
+ if (!this.has(id)) return;
176
+ this.activeId = id;
177
+ for (const t of this.tabs) {
178
+ const on = t.id === id;
179
+ t.el.style.display = on ? "" : "none";
180
+ t.button.classList.toggle("dock-drawer-tab-active", on);
181
+ t.button.setAttribute("aria-selected", String(on));
182
+ }
183
+ if (notify) this.hooks.changed();
184
+ }
185
+
186
+ setOpen(open: boolean, notify = true): void {
187
+ const show = open && this.tabs.length > 0;
188
+ if (show === this.isOpen) return;
189
+ this.isOpen = show;
190
+ this.root.style.display = show ? "flex" : "none";
191
+ this.scrim.style.display = show ? "block" : "none";
192
+ if (show) this.applySize();
193
+ if (notify) this.hooks.changed();
194
+ }
195
+
196
+ setSize(px: number, notify = true): void {
197
+ this.px = Math.round(px);
198
+ this.applySize();
199
+ if (notify) this.hooks.changed();
200
+ }
201
+
202
+ state(): DrawerState {
203
+ return { panels: this.ids, active: this.activeId, size: this.px, open: this.isOpen };
204
+ }
205
+
206
+ dispose(): void {
207
+ this.scrim.remove();
208
+ this.root.remove();
209
+ }
210
+
211
+ // Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
212
+ private applySize(): void {
213
+ const rect = this.host.getBoundingClientRect();
214
+ const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
215
+ const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
216
+ this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = `${px}px`;
217
+ }
218
+
219
+ private drag(e: PointerEvent): void {
220
+ e.preventDefault();
221
+ const move = (ev: PointerEvent): void => {
222
+ const rect = this.host.getBoundingClientRect();
223
+ const horizontal = HORIZONTAL[this.edge];
224
+ const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
225
+ const span = horizontal ? rect.width : rect.height;
226
+ this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
227
+ };
228
+ const up = (): void => {
229
+ this.handle.ownerDocument.removeEventListener("pointermove", move);
230
+ this.handle.ownerDocument.removeEventListener("pointerup", up);
231
+ this.root.classList.remove("dock-drawer-resizing");
232
+ this.hooks.changed();
233
+ };
234
+ this.root.classList.add("dock-drawer-resizing");
235
+ this.handle.ownerDocument.addEventListener("pointermove", move);
236
+ this.handle.ownerDocument.addEventListener("pointerup", up);
237
+ }
238
+ }
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,
@@ -11,7 +11,11 @@ export {
11
11
  type PanelDef,
12
12
  type PanelPosition,
13
13
  type PanelSpec,
14
+ type Placement,
15
+ type SavedDrawer,
16
+ type SavedDrawers,
14
17
  } from "./layout.js";
18
+ export type { DrawerConfig } from "./drawerView.js";
15
19
  export {
16
20
  clampSize,
17
21
  drawerSize,
package/src/layout.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { DrawerEdge } from "./drawer.js";
2
+
1
3
  // The DOM-free half of the dock: the panel registry's types, the saved layout, reconcile and prune,
2
4
  // and theme resolution. It needs neither dockview nor a browser, so it unit-tests on its own.
3
5
  // dock.ts feeds these to dockview-core.
@@ -24,8 +26,31 @@ export interface PanelDef {
24
26
  * layout is tried next, and then the grid root.
25
27
  */
26
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;
27
49
  }
28
50
 
51
+ /** The saved drawers, by edge. An edge with no drawer is absent. */
52
+ export type SavedDrawers = Partial<Record<DrawerEdge, SavedDrawer>>;
53
+
29
54
  /** A dockview `addPanel` position: a direction from a panel that is already open. */
30
55
  export interface PanelPosition {
31
56
  direction: "left" | "right" | "above" | "below" | "within";
@@ -51,9 +76,10 @@ export interface DockStorage {
51
76
  }
52
77
 
53
78
  interface SavedLayout {
54
- v: 1;
79
+ v: 1 | 2;
55
80
  panels: string[];
56
81
  layout: unknown;
82
+ drawers?: SavedDrawers;
57
83
  }
58
84
 
59
85
  /**
@@ -63,16 +89,19 @@ interface SavedLayout {
63
89
  export interface LoadedLayout {
64
90
  layout: unknown;
65
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;
66
94
  }
67
95
 
68
96
  /**
69
97
  * Saves dockview's `toJSON()` blob as `{v: 1, panels, layout}`, where `panels` is the registry's
70
- * ids. A storage that throws (quota, private mode) is ignored, so the page keeps working and the
71
- * layout just isn't saved.
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.
72
100
  */
73
- export function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown): void {
101
+ export function saveLayout(storage: DockStorage, key: string, registry: readonly PanelDef[], layout: unknown, drawers?: SavedDrawers): void {
74
102
  try {
75
- const saved: SavedLayout = { v: 1, panels: registry.map((p) => p.id), layout };
103
+ const panels = registry.map((p) => p.id);
104
+ const saved: SavedLayout = drawers ? { v: 2, panels, layout, drawers } : { v: 1, panels, layout };
76
105
  storage.setItem(key, JSON.stringify(saved));
77
106
  } catch {
78
107
  // best effort
@@ -96,9 +125,10 @@ export function loadLayout(storage: DockStorage, key: string): LoadedLayout | nu
96
125
  const parsed: unknown = JSON.parse(raw);
97
126
  if (parsed && typeof parsed === "object" && "v" in parsed && "layout" in parsed) {
98
127
  const s = parsed as SavedLayout;
99
- return { layout: s.layout, savedPanels: Array.isArray(s.panels) ? s.panels : [] };
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 };
100
130
  }
101
- return { layout: parsed, savedPanels: [] };
131
+ return { layout: parsed, savedPanels: [], drawers: {} };
102
132
  } catch {
103
133
  return null;
104
134
  }