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