@hamur/devtools 0.0.0-stage → 0.0.1
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 +28 -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,429 @@
|
|
|
1
|
+
/// <reference path="../env.d.ts" />
|
|
2
|
+
import type { DevtoolsPage } from "hamur/app"
|
|
3
|
+
import { DEVTOOLS_HOOK } from "hamur/adapter/dev"
|
|
4
|
+
import { views } from "#hamur/devtools-views"
|
|
5
|
+
import { connectBridge, OWN_WINDOW } from "../bridge.ts"
|
|
6
|
+
import { SETTINGS_ICON } from "../icons.ts"
|
|
7
|
+
import type { ToolbarControl } from "../toolbar/element.ts"
|
|
8
|
+
import { isTyping, matchesShortcut, parseShortcut } from "../toolbar/shortcut.ts"
|
|
9
|
+
import { onDevtoolsEvent, startView, viewContext } from "../view-context.ts"
|
|
10
|
+
import { PANEL_CONFIG_ID, type PanelConfig } from "./document.ts"
|
|
11
|
+
import { groupTabs } from "./groups.ts"
|
|
12
|
+
import {
|
|
13
|
+
MAX_SCALE,
|
|
14
|
+
MIN_SCALE,
|
|
15
|
+
PLACEMENTS,
|
|
16
|
+
readSettings,
|
|
17
|
+
readView,
|
|
18
|
+
THEMES,
|
|
19
|
+
writeSettings,
|
|
20
|
+
writeView,
|
|
21
|
+
type DevtoolsSettings,
|
|
22
|
+
} from "./settings.ts"
|
|
23
|
+
import { element, UI } from "./ui.ts"
|
|
24
|
+
|
|
25
|
+
interface PanelTab {
|
|
26
|
+
name: string
|
|
27
|
+
title: string
|
|
28
|
+
icon: string
|
|
29
|
+
view: { type: "iframe"; src: string } | { type: "module" }
|
|
30
|
+
module: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const config = JSON.parse(document.getElementById(PANEL_CONFIG_ID)!.textContent!) as PanelConfig
|
|
34
|
+
const nav = document.querySelector<HTMLElement>("[data-tabs]")!
|
|
35
|
+
const content = document.querySelector<HTMLElement>("[data-content]")!
|
|
36
|
+
const settingsButton = document.querySelector<HTMLButtonElement>("[data-settings]")!
|
|
37
|
+
const framed = window.parent !== window
|
|
38
|
+
const own = !framed && window.name === OWN_WINDOW
|
|
39
|
+
let bridged: DevtoolsPage | undefined
|
|
40
|
+
let settings = readSettings()
|
|
41
|
+
let tabs: PanelTab[] = []
|
|
42
|
+
const badges = new Map<string, string>()
|
|
43
|
+
let mounted: { name: string; stop?: () => void; count?: HTMLElement } | undefined
|
|
44
|
+
|
|
45
|
+
const NEEDS_PAGE_DOCUMENT = new Set(["timeline", "audit", "seo"])
|
|
46
|
+
|
|
47
|
+
const toolbarOf = (): ToolbarControl | undefined =>
|
|
48
|
+
framed
|
|
49
|
+
? (window.parent as unknown as Record<string, { toolbar?: ToolbarControl } | undefined>)[DEVTOOLS_HOOK]?.toolbar
|
|
50
|
+
: undefined
|
|
51
|
+
|
|
52
|
+
const ITEM =
|
|
53
|
+
"flex-none flex items-center gap-2 h-7 px-2 rounded-md no-underline text-sm font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
|
54
|
+
const SETTINGS_BUTTON =
|
|
55
|
+
"inline-flex items-center justify-center size-7 border-0 rounded-md cursor-pointer hover:bg-hover hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
|
56
|
+
|
|
57
|
+
function iconOf(icon: string, className: string): HTMLElement {
|
|
58
|
+
const span = element("span", { className })
|
|
59
|
+
if (icon.trimStart().startsWith("<svg")) span.innerHTML = icon
|
|
60
|
+
else
|
|
61
|
+
span.append(element("span", { className: "inline-flex justify-center w-4 font-mono text-2xs leading-none" }, icon))
|
|
62
|
+
return span
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function applySettings(): void {
|
|
66
|
+
const root = document.documentElement
|
|
67
|
+
if (settings.theme === "system") delete root.dataset.theme
|
|
68
|
+
else root.dataset.theme = settings.theme
|
|
69
|
+
root.style.fontSize = `${16 * settings.scale}px`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function save(next: DevtoolsSettings): void {
|
|
73
|
+
settings = next
|
|
74
|
+
writeSettings(settings)
|
|
75
|
+
applySettings()
|
|
76
|
+
render()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const current = (): string | undefined => location.hash.match(/^#\/([a-z][a-z0-9-]*)/)?.[1]
|
|
80
|
+
|
|
81
|
+
const shownTabs = (): PanelTab[] => groupTabs(tabs, settings).flatMap((entry) => entry.tabs)
|
|
82
|
+
|
|
83
|
+
function selectedTab(): PanelTab | undefined {
|
|
84
|
+
const shown = shownTabs()
|
|
85
|
+
return shown.find((tab) => tab.name === current()) ?? shown.find((tab) => tab.name === "overview") ?? shown[0]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function renderNav(): void {
|
|
89
|
+
const selected = current() === "settings" ? undefined : selectedTab()
|
|
90
|
+
nav.replaceChildren(
|
|
91
|
+
...groupTabs(tabs, settings).flatMap(({ group, tabs: grouped }, index) => [
|
|
92
|
+
element(
|
|
93
|
+
"div",
|
|
94
|
+
{ className: `flex-none px-2.5 ${index ? "pt-3.5" : "pt-1"} pb-1 text-xs font-medium text-fg-4` },
|
|
95
|
+
group,
|
|
96
|
+
),
|
|
97
|
+
...grouped.map((tab) => {
|
|
98
|
+
const on = tab === selected
|
|
99
|
+
const badge = badges.get(tab.name)
|
|
100
|
+
return element(
|
|
101
|
+
"a",
|
|
102
|
+
{
|
|
103
|
+
href: `#/${tab.name}`,
|
|
104
|
+
className: `${ITEM} ${on ? "bg-active text-fg" : "text-side hover:bg-hover hover:text-fg"}`,
|
|
105
|
+
...(on ? { ariaCurrent: "page" } : {}),
|
|
106
|
+
},
|
|
107
|
+
iconOf(tab.icon, `inline-flex flex-none ${on ? "text-fg" : "text-side-icon"}`),
|
|
108
|
+
element("span", { className: "flex-1 min-w-0 truncate" }, tab.title),
|
|
109
|
+
...(badge === undefined ? [] : [element("span", { className: "text-xs text-fg-4 tabular-nums" }, badge)]),
|
|
110
|
+
)
|
|
111
|
+
}),
|
|
112
|
+
]),
|
|
113
|
+
)
|
|
114
|
+
settingsButton.className = `${SETTINGS_BUTTON} ${current() === "settings" ? "bg-active text-fg" : "bg-transparent text-side-icon"}`
|
|
115
|
+
if (mounted?.count) mounted.count.textContent = badges.get(mounted.name) ?? ""
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The page header of the boards: icon, title and the tab's badge as its count, above the body that scrolls. */
|
|
119
|
+
function header(icon: string, title: string): { node: HTMLElement; count: HTMLElement } {
|
|
120
|
+
const count = element("span", { className: "text-xs text-fg-4 tabular-nums" })
|
|
121
|
+
const node = element(
|
|
122
|
+
"header",
|
|
123
|
+
{ className: "flex-none flex items-center gap-2.5 h-11 pl-4 pr-3 border-b border-line" },
|
|
124
|
+
iconOf(icon, "inline-flex flex-none text-fg-3"),
|
|
125
|
+
element("h1", { className: "m-0 text-sm font-medium text-fg" }, title),
|
|
126
|
+
count,
|
|
127
|
+
)
|
|
128
|
+
node.dataset.drag = ""
|
|
129
|
+
return { node, count }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function unmount(): void {
|
|
133
|
+
if (!mounted) return
|
|
134
|
+
mounted.stop?.()
|
|
135
|
+
const { name } = mounted
|
|
136
|
+
mounted = undefined
|
|
137
|
+
if (badges.delete(name)) renderNav()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function setBadge(name: string, text: string | undefined): void {
|
|
141
|
+
if (text === undefined) badges.delete(name)
|
|
142
|
+
else badges.set(name, String(text))
|
|
143
|
+
renderNav()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const failure = (text: string): HTMLElement => element("pre", { className: UI.error }, text)
|
|
147
|
+
|
|
148
|
+
function mountView(tab: PanelTab): void {
|
|
149
|
+
const top = header(tab.icon, tab.title)
|
|
150
|
+
const canvas = element("div", { className: "flex-1 min-h-0 overflow-auto" })
|
|
151
|
+
canvas.dataset.view = tab.name
|
|
152
|
+
content.replaceChildren(top.node, canvas)
|
|
153
|
+
const entry: { name: string; stop?: () => void; count?: HTMLElement } = { name: tab.name, count: top.count }
|
|
154
|
+
mounted = entry
|
|
155
|
+
const failed = (error: unknown) => {
|
|
156
|
+
if (mounted === entry)
|
|
157
|
+
canvas.replaceChildren(
|
|
158
|
+
failure(`Tab ${JSON.stringify(tab.name)} failed: ${error instanceof Error ? error.message : String(error)}`),
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
const load = Object.hasOwn(views, tab.name) ? views[tab.name] : undefined
|
|
162
|
+
if (!load) return failed(new Error("its view is not in the panel build"))
|
|
163
|
+
const page = framed ? (window.parent as unknown as Record<string, DevtoolsPage | undefined>)[DEVTOOLS_HOOK] : bridged
|
|
164
|
+
startView(
|
|
165
|
+
load,
|
|
166
|
+
canvas,
|
|
167
|
+
viewContext(config.baseURL, page, (text) => {
|
|
168
|
+
if (mounted === entry) setBadge(tab.name, text)
|
|
169
|
+
}),
|
|
170
|
+
).then((stop) => {
|
|
171
|
+
if (mounted === entry) entry.stop = stop
|
|
172
|
+
else stop()
|
|
173
|
+
}, failed)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function render(): void {
|
|
177
|
+
renderNav()
|
|
178
|
+
if (current() === "settings") {
|
|
179
|
+
unmount()
|
|
180
|
+
return renderSettings()
|
|
181
|
+
}
|
|
182
|
+
const selected = selectedTab()
|
|
183
|
+
if (selected && mounted?.name === selected.name) return
|
|
184
|
+
unmount()
|
|
185
|
+
if (!selected)
|
|
186
|
+
return void content.replaceChildren(element("p", { className: UI.notice }, "No devtools tabs are registered."))
|
|
187
|
+
if (own && NEEDS_PAGE_DOCUMENT.has(selected.name)) {
|
|
188
|
+
const top = header(selected.icon, selected.title)
|
|
189
|
+
mounted = { name: selected.name, count: top.count }
|
|
190
|
+
content.replaceChildren(top.node, element("p", { className: UI.notice }, "Open the panel in the page for this tab"))
|
|
191
|
+
} else if (selected.view.type === "iframe") {
|
|
192
|
+
const top = header(selected.icon, selected.title)
|
|
193
|
+
mounted = { name: selected.name, count: top.count }
|
|
194
|
+
content.replaceChildren(
|
|
195
|
+
top.node,
|
|
196
|
+
element("iframe", {
|
|
197
|
+
src: selected.view.src,
|
|
198
|
+
title: selected.title,
|
|
199
|
+
className: "block flex-1 min-h-0 w-full border-0",
|
|
200
|
+
}),
|
|
201
|
+
)
|
|
202
|
+
} else mountView(selected)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function renderSettings(): void {
|
|
206
|
+
const row = (label: string, ...value: (Node | string)[]) =>
|
|
207
|
+
element(
|
|
208
|
+
"label",
|
|
209
|
+
{ className: UI.row },
|
|
210
|
+
element("span", { className: UI.rowKey }, label),
|
|
211
|
+
element("span", { className: UI.rowValue }, ...value),
|
|
212
|
+
)
|
|
213
|
+
const choice = <T extends string>(
|
|
214
|
+
legend: string,
|
|
215
|
+
values: readonly T[],
|
|
216
|
+
value: T | undefined,
|
|
217
|
+
change: (value: T | undefined) => void,
|
|
218
|
+
unset?: string,
|
|
219
|
+
) => {
|
|
220
|
+
const select: HTMLSelectElement = element(
|
|
221
|
+
"select",
|
|
222
|
+
{ className: UI.field },
|
|
223
|
+
...(unset === undefined ? [] : [element("option", { value: "", selected: value === undefined }, unset)]),
|
|
224
|
+
...values.map((item) => element("option", { value: item, selected: item === value }, item)),
|
|
225
|
+
)
|
|
226
|
+
select.onchange = () => change((select.value || undefined) as T | undefined)
|
|
227
|
+
return row(legend, select)
|
|
228
|
+
}
|
|
229
|
+
const scale: HTMLInputElement = element("input", {
|
|
230
|
+
type: "range",
|
|
231
|
+
min: String(MIN_SCALE),
|
|
232
|
+
max: String(MAX_SCALE),
|
|
233
|
+
step: "0.05",
|
|
234
|
+
value: String(settings.scale),
|
|
235
|
+
className: "flex-1 min-w-0 accent-accent",
|
|
236
|
+
})
|
|
237
|
+
scale.onchange = () => save({ ...settings, scale: Number(scale.value) })
|
|
238
|
+
const groups = groupTabs(tabs, { hidden: [], order: settings.order })
|
|
239
|
+
const names = groups.flatMap((entry) => entry.tabs.map((tab) => tab.name))
|
|
240
|
+
const move = (name: string, by: number) => {
|
|
241
|
+
const next = [...names]
|
|
242
|
+
const index = next.indexOf(name)
|
|
243
|
+
next.splice(index, 1)
|
|
244
|
+
next.splice(index + by, 0, name)
|
|
245
|
+
save({ ...settings, order: next })
|
|
246
|
+
}
|
|
247
|
+
const arrow = (tab: PanelTab, by: number, disabled: boolean) =>
|
|
248
|
+
element(
|
|
249
|
+
"button",
|
|
250
|
+
{
|
|
251
|
+
type: "button",
|
|
252
|
+
disabled,
|
|
253
|
+
ariaLabel: `Move ${tab.title} ${by < 0 ? "up" : "down"}`,
|
|
254
|
+
className: UI.iconButton,
|
|
255
|
+
onclick: () => move(tab.name, by),
|
|
256
|
+
},
|
|
257
|
+
by < 0 ? "↑" : "↓",
|
|
258
|
+
)
|
|
259
|
+
const tabRows = groups.flatMap(({ group, tabs: grouped }) => [
|
|
260
|
+
element(
|
|
261
|
+
"div",
|
|
262
|
+
{ className: UI.groupHead },
|
|
263
|
+
element("span", { className: "font-medium text-fg-2" }, group),
|
|
264
|
+
element("span", { className: "text-fg-4" }, String(grouped.length)),
|
|
265
|
+
),
|
|
266
|
+
...grouped.map((tab, index) => {
|
|
267
|
+
const shown: HTMLInputElement = element("input", {
|
|
268
|
+
type: "checkbox",
|
|
269
|
+
checked: !settings.hidden.includes(tab.name),
|
|
270
|
+
className: "flex-none m-0 accent-accent",
|
|
271
|
+
})
|
|
272
|
+
shown.onchange = () =>
|
|
273
|
+
save({
|
|
274
|
+
...settings,
|
|
275
|
+
hidden: shown.checked ? settings.hidden.filter((name) => name !== tab.name) : [...settings.hidden, tab.name],
|
|
276
|
+
})
|
|
277
|
+
return element(
|
|
278
|
+
"div",
|
|
279
|
+
{ className: "flex items-center gap-3 h-8 mx-1.5 px-2.5 rounded-md hover:bg-hover" },
|
|
280
|
+
shown,
|
|
281
|
+
element("span", { className: "flex-1 min-w-0 truncate font-medium text-sm text-fg" }, tab.title),
|
|
282
|
+
element("span", { className: "flex-none w-28 truncate text-xs text-fg-3" }, tab.module),
|
|
283
|
+
arrow(tab, -1, index === 0),
|
|
284
|
+
arrow(tab, 1, index === grouped.length - 1),
|
|
285
|
+
)
|
|
286
|
+
}),
|
|
287
|
+
])
|
|
288
|
+
const top = header(SETTINGS_ICON, "Panel settings")
|
|
289
|
+
content.replaceChildren(
|
|
290
|
+
top.node,
|
|
291
|
+
element(
|
|
292
|
+
"div",
|
|
293
|
+
{ className: "flex-1 min-h-0 flex overflow-auto" },
|
|
294
|
+
element(
|
|
295
|
+
"fieldset",
|
|
296
|
+
{ className: "w-96 flex-none flex flex-col gap-4 m-0 px-5 py-4 border-0 border-r border-line" },
|
|
297
|
+
element("legend", { className: "sr-only" }, "Appearance"),
|
|
298
|
+
element(
|
|
299
|
+
"div",
|
|
300
|
+
{ className: "flex flex-col gap-1" },
|
|
301
|
+
element("span", { className: UI.label, ariaHidden: "true" }, "Appearance"),
|
|
302
|
+
row(
|
|
303
|
+
"Position",
|
|
304
|
+
element(
|
|
305
|
+
"button",
|
|
306
|
+
{
|
|
307
|
+
type: "button",
|
|
308
|
+
className: UI.button,
|
|
309
|
+
onclick: () => save((({ panel: _panel, pill: _pill, ...rest }) => rest)(settings)),
|
|
310
|
+
},
|
|
311
|
+
"Reset position",
|
|
312
|
+
),
|
|
313
|
+
),
|
|
314
|
+
choice("Theme", THEMES, settings.theme, (theme) => save({ ...settings, theme: theme ?? "system" })),
|
|
315
|
+
row("Scale", scale),
|
|
316
|
+
choice(
|
|
317
|
+
"Toolbar placement",
|
|
318
|
+
PLACEMENTS,
|
|
319
|
+
settings.placement,
|
|
320
|
+
(placement) =>
|
|
321
|
+
save(placement ? { ...settings, placement } : (({ placement: _, ...rest }) => rest)(settings)),
|
|
322
|
+
`config (${config.placement})`,
|
|
323
|
+
),
|
|
324
|
+
),
|
|
325
|
+
element(
|
|
326
|
+
"p",
|
|
327
|
+
{ className: "m-0 text-xs leading-normal text-fg-3" },
|
|
328
|
+
"These settings live in this browser only, under ",
|
|
329
|
+
element("code", { className: "font-mono" }, 'localStorage["hamur:devtools"]'),
|
|
330
|
+
".",
|
|
331
|
+
),
|
|
332
|
+
),
|
|
333
|
+
element(
|
|
334
|
+
"fieldset",
|
|
335
|
+
{ className: "flex-1 min-w-0 m-0 p-0 pt-0.5 pb-1.5 border-0" },
|
|
336
|
+
element("legend", { className: "sr-only" }, "Tabs"),
|
|
337
|
+
...(tabRows.length ? tabRows : [element("p", { className: UI.notice }, "No devtools tabs are registered.")]),
|
|
338
|
+
),
|
|
339
|
+
),
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const shortcut = config.shortcut === false ? undefined : parseShortcut(config.shortcut)
|
|
344
|
+
if (shortcut && framed) {
|
|
345
|
+
window.addEventListener(
|
|
346
|
+
"keydown",
|
|
347
|
+
(event) => {
|
|
348
|
+
if (!matchesShortcut(event, shortcut) || isTyping(event)) return
|
|
349
|
+
event.preventDefault()
|
|
350
|
+
;(window.parent as unknown as Record<string, { toolbar?: { close(): void } } | undefined>)[
|
|
351
|
+
DEVTOOLS_HOOK
|
|
352
|
+
]?.toolbar?.close()
|
|
353
|
+
},
|
|
354
|
+
true,
|
|
355
|
+
)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
settingsButton.onclick = () => (location.hash = "#/settings")
|
|
359
|
+
const windowButton = document.querySelector<HTMLButtonElement>("[data-window]")
|
|
360
|
+
if (windowButton) {
|
|
361
|
+
windowButton.hidden = !framed
|
|
362
|
+
windowButton.onclick = () => {
|
|
363
|
+
const url = `${config.baseURL}_hamur/devtools/${current() ? `#/${current()}` : ""}`
|
|
364
|
+
if (window.open(url, OWN_WINDOW, "popup,width=1100,height=760")) toolbarOf()?.close()
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const DRAG_IGNORED = "button, a, input, select, textarea"
|
|
369
|
+
document.addEventListener("pointerdown", (down) => {
|
|
370
|
+
const control = toolbarOf()
|
|
371
|
+
const target = down.target as Element
|
|
372
|
+
const handle = target.closest<HTMLElement>("[data-drag]")
|
|
373
|
+
if (!control || !handle || down.button !== 0 || target.closest(DRAG_IGNORED)) return
|
|
374
|
+
handle.setPointerCapture(down.pointerId)
|
|
375
|
+
const start = control.rect()
|
|
376
|
+
let moved = false
|
|
377
|
+
const inPage = (event: PointerEvent) => {
|
|
378
|
+
const frame = control.rect()
|
|
379
|
+
return { x: event.clientX + frame.x, y: event.clientY + frame.y }
|
|
380
|
+
}
|
|
381
|
+
const origin = { x: down.clientX + start.x, y: down.clientY + start.y }
|
|
382
|
+
const to = (event: PointerEvent) => {
|
|
383
|
+
const at = inPage(event)
|
|
384
|
+
return { ...start, x: start.x + at.x - origin.x, y: start.y + at.y - origin.y }
|
|
385
|
+
}
|
|
386
|
+
const move = (event: PointerEvent) => {
|
|
387
|
+
moved = true
|
|
388
|
+
control.place(to(event), false)
|
|
389
|
+
}
|
|
390
|
+
const up = (event: PointerEvent) => {
|
|
391
|
+
handle.removeEventListener("pointermove", move)
|
|
392
|
+
handle.removeEventListener("pointerup", up)
|
|
393
|
+
if (moved) control.place(to(event), true)
|
|
394
|
+
}
|
|
395
|
+
handle.addEventListener("pointermove", move)
|
|
396
|
+
handle.addEventListener("pointerup", up)
|
|
397
|
+
})
|
|
398
|
+
document.addEventListener("dblclick", (event) => {
|
|
399
|
+
const target = event.target as Element
|
|
400
|
+
if (target.closest("[data-drag]") && !target.closest(DRAG_IGNORED)) toolbarOf()?.dock()
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
window.addEventListener("hashchange", () => {
|
|
404
|
+
if (framed && readView().open) {
|
|
405
|
+
const tab = current()
|
|
406
|
+
writeView(tab === undefined ? { open: true } : { open: true, tab })
|
|
407
|
+
}
|
|
408
|
+
render()
|
|
409
|
+
})
|
|
410
|
+
window.addEventListener("storage", () => {
|
|
411
|
+
settings = readSettings()
|
|
412
|
+
applySettings()
|
|
413
|
+
render()
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
applySettings()
|
|
417
|
+
;(own ? connectBridge().then((page) => void (bridged = page)) : Promise.resolve())
|
|
418
|
+
.then(() => fetch(`${config.baseURL}_hamur/devtools/api/tabs`))
|
|
419
|
+
.then((response) => response.json() as Promise<{ tabs: PanelTab[] }>)
|
|
420
|
+
.then((answer) => {
|
|
421
|
+
tabs = answer.tabs
|
|
422
|
+
render()
|
|
423
|
+
})
|
|
424
|
+
.catch((error: unknown) =>
|
|
425
|
+
content.replaceChildren(
|
|
426
|
+
failure(`Could not read the devtools tabs: ${error instanceof Error ? error.message : String(error)}`),
|
|
427
|
+
),
|
|
428
|
+
)
|
|
429
|
+
onDevtoolsEvent("devtools:reload", () => location.reload())
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
export const SETTINGS_KEY = "hamur:devtools"
|
|
2
|
+
|
|
3
|
+
export const PLACEMENTS = [
|
|
4
|
+
"bottom-center",
|
|
5
|
+
"bottom-left",
|
|
6
|
+
"bottom-right",
|
|
7
|
+
"top-center",
|
|
8
|
+
"top-left",
|
|
9
|
+
"top-right",
|
|
10
|
+
] as const
|
|
11
|
+
export type Placement = (typeof PLACEMENTS)[number]
|
|
12
|
+
|
|
13
|
+
export const THEMES = ["system", "light", "dark"] as const
|
|
14
|
+
export type Theme = (typeof THEMES)[number]
|
|
15
|
+
|
|
16
|
+
export const MIN_SCALE = 0.75
|
|
17
|
+
export const MAX_SCALE = 1.5
|
|
18
|
+
|
|
19
|
+
export const MIN_PANEL_WIDTH = 240
|
|
20
|
+
export const MIN_PANEL_HEIGHT = 120
|
|
21
|
+
|
|
22
|
+
export interface PanelRect {
|
|
23
|
+
x: number
|
|
24
|
+
y: number
|
|
25
|
+
width: number
|
|
26
|
+
height: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** `{ dock: "bottom" }` is the panel at the bottom edge with the kept `height`. */
|
|
30
|
+
export type PanelFrame = { dock: "bottom" } | PanelRect
|
|
31
|
+
|
|
32
|
+
export const isDocked = (frame: PanelFrame | undefined): frame is { dock: "bottom" } | undefined =>
|
|
33
|
+
frame === undefined || "dock" in frame
|
|
34
|
+
|
|
35
|
+
/** Where the pill was dropped, as fractions of the viewport. */
|
|
36
|
+
export interface PillPlace {
|
|
37
|
+
x: number
|
|
38
|
+
y: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The viewer's own choices, kept in `localStorage`, not in the repo. */
|
|
42
|
+
export interface DevtoolsSettings {
|
|
43
|
+
hidden: string[]
|
|
44
|
+
order: string[]
|
|
45
|
+
theme: Theme
|
|
46
|
+
scale: number
|
|
47
|
+
placement?: Placement
|
|
48
|
+
height: number
|
|
49
|
+
panel?: PanelFrame
|
|
50
|
+
pill?: PillPlace
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const DEFAULT_SETTINGS: DevtoolsSettings = { hidden: [], order: [], theme: "system", scale: 1, height: 360 }
|
|
54
|
+
|
|
55
|
+
const finite = (value: unknown): value is number => typeof value === "number" && Number.isFinite(value)
|
|
56
|
+
|
|
57
|
+
function parseFrame(value: unknown): PanelFrame | undefined {
|
|
58
|
+
if (typeof value !== "object" || value === null) return undefined
|
|
59
|
+
const given = value as Record<string, unknown>
|
|
60
|
+
if (given.dock === "bottom") return { dock: "bottom" }
|
|
61
|
+
if (finite(given.x) && finite(given.y) && finite(given.width) && finite(given.height))
|
|
62
|
+
return { x: given.x, y: given.y, width: given.width, height: given.height }
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function parsePill(value: unknown): PillPlace | undefined {
|
|
67
|
+
if (typeof value !== "object" || value === null) return undefined
|
|
68
|
+
const { x, y } = value as Record<string, unknown>
|
|
69
|
+
return finite(x) && finite(y) && x >= 0 && x <= 1 && y >= 0 && y <= 1 ? { x, y } : undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const names = (value: unknown): string[] | undefined =>
|
|
73
|
+
Array.isArray(value) && value.every((item) => typeof item === "string") ? value : undefined
|
|
74
|
+
|
|
75
|
+
/** A missing or broken value, or a broken field, reads as the default. */
|
|
76
|
+
export function parseSettings(text: string | null): DevtoolsSettings {
|
|
77
|
+
let value: unknown
|
|
78
|
+
try {
|
|
79
|
+
value = text === null ? undefined : JSON.parse(text)
|
|
80
|
+
} catch {
|
|
81
|
+
value = undefined
|
|
82
|
+
}
|
|
83
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
84
|
+
return { ...DEFAULT_SETTINGS, hidden: [], order: [] }
|
|
85
|
+
const given = value as Record<string, unknown>
|
|
86
|
+
const settings: DevtoolsSettings = {
|
|
87
|
+
hidden: names(given.hidden) ?? [],
|
|
88
|
+
order: names(given.order) ?? [],
|
|
89
|
+
theme: THEMES.includes(given.theme as Theme) ? (given.theme as Theme) : DEFAULT_SETTINGS.theme,
|
|
90
|
+
scale:
|
|
91
|
+
typeof given.scale === "number" && given.scale >= MIN_SCALE && given.scale <= MAX_SCALE
|
|
92
|
+
? given.scale
|
|
93
|
+
: DEFAULT_SETTINGS.scale,
|
|
94
|
+
height:
|
|
95
|
+
typeof given.height === "number" && Number.isFinite(given.height) && given.height > 0
|
|
96
|
+
? given.height
|
|
97
|
+
: DEFAULT_SETTINGS.height,
|
|
98
|
+
}
|
|
99
|
+
if (PLACEMENTS.includes(given.placement as Placement)) settings.placement = given.placement as Placement
|
|
100
|
+
const panel = parseFrame(given.panel)
|
|
101
|
+
if (panel) settings.panel = panel
|
|
102
|
+
const pill = parsePill(given.pill)
|
|
103
|
+
if (pill) settings.pill = pill
|
|
104
|
+
return settings
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function readSettings(): DevtoolsSettings {
|
|
108
|
+
try {
|
|
109
|
+
return parseSettings(globalThis.localStorage.getItem(SETTINGS_KEY))
|
|
110
|
+
} catch {
|
|
111
|
+
return parseSettings(null)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function writeSettings(settings: DevtoolsSettings): void {
|
|
116
|
+
try {
|
|
117
|
+
globalThis.localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings))
|
|
118
|
+
} catch {}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `order` first, then the rest in registration order; `hidden` left out. */
|
|
122
|
+
export function orderTabs<T extends { name: string }>(
|
|
123
|
+
tabs: readonly T[],
|
|
124
|
+
settings: Pick<DevtoolsSettings, "hidden" | "order">,
|
|
125
|
+
): T[] {
|
|
126
|
+
const ranked = [...tabs].sort(
|
|
127
|
+
(a, b) => rank(settings.order, a.name) - rank(settings.order, b.name) || tabs.indexOf(a) - tabs.indexOf(b),
|
|
128
|
+
)
|
|
129
|
+
return ranked.filter((tab) => !settings.hidden.includes(tab.name))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const rank = (order: readonly string[], name: string): number => {
|
|
133
|
+
const at = order.indexOf(name)
|
|
134
|
+
return at < 0 ? order.length : at
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface Viewport {
|
|
138
|
+
width: number
|
|
139
|
+
height: number
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const between = (value: number, min: number, max: number): number => Math.round(Math.max(min, Math.min(value, max)))
|
|
143
|
+
|
|
144
|
+
/** The rectangle kept inside the viewport: at least 240 × 120 px (less when the viewport is smaller), whole in view. */
|
|
145
|
+
export function clampRect(rect: PanelRect, viewport: Viewport): PanelRect {
|
|
146
|
+
const width = between(rect.width, Math.min(MIN_PANEL_WIDTH, viewport.width), viewport.width)
|
|
147
|
+
const height = between(rect.height, Math.min(MIN_PANEL_HEIGHT, viewport.height), viewport.height)
|
|
148
|
+
return {
|
|
149
|
+
x: between(rect.x, 0, viewport.width - width),
|
|
150
|
+
y: between(rect.y, 0, viewport.height - height),
|
|
151
|
+
width,
|
|
152
|
+
height,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export type Edge = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw"
|
|
157
|
+
|
|
158
|
+
/** `start` moved by the pointer's travel `dx`, `dy` on `edge`: the opposite edge stays where it is, even when the minimum size stops the drag. */
|
|
159
|
+
export function resizeRect(start: PanelRect, edge: Edge, dx: number, dy: number, viewport: Viewport): PanelRect {
|
|
160
|
+
const right = start.x + start.width
|
|
161
|
+
const bottom = start.y + start.height
|
|
162
|
+
let left = start.x
|
|
163
|
+
let top = start.y
|
|
164
|
+
let width = start.width
|
|
165
|
+
let height = start.height
|
|
166
|
+
if (edge.includes("w")) {
|
|
167
|
+
left = between(start.x + dx, 0, right - Math.min(MIN_PANEL_WIDTH, right))
|
|
168
|
+
width = right - left
|
|
169
|
+
} else if (edge.includes("e"))
|
|
170
|
+
width = between(start.width + dx, Math.min(MIN_PANEL_WIDTH, viewport.width - start.x), viewport.width - start.x)
|
|
171
|
+
if (edge.includes("n")) {
|
|
172
|
+
top = between(start.y + dy, 0, bottom - Math.min(MIN_PANEL_HEIGHT, bottom))
|
|
173
|
+
height = bottom - top
|
|
174
|
+
} else if (edge.includes("s"))
|
|
175
|
+
height = between(
|
|
176
|
+
start.height + dy,
|
|
177
|
+
Math.min(MIN_PANEL_HEIGHT, viewport.height - start.y),
|
|
178
|
+
viewport.height - start.y,
|
|
179
|
+
)
|
|
180
|
+
return { x: left, y: top, width, height }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Where the pill sits in px for the kept fractions, whole in view. */
|
|
184
|
+
export function pillPosition(place: PillPlace, size: Viewport, viewport: Viewport): { left: number; top: number } {
|
|
185
|
+
return {
|
|
186
|
+
left: between(place.x * viewport.width, 0, Math.max(0, viewport.width - size.width)),
|
|
187
|
+
top: between(place.y * viewport.height, 0, Math.max(0, viewport.height - size.height)),
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface ViewState {
|
|
192
|
+
open: boolean
|
|
193
|
+
tab?: string
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export const VIEW_KEY = "hamur:devtools:view"
|
|
197
|
+
|
|
198
|
+
export function parseView(text: string | null): ViewState {
|
|
199
|
+
let value: unknown
|
|
200
|
+
try {
|
|
201
|
+
value = text === null ? undefined : JSON.parse(text)
|
|
202
|
+
} catch {
|
|
203
|
+
value = undefined
|
|
204
|
+
}
|
|
205
|
+
if (typeof value !== "object" || value === null) return { open: false }
|
|
206
|
+
const given = value as Record<string, unknown>
|
|
207
|
+
const view: ViewState = { open: given.open === true }
|
|
208
|
+
if (typeof given.tab === "string" && /^[a-z][a-z0-9-]*$/.test(given.tab)) view.tab = given.tab
|
|
209
|
+
return view
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function readView(): ViewState {
|
|
213
|
+
try {
|
|
214
|
+
return parseView(globalThis.sessionStorage.getItem(VIEW_KEY))
|
|
215
|
+
} catch {
|
|
216
|
+
return { open: false }
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function writeView(view: ViewState): void {
|
|
221
|
+
try {
|
|
222
|
+
globalThis.sessionStorage.setItem(VIEW_KEY, JSON.stringify(view))
|
|
223
|
+
} catch {}
|
|
224
|
+
}
|