astralis-devtools 0.0.0-stage → 0.2.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 +71 -0
- package/LICENSE +21 -0
- package/README.md +86 -3
- 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 +70 -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 +43 -0
- package/src/client/identity.mjs +117 -0
- package/src/client/launcher.mjs +143 -0
- package/src/client/overlay.d.mts +9 -0
- package/src/client/overlay.mjs +529 -0
- package/src/client/page.mjs +92 -0
- package/src/client/picker.mjs +118 -0
- package/src/client/props.mjs +154 -0
- package/src/client/report.mjs +121 -0
- package/src/client/screens/component.mjs +158 -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/props.mjs +192 -0
- package/src/client/screens/report.mjs +459 -0
- package/src/client/screens/shared.mjs +59 -0
- package/src/client/styles.mjs +541 -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,92 @@
|
|
|
1
|
+
import { IDENTITY_ATTRIBUTE, readIdentity } from "./identity.mjs";
|
|
2
|
+
|
|
3
|
+
/* What is on the page, and which of the validator's findings belong to it.
|
|
4
|
+
Pure apart from reading the DOM it is given, so it runs in Node tests. */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Every astralis-ui component rendered on the page, in document order, each
|
|
8
|
+
* with its chain (outermost first). The overlay's own element is skipped.
|
|
9
|
+
*/
|
|
10
|
+
export function scanPage(root, skip = null) {
|
|
11
|
+
const instances = [];
|
|
12
|
+
for (const element of root.querySelectorAll(`[${IDENTITY_ATTRIBUTE}]`)) {
|
|
13
|
+
if (skip && (element === skip || skip.contains(element))) continue;
|
|
14
|
+
const chain = readIdentity(element);
|
|
15
|
+
if (chain.length) instances.push({ element, chain, name: chain[0].name });
|
|
16
|
+
}
|
|
17
|
+
return instances;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The instances grouped by the component they are, most used first, then by name. */
|
|
21
|
+
export function groupByComponent(instances) {
|
|
22
|
+
const groups = new Map();
|
|
23
|
+
for (const instance of instances) {
|
|
24
|
+
if (!groups.has(instance.name)) groups.set(instance.name, []);
|
|
25
|
+
groups.get(instance.name).push(instance);
|
|
26
|
+
}
|
|
27
|
+
return [...groups]
|
|
28
|
+
.map(([name, list]) => ({ name, instances: list }))
|
|
29
|
+
.sort((a, b) => b.instances.length - a.instances.length || a.name.localeCompare(b.name));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The findings, flattened, each with its file and severity. `files` limits
|
|
34
|
+
* them to the files that render the page, when those are known.
|
|
35
|
+
*/
|
|
36
|
+
export function flatFindings(findings, files = null) {
|
|
37
|
+
const out = [];
|
|
38
|
+
for (const result of findings?.results ?? []) {
|
|
39
|
+
if (files && !files.includes(result.file)) continue;
|
|
40
|
+
for (const finding of result.errors) out.push({ ...finding, file: result.file, kind: "error" });
|
|
41
|
+
for (const finding of result.warnings) out.push({ ...finding, file: result.file, kind: "warning" });
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The findings about this instance's component, from anywhere in the project.
|
|
48
|
+
* `exact`: certainly about this very instance, because the finding names an
|
|
49
|
+
* attribute and value this instance was given, or it is in a file that renders
|
|
50
|
+
* the page and this is the only one of its component there. `onPage`: in a
|
|
51
|
+
* file that renders the page. Exact ones first, then the page's, errors first.
|
|
52
|
+
*/
|
|
53
|
+
export function findingsFor(instance, findings, { routeFiles = [], alone = false } = {}) {
|
|
54
|
+
const names = new Set(instance.chain.map((c) => c.name));
|
|
55
|
+
return flatFindings(findings)
|
|
56
|
+
.filter((finding) => finding.component && names.has(finding.component))
|
|
57
|
+
.map((finding) => {
|
|
58
|
+
const props = instance.chain.find((c) => c.name === finding.component)?.props ?? {};
|
|
59
|
+
const onPage = routeFiles.includes(finding.file);
|
|
60
|
+
const given = finding.prop !== undefined && finding.value !== undefined && finding.prop in props && String(props[finding.prop]) === String(finding.value);
|
|
61
|
+
return { ...finding, onPage, exact: given || (alone && onPage) };
|
|
62
|
+
})
|
|
63
|
+
.sort(
|
|
64
|
+
(a, b) =>
|
|
65
|
+
Number(b.exact) - Number(a.exact) ||
|
|
66
|
+
Number(b.onPage) - Number(a.onPage) ||
|
|
67
|
+
(a.kind === b.kind ? 0 : a.kind === "error" ? -1 : 1),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The findings that are likely on a group's instances: exact for one of them, or in the page's files. */
|
|
72
|
+
export function findingsForGroup(group, findings, routeFiles = []) {
|
|
73
|
+
const seen = new Map();
|
|
74
|
+
for (const instance of group.instances) {
|
|
75
|
+
for (const finding of findingsFor(instance, findings, { routeFiles, alone: group.instances.length === 1 })) {
|
|
76
|
+
if (!finding.exact && !finding.onPage) continue;
|
|
77
|
+
seen.set(`${finding.file}:${finding.line}:${finding.column}:${finding.code}`, finding);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const list = [...seen.values()];
|
|
81
|
+
return { errors: list.filter((f) => f.kind === "error").length, warnings: list.filter((f) => f.kind === "warning").length };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A short, local description of one instance for a list: its text, else its first props. */
|
|
85
|
+
export function instanceLabel(instance) {
|
|
86
|
+
const text = (instance.element.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
87
|
+
const props = Object.entries(instance.chain[0].props)
|
|
88
|
+
.slice(0, 2)
|
|
89
|
+
.map(([key, value]) => `${key}=${typeof value === "string" ? `"${value}"` : JSON.stringify(value)}`)
|
|
90
|
+
.join(" ");
|
|
91
|
+
return { text: text.length > 42 ? `${text.slice(0, 40)}…` : text, props };
|
|
92
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { h, icon } from "./dom.mjs";
|
|
2
|
+
import { IDENTITY_ATTRIBUTE, readIdentity } from "./identity.mjs";
|
|
3
|
+
import { ICONS } from "./icons.mjs";
|
|
4
|
+
|
|
5
|
+
/* Picking a component on the page, and the outline that shows one. While
|
|
6
|
+
picking, the page never sees the pointer: no click, no navigation, no menu. */
|
|
7
|
+
|
|
8
|
+
export function createPicker({ host, shadow, onPicked, onChange }) {
|
|
9
|
+
const label = h("span", { class: "label" });
|
|
10
|
+
const highlight = h("div", { class: "highlight", hidden: true }, label);
|
|
11
|
+
const toast = h(
|
|
12
|
+
"div",
|
|
13
|
+
{ class: "toast", role: "status", hidden: true },
|
|
14
|
+
icon(ICONS.pick),
|
|
15
|
+
h("span", {}, "Click a component to inspect it"),
|
|
16
|
+
h("span", { class: "kbd" }, "Esc"),
|
|
17
|
+
);
|
|
18
|
+
shadow.append(highlight, toast);
|
|
19
|
+
|
|
20
|
+
let picking = false;
|
|
21
|
+
let hovered = null;
|
|
22
|
+
let shown = null; // the element outlined when not picking
|
|
23
|
+
|
|
24
|
+
const named = (target) => {
|
|
25
|
+
if (!(target instanceof Element) || target === host || host.contains(target)) return null;
|
|
26
|
+
return target.closest(`[${IDENTITY_ATTRIBUTE}]`);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function outline(element) {
|
|
30
|
+
if (!element?.isConnected) {
|
|
31
|
+
highlight.hidden = true;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const rect = element.getBoundingClientRect();
|
|
35
|
+
Object.assign(highlight.style, {
|
|
36
|
+
transform: `translate(${rect.left}px, ${rect.top}px)`,
|
|
37
|
+
width: `${rect.width}px`,
|
|
38
|
+
height: `${rect.height}px`,
|
|
39
|
+
});
|
|
40
|
+
highlight.classList.toggle("below", rect.top < 32);
|
|
41
|
+
label.replaceChildren(
|
|
42
|
+
h("span", {}, readIdentity(element).map((c) => c.name).join(" → ")),
|
|
43
|
+
h("span", { class: "size" }, `${Math.round(rect.width)}×${Math.round(rect.height)}`),
|
|
44
|
+
);
|
|
45
|
+
highlight.hidden = false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const refresh = () => outline(picking ? hovered : shown);
|
|
49
|
+
|
|
50
|
+
function onPointerMove(event) {
|
|
51
|
+
const next = named(event.target);
|
|
52
|
+
if (next === hovered) return;
|
|
53
|
+
hovered = next;
|
|
54
|
+
refresh();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function swallow(event) {
|
|
58
|
+
if (event.target === host) return;
|
|
59
|
+
event.preventDefault();
|
|
60
|
+
event.stopPropagation();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function onClick(event) {
|
|
64
|
+
if (event.target === host) return;
|
|
65
|
+
swallow(event);
|
|
66
|
+
const element = named(event.target);
|
|
67
|
+
stop();
|
|
68
|
+
if (element) onPicked(element);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function start() {
|
|
72
|
+
if (picking) return;
|
|
73
|
+
picking = true;
|
|
74
|
+
toast.hidden = false;
|
|
75
|
+
document.addEventListener("pointermove", onPointerMove, true);
|
|
76
|
+
document.addEventListener("pointerdown", swallow, true);
|
|
77
|
+
document.addEventListener("mousedown", swallow, true);
|
|
78
|
+
document.addEventListener("click", onClick, true);
|
|
79
|
+
onChange(true);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function stop() {
|
|
83
|
+
if (!picking) return;
|
|
84
|
+
picking = false;
|
|
85
|
+
hovered = null;
|
|
86
|
+
toast.hidden = true;
|
|
87
|
+
document.removeEventListener("pointermove", onPointerMove, true);
|
|
88
|
+
document.removeEventListener("pointerdown", swallow, true);
|
|
89
|
+
document.removeEventListener("mousedown", swallow, true);
|
|
90
|
+
document.removeEventListener("click", onClick, true);
|
|
91
|
+
refresh();
|
|
92
|
+
onChange(false);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
start,
|
|
97
|
+
stop,
|
|
98
|
+
get picking() {
|
|
99
|
+
return picking;
|
|
100
|
+
},
|
|
101
|
+
/** Outline an element while not picking (the open component, a hovered row); null hides it. */
|
|
102
|
+
show(element) {
|
|
103
|
+
shown = element;
|
|
104
|
+
refresh();
|
|
105
|
+
},
|
|
106
|
+
/** Scroll an element into view and pulse its outline. */
|
|
107
|
+
locate(element) {
|
|
108
|
+
if (!element?.isConnected) return;
|
|
109
|
+
element.scrollIntoView?.({ block: "center", behavior: "smooth" });
|
|
110
|
+
shown = element;
|
|
111
|
+
highlight.classList.remove("flash");
|
|
112
|
+
void highlight.offsetWidth;
|
|
113
|
+
highlight.classList.add("flash");
|
|
114
|
+
setTimeout(refresh, 350);
|
|
115
|
+
},
|
|
116
|
+
refresh,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { specEntry } from "astralis-cli/snapshot";
|
|
2
|
+
|
|
3
|
+
/* Every prop a component takes, read from the system spec: its own props
|
|
4
|
+
first, then its style props by group. Pure, so it runs in the overlay and
|
|
5
|
+
in Node tests alike. */
|
|
6
|
+
|
|
7
|
+
const GROUP_LABELS = {
|
|
8
|
+
box: { label: "Box", hint: "spacing, size, paint, border, layout" },
|
|
9
|
+
flex: { label: "Flex", hint: "direction, wrap, alignment, gap" },
|
|
10
|
+
flexItem: { label: "Flex item", hint: "grow, shrink, basis, order" },
|
|
11
|
+
grid: { label: "Grid", hint: "columns, rows, flow, gap" },
|
|
12
|
+
gridItem: { label: "Grid item", hint: "spans and placement" },
|
|
13
|
+
text: { label: "Text", hint: "size, weight, colour, alignment" },
|
|
14
|
+
stack: { label: "Stack", hint: "direction, gap, alignment" },
|
|
15
|
+
placement: { label: "Placement", hint: "size, margin, position, motion" },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The spec lists some components' style props by name instead of by group
|
|
20
|
+
* (List, Code, Separator; Stat, Select and others): when nearly all of a
|
|
21
|
+
* group's names are there, they are shown as that group.
|
|
22
|
+
*/
|
|
23
|
+
const GROUP_COVERAGE = 0.9;
|
|
24
|
+
|
|
25
|
+
const docsOf = (entry, name) => {
|
|
26
|
+
const docs = entry.propDocs?.[name];
|
|
27
|
+
if (!docs || typeof docs !== "object") return {};
|
|
28
|
+
return {
|
|
29
|
+
...(typeof docs.description === "string" && docs.description.trim() ? { description: docs.description.trim() } : {}),
|
|
30
|
+
...(typeof docs.default === "string" && docs.default.trim() ? { default: docs.default.trim() } : {}),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The props of one group, long spellings folded into their short name ("padding" into "p"). */
|
|
35
|
+
function groupRows(definitions, names, taken) {
|
|
36
|
+
const rows = new Map();
|
|
37
|
+
for (const name of names) {
|
|
38
|
+
const definition = definitions[name];
|
|
39
|
+
if (!definition || taken.has(name)) continue;
|
|
40
|
+
const short = definition.short && names.includes(definition.short) && !taken.has(definition.short) ? definition.short : null;
|
|
41
|
+
if (short) {
|
|
42
|
+
if (!rows.has(short)) rows.set(short, null);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
rows.set(name, {
|
|
46
|
+
name,
|
|
47
|
+
aliases: [],
|
|
48
|
+
values: Array.isArray(definition.tokens) ? definition.tokens.map(String) : [],
|
|
49
|
+
type: null,
|
|
50
|
+
arbitrary: definition.arbitrary ? definition.valueType ?? "value" : null,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
for (const name of names) {
|
|
54
|
+
const definition = definitions[name];
|
|
55
|
+
if (definition?.short && rows.get(definition.short)) rows.get(definition.short).aliases.push(name);
|
|
56
|
+
}
|
|
57
|
+
for (const [name, row] of rows) {
|
|
58
|
+
if (!row) rows.delete(name);
|
|
59
|
+
}
|
|
60
|
+
for (const row of rows.values()) for (const name of [row.name, ...row.aliases]) taken.add(name);
|
|
61
|
+
return [...rows.values()];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The props `name` takes, as sections of rows. `used` is the props this
|
|
66
|
+
* instance was given; a row it uses carries `used: { as, value }`. Returns
|
|
67
|
+
* null when the spec has no entry for the name.
|
|
68
|
+
*
|
|
69
|
+
* { key, total, sections: [{ id, label, hint, inferred?, rows: [
|
|
70
|
+
* { name, aliases, values, type, arbitrary, required, note?, description?, default?, used } ] }] }
|
|
71
|
+
*/
|
|
72
|
+
export function propsModel(spec, name, used = {}) {
|
|
73
|
+
const found = specEntry(spec, name);
|
|
74
|
+
if (!found) return null;
|
|
75
|
+
const { key, entry } = found;
|
|
76
|
+
const groups = spec.propGroups ?? {};
|
|
77
|
+
const closed = entry.props ?? {};
|
|
78
|
+
const scalars = entry.scalarProps ?? {};
|
|
79
|
+
const required = entry.required ?? {};
|
|
80
|
+
const overrides = entry.overrides ?? [];
|
|
81
|
+
const exclude = new Set(entry.exclude ?? []);
|
|
82
|
+
const inGroups = new Set((entry.groups ?? []).flatMap((group) => Object.keys(groups[group] ?? {})));
|
|
83
|
+
|
|
84
|
+
const ownNames = [...new Set([...Object.keys(closed), ...Object.keys(scalars), ...Object.keys(required), ...overrides])];
|
|
85
|
+
let stray = (entry.ownStyleProps ?? []).filter((prop) => !ownNames.includes(prop) && !inGroups.has(prop) && !exclude.has(prop));
|
|
86
|
+
|
|
87
|
+
const taken = new Set(ownNames);
|
|
88
|
+
const sections = [];
|
|
89
|
+
const groupSection = (group, names, inferred) => {
|
|
90
|
+
const rows = groupRows(groups[group], names, taken);
|
|
91
|
+
if (rows.length) sections.push({ id: `group:${group}`, label: GROUP_LABELS[group]?.label ?? group, hint: GROUP_LABELS[group]?.hint ?? "", ...(inferred ? { inferred: true } : {}), rows });
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
for (const group of entry.groups ?? []) {
|
|
95
|
+
if (groups[group]) groupSection(group, Object.keys(groups[group]).filter((prop) => !exclude.has(prop)), false);
|
|
96
|
+
}
|
|
97
|
+
// Largest first: placement's names include all of flexItem's, and must claim them.
|
|
98
|
+
const bySize = Object.entries(groups).sort(([, a], [, b]) => Object.keys(b).length - Object.keys(a).length);
|
|
99
|
+
for (const [group, definitions] of bySize) {
|
|
100
|
+
const names = Object.keys(definitions);
|
|
101
|
+
const covered = names.filter((prop) => stray.includes(prop));
|
|
102
|
+
if (names.length && covered.length / names.length >= GROUP_COVERAGE) {
|
|
103
|
+
groupSection(group, covered, true);
|
|
104
|
+
stray = stray.filter((prop) => !taken.has(prop));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const own = [
|
|
109
|
+
...ownNames.map((prop) => ({
|
|
110
|
+
name: prop,
|
|
111
|
+
aliases: [],
|
|
112
|
+
values: Array.isArray(closed[prop]) ? closed[prop].map(String) : [],
|
|
113
|
+
type: scalars[prop] ?? required[prop] ?? null,
|
|
114
|
+
arbitrary: null,
|
|
115
|
+
...(overrides.includes(prop) && !closed[prop] && !scalars[prop] && !required[prop] ? { note: "Its own attribute, not the style prop" } : {}),
|
|
116
|
+
})),
|
|
117
|
+
...stray.map((prop) => ({ name: prop, aliases: [], values: [], type: null, arbitrary: null, note: "Values not in the spec" })),
|
|
118
|
+
];
|
|
119
|
+
if (own.length) sections.unshift({ id: "own", label: "Its own props", hint: "", rows: own });
|
|
120
|
+
|
|
121
|
+
for (const section of sections) {
|
|
122
|
+
for (const row of section.rows) {
|
|
123
|
+
row.required = Object.hasOwn(required, row.name);
|
|
124
|
+
Object.assign(row, docsOf(entry, row.name));
|
|
125
|
+
const as = [row.name, ...row.aliases].find((prop) => used && Object.hasOwn(used, prop));
|
|
126
|
+
row.used = as ? { as, value: used[as] } : null;
|
|
127
|
+
}
|
|
128
|
+
// Required props lead their section; the spec's order is kept otherwise.
|
|
129
|
+
section.rows.sort((a, b) => Number(b.required) - Number(a.required));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { key, total: sections.reduce((n, section) => n + section.rows.length, 0), sections };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The rows matching `query` by prop name (or long spelling) or by a value it
|
|
137
|
+
* takes. A row found by a value is marked `byValue`, so its whole list can be
|
|
138
|
+
* shown. An empty query keeps everything.
|
|
139
|
+
*/
|
|
140
|
+
export function filterProps(model, query) {
|
|
141
|
+
const q = query.trim().toLowerCase();
|
|
142
|
+
if (!model) return { sections: [], count: 0 };
|
|
143
|
+
if (!q) return { sections: model.sections, count: model.total };
|
|
144
|
+
const sections = [];
|
|
145
|
+
for (const section of model.sections) {
|
|
146
|
+
const rows = [];
|
|
147
|
+
for (const row of section.rows) {
|
|
148
|
+
if ([row.name, ...row.aliases].some((name) => name.toLowerCase().includes(q))) rows.push(row);
|
|
149
|
+
else if (row.values.some((value) => value.toLowerCase().includes(q)) || row.type?.toLowerCase() === q) rows.push({ ...row, byValue: true });
|
|
150
|
+
}
|
|
151
|
+
if (rows.length) sections.push({ ...section, rows });
|
|
152
|
+
}
|
|
153
|
+
return { sections, count: sections.reduce((n, section) => n + section.rows.length, 0) };
|
|
154
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { LIMITS, cleanConsoleMessage, contentOf, keptText, redactedText } from "astralis-cli/snapshot";
|
|
2
|
+
import { IDENTITY_ATTRIBUTE, readIdentity } from "./identity.mjs";
|
|
3
|
+
|
|
4
|
+
/* The page's part of an issue snapshot: the component tree under the
|
|
5
|
+
selected element, the browser's facts and console errors. Nothing here
|
|
6
|
+
sends anything; the bridge assembles the rest. */
|
|
7
|
+
|
|
8
|
+
const SKIP = new Set(["SCRIPT", "STYLE", "TEMPLATE", "NOSCRIPT"]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One element's chain as nested nodes. "Modal.Trigger Button" is a trigger
|
|
12
|
+
* wrapping the Button the page wrote, because a Trigger takes one element
|
|
13
|
+
* (its spec content is "element"); "Card.Body Box" would be Card.Body
|
|
14
|
+
* rendering its own Box, which the page never wrote, so it is recorded under
|
|
15
|
+
* `renders` instead of as a child.
|
|
16
|
+
*/
|
|
17
|
+
function chainNode(chain, spec, children) {
|
|
18
|
+
const [first, ...rest] = chain;
|
|
19
|
+
const node = { component: first.name, props: first.props, children };
|
|
20
|
+
if (rest.length === 0) return node;
|
|
21
|
+
if (contentOf(spec, first.name) === "element") {
|
|
22
|
+
node.children = [chainNode(rest, spec, children)];
|
|
23
|
+
} else {
|
|
24
|
+
node.renders = rest.map((c) => c.name);
|
|
25
|
+
}
|
|
26
|
+
return node;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The astralis-ui components at and under `element`, as snapshot tree nodes.
|
|
31
|
+
* Plain elements in between are passed through; text becomes a placeholder
|
|
32
|
+
* unless `includeText`. The overlay's own element is never included.
|
|
33
|
+
*/
|
|
34
|
+
export function treeFromElement(element, spec, { includeText = false, skip = null } = {}) {
|
|
35
|
+
let count = 0;
|
|
36
|
+
const walk = (nodes, depth) => {
|
|
37
|
+
const out = [];
|
|
38
|
+
for (const node of nodes) {
|
|
39
|
+
if (count >= LIMITS.nodes || depth > LIMITS.depth) break;
|
|
40
|
+
if (node.nodeType === 3) {
|
|
41
|
+
const text = node.textContent.replace(/\s+/g, " ").trim();
|
|
42
|
+
if (text) out.push(includeText ? keptText(text) : redactedText(text));
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (node.nodeType !== 1 || node === skip || SKIP.has(node.tagName)) continue;
|
|
46
|
+
if (node.hasAttribute(IDENTITY_ATTRIBUTE)) {
|
|
47
|
+
count++;
|
|
48
|
+
out.push(chainNode(readIdentity(node), spec, walk(node.childNodes, depth + 1)));
|
|
49
|
+
} else {
|
|
50
|
+
out.push(...walk(node.childNodes, depth));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
};
|
|
55
|
+
return walk([element], 0).filter((node) => "component" in node);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** "Chrome 140", "Firefox 131", "Safari 18": the browser and its major version, no more. */
|
|
59
|
+
export function browserName(userAgent = globalThis.navigator?.userAgent ?? "") {
|
|
60
|
+
const rules = [
|
|
61
|
+
[/Edg\/(\d+)/, "Edge"],
|
|
62
|
+
[/OPR\/(\d+)/, "Opera"],
|
|
63
|
+
[/Firefox\/(\d+)/, "Firefox"],
|
|
64
|
+
[/Chrome\/(\d+)/, "Chrome"],
|
|
65
|
+
[/Version\/(\d+)[\d.]* Safari/, "Safari"],
|
|
66
|
+
];
|
|
67
|
+
for (const [pattern, name] of rules) {
|
|
68
|
+
const m = userAgent.match(pattern);
|
|
69
|
+
if (m) return `${name} ${m[1]}`;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The facts about this browser a snapshot carries. */
|
|
75
|
+
export function browserEnvironment(win = globalThis.window) {
|
|
76
|
+
const dark = win.document.documentElement.classList.contains("astralis-dark");
|
|
77
|
+
return {
|
|
78
|
+
browser: browserName(win.navigator.userAgent),
|
|
79
|
+
viewport: { width: Math.round(win.innerWidth), height: Math.round(win.innerHeight) },
|
|
80
|
+
colorMode: dark ? "dark" : "light",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Record console errors, uncaught errors and unhandled rejections from now
|
|
86
|
+
* on, cleaned as they arrive, keeping the last few. Returns { messages, stop }.
|
|
87
|
+
*/
|
|
88
|
+
export function recordConsoleErrors(win = globalThis.window) {
|
|
89
|
+
const messages = [];
|
|
90
|
+
const origin = win.location.origin;
|
|
91
|
+
const push = (parts) => {
|
|
92
|
+
const text = parts.map((p) => (p instanceof Error ? p.stack || p.message : typeof p === "string" ? p : safeString(p))).join(" ");
|
|
93
|
+
messages.push(cleanConsoleMessage(text, origin));
|
|
94
|
+
if (messages.length > LIMITS.console) messages.shift();
|
|
95
|
+
};
|
|
96
|
+
const original = win.console.error;
|
|
97
|
+
win.console.error = function (...args) {
|
|
98
|
+
push(args);
|
|
99
|
+
return original.apply(this, args);
|
|
100
|
+
};
|
|
101
|
+
const onError = (event) => push([event.error ?? event.message]);
|
|
102
|
+
const onRejection = (event) => push(["Unhandled rejection:", event.reason]);
|
|
103
|
+
win.addEventListener("error", onError);
|
|
104
|
+
win.addEventListener("unhandledrejection", onRejection);
|
|
105
|
+
return {
|
|
106
|
+
messages,
|
|
107
|
+
stop() {
|
|
108
|
+
if (win.console.error !== original) win.console.error = original;
|
|
109
|
+
win.removeEventListener("error", onError);
|
|
110
|
+
win.removeEventListener("unhandledrejection", onRejection);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function safeString(value) {
|
|
116
|
+
try {
|
|
117
|
+
return JSON.stringify(value) ?? String(value);
|
|
118
|
+
} catch {
|
|
119
|
+
return String(value);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { formatValue, h, icon, plural } from "../dom.mjs";
|
|
2
|
+
import { describeComponent, IDENTITY_ATTRIBUTE, readIdentity } from "../identity.mjs";
|
|
3
|
+
import { ICONS } from "../icons.mjs";
|
|
4
|
+
import { findingsFor } from "../page.mjs";
|
|
5
|
+
import { allProps } from "./props.mjs";
|
|
6
|
+
import { findingRow } from "./shared.mjs";
|
|
7
|
+
|
|
8
|
+
/* One component on the page: what it is, the props it was given and every
|
|
9
|
+
prop it takes, how it reaches the browser, where it sits, and the findings
|
|
10
|
+
and recommendations about it. */
|
|
11
|
+
|
|
12
|
+
export function componentScreen(app, element) {
|
|
13
|
+
const root = h("div", {});
|
|
14
|
+
const chain = readIdentity(element);
|
|
15
|
+
const name = chain[0]?.name ?? "Component";
|
|
16
|
+
// Built once: findings and page changes redraw the screen, and must not reset the search.
|
|
17
|
+
const takes = allProps(app, chain);
|
|
18
|
+
|
|
19
|
+
function propsCard(entry) {
|
|
20
|
+
const about = describeComponent(app.state.spec, entry.name, app.framework);
|
|
21
|
+
const props = Object.entries(entry.props);
|
|
22
|
+
return h(
|
|
23
|
+
"div",
|
|
24
|
+
{ class: "card pad" },
|
|
25
|
+
h(
|
|
26
|
+
"div",
|
|
27
|
+
{ class: "row", style: "margin-bottom:10px" },
|
|
28
|
+
h("span", { class: "code grow", style: "font-weight:600" }, entry.name),
|
|
29
|
+
h("a", { class: "link", href: about.docsUrl, target: "_blank", rel: "noreferrer" }, "Docs", icon(ICONS.external)),
|
|
30
|
+
),
|
|
31
|
+
app.state.spec && !about.known
|
|
32
|
+
? h(
|
|
33
|
+
"p",
|
|
34
|
+
{ class: "muted", style: "margin-bottom:8px" },
|
|
35
|
+
about.partOf ? `Part of ${about.partOf}. The spec has no entry of its own for it, so no client tier is shown.` : `${entry.name} is not in the installed spec.`,
|
|
36
|
+
)
|
|
37
|
+
: null,
|
|
38
|
+
props.length
|
|
39
|
+
? h("table", { class: "props" }, h("tbody", {}, props.map(([key, value]) => h("tr", {}, h("td", {}, key), h("td", {}, formatValue(value))))))
|
|
40
|
+
: h("p", { class: "muted" }, "No props beyond its defaults."),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function findingsSection(title, list, tone) {
|
|
45
|
+
return h(
|
|
46
|
+
"section",
|
|
47
|
+
{},
|
|
48
|
+
h("h3", { class: "section-title" }, title, h("span", { class: "n" }, String(list.length))),
|
|
49
|
+
list.length
|
|
50
|
+
? h(
|
|
51
|
+
"div",
|
|
52
|
+
{ class: "file-card" },
|
|
53
|
+
list.map((finding) => findingRow(app, finding, { showFile: true, showComponent: false, exactLabel: finding.exact ? "This one" : null })),
|
|
54
|
+
)
|
|
55
|
+
: h("p", { class: "muted", style: "margin-bottom:4px" }, tone === "error" ? "Nothing broken here." : "Nothing to suggest."),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function update() {
|
|
60
|
+
const { state } = app;
|
|
61
|
+
if (!element.isConnected) {
|
|
62
|
+
root.replaceChildren(
|
|
63
|
+
h("div", { class: "gone" }, icon(ICONS.findings), "This component has left the page."),
|
|
64
|
+
h("button", { class: "btn", type: "button", onclick: () => app.navigate("components") }, icon(ICONS.back), "All components"),
|
|
65
|
+
);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const about = describeComponent(state.spec, name, app.framework);
|
|
69
|
+
const sameKind = state.instances.filter((i) => i.name === name);
|
|
70
|
+
const instance = state.instances.find((i) => i.element === element) ?? { element, chain, name };
|
|
71
|
+
const found = findingsFor(instance, state.findings, { routeFiles: state.routeFiles, alone: sameKind.length === 1 });
|
|
72
|
+
const errors = found.filter((f) => f.kind === "error" && (f.exact || f.onPage));
|
|
73
|
+
const warnings = found.filter((f) => f.kind === "warning" && (f.exact || f.onPage));
|
|
74
|
+
|
|
75
|
+
const ancestors = [];
|
|
76
|
+
for (let node = element.parentElement?.closest(`[${IDENTITY_ATTRIBUTE}]`); node; node = node.parentElement?.closest(`[${IDENTITY_ATTRIBUTE}]`)) {
|
|
77
|
+
ancestors.unshift(node);
|
|
78
|
+
}
|
|
79
|
+
const inside = state.instances.filter((i) => i.element !== element && element.contains(i.element) && i.element.parentElement?.closest(`[${IDENTITY_ATTRIBUTE}]`) === element);
|
|
80
|
+
|
|
81
|
+
const head = h(
|
|
82
|
+
"div",
|
|
83
|
+
{ class: "detail-head" },
|
|
84
|
+
h("span", { class: "detail-icon" }, icon(ICONS.components)),
|
|
85
|
+
h(
|
|
86
|
+
"div",
|
|
87
|
+
{ class: "grow" },
|
|
88
|
+
h("h2", {}, name),
|
|
89
|
+
chain.length > 1 ? h("p", { class: "chain" }, `renders through ${chain.slice(1).map((c) => c.name).join(" → ")}`) : null,
|
|
90
|
+
h(
|
|
91
|
+
"div",
|
|
92
|
+
{ class: "row" },
|
|
93
|
+
about.client ? h("span", { class: `pill ${about.client}`, title: about.clientDetail }, h("i"), about.clientLabel) : null,
|
|
94
|
+
about.category ? h("span", { class: "pill" }, about.category) : null,
|
|
95
|
+
sameKind.length > 1 ? h("span", { class: "pill" }, `${sameKind.indexOf(instance) + 1} of ${sameKind.length} on this page`) : null,
|
|
96
|
+
errors.length ? h("span", { class: "pill error" }, plural(errors.length, "finding")) : null,
|
|
97
|
+
warnings.length ? h("span", { class: "pill advice" }, plural(warnings.length, "recommendation")) : null,
|
|
98
|
+
),
|
|
99
|
+
about.client ? h("p", { class: "muted", style: "margin-top:8px;font-size:12.5px" }, about.clientDetail) : null,
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const actions = h(
|
|
104
|
+
"div",
|
|
105
|
+
{ class: "actions" },
|
|
106
|
+
h("button", { class: "btn", type: "button", onclick: () => app.locate(element) }, icon(ICONS.locate), "Locate on page"),
|
|
107
|
+
h("button", { class: "btn", type: "button", onclick: () => app.navigate("report") }, icon(ICONS.report), "Report a problem"),
|
|
108
|
+
h("a", { class: "btn ghost", href: about.docsUrl, target: "_blank", rel: "noreferrer" }, "Docs", icon(ICONS.external)),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const crumbs = (title, nodes) =>
|
|
112
|
+
nodes.length
|
|
113
|
+
? h(
|
|
114
|
+
"section",
|
|
115
|
+
{},
|
|
116
|
+
h("h3", { class: "section-title" }, title),
|
|
117
|
+
h(
|
|
118
|
+
"div",
|
|
119
|
+
{ class: "tags" },
|
|
120
|
+
nodes.map((node) =>
|
|
121
|
+
h(
|
|
122
|
+
"button",
|
|
123
|
+
{
|
|
124
|
+
class: "chip",
|
|
125
|
+
type: "button",
|
|
126
|
+
onclick: () => app.openComponent(node),
|
|
127
|
+
onpointerenter: () => app.preview(node),
|
|
128
|
+
onpointerleave: () => app.preview(element),
|
|
129
|
+
},
|
|
130
|
+
readIdentity(node)[0]?.name ?? "?",
|
|
131
|
+
),
|
|
132
|
+
),
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
: null;
|
|
136
|
+
|
|
137
|
+
takes.update();
|
|
138
|
+
const searching = takes.input.getRootNode().activeElement === takes.input;
|
|
139
|
+
root.replaceChildren(
|
|
140
|
+
head,
|
|
141
|
+
actions,
|
|
142
|
+
h(
|
|
143
|
+
"div",
|
|
144
|
+
{ class: "stack" },
|
|
145
|
+
findingsSection("Findings", errors, "error"),
|
|
146
|
+
findingsSection("Recommendations", warnings, "advice"),
|
|
147
|
+
h("section", {}, h("h3", { class: "section-title" }, "Props given"), h("div", { class: "stack" }, chain.map((entry) => propsCard(entry)))),
|
|
148
|
+
takes.root,
|
|
149
|
+
crumbs("Inside", ancestors),
|
|
150
|
+
crumbs("Contains", inside.map((i) => i.element)),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
if (searching) takes.input.focus({ preventScroll: true });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
update();
|
|
157
|
+
return { title: [{ label: "Components", screen: "components" }, { label: name, code: true }], root, update, element };
|
|
158
|
+
}
|