@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 +12 -6
- package/dist/index.d.mts +163 -66
- package/dist/index.d.ts +163 -66
- package/dist/index.js +537 -94
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +537 -94
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/dock.ts +473 -82
- package/src/drawerView.ts +238 -0
- package/src/index.ts +5 -1
- package/src/layout.ts +37 -7
package/dist/index.mjs
CHANGED
|
@@ -2,10 +2,218 @@ import { DockviewComponent, themeDark, themeLight } from 'dockview-core';
|
|
|
2
2
|
|
|
3
3
|
// src/dock.ts
|
|
4
4
|
|
|
5
|
+
// src/drawer.ts
|
|
6
|
+
var DRAWER_MIN = 280;
|
|
7
|
+
var DRAWER_MAX_FRAC = 0.9;
|
|
8
|
+
var DRAWER_DEFAULT = 420;
|
|
9
|
+
function clampSize(px, viewport, bounds = {}) {
|
|
10
|
+
const min = bounds.min ?? DRAWER_MIN;
|
|
11
|
+
const max = Math.max(min, Math.floor(viewport * (bounds.maxFrac ?? DRAWER_MAX_FRAC)));
|
|
12
|
+
return Math.min(max, Math.max(min, Math.round(px)));
|
|
13
|
+
}
|
|
14
|
+
function drawerSize(edge, pointer, viewport, bounds) {
|
|
15
|
+
const fromEdge = edge === "left" || edge === "top" ? pointer : viewport - pointer;
|
|
16
|
+
return clampSize(fromEdge, viewport, bounds);
|
|
17
|
+
}
|
|
18
|
+
function loadDrawerSize(storage, key, fallback = DRAWER_DEFAULT) {
|
|
19
|
+
let raw = null;
|
|
20
|
+
try {
|
|
21
|
+
raw = storage?.getItem(key) ?? null;
|
|
22
|
+
} catch {
|
|
23
|
+
return fallback;
|
|
24
|
+
}
|
|
25
|
+
const n = raw == null ? NaN : parseInt(raw, 10);
|
|
26
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
27
|
+
}
|
|
28
|
+
function saveDrawerSize(storage, key, px) {
|
|
29
|
+
try {
|
|
30
|
+
storage?.setItem(key, String(Math.round(px)));
|
|
31
|
+
} catch {
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/drawerView.ts
|
|
36
|
+
var Z_SCRIM = "calc(var(--dv-overlay-z-index, 999) + 200)";
|
|
37
|
+
var Z_DRAWER = "calc(var(--dv-overlay-z-index, 999) + 201)";
|
|
38
|
+
var HORIZONTAL = { left: true, right: true, top: false, bottom: false };
|
|
39
|
+
var DrawerView = class {
|
|
40
|
+
constructor(edge, host, config, hooks) {
|
|
41
|
+
this.edge = edge;
|
|
42
|
+
this.host = host;
|
|
43
|
+
this.config = config;
|
|
44
|
+
this.hooks = hooks;
|
|
45
|
+
this.tabs = [];
|
|
46
|
+
this.isOpen = false;
|
|
47
|
+
const doc = host.ownerDocument;
|
|
48
|
+
this.px = config.size ?? DRAWER_DEFAULT;
|
|
49
|
+
this.scrim = doc.createElement("div");
|
|
50
|
+
this.scrim.className = `dock-drawer-scrim dock-drawer-scrim-${edge}`;
|
|
51
|
+
Object.assign(this.scrim.style, { position: "absolute", inset: "0", background: "rgba(0,0,0,.25)", zIndex: Z_SCRIM, display: "none" });
|
|
52
|
+
this.scrim.addEventListener("click", () => this.setOpen(false));
|
|
53
|
+
this.root = doc.createElement("aside");
|
|
54
|
+
this.root.className = `dock-drawer dock-drawer-${edge}`;
|
|
55
|
+
this.root.dataset.edge = edge;
|
|
56
|
+
Object.assign(this.root.style, {
|
|
57
|
+
position: "absolute",
|
|
58
|
+
zIndex: Z_DRAWER,
|
|
59
|
+
display: "none",
|
|
60
|
+
flexDirection: "column",
|
|
61
|
+
background: "var(--dv-group-view-background-color, Canvas)",
|
|
62
|
+
color: "var(--dv-activegroup-visiblepanel-tab-color, CanvasText)",
|
|
63
|
+
boxShadow: "0 0 16px rgba(0,0,0,.35)",
|
|
64
|
+
[edge]: "0",
|
|
65
|
+
...HORIZONTAL[edge] ? { top: "0", bottom: "0" } : { left: "0", right: "0" }
|
|
66
|
+
});
|
|
67
|
+
this.tabStrip = doc.createElement("div");
|
|
68
|
+
this.tabStrip.className = "dock-drawer-tabs";
|
|
69
|
+
this.tabStrip.setAttribute("role", "tablist");
|
|
70
|
+
Object.assign(this.tabStrip.style, { display: "flex", gap: "2px", alignItems: "center", flex: "none" });
|
|
71
|
+
this.body = doc.createElement("div");
|
|
72
|
+
this.body.className = "dock-drawer-body";
|
|
73
|
+
Object.assign(this.body.style, { flex: "1", minHeight: "0", overflow: "auto", position: "relative" });
|
|
74
|
+
this.handle = doc.createElement("div");
|
|
75
|
+
this.handle.className = "dock-drawer-handle";
|
|
76
|
+
const inner = { left: "right", right: "left", top: "bottom", bottom: "top" }[edge];
|
|
77
|
+
Object.assign(this.handle.style, {
|
|
78
|
+
position: "absolute",
|
|
79
|
+
[inner]: "-3px",
|
|
80
|
+
...HORIZONTAL[edge] ? { top: "0", bottom: "0", width: "6px", cursor: "col-resize" } : { left: "0", right: "0", height: "6px", cursor: "row-resize" },
|
|
81
|
+
touchAction: "none"
|
|
82
|
+
});
|
|
83
|
+
this.handle.addEventListener("pointerdown", (e) => this.drag(e));
|
|
84
|
+
this.root.append(this.tabStrip, this.body, this.handle);
|
|
85
|
+
host.append(this.scrim, this.root);
|
|
86
|
+
this.applySize();
|
|
87
|
+
}
|
|
88
|
+
get ids() {
|
|
89
|
+
return this.tabs.map((t) => t.id);
|
|
90
|
+
}
|
|
91
|
+
get active() {
|
|
92
|
+
return this.activeId;
|
|
93
|
+
}
|
|
94
|
+
get open() {
|
|
95
|
+
return this.isOpen;
|
|
96
|
+
}
|
|
97
|
+
get size() {
|
|
98
|
+
return this.px;
|
|
99
|
+
}
|
|
100
|
+
/** The element a panel in this drawer shows, or undefined if it isn't here. */
|
|
101
|
+
elementOf(id) {
|
|
102
|
+
return this.tabs.find((t) => t.id === id)?.el;
|
|
103
|
+
}
|
|
104
|
+
has(id) {
|
|
105
|
+
return this.tabs.some((t) => t.id === id);
|
|
106
|
+
}
|
|
107
|
+
/** Adds a panel as a tab, or does nothing if it's already here. It doesn't open the drawer. */
|
|
108
|
+
add(id, title, el) {
|
|
109
|
+
if (this.has(id)) return;
|
|
110
|
+
const doc = this.host.ownerDocument;
|
|
111
|
+
const button = doc.createElement("button");
|
|
112
|
+
button.type = "button";
|
|
113
|
+
button.className = "dock-drawer-tab";
|
|
114
|
+
button.setAttribute("role", "tab");
|
|
115
|
+
button.dataset.panel = id;
|
|
116
|
+
const label = doc.createElement("span");
|
|
117
|
+
label.textContent = title;
|
|
118
|
+
const more = doc.createElement("span");
|
|
119
|
+
more.className = "dock-drawer-tab-menu";
|
|
120
|
+
more.textContent = "\u22EF";
|
|
121
|
+
more.title = "Move this panel";
|
|
122
|
+
more.addEventListener("click", (e) => {
|
|
123
|
+
e.stopPropagation();
|
|
124
|
+
this.hooks.menu(more, id);
|
|
125
|
+
});
|
|
126
|
+
button.append(label, " ", more);
|
|
127
|
+
button.addEventListener("click", () => this.activate(id));
|
|
128
|
+
this.tabStrip.appendChild(button);
|
|
129
|
+
el.style.display = "none";
|
|
130
|
+
this.body.appendChild(el);
|
|
131
|
+
this.tabs.push({ id, el, button });
|
|
132
|
+
if (!this.activeId) this.activate(id, false);
|
|
133
|
+
this.hooks.changed();
|
|
134
|
+
}
|
|
135
|
+
/** Takes a panel out and returns its element, detached. The next tab becomes active. */
|
|
136
|
+
remove(id) {
|
|
137
|
+
const i = this.tabs.findIndex((t) => t.id === id);
|
|
138
|
+
if (i < 0) return void 0;
|
|
139
|
+
const [tab] = this.tabs.splice(i, 1);
|
|
140
|
+
tab.button.remove();
|
|
141
|
+
tab.el.remove();
|
|
142
|
+
tab.el.style.display = "";
|
|
143
|
+
if (this.activeId === id) {
|
|
144
|
+
this.activeId = void 0;
|
|
145
|
+
const next = this.tabs[Math.min(i, this.tabs.length - 1)];
|
|
146
|
+
if (next) this.activate(next.id, false);
|
|
147
|
+
}
|
|
148
|
+
if (this.tabs.length === 0) this.setOpen(false, false);
|
|
149
|
+
this.hooks.changed();
|
|
150
|
+
return tab.el;
|
|
151
|
+
}
|
|
152
|
+
activate(id, notify = true) {
|
|
153
|
+
if (!this.has(id)) return;
|
|
154
|
+
this.activeId = id;
|
|
155
|
+
for (const t of this.tabs) {
|
|
156
|
+
const on = t.id === id;
|
|
157
|
+
t.el.style.display = on ? "" : "none";
|
|
158
|
+
t.button.classList.toggle("dock-drawer-tab-active", on);
|
|
159
|
+
t.button.setAttribute("aria-selected", String(on));
|
|
160
|
+
}
|
|
161
|
+
if (notify) this.hooks.changed();
|
|
162
|
+
}
|
|
163
|
+
setOpen(open, notify = true) {
|
|
164
|
+
const show = open && this.tabs.length > 0;
|
|
165
|
+
if (show === this.isOpen) return;
|
|
166
|
+
this.isOpen = show;
|
|
167
|
+
this.root.style.display = show ? "flex" : "none";
|
|
168
|
+
this.scrim.style.display = show ? "block" : "none";
|
|
169
|
+
if (show) this.applySize();
|
|
170
|
+
if (notify) this.hooks.changed();
|
|
171
|
+
}
|
|
172
|
+
setSize(px, notify = true) {
|
|
173
|
+
this.px = Math.round(px);
|
|
174
|
+
this.applySize();
|
|
175
|
+
if (notify) this.hooks.changed();
|
|
176
|
+
}
|
|
177
|
+
state() {
|
|
178
|
+
return { panels: this.ids, active: this.activeId, size: this.px, open: this.isOpen };
|
|
179
|
+
}
|
|
180
|
+
dispose() {
|
|
181
|
+
this.scrim.remove();
|
|
182
|
+
this.root.remove();
|
|
183
|
+
}
|
|
184
|
+
// Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
|
|
185
|
+
applySize() {
|
|
186
|
+
const rect = this.host.getBoundingClientRect();
|
|
187
|
+
const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
|
|
188
|
+
const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
|
|
189
|
+
this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = `${px}px`;
|
|
190
|
+
}
|
|
191
|
+
drag(e) {
|
|
192
|
+
e.preventDefault();
|
|
193
|
+
const move = (ev) => {
|
|
194
|
+
const rect = this.host.getBoundingClientRect();
|
|
195
|
+
const horizontal = HORIZONTAL[this.edge];
|
|
196
|
+
const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
|
|
197
|
+
const span = horizontal ? rect.width : rect.height;
|
|
198
|
+
this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
|
|
199
|
+
};
|
|
200
|
+
const up = () => {
|
|
201
|
+
this.handle.ownerDocument.removeEventListener("pointermove", move);
|
|
202
|
+
this.handle.ownerDocument.removeEventListener("pointerup", up);
|
|
203
|
+
this.root.classList.remove("dock-drawer-resizing");
|
|
204
|
+
this.hooks.changed();
|
|
205
|
+
};
|
|
206
|
+
this.root.classList.add("dock-drawer-resizing");
|
|
207
|
+
this.handle.ownerDocument.addEventListener("pointermove", move);
|
|
208
|
+
this.handle.ownerDocument.addEventListener("pointerup", up);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
5
212
|
// src/layout.ts
|
|
6
|
-
function saveLayout(storage, key, registry, layout) {
|
|
213
|
+
function saveLayout(storage, key, registry, layout, drawers) {
|
|
7
214
|
try {
|
|
8
|
-
const
|
|
215
|
+
const panels = registry.map((p) => p.id);
|
|
216
|
+
const saved = drawers ? { v: 2, panels, layout, drawers } : { v: 1, panels, layout };
|
|
9
217
|
storage.setItem(key, JSON.stringify(saved));
|
|
10
218
|
} catch {
|
|
11
219
|
}
|
|
@@ -22,9 +230,10 @@ function loadLayout(storage, key) {
|
|
|
22
230
|
const parsed = JSON.parse(raw);
|
|
23
231
|
if (parsed && typeof parsed === "object" && "v" in parsed && "layout" in parsed) {
|
|
24
232
|
const s = parsed;
|
|
25
|
-
|
|
233
|
+
const drawers = s.v === 2 && s.drawers && typeof s.drawers === "object" ? s.drawers : {};
|
|
234
|
+
return { layout: s.layout, savedPanels: Array.isArray(s.panels) ? s.panels : [], drawers };
|
|
26
235
|
}
|
|
27
|
-
return { layout: parsed, savedPanels: [] };
|
|
236
|
+
return { layout: parsed, savedPanels: [], drawers: {} };
|
|
28
237
|
} catch {
|
|
29
238
|
return null;
|
|
30
239
|
}
|
|
@@ -48,6 +257,9 @@ function documentTheme(doc = document) {
|
|
|
48
257
|
}
|
|
49
258
|
|
|
50
259
|
// src/dock.ts
|
|
260
|
+
var EDGES = ["left", "right", "top", "bottom"];
|
|
261
|
+
var placementLabel = (p) => !p || p.kind === "grid" ? "" : p.kind === "drawer" ? ` \xB7 ${p.edge} drawer` : ` \xB7 ${p.kind}`;
|
|
262
|
+
var TOWARD_EDGE = { left: "left", right: "right", top: "above", bottom: "below" };
|
|
51
263
|
function createDock(opts) {
|
|
52
264
|
const { container, panels, hostFor, release } = opts;
|
|
53
265
|
const storage = opts.storage ?? null;
|
|
@@ -58,9 +270,18 @@ function createDock(opts) {
|
|
|
58
270
|
const byId = new Map(panels.map((p) => [p.id, p]));
|
|
59
271
|
const specs = Array.isArray(opts.defaultLayout) ? opts.defaultLayout : null;
|
|
60
272
|
const cleanups = [];
|
|
273
|
+
const drawerHost = opts.drawerHost ?? container;
|
|
274
|
+
const drawers = /* @__PURE__ */ new Map();
|
|
61
275
|
let disposed = false;
|
|
276
|
+
let quiet = 0;
|
|
277
|
+
const defOf = (id) => {
|
|
278
|
+
const known = byId.get(id);
|
|
279
|
+
if (known) return known;
|
|
280
|
+
const def = opts.resolve?.(id);
|
|
281
|
+
return def && def.id === id ? def : void 0;
|
|
282
|
+
};
|
|
62
283
|
const renderer = (id) => {
|
|
63
|
-
if (!
|
|
284
|
+
if (!defOf(id)) return { element: doc.createElement("div"), init: () => {
|
|
64
285
|
} };
|
|
65
286
|
const element = hostFor(id);
|
|
66
287
|
return { element, init: () => {
|
|
@@ -68,29 +289,171 @@ function createDock(opts) {
|
|
|
68
289
|
};
|
|
69
290
|
const component = new DockviewComponent(container, {
|
|
70
291
|
createComponent: (o) => renderer(o.id),
|
|
71
|
-
// "always" keeps a background tab's content attached, only hidden
|
|
72
|
-
// and an app that finds its content by id
|
|
292
|
+
// "always" keeps a background tab's content attached, only hidden, in a layer dockview
|
|
293
|
+
// positions over the group. The default detaches it, and an app that finds its content by id
|
|
294
|
+
// at startup would miss the panels behind a tab. The layer also means dragging a tab between
|
|
295
|
+
// groups doesn't re-parent the content, so an iframe in it doesn't reload.
|
|
73
296
|
defaultRenderer: "always",
|
|
74
|
-
theme: theme() === "dark" ? themeDark : themeLight
|
|
297
|
+
theme: theme() === "dark" ? themeDark : themeLight,
|
|
298
|
+
...opts.moveMenu === false ? {} : { createRightHeaderActionComponent: () => headerMenu() }
|
|
75
299
|
});
|
|
76
300
|
const api = component.api;
|
|
301
|
+
const drawerWith = (id) => [...drawers.values()].find((d) => d.has(id));
|
|
302
|
+
const where = (id) => {
|
|
303
|
+
const d = drawerWith(id);
|
|
304
|
+
if (d) return { kind: "drawer", edge: d.edge };
|
|
305
|
+
const panel = api.getPanel(id);
|
|
306
|
+
if (!panel) return null;
|
|
307
|
+
const type = panel.group.api.location.type;
|
|
308
|
+
return type === "floating" ? { kind: "float" } : type === "popout" ? { kind: "popout" } : { kind: "grid" };
|
|
309
|
+
};
|
|
310
|
+
const elementOf = (id) => drawerWith(id)?.elementOf(id) ?? api.getPanel(id)?.view.content.element;
|
|
311
|
+
const same = (a, b) => !!a && a.kind === b.kind && (a.kind !== "drawer" || b.kind === "drawer" && a.edge === b.edge);
|
|
312
|
+
const drawer = (edge) => {
|
|
313
|
+
let d = drawers.get(edge);
|
|
314
|
+
if (!d) {
|
|
315
|
+
if (doc.defaultView?.getComputedStyle(drawerHost).position === "static") drawerHost.style.position = "relative";
|
|
316
|
+
d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => changed(), menu: (anchor, id) => moveMenu(anchor, id) });
|
|
317
|
+
drawers.set(edge, d);
|
|
318
|
+
}
|
|
319
|
+
return d;
|
|
320
|
+
};
|
|
321
|
+
const drawerStates = () => {
|
|
322
|
+
const out = {};
|
|
323
|
+
for (const [edge, d] of drawers) if (d.ids.length || d.size !== (opts.drawers?.[edge]?.size ?? DRAWER_DEFAULT)) out[edge] = d.state();
|
|
324
|
+
return out;
|
|
325
|
+
};
|
|
77
326
|
const usable = (p) => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
|
|
78
|
-
const
|
|
79
|
-
|
|
327
|
+
const inGrid = (p) => !!p && (!p.referencePanel || api.getPanel(p.referencePanel)?.group.api.location.type === "grid" && api.getPanel(p.referencePanel).group.api.isVisible);
|
|
328
|
+
const addToGrid = (id, prefer) => {
|
|
329
|
+
const def = defOf(id);
|
|
80
330
|
const spec = specs?.find((s) => s.id === id);
|
|
81
|
-
const position = [def.position, spec?.position].find(
|
|
82
|
-
|
|
331
|
+
const position = [prefer, def.position, spec?.position].find(inGrid);
|
|
332
|
+
if (position) {
|
|
333
|
+
api.addPanel({ id, component: id, title: def.title, position });
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const showing = (g) => !!g && g.api.location.type === "grid" && g.api.isVisible;
|
|
337
|
+
const active = api.activeGroup;
|
|
338
|
+
const group = showing(active) ? active : api.groups.find(showing);
|
|
339
|
+
api.addPanel({
|
|
340
|
+
id,
|
|
341
|
+
component: id,
|
|
342
|
+
title: def.title,
|
|
343
|
+
position: group ? { referenceGroup: group, direction: "within" } : { direction: "right" }
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
const enter = (id, target) => {
|
|
347
|
+
if (target.kind === "drawer") {
|
|
348
|
+
drawer(target.edge).add(id, defOf(id).title, hostFor(id));
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (!api.getPanel(id)) addToGrid(id);
|
|
352
|
+
const panel = api.getPanel(id);
|
|
353
|
+
if (target.kind === "float" && panel.group.api.location.type !== "floating") api.addFloatingGroup(panel);
|
|
354
|
+
};
|
|
355
|
+
const leave = (id) => {
|
|
356
|
+
const d = drawerWith(id);
|
|
357
|
+
if (d) {
|
|
358
|
+
const el = d.remove(id);
|
|
359
|
+
if (el) release?.(id, el);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const panel = api.getPanel(id);
|
|
363
|
+
if (panel) api.removePanel(panel);
|
|
364
|
+
};
|
|
365
|
+
const reveal = (id) => {
|
|
366
|
+
const d = drawerWith(id);
|
|
367
|
+
if (d) {
|
|
368
|
+
d.activate(id);
|
|
369
|
+
d.setOpen(true);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
api.getPanel(id)?.api.setActive();
|
|
373
|
+
};
|
|
374
|
+
const batch = (fn) => {
|
|
375
|
+
quiet++;
|
|
376
|
+
try {
|
|
377
|
+
fn();
|
|
378
|
+
} finally {
|
|
379
|
+
quiet--;
|
|
380
|
+
}
|
|
381
|
+
changed();
|
|
382
|
+
};
|
|
383
|
+
const place = (id, target) => {
|
|
384
|
+
if (!defOf(id)) return false;
|
|
385
|
+
batch(() => placeNow(id, target));
|
|
386
|
+
return true;
|
|
387
|
+
};
|
|
388
|
+
const placeNow = (id, target) => {
|
|
389
|
+
const now = where(id);
|
|
390
|
+
if (!same(now, target)) {
|
|
391
|
+
if (now?.kind === "drawer" && target.kind === "grid") {
|
|
392
|
+
const size = drawer(now.edge).size;
|
|
393
|
+
leave(id);
|
|
394
|
+
addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
|
|
395
|
+
const horizontal = now.edge === "left" || now.edge === "right";
|
|
396
|
+
api.getPanel(id)?.api.setSize(horizontal ? { width: size } : { height: size });
|
|
397
|
+
} else {
|
|
398
|
+
if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
|
|
399
|
+
enter(id, target);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
reveal(id);
|
|
403
|
+
};
|
|
404
|
+
const show = (id) => {
|
|
405
|
+
const def = defOf(id);
|
|
406
|
+
if (!def) return false;
|
|
407
|
+
batch(() => {
|
|
408
|
+
if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
|
|
409
|
+
reveal(id);
|
|
410
|
+
});
|
|
411
|
+
return true;
|
|
412
|
+
};
|
|
413
|
+
const toggle = (id) => {
|
|
414
|
+
if (!defOf(id)) return false;
|
|
415
|
+
if (!where(id)) return show(id);
|
|
416
|
+
batch(() => leave(id));
|
|
417
|
+
return true;
|
|
83
418
|
};
|
|
84
419
|
const applyDefault = () => {
|
|
85
|
-
|
|
420
|
+
const elsewhere = (def) => !!def?.defaultPlacement && def.defaultPlacement.kind === "drawer";
|
|
421
|
+
if (specs) {
|
|
422
|
+
for (const spec of specs) {
|
|
423
|
+
const def = byId.get(spec.id);
|
|
424
|
+
if (!def || elsewhere(def) || api.getPanel(spec.id)) continue;
|
|
425
|
+
const position = usable(spec.position) ? spec.position : void 0;
|
|
426
|
+
api.addPanel({ id: spec.id, component: spec.id, title: spec.title ?? def.title, ...position ? { position } : {} });
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
86
429
|
opts.defaultLayout(api);
|
|
87
|
-
return;
|
|
88
430
|
}
|
|
89
|
-
for (const
|
|
90
|
-
const
|
|
91
|
-
if (!
|
|
92
|
-
|
|
93
|
-
|
|
431
|
+
for (const def of panels) {
|
|
432
|
+
const p = def.defaultPlacement;
|
|
433
|
+
if (!p || p.kind === "grid") continue;
|
|
434
|
+
if (p.kind === "drawer" && !where(def.id)) enter(def.id, p);
|
|
435
|
+
if (p.kind === "float" && (where(def.id)?.kind ?? "grid") === "grid") enter(def.id, p);
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
const clearDrawers = () => {
|
|
439
|
+
for (const d of drawers.values()) {
|
|
440
|
+
for (const id of d.ids) leave(id);
|
|
441
|
+
d.setOpen(false, false);
|
|
442
|
+
d.setSize(opts.drawers?.[d.edge]?.size ?? DRAWER_DEFAULT, false);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
const restoreDrawers = (saved) => {
|
|
446
|
+
for (const edge of EDGES) {
|
|
447
|
+
const s = saved[edge];
|
|
448
|
+
if (!s) continue;
|
|
449
|
+
const d = drawer(edge);
|
|
450
|
+
if (typeof s.size === "number" && s.size > 0) d.setSize(s.size, false);
|
|
451
|
+
for (const id of Array.isArray(s.panels) ? s.panels : []) {
|
|
452
|
+
const def = defOf(id);
|
|
453
|
+
if (def && !where(id)) d.add(id, def.title, hostFor(id));
|
|
454
|
+
}
|
|
455
|
+
if (s.active) d.activate(s.active, false);
|
|
456
|
+
if (s.open) d.setOpen(true, false);
|
|
94
457
|
}
|
|
95
458
|
};
|
|
96
459
|
const restore = () => {
|
|
@@ -101,26 +464,66 @@ function createDock(opts) {
|
|
|
101
464
|
}
|
|
102
465
|
try {
|
|
103
466
|
api.fromJSON(saved.layout);
|
|
104
|
-
for (const
|
|
105
|
-
|
|
106
|
-
if (panel) api.removePanel(panel);
|
|
107
|
-
}
|
|
467
|
+
for (const panel of [...api.panels]) if (!defOf(panel.id)) api.removePanel(panel);
|
|
468
|
+
restoreDrawers(saved.drawers);
|
|
108
469
|
for (const id of panelsToReconcile(saved.savedPanels, panels)) {
|
|
109
|
-
if (!
|
|
470
|
+
if (!where(id)) enter(id, byId.get(id).defaultPlacement ?? { kind: "grid" });
|
|
110
471
|
}
|
|
111
472
|
} catch (err) {
|
|
112
473
|
console.warn("tsappkit-dock: saved layout rejected, using the default", err);
|
|
113
474
|
storage?.removeItem(key);
|
|
475
|
+
clearDrawers();
|
|
114
476
|
api.clear();
|
|
115
477
|
applyDefault();
|
|
116
478
|
}
|
|
117
479
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
saveLayout(storage, key, panels, api.toJSON());
|
|
121
|
-
|
|
122
|
-
|
|
480
|
+
const save = () => {
|
|
481
|
+
if (quiet || disposed || !storage) return;
|
|
482
|
+
saveLayout(storage, key, panels, api.toJSON(), drawerStates());
|
|
483
|
+
};
|
|
484
|
+
const seen = /* @__PURE__ */ new Map();
|
|
485
|
+
const key0 = (p) => JSON.stringify(p);
|
|
486
|
+
const sync = (notify = true) => {
|
|
487
|
+
if (disposed || quiet) return;
|
|
488
|
+
const ids = /* @__PURE__ */ new Set([...panels.map((p) => p.id), ...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids), ...seen.keys()]);
|
|
489
|
+
for (const id of ids) {
|
|
490
|
+
if (!defOf(id)) continue;
|
|
491
|
+
const at = where(id);
|
|
492
|
+
const prev = seen.get(id);
|
|
493
|
+
const el = at ? elementOf(id) : prev?.el;
|
|
494
|
+
const doc2 = at && el ? el.ownerDocument : prev?.doc;
|
|
495
|
+
seen.set(id, { at, el, doc: doc2 });
|
|
496
|
+
if (!notify || !opts.moved || !el) continue;
|
|
497
|
+
if (key0(at) === key0(prev?.at ?? null) && doc2 === prev?.doc) continue;
|
|
498
|
+
opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc2 !== prev.doc });
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
const changed = () => {
|
|
502
|
+
save();
|
|
503
|
+
sync();
|
|
504
|
+
};
|
|
505
|
+
const reset = () => {
|
|
506
|
+
quiet++;
|
|
507
|
+
try {
|
|
508
|
+
storage?.removeItem(key);
|
|
509
|
+
clearDrawers();
|
|
510
|
+
api.clear();
|
|
511
|
+
applyDefault();
|
|
512
|
+
} finally {
|
|
513
|
+
quiet--;
|
|
514
|
+
}
|
|
515
|
+
changed();
|
|
516
|
+
};
|
|
517
|
+
quiet++;
|
|
518
|
+
try {
|
|
519
|
+
restore();
|
|
520
|
+
} finally {
|
|
521
|
+
quiet--;
|
|
123
522
|
}
|
|
523
|
+
save();
|
|
524
|
+
sync(false);
|
|
525
|
+
const sub = api.onDidLayoutChange(() => changed());
|
|
526
|
+
cleanups.push(() => sub.dispose());
|
|
124
527
|
const applyTheme = () => component.updateOptions({ theme: theme() === "dark" ? themeDark : themeLight });
|
|
125
528
|
const view = doc.defaultView;
|
|
126
529
|
if (view?.MutationObserver) {
|
|
@@ -133,18 +536,78 @@ function createDock(opts) {
|
|
|
133
536
|
mq.addEventListener("change", applyTheme);
|
|
134
537
|
cleanups.push(() => mq.removeEventListener("change", applyTheme));
|
|
135
538
|
}
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
539
|
+
const escape = (e) => {
|
|
540
|
+
if (e.key !== "Escape") return;
|
|
541
|
+
closeMenu();
|
|
542
|
+
for (const d of drawers.values()) d.setOpen(false);
|
|
140
543
|
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
544
|
+
doc.addEventListener("keydown", escape);
|
|
545
|
+
cleanups.push(() => doc.removeEventListener("keydown", escape));
|
|
546
|
+
let menu = null;
|
|
547
|
+
const closeMenu = () => {
|
|
548
|
+
menu?.remove();
|
|
549
|
+
menu = null;
|
|
550
|
+
};
|
|
551
|
+
const outside = (e) => {
|
|
552
|
+
if (menu && !menu.contains(e.target)) closeMenu();
|
|
553
|
+
};
|
|
554
|
+
doc.addEventListener("pointerdown", outside, true);
|
|
555
|
+
cleanups.push(() => doc.removeEventListener("pointerdown", outside, true), closeMenu);
|
|
556
|
+
const moveItems = (id) => {
|
|
557
|
+
const now = where(id);
|
|
558
|
+
const items = [];
|
|
559
|
+
if (now?.kind !== "grid") items.push(["Dock", { kind: "grid" }]);
|
|
560
|
+
if (now?.kind !== "float") items.push(["Float", { kind: "float" }]);
|
|
561
|
+
for (const edge of EDGES) {
|
|
562
|
+
if (!(now?.kind === "drawer" && now.edge === edge)) items.push([`Move to ${edge} drawer`, { kind: "drawer", edge }]);
|
|
563
|
+
}
|
|
564
|
+
items.push(["Close", null]);
|
|
565
|
+
return items;
|
|
566
|
+
};
|
|
567
|
+
const moveMenu = (anchor, id) => {
|
|
568
|
+
closeMenu();
|
|
569
|
+
const list = doc.createElement("div");
|
|
570
|
+
list.className = "dock-menu";
|
|
571
|
+
list.setAttribute("role", "menu");
|
|
572
|
+
list.dataset.panel = id;
|
|
573
|
+
const r = anchor.getBoundingClientRect();
|
|
574
|
+
Object.assign(list.style, { position: "fixed", left: `${Math.round(r.left)}px`, top: `${Math.round(r.bottom + 2)}px`, zIndex: "calc(var(--dv-overlay-z-index, 999) + 300)" });
|
|
575
|
+
for (const [label, target] of moveItems(id)) {
|
|
576
|
+
const b = doc.createElement("button");
|
|
577
|
+
b.type = "button";
|
|
578
|
+
b.className = "dock-menu-item";
|
|
579
|
+
b.setAttribute("role", "menuitem");
|
|
580
|
+
b.textContent = label;
|
|
581
|
+
b.addEventListener("click", () => {
|
|
582
|
+
closeMenu();
|
|
583
|
+
if (target) place(id, target);
|
|
584
|
+
else toggle(id);
|
|
585
|
+
});
|
|
586
|
+
list.appendChild(b);
|
|
587
|
+
}
|
|
588
|
+
doc.body.appendChild(list);
|
|
589
|
+
menu = list;
|
|
147
590
|
};
|
|
591
|
+
function headerMenu() {
|
|
592
|
+
const btn = doc.createElement("button");
|
|
593
|
+
btn.type = "button";
|
|
594
|
+
btn.className = "dock-move-btn";
|
|
595
|
+
btn.textContent = "\u22EF";
|
|
596
|
+
btn.title = "Move this panel";
|
|
597
|
+
let group;
|
|
598
|
+
btn.addEventListener("click", (e) => {
|
|
599
|
+
e.stopPropagation();
|
|
600
|
+
const id = group?.activePanel?.id;
|
|
601
|
+
if (id && defOf(id)) moveMenu(btn, id);
|
|
602
|
+
});
|
|
603
|
+
return {
|
|
604
|
+
element: btn,
|
|
605
|
+
init: (params) => {
|
|
606
|
+
group = params.group;
|
|
607
|
+
},
|
|
608
|
+
dispose: () => btn.remove()
|
|
609
|
+
};
|
|
610
|
+
}
|
|
148
611
|
const panelsMenu = (host) => {
|
|
149
612
|
const btn = doc.createElement("button");
|
|
150
613
|
btn.type = "button";
|
|
@@ -155,9 +618,9 @@ function createDock(opts) {
|
|
|
155
618
|
const list = doc.createElement("div");
|
|
156
619
|
list.className = "dock-panels-list";
|
|
157
620
|
list.style.display = "none";
|
|
158
|
-
const setOpen = (
|
|
159
|
-
list.style.display =
|
|
160
|
-
btn.setAttribute("aria-expanded", String(
|
|
621
|
+
const setOpen = (show2) => {
|
|
622
|
+
list.style.display = show2 ? "block" : "none";
|
|
623
|
+
btn.setAttribute("aria-expanded", String(show2));
|
|
161
624
|
};
|
|
162
625
|
const item = (label, cls, act) => {
|
|
163
626
|
const b = doc.createElement("button");
|
|
@@ -171,30 +634,30 @@ function createDock(opts) {
|
|
|
171
634
|
return b;
|
|
172
635
|
};
|
|
173
636
|
const rebuild = () => {
|
|
637
|
+
const open = /* @__PURE__ */ new Set([...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids)]);
|
|
638
|
+
const listed = [...panels, ...[...open].filter((id) => !byId.has(id)).map((id) => defOf(id)).filter((d) => !!d)];
|
|
174
639
|
list.replaceChildren(
|
|
175
|
-
...
|
|
176
|
-
|
|
177
|
-
|
|
640
|
+
...listed.map((def) => {
|
|
641
|
+
const at = where(def.id);
|
|
642
|
+
return item(`${at ? "\u2713" : " "} ${def.title}${placementLabel(at)}`, "dock-panels-item", () => toggle(def.id));
|
|
643
|
+
}),
|
|
178
644
|
item(" Reset layout", "dock-panels-item dock-panels-reset", reset)
|
|
179
645
|
);
|
|
180
646
|
};
|
|
181
647
|
btn.addEventListener("click", () => {
|
|
182
|
-
const
|
|
183
|
-
if (
|
|
184
|
-
setOpen(
|
|
648
|
+
const show2 = list.style.display === "none";
|
|
649
|
+
if (show2) rebuild();
|
|
650
|
+
setOpen(show2);
|
|
185
651
|
});
|
|
186
|
-
const
|
|
187
|
-
if (!host.contains(e.target)) setOpen(false);
|
|
188
|
-
};
|
|
189
|
-
const escape = (e) => {
|
|
190
|
-
if (e.key === "Escape") setOpen(false);
|
|
652
|
+
const close = (e) => {
|
|
653
|
+
if (e instanceof KeyboardEvent ? e.key === "Escape" : !host.contains(e.target)) setOpen(false);
|
|
191
654
|
};
|
|
192
|
-
doc.addEventListener("click",
|
|
193
|
-
doc.addEventListener("keydown",
|
|
655
|
+
doc.addEventListener("click", close);
|
|
656
|
+
doc.addEventListener("keydown", close);
|
|
194
657
|
host.append(btn, list);
|
|
195
658
|
const remove = () => {
|
|
196
|
-
doc.removeEventListener("click",
|
|
197
|
-
doc.removeEventListener("keydown",
|
|
659
|
+
doc.removeEventListener("click", close);
|
|
660
|
+
doc.removeEventListener("keydown", close);
|
|
198
661
|
btn.remove();
|
|
199
662
|
list.remove();
|
|
200
663
|
};
|
|
@@ -203,56 +666,36 @@ function createDock(opts) {
|
|
|
203
666
|
};
|
|
204
667
|
return {
|
|
205
668
|
api,
|
|
206
|
-
show
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
api.getPanel(id)?.api.setActive();
|
|
210
|
-
return true;
|
|
211
|
-
},
|
|
669
|
+
show,
|
|
670
|
+
place,
|
|
671
|
+
where,
|
|
212
672
|
toggle,
|
|
673
|
+
toggleDrawer(edge, open) {
|
|
674
|
+
const d = drawers.get(edge);
|
|
675
|
+
if (!d) return false;
|
|
676
|
+
d.setOpen(open ?? !d.open);
|
|
677
|
+
return d.open;
|
|
678
|
+
},
|
|
213
679
|
reset,
|
|
214
680
|
applyTheme,
|
|
215
681
|
panelsMenu,
|
|
216
682
|
dispose() {
|
|
217
683
|
if (disposed) return;
|
|
684
|
+
save();
|
|
218
685
|
disposed = true;
|
|
219
686
|
for (const c of cleanups.splice(0)) c();
|
|
220
|
-
|
|
687
|
+
for (const d of drawers.values()) {
|
|
688
|
+
for (const id of d.ids) {
|
|
689
|
+
const el = d.remove(id);
|
|
690
|
+
if (el) release?.(id, el);
|
|
691
|
+
}
|
|
692
|
+
d.dispose();
|
|
693
|
+
}
|
|
221
694
|
component.dispose();
|
|
222
695
|
}
|
|
223
696
|
};
|
|
224
697
|
}
|
|
225
698
|
|
|
226
|
-
// src/drawer.ts
|
|
227
|
-
var DRAWER_MIN = 280;
|
|
228
|
-
var DRAWER_MAX_FRAC = 0.9;
|
|
229
|
-
var DRAWER_DEFAULT = 420;
|
|
230
|
-
function clampSize(px, viewport, bounds = {}) {
|
|
231
|
-
const min = bounds.min ?? DRAWER_MIN;
|
|
232
|
-
const max = Math.max(min, Math.floor(viewport * (bounds.maxFrac ?? DRAWER_MAX_FRAC)));
|
|
233
|
-
return Math.min(max, Math.max(min, Math.round(px)));
|
|
234
|
-
}
|
|
235
|
-
function drawerSize(edge, pointer, viewport, bounds) {
|
|
236
|
-
const fromEdge = edge === "left" || edge === "top" ? pointer : viewport - pointer;
|
|
237
|
-
return clampSize(fromEdge, viewport, bounds);
|
|
238
|
-
}
|
|
239
|
-
function loadDrawerSize(storage, key, fallback = DRAWER_DEFAULT) {
|
|
240
|
-
let raw = null;
|
|
241
|
-
try {
|
|
242
|
-
raw = storage?.getItem(key) ?? null;
|
|
243
|
-
} catch {
|
|
244
|
-
return fallback;
|
|
245
|
-
}
|
|
246
|
-
const n = raw == null ? NaN : parseInt(raw, 10);
|
|
247
|
-
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
248
|
-
}
|
|
249
|
-
function saveDrawerSize(storage, key, px) {
|
|
250
|
-
try {
|
|
251
|
-
storage?.setItem(key, String(Math.round(px)));
|
|
252
|
-
} catch {
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
699
|
export { DRAWER_DEFAULT, DRAWER_MAX_FRAC, DRAWER_MIN, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
|
|
257
700
|
//# sourceMappingURL=index.mjs.map
|
|
258
701
|
//# sourceMappingURL=index.mjs.map
|