astralis-devtools 0.0.0-stage → 0.1.0
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/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/package.json +66 -3
- package/src/bridge/bridge.mjs +169 -0
- package/src/bridge/github.mjs +139 -0
- package/src/bridge/http.mjs +194 -0
- package/src/bridge/node-adapter.mjs +68 -0
- package/src/bridge/project.mjs +103 -0
- package/src/bridge/snapshot.mjs +71 -0
- package/src/client/dom.mjs +82 -0
- package/src/client/fonts/OFL.txt +94 -0
- package/src/client/fonts.mjs +22 -0
- package/src/client/icons.mjs +44 -0
- package/src/client/identity.mjs +117 -0
- package/src/client/launcher.mjs +142 -0
- package/src/client/overlay.d.mts +9 -0
- package/src/client/overlay.mjs +525 -0
- package/src/client/page.mjs +92 -0
- package/src/client/picker.mjs +118 -0
- package/src/client/report.mjs +161 -0
- package/src/client/screens/component.mjs +151 -0
- package/src/client/screens/components.mjs +117 -0
- package/src/client/screens/findings.mjs +92 -0
- package/src/client/screens/home.mjs +100 -0
- package/src/client/screens/report.mjs +498 -0
- package/src/client/screens/shared.mjs +59 -0
- package/src/client/styles.mjs +512 -0
- package/src/next/devtools.d.mts +6 -0
- package/src/next/devtools.mjs +32 -0
- package/src/next/route.d.mts +10 -0
- package/src/next/route.mjs +17 -0
- package/src/vite.d.mts +12 -0
- package/src/vite.mjs +59 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { h, icon, store } from "./dom.mjs";
|
|
2
|
+
import { ICONS, MARK } from "./icons.mjs";
|
|
3
|
+
|
|
4
|
+
/* The launcher: the Astralis mark, which opens the panel, and a pick button.
|
|
5
|
+
It can be dragged anywhere on the screen and stays where it was left,
|
|
6
|
+
across reloads, kept as a fraction of the viewport so a resize keeps it in
|
|
7
|
+
the same corner. */
|
|
8
|
+
|
|
9
|
+
const MARGIN = 12;
|
|
10
|
+
/** How far the pointer must travel before a press becomes a drag, not a click. */
|
|
11
|
+
const DRAG_THRESHOLD = 4;
|
|
12
|
+
const DEFAULT_SPOT = { fx: 0.5, fy: 1 }; // bottom centre
|
|
13
|
+
|
|
14
|
+
export function createLauncher({ onToggle, onPick, onMove }) {
|
|
15
|
+
const dot = h("span", { class: "dot", hidden: true });
|
|
16
|
+
const markButton = h(
|
|
17
|
+
"button",
|
|
18
|
+
{ class: "launcher-button mark", type: "button", "aria-label": "Astralis DevTools", "aria-expanded": "false", title: "Astralis DevTools (Shift+Alt+A)" },
|
|
19
|
+
icon(MARK),
|
|
20
|
+
dot,
|
|
21
|
+
);
|
|
22
|
+
const pickButton = h(
|
|
23
|
+
"button",
|
|
24
|
+
{ class: "launcher-button", type: "button", "aria-label": "Pick a component", "aria-pressed": "false", title: "Pick a component (Shift+Alt+C)" },
|
|
25
|
+
icon(ICONS.pick),
|
|
26
|
+
);
|
|
27
|
+
const element = h("div", { class: "launcher", role: "toolbar", "aria-label": "Astralis DevTools" }, markButton, h("span", { class: "launcher-sep" }), pickButton);
|
|
28
|
+
|
|
29
|
+
let spot = { ...DEFAULT_SPOT, ...store.read("localStorage", "launcher") };
|
|
30
|
+
let position = { x: 0, y: 0 };
|
|
31
|
+
let suppressClick = false;
|
|
32
|
+
|
|
33
|
+
const size = () => ({ width: element.offsetWidth || 88, height: element.offsetHeight || 44 });
|
|
34
|
+
const room = () => {
|
|
35
|
+
const { width, height } = size();
|
|
36
|
+
return { w: Math.max(0, window.innerWidth - width - MARGIN * 2), h: Math.max(0, window.innerHeight - height - MARGIN * 2) };
|
|
37
|
+
};
|
|
38
|
+
const clamp = (value) => Math.min(1, Math.max(0, value));
|
|
39
|
+
|
|
40
|
+
function place(x, y) {
|
|
41
|
+
position = { x, y };
|
|
42
|
+
element.style.transform = `translate(${Math.round(x)}px, ${Math.round(y)}px)`;
|
|
43
|
+
onMove?.();
|
|
44
|
+
}
|
|
45
|
+
function placeFromSpot() {
|
|
46
|
+
const { w, h: height } = room();
|
|
47
|
+
place(MARGIN + spot.fx * w, MARGIN + spot.fy * height);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/* ---- dragging -------------------------------------------------------------- */
|
|
51
|
+
|
|
52
|
+
let press = null;
|
|
53
|
+
// The move and release are followed on the window: a quick drag leaves the launcher before it moves.
|
|
54
|
+
function onPointerDown(event) {
|
|
55
|
+
if (event.button !== 0) return;
|
|
56
|
+
press = { id: event.pointerId, startX: event.clientX, startY: event.clientY, x: position.x, y: position.y, dragging: false };
|
|
57
|
+
window.addEventListener("pointermove", onPointerMove, true);
|
|
58
|
+
window.addEventListener("pointerup", onPointerUp, true);
|
|
59
|
+
window.addEventListener("pointercancel", onPointerUp, true);
|
|
60
|
+
}
|
|
61
|
+
function onPointerMove(event) {
|
|
62
|
+
if (!press || event.pointerId !== press.id) return;
|
|
63
|
+
const dx = event.clientX - press.startX;
|
|
64
|
+
const dy = event.clientY - press.startY;
|
|
65
|
+
if (!press.dragging) {
|
|
66
|
+
if (Math.hypot(dx, dy) < DRAG_THRESHOLD) return;
|
|
67
|
+
press.dragging = true;
|
|
68
|
+
element.classList.add("dragging");
|
|
69
|
+
}
|
|
70
|
+
const { width, height } = size();
|
|
71
|
+
place(
|
|
72
|
+
Math.min(window.innerWidth - width - MARGIN, Math.max(MARGIN, press.x + dx)),
|
|
73
|
+
Math.min(window.innerHeight - height - MARGIN, Math.max(MARGIN, press.y + dy)),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function onPointerUp(event) {
|
|
77
|
+
if (!press || event.pointerId !== press.id) return;
|
|
78
|
+
window.removeEventListener("pointermove", onPointerMove, true);
|
|
79
|
+
window.removeEventListener("pointerup", onPointerUp, true);
|
|
80
|
+
window.removeEventListener("pointercancel", onPointerUp, true);
|
|
81
|
+
if (press.dragging) {
|
|
82
|
+
// Only the click this release makes, if it lands on the launcher at all.
|
|
83
|
+
suppressClick = true;
|
|
84
|
+
setTimeout(() => (suppressClick = false), 0);
|
|
85
|
+
element.classList.remove("dragging");
|
|
86
|
+
const { w, h: height } = room();
|
|
87
|
+
spot = { fx: w ? clamp((position.x - MARGIN) / w) : 1, fy: height ? clamp((position.y - MARGIN) / height) : 1 };
|
|
88
|
+
store.write("localStorage", "launcher", spot);
|
|
89
|
+
}
|
|
90
|
+
press = null;
|
|
91
|
+
}
|
|
92
|
+
// The click that ends a drag is not a press of either button.
|
|
93
|
+
function onClickCapture(event) {
|
|
94
|
+
if (!suppressClick) return;
|
|
95
|
+
suppressClick = false;
|
|
96
|
+
event.stopPropagation();
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
element.addEventListener("pointerdown", onPointerDown);
|
|
101
|
+
element.addEventListener("click", onClickCapture, true);
|
|
102
|
+
markButton.addEventListener("click", () => onToggle());
|
|
103
|
+
pickButton.addEventListener("click", () => onPick());
|
|
104
|
+
|
|
105
|
+
// Double-clicking the grip area sends the launcher home.
|
|
106
|
+
element.addEventListener("dblclick", (event) => {
|
|
107
|
+
if (event.target !== element) return;
|
|
108
|
+
spot = { ...DEFAULT_SPOT };
|
|
109
|
+
store.write("localStorage", "launcher", spot);
|
|
110
|
+
element.classList.add("settling");
|
|
111
|
+
placeFromSpot();
|
|
112
|
+
setTimeout(() => element.classList.remove("settling"), 400);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
element,
|
|
117
|
+
/** Lay it out once it is in the document, and again whenever the viewport changes. */
|
|
118
|
+
layout: placeFromSpot,
|
|
119
|
+
rect: () => {
|
|
120
|
+
const { width, height } = size();
|
|
121
|
+
return { left: position.x, top: position.y, right: position.x + width, bottom: position.y + height, width, height };
|
|
122
|
+
},
|
|
123
|
+
setOpen(open) {
|
|
124
|
+
element.dataset.open = String(open);
|
|
125
|
+
markButton.setAttribute("aria-expanded", String(open));
|
|
126
|
+
},
|
|
127
|
+
setPicking(picking) {
|
|
128
|
+
pickButton.setAttribute("aria-pressed", String(picking));
|
|
129
|
+
},
|
|
130
|
+
setCounts({ errors, warnings }) {
|
|
131
|
+
dot.hidden = errors + warnings === 0;
|
|
132
|
+
dot.className = errors > 0 ? "dot" : "dot advice";
|
|
133
|
+
dot.textContent = String(errors > 0 ? errors : warnings);
|
|
134
|
+
markButton.setAttribute(
|
|
135
|
+
"aria-label",
|
|
136
|
+
errors + warnings > 0
|
|
137
|
+
? `Astralis DevTools: ${errors} ${errors === 1 ? "error" : "errors"}, ${warnings} ${warnings === 1 ? "recommendation" : "recommendations"}`
|
|
138
|
+
: "Astralis DevTools",
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface MountOptions {
|
|
2
|
+
/** Where the bridge is served. Default "/api/astralis-devtools". */
|
|
3
|
+
base?: string;
|
|
4
|
+
/** Which framework's route hints to show. */
|
|
5
|
+
framework?: "next" | "vite" | null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Mount the overlay on this page. Returns a function that removes it. */
|
|
9
|
+
export function mountOverlay(options?: MountOptions): () => void;
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { animate, h, icon, store } from "./dom.mjs";
|
|
2
|
+
import { FONT_FACES } from "./fonts.mjs";
|
|
3
|
+
import { ICONS, MARK } from "./icons.mjs";
|
|
4
|
+
import { createLauncher } from "./launcher.mjs";
|
|
5
|
+
import { scanPage } from "./page.mjs";
|
|
6
|
+
import { createPicker } from "./picker.mjs";
|
|
7
|
+
import { recordConsoleErrors } from "./report.mjs";
|
|
8
|
+
import { componentScreen } from "./screens/component.mjs";
|
|
9
|
+
import { componentsScreen } from "./screens/components.mjs";
|
|
10
|
+
import { findingsScreen } from "./screens/findings.mjs";
|
|
11
|
+
import { homeScreen } from "./screens/home.mjs";
|
|
12
|
+
import { reportScreen } from "./screens/report.mjs";
|
|
13
|
+
import { STYLES } from "./styles.mjs";
|
|
14
|
+
|
|
15
|
+
/* The Astralis DevTools overlay: a launcher that can sit anywhere on the
|
|
16
|
+
page in development, and a panel with five screens.
|
|
17
|
+
|
|
18
|
+
Home what is on this page, at a glance
|
|
19
|
+
Components every astralis-ui component on the page; each opens a
|
|
20
|
+
page of its own with its props, how it reaches the
|
|
21
|
+
browser, and the findings about it
|
|
22
|
+
Findings the validator's errors, live: what renders wrong
|
|
23
|
+
Recommendations the validator's warnings: what works but could be better
|
|
24
|
+
Report a problem opened as a GitHub issue from the reporter's own
|
|
25
|
+
account, shown in full before anything is sent, and the
|
|
26
|
+
issues they have opened so far
|
|
27
|
+
|
|
28
|
+
Shift+Alt+A opens and closes it. Shift+Alt+C picks a component on the page
|
|
29
|
+
and opens its page. Plain DOM in a shadow root, so it needs no framework and
|
|
30
|
+
the page's CSS cannot reach it. Everything from the page or the bridge goes
|
|
31
|
+
in as text. */
|
|
32
|
+
|
|
33
|
+
const HOST_TAG = "astralis-devtools";
|
|
34
|
+
const FONTS_ID = "astralis-devtools-fonts";
|
|
35
|
+
const ROUTE_POLL_MS = 1000;
|
|
36
|
+
const SCAN_DEBOUNCE_MS = 200;
|
|
37
|
+
const PANEL_GAP = 10;
|
|
38
|
+
/** How often a waiting sign-in is checked. The bridge polls GitHub; this asks the bridge. */
|
|
39
|
+
const SIGN_IN_POLL_MS = 2000;
|
|
40
|
+
const EDGE = 12;
|
|
41
|
+
|
|
42
|
+
const NAV = [
|
|
43
|
+
{ screen: "home", label: "Home", icon: ICONS.home },
|
|
44
|
+
{ screen: "components", label: "Components", icon: ICONS.components },
|
|
45
|
+
{ screen: "findings", label: "Findings", icon: ICONS.findings },
|
|
46
|
+
{ screen: "recommendations", label: "Recommendations", icon: ICONS.recommendations },
|
|
47
|
+
{ screen: "report", label: "Report", icon: ICONS.report },
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
const SHORTCUTS = { open: "KeyA", pick: "KeyC" };
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Mount the overlay on this page. Returns a function that removes it and
|
|
54
|
+
* everything it listens to. Mounting twice is a no-op.
|
|
55
|
+
*/
|
|
56
|
+
export function mountOverlay({ base = "/api/astralis-devtools", framework = null } = {}) {
|
|
57
|
+
if (typeof document === "undefined" || document.querySelector(HOST_TAG)) return () => {};
|
|
58
|
+
|
|
59
|
+
const host = document.createElement(HOST_TAG);
|
|
60
|
+
const shadow = host.attachShadow({ mode: "open" });
|
|
61
|
+
shadow.append(h("style", {}, STYLES));
|
|
62
|
+
|
|
63
|
+
// @font-face is ignored inside a shadow root, so the faces go on the document, under names no page uses.
|
|
64
|
+
let fonts = document.getElementById(FONTS_ID);
|
|
65
|
+
const ownFonts = !fonts;
|
|
66
|
+
if (ownFonts) {
|
|
67
|
+
fonts = h("style", { id: FONTS_ID }, FONT_FACES);
|
|
68
|
+
document.head.append(fonts);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const remembered = store.read("sessionStorage", "panel") ?? {};
|
|
72
|
+
const state = {
|
|
73
|
+
open: false,
|
|
74
|
+
screen: NAV.some((n) => n.screen === remembered.screen) ? remembered.screen : "home",
|
|
75
|
+
selected: null,
|
|
76
|
+
spec: null,
|
|
77
|
+
info: null,
|
|
78
|
+
findings: null,
|
|
79
|
+
bridge: "connecting", // connecting | live | unreachable
|
|
80
|
+
route: location.pathname,
|
|
81
|
+
routeFiles: [],
|
|
82
|
+
instances: [],
|
|
83
|
+
github: null, // the bridge's GitHub status: { configured, repo, login, pending, error }
|
|
84
|
+
issues: { list: null, loading: false, error: null },
|
|
85
|
+
reportTab: "new",
|
|
86
|
+
report: {
|
|
87
|
+
title: "",
|
|
88
|
+
happened: "",
|
|
89
|
+
expected: "",
|
|
90
|
+
includeText: false,
|
|
91
|
+
includeFindings: true,
|
|
92
|
+
includeConsole: false,
|
|
93
|
+
screenshot: null,
|
|
94
|
+
snapshot: null,
|
|
95
|
+
problems: [],
|
|
96
|
+
status: "idle", // idle | sending | sent | failed | notice
|
|
97
|
+
message: "",
|
|
98
|
+
url: null,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const cleanups = [];
|
|
103
|
+
const listen = (target, type, handler, options) => {
|
|
104
|
+
target.addEventListener(type, handler, options);
|
|
105
|
+
cleanups.push(() => target.removeEventListener(type, handler, options));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/* ---- the panel's frame ------------------------------------------------------ */
|
|
109
|
+
|
|
110
|
+
const indicator = h("span", { class: "nav-indicator" });
|
|
111
|
+
const navButtons = new Map();
|
|
112
|
+
for (const item of NAV) {
|
|
113
|
+
navButtons.set(
|
|
114
|
+
item.screen,
|
|
115
|
+
h(
|
|
116
|
+
"button",
|
|
117
|
+
{ class: "nav-button", type: "button", "data-screen": item.screen, "aria-label": item.label, onclick: () => navigate(item.screen) },
|
|
118
|
+
icon(item.icon),
|
|
119
|
+
h("span", { class: "count", hidden: true }),
|
|
120
|
+
h("span", { class: "tip", "aria-hidden": "true" }, item.label),
|
|
121
|
+
),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const brand = h("button", { class: "brand", type: "button", "aria-label": "Home", onclick: () => navigate("home") }, icon(MARK));
|
|
125
|
+
const sidebarPick = h(
|
|
126
|
+
"button",
|
|
127
|
+
{ class: "nav-button", type: "button", "aria-label": "Pick a component", "aria-pressed": "false", onclick: () => startPicking() },
|
|
128
|
+
icon(ICONS.pick),
|
|
129
|
+
h("span", { class: "tip", "aria-hidden": "true" }, "Pick a component"),
|
|
130
|
+
);
|
|
131
|
+
const sidebar = h(
|
|
132
|
+
"nav",
|
|
133
|
+
{ class: "sidebar", "aria-label": "DevTools" },
|
|
134
|
+
brand,
|
|
135
|
+
h("div", { class: "nav" }, indicator, [...navButtons.values()]),
|
|
136
|
+
h("div", { class: "sidebar-end" }, sidebarPick),
|
|
137
|
+
);
|
|
138
|
+
const crumbs = h("div", { class: "crumbs" });
|
|
139
|
+
const statusSlot = h("span", {});
|
|
140
|
+
const closeButton = h("button", { class: "icon-button close", type: "button", "aria-label": "Close", title: "Close (Shift+Alt+A)", onclick: () => setOpen(false) }, icon(ICONS.close));
|
|
141
|
+
const view = h("div", { class: "view" });
|
|
142
|
+
const panel = h(
|
|
143
|
+
"section",
|
|
144
|
+
{ class: "panel", role: "dialog", "aria-label": "Astralis DevTools", "data-open": "false", inert: true },
|
|
145
|
+
sidebar,
|
|
146
|
+
h("div", { class: "main" }, h("header", { class: "topbar" }, crumbs, statusSlot, closeButton), view),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const launcher = createLauncher({
|
|
150
|
+
onToggle: () => setOpen(!state.open),
|
|
151
|
+
onPick: () => (picker.picking ? picker.stop() : startPicking()),
|
|
152
|
+
onMove: () => state.open && placePanel(),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const picker = createPicker({
|
|
156
|
+
host,
|
|
157
|
+
shadow,
|
|
158
|
+
onPicked: (element) => {
|
|
159
|
+
state.selected = element;
|
|
160
|
+
const back = pickReturn;
|
|
161
|
+
pickReturn = null;
|
|
162
|
+
navigate(back ?? "component", { open: true });
|
|
163
|
+
},
|
|
164
|
+
onChange: (picking) => {
|
|
165
|
+
// The panel steps aside while picking, so the whole page can be pointed at.
|
|
166
|
+
showPanel(state.open && !picking);
|
|
167
|
+
launcher.setPicking(picking);
|
|
168
|
+
sidebarPick.setAttribute("aria-pressed", String(picking));
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
let pickReturn = null; // the screen to go back to after a pick, set by every start
|
|
172
|
+
|
|
173
|
+
shadow.append(panel, launcher.element);
|
|
174
|
+
document.body.append(host);
|
|
175
|
+
launcher.layout();
|
|
176
|
+
|
|
177
|
+
/* ---- what the screens can do ---------------------------------------------------- */
|
|
178
|
+
|
|
179
|
+
const consoleErrors = recordConsoleErrors(window);
|
|
180
|
+
cleanups.push(() => consoleErrors.stop());
|
|
181
|
+
|
|
182
|
+
const app = {
|
|
183
|
+
state,
|
|
184
|
+
base,
|
|
185
|
+
framework,
|
|
186
|
+
host,
|
|
187
|
+
consoleErrors,
|
|
188
|
+
navigate: (screen) => navigate(screen),
|
|
189
|
+
openComponent(element) {
|
|
190
|
+
state.selected = element;
|
|
191
|
+
navigate("component", { open: true });
|
|
192
|
+
},
|
|
193
|
+
startPicking: (returnTo) => startPicking(returnTo),
|
|
194
|
+
/** Outline an element on the page while the reader points at its row; null restores the default. */
|
|
195
|
+
preview: (element) => picker.show(element ?? defaultOutline()),
|
|
196
|
+
locate: (element) => picker.locate(element),
|
|
197
|
+
/** The instance on the page a finding is most likely about, if the page has one. */
|
|
198
|
+
instanceFor(finding) {
|
|
199
|
+
const same = state.instances.filter((i) => i.chain.some((c) => c.name === finding.component));
|
|
200
|
+
const exact = same.find((i) => {
|
|
201
|
+
const props = i.chain.find((c) => c.name === finding.component)?.props ?? {};
|
|
202
|
+
return finding.prop !== undefined && finding.prop in props && String(props[finding.prop]) === String(finding.value);
|
|
203
|
+
});
|
|
204
|
+
return exact ?? same[0] ?? null;
|
|
205
|
+
},
|
|
206
|
+
github: {
|
|
207
|
+
reload: () => loadGitHub(),
|
|
208
|
+
signIn: () => postGitHub("github/sign-in"),
|
|
209
|
+
signOut: () => postGitHub("github/sign-out"),
|
|
210
|
+
loadIssues: () => loadIssues(),
|
|
211
|
+
},
|
|
212
|
+
statusPill() {
|
|
213
|
+
const label = { live: "Validator live", connecting: "Connecting…", unreachable: "Bridge offline" }[state.bridge];
|
|
214
|
+
return h("span", { class: `status ${state.bridge}`, title: `DevTools bridge at ${base}` }, h("i"), label);
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/* ---- screens -------------------------------------------------------------------- */
|
|
219
|
+
|
|
220
|
+
let current = null; // { screen, title, root, update, dispose?, element? }
|
|
221
|
+
|
|
222
|
+
function build(screen) {
|
|
223
|
+
switch (screen) {
|
|
224
|
+
case "components":
|
|
225
|
+
return componentsScreen(app);
|
|
226
|
+
case "findings":
|
|
227
|
+
case "recommendations":
|
|
228
|
+
return findingsScreen(app, screen);
|
|
229
|
+
case "report":
|
|
230
|
+
return reportScreen(app);
|
|
231
|
+
case "component":
|
|
232
|
+
return state.selected ? componentScreen(app, state.selected) : componentsScreen(app);
|
|
233
|
+
default:
|
|
234
|
+
return homeScreen(app);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function navigate(screen, { open = false } = {}) {
|
|
239
|
+
if (screen === "component" && !state.selected) screen = "components";
|
|
240
|
+
current?.dispose?.();
|
|
241
|
+
state.screen = screen;
|
|
242
|
+
current = { screen, ...build(screen) };
|
|
243
|
+
view.replaceChildren(current.root);
|
|
244
|
+
view.scrollTop = 0;
|
|
245
|
+
animate(current.root, [{ opacity: 0, transform: "translateY(6px)" }, { opacity: 1, transform: "none" }], { duration: 240, easing: "cubic-bezier(0.22, 1, 0.36, 1)" });
|
|
246
|
+
renderFrame();
|
|
247
|
+
remember();
|
|
248
|
+
if (open && !state.open) setOpen(true);
|
|
249
|
+
picker.show(defaultOutline());
|
|
250
|
+
if (state.open) current.focus?.();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The selection stays outlined while its page is open. */
|
|
254
|
+
const defaultOutline = () => (state.open && current?.screen === "component" ? current.element : null);
|
|
255
|
+
|
|
256
|
+
function renderFrame() {
|
|
257
|
+
const active = state.screen === "component" ? "components" : state.screen;
|
|
258
|
+
const index = NAV.findIndex((n) => n.screen === active);
|
|
259
|
+
for (const [screen, button] of navButtons) {
|
|
260
|
+
if (screen === active) button.setAttribute("aria-current", "page");
|
|
261
|
+
else button.removeAttribute("aria-current");
|
|
262
|
+
}
|
|
263
|
+
indicator.style.transform = `translateY(${index * 44}px)`;
|
|
264
|
+
|
|
265
|
+
const findings = state.findings && !state.findings.problem ? state.findings : null;
|
|
266
|
+
const errors = findings?.errors ?? 0;
|
|
267
|
+
const warnings = findings?.warnings ?? 0;
|
|
268
|
+
const setCount = (screen, n, tone) => {
|
|
269
|
+
const badge = navButtons.get(screen).querySelector(".count");
|
|
270
|
+
badge.hidden = n === 0;
|
|
271
|
+
badge.className = `count ${tone}`;
|
|
272
|
+
badge.textContent = n > 99 ? "99+" : String(n);
|
|
273
|
+
};
|
|
274
|
+
setCount("findings", errors, "");
|
|
275
|
+
setCount("recommendations", warnings, "advice");
|
|
276
|
+
launcher.setCounts({ errors, warnings });
|
|
277
|
+
|
|
278
|
+
crumbs.replaceChildren(
|
|
279
|
+
...(current?.title ?? []).flatMap((part, i, all) => {
|
|
280
|
+
const last = i === all.length - 1;
|
|
281
|
+
const node = last
|
|
282
|
+
? h("span", { class: `crumb-current${part.code ? " code" : ""}` }, part.label)
|
|
283
|
+
: h("button", { class: "crumb-link", type: "button", onclick: () => navigate(part.screen) }, part.label);
|
|
284
|
+
return last ? [node] : [node, icon(ICONS.chevron)];
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
statusSlot.replaceChildren(app.statusPill());
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Data changed: redraw the frame and let the screen bring itself up to date. */
|
|
291
|
+
function refresh(reason) {
|
|
292
|
+
renderFrame();
|
|
293
|
+
current?.update(reason);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function remember() {
|
|
297
|
+
store.write("sessionStorage", "panel", { open: state.open, screen: state.screen === "component" ? "components" : state.screen });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* ---- opening, and where the panel sits ------------------------------------------ */
|
|
301
|
+
|
|
302
|
+
/** Above or below the launcher, centred on it near the middle, else on the side with room; always on screen. */
|
|
303
|
+
function placePanel() {
|
|
304
|
+
const anchor = launcher.rect();
|
|
305
|
+
const width = panel.offsetWidth || Math.min(640, window.innerWidth - 24);
|
|
306
|
+
const height = panel.offsetHeight || Math.min(560, window.innerHeight - 24);
|
|
307
|
+
const centre = anchor.left + anchor.width / 2;
|
|
308
|
+
const middle = Math.abs(centre - window.innerWidth / 2) < window.innerWidth * 0.15;
|
|
309
|
+
const right = centre > window.innerWidth / 2;
|
|
310
|
+
const below = anchor.top + anchor.height / 2 < window.innerHeight / 2;
|
|
311
|
+
let x = middle ? centre - width / 2 : right ? anchor.right - width : anchor.left;
|
|
312
|
+
let y = below ? anchor.bottom + PANEL_GAP : anchor.top - PANEL_GAP - height;
|
|
313
|
+
x = Math.min(window.innerWidth - width - EDGE, Math.max(EDGE, x));
|
|
314
|
+
y = Math.min(window.innerHeight - height - EDGE, Math.max(EDGE, y));
|
|
315
|
+
panel.style.left = `${Math.round(x)}px`;
|
|
316
|
+
panel.style.top = `${Math.round(y)}px`;
|
|
317
|
+
panel.style.transformOrigin = `${middle ? "center" : right ? "right" : "left"} ${below ? "top" : "bottom"}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function showPanel(visible) {
|
|
321
|
+
panel.dataset.open = String(visible);
|
|
322
|
+
panel.toggleAttribute("inert", !visible);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function setOpen(open) {
|
|
326
|
+
state.open = open;
|
|
327
|
+
if (open) placePanel();
|
|
328
|
+
showPanel(open && !picker.picking);
|
|
329
|
+
launcher.setOpen(open);
|
|
330
|
+
picker.show(defaultOutline());
|
|
331
|
+
remember();
|
|
332
|
+
if (open) current?.focus?.();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function startPicking(returnTo = null) {
|
|
336
|
+
pickReturn = returnTo;
|
|
337
|
+
picker.start();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/* ---- keyboard ---------------------------------------------------------------------- */
|
|
341
|
+
|
|
342
|
+
function onKey(event) {
|
|
343
|
+
if (event.shiftKey && event.altKey && !event.ctrlKey && !event.metaKey) {
|
|
344
|
+
if (event.code === SHORTCUTS.open) {
|
|
345
|
+
event.preventDefault();
|
|
346
|
+
if (picker.picking) picker.stop();
|
|
347
|
+
if (state.open) setOpen(false);
|
|
348
|
+
else {
|
|
349
|
+
if (state.screen !== "home") navigate("home");
|
|
350
|
+
setOpen(true);
|
|
351
|
+
}
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (event.code === SHORTCUTS.pick) {
|
|
355
|
+
event.preventDefault();
|
|
356
|
+
if (picker.picking) picker.stop();
|
|
357
|
+
else startPicking();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (event.key === "Escape") {
|
|
362
|
+
if (picker.picking) {
|
|
363
|
+
event.preventDefault();
|
|
364
|
+
picker.stop();
|
|
365
|
+
} else if (state.open && event.composedPath?.().includes(host)) {
|
|
366
|
+
event.preventDefault();
|
|
367
|
+
setOpen(false);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
listen(document, "keydown", onKey, true);
|
|
372
|
+
|
|
373
|
+
/* ---- the page --------------------------------------------------------------------- */
|
|
374
|
+
|
|
375
|
+
let scanTimer = null;
|
|
376
|
+
function scan() {
|
|
377
|
+
state.instances = scanPage(document, host);
|
|
378
|
+
refresh("page");
|
|
379
|
+
}
|
|
380
|
+
const scheduleScan = () => {
|
|
381
|
+
clearTimeout(scanTimer);
|
|
382
|
+
scanTimer = setTimeout(scan, SCAN_DEBOUNCE_MS);
|
|
383
|
+
};
|
|
384
|
+
if (typeof MutationObserver !== "undefined") {
|
|
385
|
+
const observer = new MutationObserver((records) => {
|
|
386
|
+
if (records.every((r) => r.target === host || host.contains(r.target))) return;
|
|
387
|
+
scheduleScan();
|
|
388
|
+
});
|
|
389
|
+
observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ["data-astralis", "data-astralis-props"] });
|
|
390
|
+
cleanups.push(() => observer.disconnect());
|
|
391
|
+
}
|
|
392
|
+
cleanups.push(() => clearTimeout(scanTimer));
|
|
393
|
+
|
|
394
|
+
listen(window, "scroll", () => picker.refresh(), true);
|
|
395
|
+
listen(window, "resize", () => {
|
|
396
|
+
launcher.layout();
|
|
397
|
+
if (state.open) placePanel();
|
|
398
|
+
picker.refresh();
|
|
399
|
+
});
|
|
400
|
+
cleanups.push(() => picker.stop());
|
|
401
|
+
|
|
402
|
+
/* ---- the bridge ------------------------------------------------------------------ */
|
|
403
|
+
|
|
404
|
+
const getJson = (path) => fetch(`${base}/${path}`, { cache: "no-store" }).then((response) => (response.ok ? response.json() : null));
|
|
405
|
+
|
|
406
|
+
getJson("spec")
|
|
407
|
+
.then((spec) => {
|
|
408
|
+
if (spec?.components) {
|
|
409
|
+
state.spec = spec;
|
|
410
|
+
refresh("spec");
|
|
411
|
+
}
|
|
412
|
+
})
|
|
413
|
+
.catch(() => {});
|
|
414
|
+
getJson("info")
|
|
415
|
+
.then((info) => {
|
|
416
|
+
if (info && !info.problem) {
|
|
417
|
+
state.info = info;
|
|
418
|
+
refresh("info");
|
|
419
|
+
}
|
|
420
|
+
})
|
|
421
|
+
.catch(() => {});
|
|
422
|
+
|
|
423
|
+
if (typeof EventSource !== "undefined") {
|
|
424
|
+
const events = new EventSource(`${base}/events`);
|
|
425
|
+
events.addEventListener("findings", (event) => {
|
|
426
|
+
try {
|
|
427
|
+
state.findings = JSON.parse(event.data);
|
|
428
|
+
state.bridge = "live";
|
|
429
|
+
refresh("findings");
|
|
430
|
+
} catch {
|
|
431
|
+
// a malformed event changes nothing
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
events.addEventListener("error", () => {
|
|
435
|
+
state.bridge = "unreachable";
|
|
436
|
+
refresh("bridge");
|
|
437
|
+
});
|
|
438
|
+
cleanups.push(() => events.close());
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/* ---- GitHub -------------------------------------------------------------------------- */
|
|
442
|
+
|
|
443
|
+
let signInPoll = null;
|
|
444
|
+
cleanups.push(() => clearTimeout(signInPoll));
|
|
445
|
+
|
|
446
|
+
function setGitHub(status) {
|
|
447
|
+
const before = state.github?.login ?? null;
|
|
448
|
+
state.github = status;
|
|
449
|
+
if (status.login !== before) {
|
|
450
|
+
state.issues = { list: null, loading: false, error: null };
|
|
451
|
+
if (status.login) loadIssues();
|
|
452
|
+
}
|
|
453
|
+
clearTimeout(signInPoll);
|
|
454
|
+
if (status.pending) signInPoll = setTimeout(loadGitHub, SIGN_IN_POLL_MS);
|
|
455
|
+
refresh("github");
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function loadGitHub() {
|
|
459
|
+
return getJson("github")
|
|
460
|
+
.then((status) => status && setGitHub(status))
|
|
461
|
+
.catch(() => {});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
async function postGitHub(path) {
|
|
465
|
+
try {
|
|
466
|
+
const response = await fetch(`${base}/${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
|
|
467
|
+
const result = await response.json();
|
|
468
|
+
if (!response.ok) throw new Error(result?.error ?? `The bridge answered ${response.status}.`);
|
|
469
|
+
setGitHub(result);
|
|
470
|
+
} catch (error) {
|
|
471
|
+
setGitHub({ ...(state.github ?? { configured: true, repo: null, login: null, pending: null }), error: error.message });
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async function loadIssues() {
|
|
476
|
+
state.issues = { ...state.issues, loading: true, error: null };
|
|
477
|
+
refresh("github");
|
|
478
|
+
try {
|
|
479
|
+
const response = await fetch(`${base}/github/issues`, { cache: "no-store" });
|
|
480
|
+
const result = await response.json();
|
|
481
|
+
if (response.status === 401) return loadGitHub();
|
|
482
|
+
if (!response.ok) throw new Error(result?.error ?? `The bridge answered ${response.status}.`);
|
|
483
|
+
state.issues = { list: result.issues, loading: false, error: null };
|
|
484
|
+
} catch (error) {
|
|
485
|
+
state.issues = { ...state.issues, loading: false, error: error.message };
|
|
486
|
+
}
|
|
487
|
+
refresh("github");
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
loadGitHub();
|
|
491
|
+
|
|
492
|
+
function loadRouteFiles() {
|
|
493
|
+
if (framework !== "next") return;
|
|
494
|
+
getJson(`route-files?path=${encodeURIComponent(state.route)}`)
|
|
495
|
+
.then((result) => {
|
|
496
|
+
if (result?.path === state.route) {
|
|
497
|
+
state.routeFiles = result.files ?? [];
|
|
498
|
+
refresh("route");
|
|
499
|
+
}
|
|
500
|
+
})
|
|
501
|
+
.catch(() => {});
|
|
502
|
+
}
|
|
503
|
+
loadRouteFiles();
|
|
504
|
+
// Client-side navigation changes the URL without an event to hook reliably.
|
|
505
|
+
const routePoll = setInterval(() => {
|
|
506
|
+
if (location.pathname !== state.route) {
|
|
507
|
+
state.route = location.pathname;
|
|
508
|
+
state.routeFiles = [];
|
|
509
|
+
loadRouteFiles();
|
|
510
|
+
scan();
|
|
511
|
+
}
|
|
512
|
+
}, ROUTE_POLL_MS);
|
|
513
|
+
cleanups.push(() => clearInterval(routePoll));
|
|
514
|
+
|
|
515
|
+
state.instances = scanPage(document, host);
|
|
516
|
+
navigate(state.screen);
|
|
517
|
+
if (remembered.open) setOpen(true);
|
|
518
|
+
|
|
519
|
+
return () => {
|
|
520
|
+
current?.dispose?.();
|
|
521
|
+
for (const cleanup of cleanups.splice(0)) cleanup();
|
|
522
|
+
host.remove();
|
|
523
|
+
if (ownFonts) fonts.remove();
|
|
524
|
+
};
|
|
525
|
+
}
|