@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,375 @@
|
|
|
1
|
+
import { defineDevtoolsApp } from "hamur/app"
|
|
2
|
+
import type { AssetRow, AssetType } from "../dev/assets.ts"
|
|
3
|
+
import { element, pill, UI } from "../panel/ui.ts"
|
|
4
|
+
import { sourceLink } from "../source-link.ts"
|
|
5
|
+
|
|
6
|
+
export type AssetFilter = AssetType | "all"
|
|
7
|
+
|
|
8
|
+
export const FILTERS: Array<[AssetFilter, string]> = [
|
|
9
|
+
["all", "All"],
|
|
10
|
+
["image", "Images"],
|
|
11
|
+
["font", "Fonts"],
|
|
12
|
+
["text", "Text"],
|
|
13
|
+
["other", "Other"],
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
const TEXT_PREVIEW_BYTES = 64 * 1024
|
|
17
|
+
|
|
18
|
+
/** `1536` → `1.5 KB`: bytes below 1024, then KB and MB with one decimal. */
|
|
19
|
+
export function formatBytes(bytes: number): string {
|
|
20
|
+
if (bytes < 1024) return `${bytes} B`
|
|
21
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
|
22
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The rows of one type whose URL holds `text`, ignoring case. */
|
|
26
|
+
export function filterAssets(rows: AssetRow[], type: AssetFilter, text: string): AssetRow[] {
|
|
27
|
+
const needle = text.trim().toLowerCase()
|
|
28
|
+
return rows.filter(
|
|
29
|
+
(row) => (type === "all" || row.type === type) && (needle === "" || row.url.toLowerCase().includes(needle)),
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The card's second line: size, then `width×height` for an image whose header was read. */
|
|
34
|
+
export function assetDetail(row: AssetRow): string {
|
|
35
|
+
return row.width !== undefined && row.height !== undefined
|
|
36
|
+
? `${formatBytes(row.bytes)} · ${row.width}×${row.height}`
|
|
37
|
+
: formatBytes(row.bytes)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const nameOf = (url: string): string => url.slice(url.lastIndexOf("/") + 1)
|
|
41
|
+
|
|
42
|
+
const CARD =
|
|
43
|
+
"flex flex-col gap-1 w-full p-1.5 rounded-md border-0 bg-field ring-1 ring-inset text-left text-fg cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
|
44
|
+
|
|
45
|
+
function lit(card: HTMLElement, on: boolean): void {
|
|
46
|
+
card.setAttribute("aria-current", String(on))
|
|
47
|
+
card.classList.toggle("ring-accent", on)
|
|
48
|
+
card.classList.toggle("ring-line", !on)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const messageOf = (error: unknown): string => (error instanceof Error ? error.message : String(error))
|
|
52
|
+
|
|
53
|
+
async function copy(row: AssetRow, button: HTMLButtonElement): Promise<void> {
|
|
54
|
+
try {
|
|
55
|
+
await navigator.clipboard.writeText(new URL(row.url, location.origin).href)
|
|
56
|
+
button.textContent = "Copied"
|
|
57
|
+
} catch {
|
|
58
|
+
button.textContent = "Copy failed"
|
|
59
|
+
}
|
|
60
|
+
setTimeout(() => void (button.textContent = "Copy"), 1200)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function thumbnail(row: AssetRow): HTMLElement {
|
|
64
|
+
if (row.type === "image")
|
|
65
|
+
return element("img", {
|
|
66
|
+
src: row.url,
|
|
67
|
+
alt: "",
|
|
68
|
+
loading: "lazy",
|
|
69
|
+
decoding: "async",
|
|
70
|
+
className: "max-w-full max-h-full object-contain",
|
|
71
|
+
})
|
|
72
|
+
return element(
|
|
73
|
+
"span",
|
|
74
|
+
{ className: "font-mono text-xs font-semibold text-fg-3" },
|
|
75
|
+
row.url.includes(".")
|
|
76
|
+
? row.url
|
|
77
|
+
.slice(row.url.lastIndexOf(".") + 1)
|
|
78
|
+
.toUpperCase()
|
|
79
|
+
.slice(0, 5)
|
|
80
|
+
: "FILE",
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function preview(row: AssetRow): Promise<HTMLElement> {
|
|
85
|
+
if (row.type === "image")
|
|
86
|
+
return element("img", { src: row.url, alt: nameOf(row.url), className: "block max-w-full max-h-80 mx-auto" })
|
|
87
|
+
if (row.type === "font") {
|
|
88
|
+
const family = `hamur-devtools-${row.url.replace(/[^a-z0-9]/gi, "-")}`
|
|
89
|
+
try {
|
|
90
|
+
const face = new FontFace(family, `url(${JSON.stringify(row.url)})`)
|
|
91
|
+
document.fonts.add(await face.load())
|
|
92
|
+
const sample = element(
|
|
93
|
+
"p",
|
|
94
|
+
{ className: "m-0 text-xl break-words" },
|
|
95
|
+
"The quick brown fox jumps over the lazy dog 0123456789",
|
|
96
|
+
)
|
|
97
|
+
sample.style.fontFamily = family
|
|
98
|
+
return sample
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return element("p", { className: UI.fail }, `This font does not load: ${messageOf(error)}`)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (row.type === "text") {
|
|
104
|
+
const response = await fetch(row.url, { headers: { range: `bytes=0-${TEXT_PREVIEW_BYTES - 1}` } })
|
|
105
|
+
const text = await response.text()
|
|
106
|
+
return element(
|
|
107
|
+
"pre",
|
|
108
|
+
{ className: "m-0 max-h-80 overflow-auto font-mono text-xs leading-normal whitespace-pre-wrap" },
|
|
109
|
+
row.bytes > TEXT_PREVIEW_BYTES ? `${text}\n…` : text,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
return element("p", { className: UI.hint }, "No preview for this type")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function upload(
|
|
116
|
+
file: File,
|
|
117
|
+
overwrite: boolean,
|
|
118
|
+
): Promise<{ status: number; body: { error?: string } & Partial<AssetRow> }> {
|
|
119
|
+
const form = new FormData()
|
|
120
|
+
form.append("file", file)
|
|
121
|
+
const response = await fetch(`api/assets${overwrite ? "?overwrite=1" : ""}`, { method: "POST", body: form })
|
|
122
|
+
return {
|
|
123
|
+
status: response.status,
|
|
124
|
+
body: (await response.json().catch(() => ({}))) as { error?: string } & Partial<AssetRow>,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const STATUS = "flex flex-col gap-1 text-xs empty:hidden"
|
|
129
|
+
const statusClass = (failed: boolean): string => `${STATUS} ${failed ? "text-red" : "text-fg-3"}`
|
|
130
|
+
const dropClass = (over: boolean): string =>
|
|
131
|
+
`flex flex-col items-center gap-2 p-4 border-2 border-dashed rounded-md text-center text-fg-3 ${over ? "border-accent" : "border-line-strong"}`
|
|
132
|
+
|
|
133
|
+
export default defineDevtoolsApp({
|
|
134
|
+
async init(canvas) {
|
|
135
|
+
canvas.className = "flex flex-1 min-h-0"
|
|
136
|
+
const filterBar = element("div", { className: "flex gap-0.5" })
|
|
137
|
+
filterBar.setAttribute("role", "group")
|
|
138
|
+
filterBar.setAttribute("aria-label", "Type")
|
|
139
|
+
const search = element("input", {
|
|
140
|
+
type: "search",
|
|
141
|
+
placeholder: "Filter by name",
|
|
142
|
+
name: "filter",
|
|
143
|
+
className: `${UI.field} w-56`,
|
|
144
|
+
})
|
|
145
|
+
search.setAttribute("aria-label", "Filter by name")
|
|
146
|
+
const grid = element("ul", {
|
|
147
|
+
className: "grid grid-cols-[repeat(auto-fill,minmax(8.5rem,1fr))] gap-2 m-0 p-2 list-none",
|
|
148
|
+
})
|
|
149
|
+
const note = element("p", { className: `${UI.notice} empty:hidden` })
|
|
150
|
+
const pane = element("section", { className: "flex flex-col gap-2" })
|
|
151
|
+
pane.dataset.pane = ""
|
|
152
|
+
const picker = element("input", {
|
|
153
|
+
type: "file",
|
|
154
|
+
name: "file",
|
|
155
|
+
multiple: true,
|
|
156
|
+
className: "max-w-full text-xs text-fg-3",
|
|
157
|
+
})
|
|
158
|
+
picker.setAttribute("aria-label", "Upload to public/")
|
|
159
|
+
const dropStatus = element("output", { className: statusClass(false) })
|
|
160
|
+
const drop = element(
|
|
161
|
+
"div",
|
|
162
|
+
{ className: dropClass(false) },
|
|
163
|
+
element(
|
|
164
|
+
"p",
|
|
165
|
+
{ className: "m-0 text-xs text-fg-3" },
|
|
166
|
+
"Drop files here to write them to ",
|
|
167
|
+
element("code", { className: "font-mono" }, "public/"),
|
|
168
|
+
),
|
|
169
|
+
picker,
|
|
170
|
+
dropStatus,
|
|
171
|
+
)
|
|
172
|
+
drop.dataset.drop = ""
|
|
173
|
+
const side = element(
|
|
174
|
+
"aside",
|
|
175
|
+
{ className: "w-80 flex-none flex flex-col gap-4 p-4 border-l border-line overflow-auto" },
|
|
176
|
+
pane,
|
|
177
|
+
drop,
|
|
178
|
+
)
|
|
179
|
+
const root = element(
|
|
180
|
+
"div",
|
|
181
|
+
{ className: "flex flex-1 min-w-0 min-h-0" },
|
|
182
|
+
element(
|
|
183
|
+
"section",
|
|
184
|
+
{ className: "flex-1 min-w-0 flex flex-col" },
|
|
185
|
+
element("div", { className: UI.strip }, filterBar, element("span", { className: "flex-1" }), search),
|
|
186
|
+
note,
|
|
187
|
+
element("div", { className: "flex-1 min-h-0 overflow-auto" }, grid),
|
|
188
|
+
),
|
|
189
|
+
side,
|
|
190
|
+
)
|
|
191
|
+
canvas.append(root)
|
|
192
|
+
|
|
193
|
+
let rows: AssetRow[] = []
|
|
194
|
+
let filter: AssetFilter = "all"
|
|
195
|
+
let selected: string | undefined
|
|
196
|
+
|
|
197
|
+
function select(url: string): void {
|
|
198
|
+
selected = url
|
|
199
|
+
for (const card of grid.querySelectorAll<HTMLButtonElement>("button[data-url]"))
|
|
200
|
+
lit(card, card.dataset.url === url)
|
|
201
|
+
const row = rows.find((candidate) => candidate.url === url)
|
|
202
|
+
if (!row) {
|
|
203
|
+
pane.replaceChildren(element("p", { className: UI.hint }, "Choose an asset"))
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
const copyButton = element("button", { type: "button", className: UI.button }, "Copy")
|
|
207
|
+
copyButton.dataset.copy = ""
|
|
208
|
+
copyButton.addEventListener("click", () => void copy(row, copyButton))
|
|
209
|
+
const actions = element(
|
|
210
|
+
"div",
|
|
211
|
+
{ className: "flex flex-wrap items-center gap-1.5" },
|
|
212
|
+
element("code", { className: "font-mono text-xs text-fg-2 break-all" }, row.url),
|
|
213
|
+
copyButton,
|
|
214
|
+
)
|
|
215
|
+
if (row.file) {
|
|
216
|
+
const open = sourceLink(row.file)
|
|
217
|
+
open.textContent = "Open in editor"
|
|
218
|
+
open.title = row.file
|
|
219
|
+
actions.append(open)
|
|
220
|
+
}
|
|
221
|
+
const body = element(
|
|
222
|
+
"div",
|
|
223
|
+
{ className: "p-2 rounded-md bg-field ring-1 ring-inset ring-line" },
|
|
224
|
+
element("p", { className: UI.hint }, "Loading…"),
|
|
225
|
+
)
|
|
226
|
+
pane.replaceChildren(
|
|
227
|
+
element("h2", { className: "m-0 font-mono text-sm font-medium text-fg break-all" }, nameOf(row.url)),
|
|
228
|
+
actions,
|
|
229
|
+
element(
|
|
230
|
+
"p",
|
|
231
|
+
{ className: `m-0 ${UI.hint} break-all` },
|
|
232
|
+
assetDetail(row),
|
|
233
|
+
row.module ? ` · from ${row.module}, read-only` : row.file ? ` · ${row.file}` : "",
|
|
234
|
+
),
|
|
235
|
+
body,
|
|
236
|
+
)
|
|
237
|
+
pane.dataset.url = row.url
|
|
238
|
+
void preview(row).then(
|
|
239
|
+
(node) => selected === url && body.replaceChildren(node),
|
|
240
|
+
(error) => selected === url && body.replaceChildren(element("p", { className: UI.fail }, messageOf(error))),
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function draw(): void {
|
|
245
|
+
const shown = filterAssets(rows, filter, search.value)
|
|
246
|
+
note.textContent = rows.length === 0 ? "No public files" : shown.length === 0 ? "No asset matches" : ""
|
|
247
|
+
grid.replaceChildren(
|
|
248
|
+
...shown.map((row) => {
|
|
249
|
+
const detail = element("span", { className: "text-2xs text-fg-3" }, assetDetail(row))
|
|
250
|
+
detail.dataset.detail = ""
|
|
251
|
+
const card = element(
|
|
252
|
+
"button",
|
|
253
|
+
{ type: "button", className: CARD },
|
|
254
|
+
element(
|
|
255
|
+
"span",
|
|
256
|
+
{ className: "grid place-items-center h-20 rounded bg-panel overflow-hidden" },
|
|
257
|
+
thumbnail(row),
|
|
258
|
+
),
|
|
259
|
+
element("strong", { className: "font-mono text-xs font-semibold break-all" }, nameOf(row.url)),
|
|
260
|
+
detail,
|
|
261
|
+
)
|
|
262
|
+
card.dataset.url = row.url
|
|
263
|
+
card.dataset.type = row.type
|
|
264
|
+
if (row.module) card.dataset.module = row.module
|
|
265
|
+
lit(card, row.url === selected)
|
|
266
|
+
card.addEventListener("click", () => select(row.url))
|
|
267
|
+
const copyButton = element(
|
|
268
|
+
"button",
|
|
269
|
+
{
|
|
270
|
+
type: "button",
|
|
271
|
+
className: `${UI.button} absolute top-2 right-2 opacity-0 group-hover:opacity-100 focus:opacity-100`,
|
|
272
|
+
},
|
|
273
|
+
"Copy",
|
|
274
|
+
)
|
|
275
|
+
copyButton.dataset.copy = row.url
|
|
276
|
+
copyButton.addEventListener("click", () => void copy(row, copyButton))
|
|
277
|
+
return element("li", { className: "relative group" }, card, copyButton)
|
|
278
|
+
}),
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
for (const [value, label] of FILTERS) {
|
|
283
|
+
const button = element("button", { type: "button", className: pill(value === filter) }, label)
|
|
284
|
+
button.dataset.filter = value
|
|
285
|
+
button.setAttribute("aria-pressed", String(value === filter))
|
|
286
|
+
button.addEventListener("click", () => {
|
|
287
|
+
filter = value
|
|
288
|
+
for (const other of filterBar.querySelectorAll<HTMLButtonElement>("button")) {
|
|
289
|
+
other.setAttribute("aria-pressed", String(other.dataset.filter === value))
|
|
290
|
+
other.className = pill(other.dataset.filter === value)
|
|
291
|
+
}
|
|
292
|
+
draw()
|
|
293
|
+
})
|
|
294
|
+
filterBar.append(button)
|
|
295
|
+
}
|
|
296
|
+
search.addEventListener("input", draw)
|
|
297
|
+
|
|
298
|
+
async function load(): Promise<void> {
|
|
299
|
+
try {
|
|
300
|
+
const response = await fetch("api/assets")
|
|
301
|
+
const body = (await response.json()) as AssetRow[] | { error?: string }
|
|
302
|
+
if (!response.ok || !Array.isArray(body))
|
|
303
|
+
throw new Error((body as { error?: string }).error ?? `api/assets answered ${response.status}`)
|
|
304
|
+
rows = body
|
|
305
|
+
note.className = `${UI.notice} empty:hidden`
|
|
306
|
+
draw()
|
|
307
|
+
select(selected ?? rows[0]?.url ?? "")
|
|
308
|
+
} catch (error) {
|
|
309
|
+
note.className = `${UI.error} empty:hidden`
|
|
310
|
+
note.textContent = messageOf(error)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async function send(files: File[]): Promise<void> {
|
|
315
|
+
dropStatus.className = statusClass(false)
|
|
316
|
+
dropStatus.replaceChildren()
|
|
317
|
+
let last: string | undefined
|
|
318
|
+
for (const file of files) {
|
|
319
|
+
let { status, body } = await upload(file, false)
|
|
320
|
+
if (status === 409 && body.error?.endsWith("send overwrite=1 to replace it")) {
|
|
321
|
+
const replace = await new Promise<boolean>((resolve) => {
|
|
322
|
+
const yes = element("button", { type: "button", className: UI.button }, "Replace")
|
|
323
|
+
yes.dataset.replace = ""
|
|
324
|
+
const no = element("button", { type: "button", className: UI.button }, "Keep")
|
|
325
|
+
yes.addEventListener("click", () => resolve(true))
|
|
326
|
+
no.addEventListener("click", () => resolve(false))
|
|
327
|
+
dropStatus.replaceChildren(element("span", {}, `public/${file.name} exists`), yes, no)
|
|
328
|
+
})
|
|
329
|
+
dropStatus.replaceChildren()
|
|
330
|
+
if (!replace) continue
|
|
331
|
+
;({ status, body } = await upload(file, true))
|
|
332
|
+
}
|
|
333
|
+
if (status >= 400) {
|
|
334
|
+
dropStatus.className = statusClass(true)
|
|
335
|
+
dropStatus.append(element("span", {}, `${file.name}: ${body.error ?? `answered ${status}`}`))
|
|
336
|
+
continue
|
|
337
|
+
}
|
|
338
|
+
last = body.url
|
|
339
|
+
dropStatus.append(element("span", {}, `Wrote public/${file.name}`))
|
|
340
|
+
}
|
|
341
|
+
if (last) selected = last
|
|
342
|
+
await load()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
let depth = 0
|
|
346
|
+
root.addEventListener("dragenter", (event) => {
|
|
347
|
+
event.preventDefault()
|
|
348
|
+
depth++
|
|
349
|
+
drop.dataset.over = ""
|
|
350
|
+
drop.className = dropClass(true)
|
|
351
|
+
})
|
|
352
|
+
root.addEventListener("dragleave", () => {
|
|
353
|
+
if (--depth <= 0) {
|
|
354
|
+
delete drop.dataset.over
|
|
355
|
+
drop.className = dropClass(false)
|
|
356
|
+
}
|
|
357
|
+
})
|
|
358
|
+
root.addEventListener("dragover", (event) => event.preventDefault())
|
|
359
|
+
root.addEventListener("drop", (event) => {
|
|
360
|
+
event.preventDefault()
|
|
361
|
+
depth = 0
|
|
362
|
+
delete drop.dataset.over
|
|
363
|
+
drop.className = dropClass(false)
|
|
364
|
+
const files = Array.from(event.dataTransfer?.files ?? [])
|
|
365
|
+
if (files.length) void send(files)
|
|
366
|
+
})
|
|
367
|
+
picker.addEventListener("change", () => {
|
|
368
|
+
const files = Array.from(picker.files ?? [])
|
|
369
|
+
picker.value = ""
|
|
370
|
+
if (files.length) void send(files)
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
await load()
|
|
374
|
+
},
|
|
375
|
+
})
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { parseLocation, type SourceLocation } from "../locator.ts"
|
|
2
|
+
|
|
3
|
+
export type AuditSeverity = "error" | "warning"
|
|
4
|
+
|
|
5
|
+
/** One element of the page as the rules read it; `ref` is its index in the list the snapshot was made from. */
|
|
6
|
+
export interface AuditElement {
|
|
7
|
+
ref: number
|
|
8
|
+
tag: string
|
|
9
|
+
attrs: Record<string, string>
|
|
10
|
+
/** The accessible name: `aria-label`, `aria-labelledby`, the text, an inner image's `alt`, or `title`. */
|
|
11
|
+
name: string
|
|
12
|
+
/** Whether a `<label>`, `aria-label`, `aria-labelledby` or `title` names a form control. */
|
|
13
|
+
labelled: boolean
|
|
14
|
+
/** Distance from the top of the document, in CSS pixels. */
|
|
15
|
+
top: number
|
|
16
|
+
width: number
|
|
17
|
+
height: number
|
|
18
|
+
naturalWidth: number
|
|
19
|
+
naturalHeight: number
|
|
20
|
+
/** `data-hamur-src` of the nearest element that has one. */
|
|
21
|
+
source?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AuditSnapshot {
|
|
25
|
+
viewportHeight: number
|
|
26
|
+
elements: AuditElement[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface AuditFinding {
|
|
30
|
+
rule: string
|
|
31
|
+
severity: AuditSeverity
|
|
32
|
+
title: string
|
|
33
|
+
hint: string
|
|
34
|
+
ref: number
|
|
35
|
+
snippet: string
|
|
36
|
+
source?: SourceLocation
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const SNIPPET_LIMIT = 120
|
|
40
|
+
const CONTROLS_WITHOUT_LABEL = new Set(["hidden", "submit", "reset", "button", "image"])
|
|
41
|
+
|
|
42
|
+
/** The opening tag of an element as the finding shows it, cut at 120 characters. */
|
|
43
|
+
export function snippetOf(element: Pick<AuditElement, "tag" | "attrs">): string {
|
|
44
|
+
const attrs = Object.entries(element.attrs)
|
|
45
|
+
.map(([name, value]) => (value === "" ? ` ${name}` : ` ${name}="${value}"`))
|
|
46
|
+
.join("")
|
|
47
|
+
const text = `<${element.tag}${attrs}>`
|
|
48
|
+
return text.length > SNIPPET_LIMIT ? `${text.slice(0, SNIPPET_LIMIT - 2)}…>` : text
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const has = (element: AuditElement, name: string): boolean => Object.hasOwn(element.attrs, name)
|
|
52
|
+
|
|
53
|
+
interface Rule {
|
|
54
|
+
id: string
|
|
55
|
+
severity: AuditSeverity
|
|
56
|
+
title: string
|
|
57
|
+
hint: (element: AuditElement) => string
|
|
58
|
+
matches(element: AuditElement, snapshot: AuditSnapshot): boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const isHeading = (element: AuditElement): boolean => /^h[1-6]$/.test(element.tag)
|
|
62
|
+
|
|
63
|
+
const RULES: Rule[] = [
|
|
64
|
+
{
|
|
65
|
+
id: "html-lang",
|
|
66
|
+
severity: "error",
|
|
67
|
+
title: "Page without a language",
|
|
68
|
+
hint: () => 'Set lang on <html>, as in <html lang="en">.',
|
|
69
|
+
matches: (element) => element.tag === "html" && !element.attrs.lang?.trim(),
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "img-alt",
|
|
73
|
+
severity: "error",
|
|
74
|
+
title: "Image without alt text",
|
|
75
|
+
hint: () => 'Add alt="", or a description if the image carries meaning.',
|
|
76
|
+
matches: (element) => element.tag === "img" && !has(element, "alt"),
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "img-size",
|
|
80
|
+
severity: "warning",
|
|
81
|
+
title: "Image without width and height",
|
|
82
|
+
hint: () => "Set width and height so the page does not shift while the image loads.",
|
|
83
|
+
matches: (element) => element.tag === "img" && (!has(element, "width") || !has(element, "height")),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: "img-lazy-above-fold",
|
|
87
|
+
severity: "warning",
|
|
88
|
+
title: "Lazy image above the fold",
|
|
89
|
+
hint: () => 'Drop loading="lazy" on images visible at load.',
|
|
90
|
+
matches: (element, snapshot) =>
|
|
91
|
+
element.tag === "img" && element.attrs.loading === "lazy" && element.top < snapshot.viewportHeight,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
id: "img-oversized",
|
|
95
|
+
severity: "warning",
|
|
96
|
+
title: "Image larger than it is shown",
|
|
97
|
+
hint: (element) =>
|
|
98
|
+
`The file is ${element.naturalWidth}×${element.naturalHeight} and is shown at ${Math.round(element.width)}×${Math.round(element.height)}; serve a smaller file or a srcset.`,
|
|
99
|
+
matches: (element) =>
|
|
100
|
+
element.tag === "img" &&
|
|
101
|
+
element.width > 0 &&
|
|
102
|
+
element.naturalWidth > 0 &&
|
|
103
|
+
element.width * 2 < element.naturalWidth &&
|
|
104
|
+
element.height * 2 < element.naturalHeight,
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: "link-name",
|
|
108
|
+
severity: "error",
|
|
109
|
+
title: "Link without a name",
|
|
110
|
+
hint: () => "Give the link text, or aria-label when it holds only an icon.",
|
|
111
|
+
matches: (element) => element.tag === "a" && has(element, "href") && element.name === "",
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
id: "button-name",
|
|
115
|
+
severity: "error",
|
|
116
|
+
title: "Button without a name",
|
|
117
|
+
hint: () => "Give the button text, or aria-label when it holds only an icon.",
|
|
118
|
+
matches: (element) => (element.tag === "button" || element.attrs.role === "button") && element.name === "",
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: "input-label",
|
|
122
|
+
severity: "error",
|
|
123
|
+
title: "Input without a label",
|
|
124
|
+
hint: () => "Wrap it in a <label>, point a <label for> at its id, or set aria-label.",
|
|
125
|
+
matches: (element) =>
|
|
126
|
+
(element.tag === "input"
|
|
127
|
+
? !CONTROLS_WITHOUT_LABEL.has((element.attrs.type ?? "text").toLowerCase())
|
|
128
|
+
: element.tag === "select" || element.tag === "textarea") && !element.labelled,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: "tabindex-positive",
|
|
132
|
+
severity: "warning",
|
|
133
|
+
title: "tabindex above 0",
|
|
134
|
+
hint: () => 'Use tabindex="0" or "-1"; a positive value breaks the natural focus order.',
|
|
135
|
+
matches: (element) => Number.parseInt(element.attrs.tabindex ?? "", 10) > 0,
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
id: "media-autoplay",
|
|
139
|
+
severity: "warning",
|
|
140
|
+
title: "Media plays on its own",
|
|
141
|
+
hint: () => "Remove autoplay, or mute the media and let the visitor start the sound.",
|
|
142
|
+
matches: (element) =>
|
|
143
|
+
(element.tag === "video" || element.tag === "audio") && has(element, "autoplay") && !has(element, "muted"),
|
|
144
|
+
},
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
const level = (element: AuditElement): number => Number(element.tag.slice(1))
|
|
148
|
+
|
|
149
|
+
/** The findings of the Hamur-written rules on a snapshot of the page, in document order and then rule order. */
|
|
150
|
+
export function auditSnapshot(snapshot: AuditSnapshot): AuditFinding[] {
|
|
151
|
+
const found: Array<{ order: number; finding: AuditFinding }> = []
|
|
152
|
+
const add = (
|
|
153
|
+
element: AuditElement,
|
|
154
|
+
rule: number,
|
|
155
|
+
id: string,
|
|
156
|
+
severity: AuditSeverity,
|
|
157
|
+
title: string,
|
|
158
|
+
hint: string,
|
|
159
|
+
) => {
|
|
160
|
+
const source = parseLocation(element.source)
|
|
161
|
+
found.push({
|
|
162
|
+
order: element.ref * 100 + rule,
|
|
163
|
+
finding: {
|
|
164
|
+
rule: id,
|
|
165
|
+
severity,
|
|
166
|
+
title,
|
|
167
|
+
hint,
|
|
168
|
+
ref: element.ref,
|
|
169
|
+
snippet: snippetOf(element),
|
|
170
|
+
...(source ? { source } : {}),
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
for (const element of snapshot.elements) {
|
|
175
|
+
RULES.forEach((rule, index) => {
|
|
176
|
+
if (rule.matches(element, snapshot)) add(element, index, rule.id, rule.severity, rule.title, rule.hint(element))
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
const seen = new Set<string>()
|
|
180
|
+
for (const element of snapshot.elements) {
|
|
181
|
+
const id = element.attrs.id
|
|
182
|
+
if (id === undefined || id === "") continue
|
|
183
|
+
if (seen.has(id))
|
|
184
|
+
add(
|
|
185
|
+
element,
|
|
186
|
+
RULES.length,
|
|
187
|
+
"duplicate-id",
|
|
188
|
+
"error",
|
|
189
|
+
"Duplicate id",
|
|
190
|
+
`The id "${id}" is already used earlier on the page; ids must be unique.`,
|
|
191
|
+
)
|
|
192
|
+
seen.add(id)
|
|
193
|
+
}
|
|
194
|
+
let previous = 0
|
|
195
|
+
for (const element of snapshot.elements.filter(isHeading)) {
|
|
196
|
+
if (previous > 0 && level(element) > previous + 1)
|
|
197
|
+
add(
|
|
198
|
+
element,
|
|
199
|
+
RULES.length + 1,
|
|
200
|
+
"heading-skip",
|
|
201
|
+
"warning",
|
|
202
|
+
"Heading level skipped",
|
|
203
|
+
`Use an h${previous + 1} between h${previous} and h${level(element)}.`,
|
|
204
|
+
)
|
|
205
|
+
previous = level(element)
|
|
206
|
+
}
|
|
207
|
+
return found.sort((a, b) => a.order - b.order).map(({ finding }) => finding)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** How many findings of each severity there are. */
|
|
211
|
+
export function countBySeverity(findings: readonly AuditFinding[]): { error: number; warning: number } {
|
|
212
|
+
return {
|
|
213
|
+
error: findings.filter(({ severity }) => severity === "error").length,
|
|
214
|
+
warning: findings.filter(({ severity }) => severity === "warning").length,
|
|
215
|
+
}
|
|
216
|
+
}
|