@hamur/devtools 0.0.0-stage → 0.0.2
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/LICENSE +22 -0
- package/README.md +24 -2
- package/dist/src/client.js +780 -0
- package/dist/src/index.js +1711 -0
- package/dist/types/src/apps/locator.d.ts +3 -0
- package/dist/types/src/bridge.d.ts +8 -0
- package/dist/types/src/client.d.ts +3 -0
- package/dist/types/src/dev/analyze.d.ts +31 -0
- package/dist/types/src/dev/assets.d.ts +33 -0
- package/dist/types/src/dev/authz.d.ts +49 -0
- package/dist/types/src/dev/bundle.d.ts +22 -0
- package/dist/types/src/dev/config.d.ts +24 -0
- package/dist/types/src/dev/handler.d.ts +20 -0
- package/dist/types/src/dev/modules.d.ts +28 -0
- package/dist/types/src/dev/overview.d.ts +20 -0
- package/dist/types/src/dev/processes.d.ts +4 -0
- package/dist/types/src/dev/server-routes.d.ts +7 -0
- package/dist/types/src/dev/tasks.d.ts +26 -0
- package/dist/types/src/dev/timeline.d.ts +20 -0
- package/dist/types/src/dev/virtual.d.ts +9 -0
- package/dist/types/src/icons.d.ts +24 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/locator.d.ts +17 -0
- package/dist/types/src/mask.d.ts +8 -0
- package/dist/types/src/module.d.ts +27 -0
- package/dist/types/src/open-in-editor.d.ts +4 -0
- package/dist/types/src/options.d.ts +14 -0
- package/dist/types/src/panel/document.d.ts +16 -0
- package/dist/types/src/panel/groups.d.ts +14 -0
- package/dist/types/src/panel/index.d.ts +1 -0
- package/dist/types/src/panel/settings.d.ts +69 -0
- package/dist/types/src/panel/style.d.ts +1 -0
- package/dist/types/src/panel/ui.d.ts +34 -0
- package/dist/types/src/server/authz.d.ts +30 -0
- package/dist/types/src/server/index.d.ts +3 -0
- package/dist/types/src/server/routes.d.ts +6 -0
- package/dist/types/src/server/storage.d.ts +56 -0
- package/dist/types/src/source-link.d.ts +2 -0
- package/dist/types/src/tabs/analyze-model.d.ts +31 -0
- package/dist/types/src/tabs/analyze.d.ts +2 -0
- package/dist/types/src/tabs/assets.d.ts +11 -0
- package/dist/types/src/tabs/audit-model.d.ts +42 -0
- package/dist/types/src/tabs/audit.d.ts +2 -0
- package/dist/types/src/tabs/authz-model.d.ts +11 -0
- package/dist/types/src/tabs/authz.d.ts +2 -0
- package/dist/types/src/tabs/config.d.ts +8 -0
- package/dist/types/src/tabs/inspect.d.ts +7 -0
- package/dist/types/src/tabs/modules.d.ts +4 -0
- package/dist/types/src/tabs/overview.d.ts +2 -0
- package/dist/types/src/tabs/pages.d.ts +2 -0
- package/dist/types/src/tabs/payload.d.ts +6 -0
- package/dist/types/src/tabs/plugins-model.d.ts +15 -0
- package/dist/types/src/tabs/plugins.d.ts +2 -0
- package/dist/types/src/tabs/seo-model.d.ts +36 -0
- package/dist/types/src/tabs/seo.d.ts +2 -0
- package/dist/types/src/tabs/server-routes.d.ts +2 -0
- package/dist/types/src/tabs/storage.d.ts +13 -0
- package/dist/types/src/tabs/tasks-model.d.ts +16 -0
- package/dist/types/src/tabs/tasks.d.ts +2 -0
- package/dist/types/src/tabs/terminals-model.d.ts +53 -0
- package/dist/types/src/tabs/terminals.d.ts +2 -0
- package/dist/types/src/tabs/timeline-model.d.ts +58 -0
- package/dist/types/src/tabs/timeline.d.ts +2 -0
- package/dist/types/src/tester.d.ts +43 -0
- package/dist/types/src/toolbar/element.d.ts +28 -0
- package/dist/types/src/toolbar/shortcut.d.ts +14 -0
- package/dist/types/src/toolbar/style.d.ts +3 -0
- package/dist/types/src/view-context.d.ts +13 -0
- package/package.json +34 -4
- package/src/apps/locator.ts +131 -0
- package/src/bridge.ts +172 -0
- package/src/client.ts +10 -0
- package/src/dev/analyze.ts +128 -0
- package/src/dev/assets.ts +208 -0
- package/src/dev/authz.ts +162 -0
- package/src/dev/bundle.ts +91 -0
- package/src/dev/config.ts +132 -0
- package/src/dev/handler.ts +161 -0
- package/src/dev/modules.ts +60 -0
- package/src/dev/overview.ts +57 -0
- package/src/dev/processes.ts +34 -0
- package/src/dev/server-routes.ts +14 -0
- package/src/dev/tasks.ts +93 -0
- package/src/dev/timeline.ts +43 -0
- package/src/dev/virtual.ts +40 -0
- package/src/env.d.ts +21 -0
- package/src/icons.ts +48 -0
- package/src/index.ts +12 -0
- package/src/locator.ts +31 -0
- package/src/mask.ts +16 -0
- package/src/module.ts +352 -0
- package/src/open-in-editor.ts +18 -0
- package/src/options.ts +36 -0
- package/src/panel/document.ts +60 -0
- package/src/panel/groups.ts +41 -0
- package/src/panel/index.ts +429 -0
- package/src/panel/settings.ts +224 -0
- package/src/panel/style.ts +321 -0
- package/src/panel/ui.ts +52 -0
- package/src/server/authz.ts +110 -0
- package/src/server/index.ts +25 -0
- package/src/server/routes.ts +51 -0
- package/src/server/storage.ts +251 -0
- package/src/source-link.ts +29 -0
- package/src/tabs/analyze-model.ts +97 -0
- package/src/tabs/analyze.ts +217 -0
- package/src/tabs/assets.ts +375 -0
- package/src/tabs/audit-model.ts +216 -0
- package/src/tabs/audit.ts +223 -0
- package/src/tabs/authz-model.ts +28 -0
- package/src/tabs/authz.ts +337 -0
- package/src/tabs/config.ts +250 -0
- package/src/tabs/inspect.ts +101 -0
- package/src/tabs/modules.ts +58 -0
- package/src/tabs/overview.ts +104 -0
- package/src/tabs/pages.ts +188 -0
- package/src/tabs/payload.ts +224 -0
- package/src/tabs/plugins-model.ts +44 -0
- package/src/tabs/plugins.ts +95 -0
- package/src/tabs/seo-model.ts +140 -0
- package/src/tabs/seo.ts +162 -0
- package/src/tabs/server-routes.ts +469 -0
- package/src/tabs/storage.ts +313 -0
- package/src/tabs/tasks-model.ts +59 -0
- package/src/tabs/tasks.ts +287 -0
- package/src/tabs/terminals-model.ts +127 -0
- package/src/tabs/terminals.ts +294 -0
- package/src/tabs/timeline-model.ts +200 -0
- package/src/tabs/timeline.ts +260 -0
- package/src/tester.ts +158 -0
- package/src/toolbar/element.ts +428 -0
- package/src/toolbar/shortcut.ts +56 -0
- package/src/toolbar/style.ts +94 -0
- package/src/view-context.ts +93 -0
|
@@ -0,0 +1,780 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/devtools/src/client.ts
|
|
3
|
+
import { definePlugin } from "hamur/app";
|
|
4
|
+
import { apps } from "#hamur/devtools/apps";
|
|
5
|
+
import { baseURL, placement, shortcut } from "#hamur/devtools/config";
|
|
6
|
+
|
|
7
|
+
// packages/devtools/src/toolbar/element.ts
|
|
8
|
+
import { DEVTOOLS_HOOK } from "hamur/adapter/dev";
|
|
9
|
+
|
|
10
|
+
// packages/devtools/src/bridge.ts
|
|
11
|
+
var BRIDGE_CHANNEL = "hamur:devtools";
|
|
12
|
+
var EVENTS = ["mark", "hydration", "badge"];
|
|
13
|
+
var CALLS = ["navigate", "setState", "refresh"];
|
|
14
|
+
var HELLO_MS = 1000;
|
|
15
|
+
var ATTACHED_MS = 3000;
|
|
16
|
+
var message = (error) => error instanceof Error ? error.message : String(error);
|
|
17
|
+
function snapshotOf(page) {
|
|
18
|
+
return {
|
|
19
|
+
route: page.router.current(),
|
|
20
|
+
pending: page.router.pending(),
|
|
21
|
+
payload: page.payload(),
|
|
22
|
+
head: page.head(),
|
|
23
|
+
marks: page.marks().map((entry) => entry.toJSON()),
|
|
24
|
+
hydration: page.hydration(),
|
|
25
|
+
badge: page.badge()
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function serveBridge(page) {
|
|
29
|
+
if (typeof BroadcastChannel === "undefined")
|
|
30
|
+
return;
|
|
31
|
+
const channel = new BroadcastChannel(BRIDGE_CHANNEL);
|
|
32
|
+
let lastHello = 0;
|
|
33
|
+
const attached = () => Date.now() - lastHello < ATTACHED_MS;
|
|
34
|
+
const send = (data) => {
|
|
35
|
+
try {
|
|
36
|
+
channel.postMessage(data);
|
|
37
|
+
return true;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const push = () => {
|
|
43
|
+
if (!attached())
|
|
44
|
+
return;
|
|
45
|
+
const snapshot = snapshotOf(page);
|
|
46
|
+
if (!send({ type: "snapshot", snapshot }))
|
|
47
|
+
send({
|
|
48
|
+
type: "snapshot",
|
|
49
|
+
snapshot: { ...snapshot, payload: { ...snapshot.payload, data: {}, state: {} }, marks: [] }
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
setInterval(push, HELLO_MS);
|
|
53
|
+
for (const event of EVENTS) {
|
|
54
|
+
page.on(event, (data) => {
|
|
55
|
+
if (attached())
|
|
56
|
+
send({
|
|
57
|
+
type: "event",
|
|
58
|
+
event,
|
|
59
|
+
data: event === "mark" && data instanceof PerformanceEntry ? data.toJSON() : data
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
channel.onmessage = (event) => {
|
|
64
|
+
const data = event.data;
|
|
65
|
+
if (data?.type === "hello") {
|
|
66
|
+
lastHello = Date.now();
|
|
67
|
+
push();
|
|
68
|
+
} else if (data?.type === "call" && CALLS.includes(data.method)) {
|
|
69
|
+
const { id, method, args } = data;
|
|
70
|
+
lastHello = Date.now();
|
|
71
|
+
const run = async () => {
|
|
72
|
+
if (method === "setState")
|
|
73
|
+
page.setState(String(args[0]), args[1]);
|
|
74
|
+
else if (method === "navigate")
|
|
75
|
+
await page.router.navigate(String(args[0]));
|
|
76
|
+
else
|
|
77
|
+
await page.refresh(String(args[0]));
|
|
78
|
+
};
|
|
79
|
+
run().then(() => void send({ type: "answer", id }), (error) => void send({ type: "answer", id, error: message(error) }));
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// packages/devtools/src/panel/settings.ts
|
|
85
|
+
var SETTINGS_KEY = "hamur:devtools";
|
|
86
|
+
var PLACEMENTS = [
|
|
87
|
+
"bottom-center",
|
|
88
|
+
"bottom-left",
|
|
89
|
+
"bottom-right",
|
|
90
|
+
"top-center",
|
|
91
|
+
"top-left",
|
|
92
|
+
"top-right"
|
|
93
|
+
];
|
|
94
|
+
var THEMES = ["system", "light", "dark"];
|
|
95
|
+
var MIN_SCALE = 0.75;
|
|
96
|
+
var MAX_SCALE = 1.5;
|
|
97
|
+
var MIN_PANEL_WIDTH = 240;
|
|
98
|
+
var MIN_PANEL_HEIGHT = 120;
|
|
99
|
+
var isDocked = (frame) => frame === undefined || ("dock" in frame);
|
|
100
|
+
var DEFAULT_SETTINGS = { hidden: [], order: [], theme: "system", scale: 1, height: 360 };
|
|
101
|
+
var finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
102
|
+
function parseFrame(value) {
|
|
103
|
+
if (typeof value !== "object" || value === null)
|
|
104
|
+
return;
|
|
105
|
+
const given = value;
|
|
106
|
+
if (given.dock === "bottom")
|
|
107
|
+
return { dock: "bottom" };
|
|
108
|
+
if (finite(given.x) && finite(given.y) && finite(given.width) && finite(given.height))
|
|
109
|
+
return { x: given.x, y: given.y, width: given.width, height: given.height };
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
function parsePill(value) {
|
|
113
|
+
if (typeof value !== "object" || value === null)
|
|
114
|
+
return;
|
|
115
|
+
const { x, y } = value;
|
|
116
|
+
return finite(x) && finite(y) && x >= 0 && x <= 1 && y >= 0 && y <= 1 ? { x, y } : undefined;
|
|
117
|
+
}
|
|
118
|
+
var names = (value) => Array.isArray(value) && value.every((item) => typeof item === "string") ? value : undefined;
|
|
119
|
+
function parseSettings(text) {
|
|
120
|
+
let value;
|
|
121
|
+
try {
|
|
122
|
+
value = text === null ? undefined : JSON.parse(text);
|
|
123
|
+
} catch {
|
|
124
|
+
value = undefined;
|
|
125
|
+
}
|
|
126
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
127
|
+
return { ...DEFAULT_SETTINGS, hidden: [], order: [] };
|
|
128
|
+
const given = value;
|
|
129
|
+
const settings = {
|
|
130
|
+
hidden: names(given.hidden) ?? [],
|
|
131
|
+
order: names(given.order) ?? [],
|
|
132
|
+
theme: THEMES.includes(given.theme) ? given.theme : DEFAULT_SETTINGS.theme,
|
|
133
|
+
scale: typeof given.scale === "number" && given.scale >= MIN_SCALE && given.scale <= MAX_SCALE ? given.scale : DEFAULT_SETTINGS.scale,
|
|
134
|
+
height: typeof given.height === "number" && Number.isFinite(given.height) && given.height > 0 ? given.height : DEFAULT_SETTINGS.height
|
|
135
|
+
};
|
|
136
|
+
if (PLACEMENTS.includes(given.placement))
|
|
137
|
+
settings.placement = given.placement;
|
|
138
|
+
const panel = parseFrame(given.panel);
|
|
139
|
+
if (panel)
|
|
140
|
+
settings.panel = panel;
|
|
141
|
+
const pill = parsePill(given.pill);
|
|
142
|
+
if (pill)
|
|
143
|
+
settings.pill = pill;
|
|
144
|
+
return settings;
|
|
145
|
+
}
|
|
146
|
+
function readSettings() {
|
|
147
|
+
try {
|
|
148
|
+
return parseSettings(globalThis.localStorage.getItem(SETTINGS_KEY));
|
|
149
|
+
} catch {
|
|
150
|
+
return parseSettings(null);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function writeSettings(settings) {
|
|
154
|
+
try {
|
|
155
|
+
globalThis.localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
var between = (value, min, max) => Math.round(Math.max(min, Math.min(value, max)));
|
|
159
|
+
function clampRect(rect, viewport) {
|
|
160
|
+
const width = between(rect.width, Math.min(MIN_PANEL_WIDTH, viewport.width), viewport.width);
|
|
161
|
+
const height = between(rect.height, Math.min(MIN_PANEL_HEIGHT, viewport.height), viewport.height);
|
|
162
|
+
return {
|
|
163
|
+
x: between(rect.x, 0, viewport.width - width),
|
|
164
|
+
y: between(rect.y, 0, viewport.height - height),
|
|
165
|
+
width,
|
|
166
|
+
height
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function resizeRect(start, edge, dx, dy, viewport) {
|
|
170
|
+
const right = start.x + start.width;
|
|
171
|
+
const bottom = start.y + start.height;
|
|
172
|
+
let left = start.x;
|
|
173
|
+
let top = start.y;
|
|
174
|
+
let width = start.width;
|
|
175
|
+
let height = start.height;
|
|
176
|
+
if (edge.includes("w")) {
|
|
177
|
+
left = between(start.x + dx, 0, right - Math.min(MIN_PANEL_WIDTH, right));
|
|
178
|
+
width = right - left;
|
|
179
|
+
} else if (edge.includes("e"))
|
|
180
|
+
width = between(start.width + dx, Math.min(MIN_PANEL_WIDTH, viewport.width - start.x), viewport.width - start.x);
|
|
181
|
+
if (edge.includes("n")) {
|
|
182
|
+
top = between(start.y + dy, 0, bottom - Math.min(MIN_PANEL_HEIGHT, bottom));
|
|
183
|
+
height = bottom - top;
|
|
184
|
+
} else if (edge.includes("s"))
|
|
185
|
+
height = between(start.height + dy, Math.min(MIN_PANEL_HEIGHT, viewport.height - start.y), viewport.height - start.y);
|
|
186
|
+
return { x: left, y: top, width, height };
|
|
187
|
+
}
|
|
188
|
+
function pillPosition(place, size, viewport) {
|
|
189
|
+
return {
|
|
190
|
+
left: between(place.x * viewport.width, 0, Math.max(0, viewport.width - size.width)),
|
|
191
|
+
top: between(place.y * viewport.height, 0, Math.max(0, viewport.height - size.height))
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
var VIEW_KEY = "hamur:devtools:view";
|
|
195
|
+
function parseView(text) {
|
|
196
|
+
let value;
|
|
197
|
+
try {
|
|
198
|
+
value = text === null ? undefined : JSON.parse(text);
|
|
199
|
+
} catch {
|
|
200
|
+
value = undefined;
|
|
201
|
+
}
|
|
202
|
+
if (typeof value !== "object" || value === null)
|
|
203
|
+
return { open: false };
|
|
204
|
+
const given = value;
|
|
205
|
+
const view = { open: given.open === true };
|
|
206
|
+
if (typeof given.tab === "string" && /^[a-z][a-z0-9-]*$/.test(given.tab))
|
|
207
|
+
view.tab = given.tab;
|
|
208
|
+
return view;
|
|
209
|
+
}
|
|
210
|
+
function readView() {
|
|
211
|
+
try {
|
|
212
|
+
return parseView(globalThis.sessionStorage.getItem(VIEW_KEY));
|
|
213
|
+
} catch {
|
|
214
|
+
return { open: false };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function writeView(view) {
|
|
218
|
+
try {
|
|
219
|
+
globalThis.sessionStorage.setItem(VIEW_KEY, JSON.stringify(view));
|
|
220
|
+
} catch {}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// packages/devtools/src/view-context.ts
|
|
224
|
+
var listeners = new Map;
|
|
225
|
+
var socket;
|
|
226
|
+
function connect() {
|
|
227
|
+
socket = new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/_hamur/reload?devtools=1`);
|
|
228
|
+
socket.onmessage = (event) => {
|
|
229
|
+
let message;
|
|
230
|
+
try {
|
|
231
|
+
message = JSON.parse(String(event.data));
|
|
232
|
+
} catch {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (message?.type !== "custom" || typeof message.event !== "string")
|
|
236
|
+
return;
|
|
237
|
+
for (const listener of listeners.get(message.event) ?? [])
|
|
238
|
+
listener(message.data);
|
|
239
|
+
};
|
|
240
|
+
socket.onclose = () => setTimeout(connect, 1000);
|
|
241
|
+
}
|
|
242
|
+
function onDevtoolsEvent(event, fn) {
|
|
243
|
+
const set = listeners.get(event) ?? new Set;
|
|
244
|
+
listeners.set(event, set);
|
|
245
|
+
set.add(fn);
|
|
246
|
+
if (!socket)
|
|
247
|
+
connect();
|
|
248
|
+
return () => void set.delete(fn);
|
|
249
|
+
}
|
|
250
|
+
async function callDevtoolsRpc(baseURL, name, args) {
|
|
251
|
+
const response = await fetch(`${baseURL}_hamur/devtools/api/rpc/${encodeURIComponent(name)}`, {
|
|
252
|
+
method: "POST",
|
|
253
|
+
headers: { "content-type": "application/json" },
|
|
254
|
+
...args === undefined ? {} : { body: JSON.stringify(args) }
|
|
255
|
+
});
|
|
256
|
+
const answer = await response.json().catch(() => ({}));
|
|
257
|
+
if (!response.ok)
|
|
258
|
+
throw new Error(answer.error ?? `devtools RPC ${name} answered ${response.status}`);
|
|
259
|
+
return answer.result;
|
|
260
|
+
}
|
|
261
|
+
function viewContext(baseURL, page, setBadge) {
|
|
262
|
+
return { rpc: (name, args) => callDevtoolsRpc(baseURL, name, args), on: onDevtoolsEvent, page, setBadge };
|
|
263
|
+
}
|
|
264
|
+
async function startView(load, canvas, ctx) {
|
|
265
|
+
const offs = [];
|
|
266
|
+
let stopped = false;
|
|
267
|
+
const stopListeners = () => {
|
|
268
|
+
stopped = true;
|
|
269
|
+
for (const off of offs.splice(0))
|
|
270
|
+
off();
|
|
271
|
+
};
|
|
272
|
+
const tracked = {
|
|
273
|
+
...ctx,
|
|
274
|
+
on(event, fn) {
|
|
275
|
+
if (stopped)
|
|
276
|
+
return () => {};
|
|
277
|
+
const off = ctx.on(event, fn);
|
|
278
|
+
offs.push(off);
|
|
279
|
+
return off;
|
|
280
|
+
},
|
|
281
|
+
setBadge(text) {
|
|
282
|
+
if (!stopped)
|
|
283
|
+
ctx.setBadge(text);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
let teardown;
|
|
287
|
+
try {
|
|
288
|
+
const { default: view } = await load();
|
|
289
|
+
teardown = await view.init(canvas, tracked);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
stopListeners();
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
return () => {
|
|
295
|
+
if (stopped)
|
|
296
|
+
return;
|
|
297
|
+
stopListeners();
|
|
298
|
+
if (typeof teardown === "function")
|
|
299
|
+
teardown();
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// packages/devtools/src/toolbar/shortcut.ts
|
|
304
|
+
var MODIFIERS = ["ctrl", "alt", "shift", "meta"];
|
|
305
|
+
function parseShortcut(text) {
|
|
306
|
+
const parts = text.toLowerCase().split("+").map((part) => part.trim());
|
|
307
|
+
const key = parts.pop() ?? "";
|
|
308
|
+
const shortcut = { ctrl: false, alt: false, shift: false, meta: false, code: "" };
|
|
309
|
+
for (const part of parts) {
|
|
310
|
+
if (!MODIFIERS.includes(part) || shortcut[part])
|
|
311
|
+
return;
|
|
312
|
+
shortcut[part] = true;
|
|
313
|
+
}
|
|
314
|
+
if (/^[a-z]$/.test(key))
|
|
315
|
+
shortcut.code = `Key${key.toUpperCase()}`;
|
|
316
|
+
else if (/^[0-9]$/.test(key))
|
|
317
|
+
shortcut.code = `Digit${key}`;
|
|
318
|
+
else if (/^f(?:[1-9]|1[0-2])$/.test(key))
|
|
319
|
+
shortcut.code = key.toUpperCase();
|
|
320
|
+
else
|
|
321
|
+
return;
|
|
322
|
+
return shortcut;
|
|
323
|
+
}
|
|
324
|
+
function matchesShortcut(event, shortcut) {
|
|
325
|
+
return event.code === shortcut.code && event.ctrlKey === shortcut.ctrl && event.altKey === shortcut.alt && event.shiftKey === shortcut.shift && event.metaKey === shortcut.meta;
|
|
326
|
+
}
|
|
327
|
+
function isTyping(event) {
|
|
328
|
+
const target = event.composedPath()[0];
|
|
329
|
+
if (!(target instanceof HTMLElement))
|
|
330
|
+
return false;
|
|
331
|
+
return target.isContentEditable || target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// packages/devtools/src/toolbar/style.ts
|
|
335
|
+
var HAMUR_ICON = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 16.5c0-5.5 3.6-9.5 8-9.5s8 4 8 9.5z" fill="currentColor" fill-opacity="0.16" stroke="none"></path><path d="M4 16.5c0-5.5 3.6-9.5 8-9.5s8 4 8 9.5M3 16.5h18M9.5 12v1M14.5 12v1"></path></svg>';
|
|
336
|
+
var TOOLBAR_CSS = String.raw`
|
|
337
|
+
:host {
|
|
338
|
+
all: initial;
|
|
339
|
+
position: fixed;
|
|
340
|
+
z-index: 2147483646;
|
|
341
|
+
--app: #f5f4f2; --raised: #ffffff; --hover: #f6f5f3; --active: #f0efee; --line: #e8e6e5;
|
|
342
|
+
--fg: #222120; --fg-2: #5b5956; --accent: #db5e29; --red: #cf4445;
|
|
343
|
+
--hairline: rgba(255,248,240,0.08);
|
|
344
|
+
color-scheme: light;
|
|
345
|
+
}
|
|
346
|
+
@media (prefers-color-scheme: dark) {
|
|
347
|
+
:host {
|
|
348
|
+
--app: #121110; --raised: #1f1e1d; --hover: #21201f; --active: #252423; --line: #272725;
|
|
349
|
+
--fg: #f8f6f4; --fg-2: #a3a09d; --accent: #ff8a4a; --red: #eb635e;
|
|
350
|
+
color-scheme: dark;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
*, ::before, ::after { box-sizing: border-box; }
|
|
354
|
+
svg { overflow: visible; flex: none; }
|
|
355
|
+
[hidden] { display: none !important; }
|
|
356
|
+
button { all: unset; box-sizing: border-box; }
|
|
357
|
+
.fixed { position: fixed; }
|
|
358
|
+
.absolute { position: absolute; }
|
|
359
|
+
.inset-0 { inset: 0; }
|
|
360
|
+
.left-0 { left: 0; }
|
|
361
|
+
.right-0 { right: 0; }
|
|
362
|
+
.bottom-0 { bottom: 0; }
|
|
363
|
+
.-top-1 { top: -4px; }
|
|
364
|
+
.top-0 { top: 0; }
|
|
365
|
+
.-bottom-1 { bottom: -4px; }
|
|
366
|
+
.-left-1 { left: -4px; }
|
|
367
|
+
.-right-1 { right: -4px; }
|
|
368
|
+
.top-3 { top: 12px; }
|
|
369
|
+
.bottom-3 { bottom: 12px; }
|
|
370
|
+
.left-3 { left: 12px; }
|
|
371
|
+
.right-3 { right: 12px; }
|
|
372
|
+
.left-1\/2 { left: 50%; }
|
|
373
|
+
.-translate-x-1\/2 { transform: translateX(-50%); }
|
|
374
|
+
.bottom-\[calc\(var\(--panel-height\)\+12px\)\] { bottom: calc(var(--panel-height) + 12px); }
|
|
375
|
+
.flex { display: flex; }
|
|
376
|
+
.inline-flex { display: inline-flex; }
|
|
377
|
+
.block { display: block; }
|
|
378
|
+
.items-center { align-items: center; }
|
|
379
|
+
.justify-center { justify-content: center; }
|
|
380
|
+
.gap-0\.5 { gap: 2px; }
|
|
381
|
+
.gap-1 { gap: 4px; }
|
|
382
|
+
.h-2 { height: 8px; }
|
|
383
|
+
.w-2 { width: 8px; }
|
|
384
|
+
.size-3 { width: 12px; height: 12px; }
|
|
385
|
+
.h-4 { height: 16px; }
|
|
386
|
+
.h-8 { height: 32px; }
|
|
387
|
+
.h-10 { height: 40px; }
|
|
388
|
+
.h-full { height: 100%; }
|
|
389
|
+
.h-\[var\(--panel-height\)\] { height: var(--panel-height); }
|
|
390
|
+
.w-full { width: 100%; }
|
|
391
|
+
.min-w-8 { min-width: 32px; }
|
|
392
|
+
.px-1 { padding-left: 4px; padding-right: 4px; }
|
|
393
|
+
.px-1\.5 { padding-left: 6px; padding-right: 6px; }
|
|
394
|
+
.rounded { border-radius: 4px; }
|
|
395
|
+
.rounded-lg { border-radius: 8px; }
|
|
396
|
+
.rounded-xl { border-radius: 12px; }
|
|
397
|
+
.border-0 { border: 0; }
|
|
398
|
+
.bg-app { background: var(--app); }
|
|
399
|
+
.bg-raised { background: var(--raised); }
|
|
400
|
+
.bg-transparent { background: transparent; }
|
|
401
|
+
.bg-accent\/10 { background: color-mix(in srgb, var(--accent) 10%, transparent); }
|
|
402
|
+
.text-fg { color: var(--fg); }
|
|
403
|
+
.text-fg-2 { color: var(--fg-2); }
|
|
404
|
+
.text-accent { color: var(--accent); }
|
|
405
|
+
.font-sans { font-family: Inter, system-ui, sans-serif; }
|
|
406
|
+
.text-sm { font-size: 13px; line-height: 1; }
|
|
407
|
+
.text-2xs { font-size: 11px; line-height: 1; }
|
|
408
|
+
.font-medium { font-weight: 500; }
|
|
409
|
+
.whitespace-nowrap { white-space: nowrap; }
|
|
410
|
+
.cursor-pointer { cursor: pointer; }
|
|
411
|
+
.cursor-ns-resize { cursor: ns-resize; }
|
|
412
|
+
.cursor-ew-resize { cursor: ew-resize; }
|
|
413
|
+
.cursor-nwse-resize { cursor: nwse-resize; }
|
|
414
|
+
.cursor-nesw-resize { cursor: nesw-resize; }
|
|
415
|
+
.touch-none { touch-action: none; }
|
|
416
|
+
.pointer-events-none { pointer-events: none; }
|
|
417
|
+
.shadow-pop { box-shadow: 0 0 0 1px var(--hairline), inset 0 1px 0 rgba(255,255,255,0.04), 0 16px 40px -12px rgba(0,0,0,0.55); }
|
|
418
|
+
.shadow-panel { box-shadow: 0 -1px 0 var(--line), 0 -12px 32px -16px rgba(0,0,0,0.5); }
|
|
419
|
+
.hover\:bg-hover:hover { background: var(--hover); }
|
|
420
|
+
.hover\:text-fg:hover { color: var(--fg); }
|
|
421
|
+
.aria-expanded\:bg-active[aria-expanded="true"], .aria-pressed\:bg-active[aria-pressed="true"] { background: var(--active); }
|
|
422
|
+
.aria-expanded\:text-accent[aria-expanded="true"], .aria-pressed\:text-accent[aria-pressed="true"] { color: var(--accent); }
|
|
423
|
+
.data-\[failed\]\:text-red[data-failed] { color: var(--red); }
|
|
424
|
+
.focus-visible\:outline-accent:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
|
|
425
|
+
`;
|
|
426
|
+
|
|
427
|
+
// packages/devtools/src/toolbar/element.ts
|
|
428
|
+
var TOOLBAR_TAG = "hamur-devtools";
|
|
429
|
+
var MIN_HEIGHT = 120;
|
|
430
|
+
var DRAG_THRESHOLD = 4;
|
|
431
|
+
var GRIPS = [
|
|
432
|
+
["n", "-top-1 left-0 right-0 h-2 cursor-ns-resize"],
|
|
433
|
+
["s", "-bottom-1 left-0 right-0 h-2 cursor-ns-resize"],
|
|
434
|
+
["w", "-left-1 top-0 bottom-0 w-2 cursor-ew-resize"],
|
|
435
|
+
["e", "-right-1 top-0 bottom-0 w-2 cursor-ew-resize"],
|
|
436
|
+
["nw", "-top-1 -left-1 size-3 cursor-nwse-resize"],
|
|
437
|
+
["ne", "-top-1 -right-1 size-3 cursor-nesw-resize"],
|
|
438
|
+
["sw", "-bottom-1 -left-1 size-3 cursor-nesw-resize"],
|
|
439
|
+
["se", "-bottom-1 -right-1 size-3 cursor-nwse-resize"]
|
|
440
|
+
];
|
|
441
|
+
var viewport = () => ({ width: window.innerWidth, height: window.innerHeight });
|
|
442
|
+
var clampHeight = (height) => Math.round(Math.max(MIN_HEIGHT, Math.min(height, window.innerHeight - 48)));
|
|
443
|
+
var PILL = "fixed flex items-center gap-0.5 h-10 px-1 rounded-xl bg-raised shadow-pop text-fg text-sm font-sans whitespace-nowrap";
|
|
444
|
+
var BUTTON = "inline-flex items-center justify-center gap-1 min-w-8 h-8 px-1.5 rounded-lg bg-transparent cursor-pointer hover:bg-hover hover:text-fg focus-visible:outline-accent";
|
|
445
|
+
function placementClasses(placement, open) {
|
|
446
|
+
const [edge, side] = placement.split("-");
|
|
447
|
+
const vertical = edge === "top" ? "top-3" : open ? "bottom-[calc(var(--panel-height)+12px)]" : "bottom-3";
|
|
448
|
+
return `${vertical} ${side === "left" ? "left-3" : side === "right" ? "right-3" : "left-1/2 -translate-x-1/2"}`;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
class DevtoolsToolbar extends HTMLElement {
|
|
452
|
+
static config;
|
|
453
|
+
#root;
|
|
454
|
+
#pill;
|
|
455
|
+
#button;
|
|
456
|
+
#panel;
|
|
457
|
+
#frame;
|
|
458
|
+
#live;
|
|
459
|
+
constructor() {
|
|
460
|
+
super();
|
|
461
|
+
const shadow = this.attachShadow({ mode: "open" });
|
|
462
|
+
shadow.innerHTML = `<style>${TOOLBAR_CSS}</style><div data-root><div data-panel class="fixed bg-app shadow-panel" hidden></div><div data-pill class="${PILL}"><button type="button" data-hamur class="${BUTTON} text-fg aria-expanded:bg-active aria-expanded:text-accent" title="Hamur devtools" aria-label="Hamur devtools" aria-expanded="false">${HAMUR_ICON}</button><span data-badge class="inline-flex items-center h-8 px-1.5 text-fg-2 text-sm whitespace-nowrap" hidden></span></div></div>`;
|
|
463
|
+
this.#root = shadow.querySelector("[data-root]");
|
|
464
|
+
this.#pill = shadow.querySelector("[data-pill]");
|
|
465
|
+
this.#button = shadow.querySelector("[data-hamur]");
|
|
466
|
+
this.#panel = shadow.querySelector("[data-panel]");
|
|
467
|
+
this.#button.addEventListener("click", () => this.toggle());
|
|
468
|
+
this.#showBadge(shadow.querySelector("[data-badge]"));
|
|
469
|
+
for (const [edge, classes] of GRIPS) {
|
|
470
|
+
const grip = document.createElement("div");
|
|
471
|
+
grip.className = `absolute touch-none ${classes}`;
|
|
472
|
+
grip.dataset.grip = edge;
|
|
473
|
+
this.#panel.append(grip);
|
|
474
|
+
this.#resizable(grip, edge);
|
|
475
|
+
}
|
|
476
|
+
this.#draggable(this.#pill);
|
|
477
|
+
for (const app of DevtoolsToolbar.config.apps)
|
|
478
|
+
this.#addApp(app);
|
|
479
|
+
this.apply();
|
|
480
|
+
}
|
|
481
|
+
#showBadge(badge) {
|
|
482
|
+
const page = window[DEVTOOLS_HOOK];
|
|
483
|
+
const show = (text) => {
|
|
484
|
+
badge.textContent = text;
|
|
485
|
+
badge.hidden = text === "";
|
|
486
|
+
};
|
|
487
|
+
show(page?.badge() ?? "");
|
|
488
|
+
page?.on("badge", (text) => show(String(text)));
|
|
489
|
+
}
|
|
490
|
+
#addApp(app) {
|
|
491
|
+
const button = document.createElement("button");
|
|
492
|
+
button.type = "button";
|
|
493
|
+
button.className = `${BUTTON} text-fg-2 aria-pressed:bg-active aria-pressed:text-accent data-[failed]:text-red`;
|
|
494
|
+
button.title = app.title;
|
|
495
|
+
button.ariaLabel = app.title;
|
|
496
|
+
button.ariaPressed = "false";
|
|
497
|
+
button.dataset.app = app.name;
|
|
498
|
+
const icon = document.createElement("span");
|
|
499
|
+
icon.className = "inline-flex";
|
|
500
|
+
if (app.icon.trimStart().startsWith("<svg"))
|
|
501
|
+
icon.innerHTML = app.icon;
|
|
502
|
+
else
|
|
503
|
+
icon.textContent = app.icon;
|
|
504
|
+
const badge = document.createElement("span");
|
|
505
|
+
badge.className = "inline-flex items-center h-4 px-1.5 rounded bg-accent/10 text-accent text-2xs font-medium";
|
|
506
|
+
badge.dataset.badge = "";
|
|
507
|
+
badge.hidden = true;
|
|
508
|
+
button.append(icon, badge);
|
|
509
|
+
this.#pill.append(button);
|
|
510
|
+
const canvas = document.createElement("div");
|
|
511
|
+
canvas.className = "fixed inset-0 pointer-events-none";
|
|
512
|
+
canvas.dataset.app = app.name;
|
|
513
|
+
this.#root.append(canvas);
|
|
514
|
+
const setBadge = (text) => {
|
|
515
|
+
badge.hidden = text === undefined;
|
|
516
|
+
badge.textContent = text ?? "";
|
|
517
|
+
};
|
|
518
|
+
let running;
|
|
519
|
+
button.addEventListener("click", () => {
|
|
520
|
+
if (running) {
|
|
521
|
+
const stopping = running;
|
|
522
|
+
running = undefined;
|
|
523
|
+
button.ariaPressed = "false";
|
|
524
|
+
stopping.then((stop) => {
|
|
525
|
+
stop?.();
|
|
526
|
+
setBadge(undefined);
|
|
527
|
+
canvas.replaceChildren();
|
|
528
|
+
canvas.style.pointerEvents = "";
|
|
529
|
+
});
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
button.ariaPressed = "true";
|
|
533
|
+
delete button.dataset.failed;
|
|
534
|
+
const page = window[DEVTOOLS_HOOK];
|
|
535
|
+
const run = startView(app.load, canvas, viewContext(DevtoolsToolbar.config.baseURL, page, setBadge)).catch((error) => {
|
|
536
|
+
if (running === run) {
|
|
537
|
+
button.dataset.failed = "";
|
|
538
|
+
button.ariaPressed = "false";
|
|
539
|
+
running = undefined;
|
|
540
|
+
}
|
|
541
|
+
console.error(`[hamur] devtools app ${JSON.stringify(app.name)} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
542
|
+
return;
|
|
543
|
+
});
|
|
544
|
+
running = run;
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
get isOpen() {
|
|
548
|
+
return !this.#panel.hidden;
|
|
549
|
+
}
|
|
550
|
+
apply() {
|
|
551
|
+
const settings = readSettings();
|
|
552
|
+
this.#pill.dataset.placement = settings.placement ?? DevtoolsToolbar.config.placement;
|
|
553
|
+
this.#layout();
|
|
554
|
+
}
|
|
555
|
+
rect() {
|
|
556
|
+
const settings = readSettings();
|
|
557
|
+
if (this.#live)
|
|
558
|
+
return this.#live;
|
|
559
|
+
if (isDocked(settings.panel)) {
|
|
560
|
+
const height = clampHeight(settings.height);
|
|
561
|
+
return { x: 0, y: window.innerHeight - height, width: window.innerWidth, height };
|
|
562
|
+
}
|
|
563
|
+
return clampRect(settings.panel, viewport());
|
|
564
|
+
}
|
|
565
|
+
#layout() {
|
|
566
|
+
const settings = readSettings();
|
|
567
|
+
const style = this.#panel.style;
|
|
568
|
+
if (isDocked(settings.panel) && !this.#live) {
|
|
569
|
+
const height = clampHeight(settings.height);
|
|
570
|
+
this.#root.style.setProperty("--panel-height", `${height}px`);
|
|
571
|
+
style.setProperty("left", "0");
|
|
572
|
+
style.setProperty("top", "auto");
|
|
573
|
+
style.setProperty("bottom", "0");
|
|
574
|
+
style.setProperty("width", "100%");
|
|
575
|
+
style.setProperty("height", `${height}px`);
|
|
576
|
+
} else {
|
|
577
|
+
const rect = this.rect();
|
|
578
|
+
style.setProperty("left", `${rect.x}px`);
|
|
579
|
+
style.setProperty("top", `${rect.y}px`);
|
|
580
|
+
style.setProperty("bottom", "auto");
|
|
581
|
+
style.setProperty("width", `${rect.width}px`);
|
|
582
|
+
style.setProperty("height", `${rect.height}px`);
|
|
583
|
+
}
|
|
584
|
+
this.#place();
|
|
585
|
+
}
|
|
586
|
+
place(rect, commit) {
|
|
587
|
+
const next = clampRect(rect, viewport());
|
|
588
|
+
if (commit) {
|
|
589
|
+
this.#live = undefined;
|
|
590
|
+
writeSettings({ ...readSettings(), panel: next });
|
|
591
|
+
} else
|
|
592
|
+
this.#live = next;
|
|
593
|
+
this.#layout();
|
|
594
|
+
}
|
|
595
|
+
dock() {
|
|
596
|
+
this.#live = undefined;
|
|
597
|
+
writeSettings({ ...readSettings(), panel: { dock: "bottom" } });
|
|
598
|
+
this.#layout();
|
|
599
|
+
}
|
|
600
|
+
#place() {
|
|
601
|
+
const settings = readSettings();
|
|
602
|
+
if (settings.pill) {
|
|
603
|
+
this.#pill.className = PILL;
|
|
604
|
+
const box = this.#pill.getBoundingClientRect();
|
|
605
|
+
const { left, top } = pillPosition(settings.pill, box, viewport());
|
|
606
|
+
this.#pill.style.setProperty("left", `${left}px`);
|
|
607
|
+
this.#pill.style.setProperty("top", `${top}px`);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
this.#pill.style.removeProperty("left");
|
|
611
|
+
this.#pill.style.removeProperty("top");
|
|
612
|
+
this.#pill.className = `${PILL} ${placementClasses(this.#pill.dataset.placement, this.isOpen && isDocked(settings.panel) && !this.#live)}`;
|
|
613
|
+
}
|
|
614
|
+
open(tab) {
|
|
615
|
+
const view = readView();
|
|
616
|
+
const shown = tab ?? (this.#frame ? undefined : view.tab);
|
|
617
|
+
const src = `${DevtoolsToolbar.config.baseURL}_hamur/devtools/${shown ? `#/${shown}` : ""}`;
|
|
618
|
+
const kept = shown ?? view.tab;
|
|
619
|
+
writeView(kept === undefined ? { open: true } : { open: true, tab: kept });
|
|
620
|
+
if (!this.#frame) {
|
|
621
|
+
this.#frame = document.createElement("iframe");
|
|
622
|
+
this.#frame.title = "Hamur devtools";
|
|
623
|
+
this.#frame.className = "block w-full h-full border-0";
|
|
624
|
+
this.#frame.src = src;
|
|
625
|
+
this.#panel.append(this.#frame);
|
|
626
|
+
} else if (tab)
|
|
627
|
+
this.#frame.contentWindow?.location.replace(src);
|
|
628
|
+
this.#panel.hidden = false;
|
|
629
|
+
this.#button.setAttribute("aria-expanded", "true");
|
|
630
|
+
this.#layout();
|
|
631
|
+
}
|
|
632
|
+
close() {
|
|
633
|
+
writeView({ ...readView(), open: false });
|
|
634
|
+
this.#panel.hidden = true;
|
|
635
|
+
this.#button.setAttribute("aria-expanded", "false");
|
|
636
|
+
this.#layout();
|
|
637
|
+
}
|
|
638
|
+
toggle() {
|
|
639
|
+
if (this.isOpen)
|
|
640
|
+
this.close();
|
|
641
|
+
else
|
|
642
|
+
this.open();
|
|
643
|
+
}
|
|
644
|
+
#resizable(grip, edge) {
|
|
645
|
+
grip.addEventListener("pointerdown", (down) => {
|
|
646
|
+
if (down.button !== 0)
|
|
647
|
+
return;
|
|
648
|
+
grip.setPointerCapture(down.pointerId);
|
|
649
|
+
this.#frame?.classList.add("pointer-events-none");
|
|
650
|
+
const docked = isDocked(readSettings().panel);
|
|
651
|
+
const start = this.rect();
|
|
652
|
+
const dockedHeight = (event) => clampHeight(start.height - (event.clientY - down.clientY));
|
|
653
|
+
const move = (event) => {
|
|
654
|
+
if (docked && edge === "n") {
|
|
655
|
+
const height = dockedHeight(event);
|
|
656
|
+
this.#root.style.setProperty("--panel-height", `${height}px`);
|
|
657
|
+
this.#panel.style.setProperty("height", `${height}px`);
|
|
658
|
+
this.#place();
|
|
659
|
+
} else
|
|
660
|
+
this.place(resizeRect(start, edge, event.clientX - down.clientX, event.clientY - down.clientY, viewport()), false);
|
|
661
|
+
};
|
|
662
|
+
const up = (event) => {
|
|
663
|
+
grip.removeEventListener("pointermove", move);
|
|
664
|
+
grip.removeEventListener("pointerup", up);
|
|
665
|
+
this.#frame?.classList.remove("pointer-events-none");
|
|
666
|
+
if (docked && edge === "n")
|
|
667
|
+
writeSettings({ ...readSettings(), height: dockedHeight(event) });
|
|
668
|
+
else
|
|
669
|
+
this.place(resizeRect(start, edge, event.clientX - down.clientX, event.clientY - down.clientY, viewport()), true);
|
|
670
|
+
};
|
|
671
|
+
grip.addEventListener("pointermove", move);
|
|
672
|
+
grip.addEventListener("pointerup", up);
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
#draggable(pill) {
|
|
676
|
+
let moved = false;
|
|
677
|
+
pill.addEventListener("click", (event) => {
|
|
678
|
+
if (!moved)
|
|
679
|
+
return;
|
|
680
|
+
moved = false;
|
|
681
|
+
event.stopPropagation();
|
|
682
|
+
event.preventDefault();
|
|
683
|
+
}, true);
|
|
684
|
+
pill.addEventListener("pointerdown", (down) => {
|
|
685
|
+
if (down.button !== 0)
|
|
686
|
+
return;
|
|
687
|
+
const box = pill.getBoundingClientRect();
|
|
688
|
+
let dragging = false;
|
|
689
|
+
moved = false;
|
|
690
|
+
const at = (event) => {
|
|
691
|
+
const left = Math.max(0, Math.min(box.left + event.clientX - down.clientX, window.innerWidth - box.width));
|
|
692
|
+
const top = Math.max(0, Math.min(box.top + event.clientY - down.clientY, window.innerHeight - box.height));
|
|
693
|
+
return { left, top };
|
|
694
|
+
};
|
|
695
|
+
const move = (event) => {
|
|
696
|
+
if (!dragging) {
|
|
697
|
+
if (Math.hypot(event.clientX - down.clientX, event.clientY - down.clientY) < DRAG_THRESHOLD)
|
|
698
|
+
return;
|
|
699
|
+
dragging = true;
|
|
700
|
+
pill.setPointerCapture(down.pointerId);
|
|
701
|
+
this.#frame?.classList.add("pointer-events-none");
|
|
702
|
+
pill.className = PILL;
|
|
703
|
+
}
|
|
704
|
+
const { left, top } = at(event);
|
|
705
|
+
pill.style.setProperty("left", `${left}px`);
|
|
706
|
+
pill.style.setProperty("top", `${top}px`);
|
|
707
|
+
};
|
|
708
|
+
const up = (event) => {
|
|
709
|
+
window.removeEventListener("pointermove", move);
|
|
710
|
+
window.removeEventListener("pointerup", up);
|
|
711
|
+
window.removeEventListener("pointercancel", up);
|
|
712
|
+
if (!dragging)
|
|
713
|
+
return;
|
|
714
|
+
this.#frame?.classList.remove("pointer-events-none");
|
|
715
|
+
moved = true;
|
|
716
|
+
setTimeout(() => moved = false, 0);
|
|
717
|
+
const { left, top } = at(event);
|
|
718
|
+
writeSettings({ ...readSettings(), pill: { x: left / window.innerWidth, y: top / window.innerHeight } });
|
|
719
|
+
this.#place();
|
|
720
|
+
};
|
|
721
|
+
window.addEventListener("pointermove", move);
|
|
722
|
+
window.addEventListener("pointerup", up);
|
|
723
|
+
window.addEventListener("pointercancel", up);
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
function mountToolbar(config) {
|
|
728
|
+
if (document.querySelector(TOOLBAR_TAG))
|
|
729
|
+
return;
|
|
730
|
+
DevtoolsToolbar.config = config;
|
|
731
|
+
if (!customElements.get(TOOLBAR_TAG))
|
|
732
|
+
customElements.define(TOOLBAR_TAG, DevtoolsToolbar);
|
|
733
|
+
const toolbar = document.createElement(TOOLBAR_TAG);
|
|
734
|
+
document.body.append(toolbar);
|
|
735
|
+
const keep = () => {
|
|
736
|
+
if (toolbar.parentNode !== document.body)
|
|
737
|
+
document.body.append(toolbar);
|
|
738
|
+
bodyObserver.disconnect();
|
|
739
|
+
bodyObserver.observe(document.body, { childList: true });
|
|
740
|
+
};
|
|
741
|
+
const bodyObserver = new MutationObserver(keep);
|
|
742
|
+
bodyObserver.observe(document.body, { childList: true });
|
|
743
|
+
new MutationObserver(keep).observe(document.documentElement, { childList: true });
|
|
744
|
+
const shortcut = config.shortcut === false ? undefined : parseShortcut(config.shortcut);
|
|
745
|
+
if (shortcut) {
|
|
746
|
+
window.addEventListener("keydown", (event) => {
|
|
747
|
+
if (!matchesShortcut(event, shortcut) || isTyping(event))
|
|
748
|
+
return;
|
|
749
|
+
event.preventDefault();
|
|
750
|
+
toolbar.toggle();
|
|
751
|
+
}, true);
|
|
752
|
+
}
|
|
753
|
+
window.addEventListener("storage", (event) => {
|
|
754
|
+
if (event.key === SETTINGS_KEY || event.key === null)
|
|
755
|
+
toolbar.apply();
|
|
756
|
+
});
|
|
757
|
+
window.addEventListener("resize", () => toolbar.apply());
|
|
758
|
+
const hook = window[DEVTOOLS_HOOK];
|
|
759
|
+
if (hook) {
|
|
760
|
+
hook.toolbar = {
|
|
761
|
+
open: (tab) => toolbar.open(tab),
|
|
762
|
+
close: () => toolbar.close(),
|
|
763
|
+
rect: () => toolbar.rect(),
|
|
764
|
+
place: (rect, commit) => toolbar.place(rect, commit),
|
|
765
|
+
dock: () => toolbar.dock()
|
|
766
|
+
};
|
|
767
|
+
serveBridge(hook);
|
|
768
|
+
}
|
|
769
|
+
const view = readView();
|
|
770
|
+
if (view.open)
|
|
771
|
+
toolbar.open(view.tab);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// packages/devtools/src/client.ts
|
|
775
|
+
var client_default = definePlugin((app) => {
|
|
776
|
+
app.hooks.hookOnce("app:mounted", () => mountToolbar({ baseURL, shortcut, placement, apps }));
|
|
777
|
+
});
|
|
778
|
+
export {
|
|
779
|
+
client_default as default
|
|
780
|
+
};
|