astralis-devtools 0.1.0 → 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.
@@ -1,71 +1,70 @@
1
- import {
2
- LIMITS,
3
- SNAPSHOT_FORMAT,
4
- checkSnapshot,
5
- cleanConsoleMessage,
6
- cleanRoute,
7
- narrowTree,
8
- } from "astralis-cli/snapshot";
9
-
10
- /* The bridge's half of an issue snapshot. The page sends what only the page
11
- knows (the component tree, the browser, the reporter's words); the bridge
12
- adds what only the dev server knows (versions, the spec's hash, the
13
- validator's findings) and checks the whole. Nothing here makes a network
14
- request; github.mjs sends a snapshot once the reporter confirms. */
15
-
16
- const shortString = (value, max) => (typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined);
17
-
18
- /** Findings for the files that render `route` (Next.js), else for the whole project. */
19
- function findingsFor(bridge, route) {
20
- const { results = [] } = bridge.findings();
21
- const files = route ? new Set(bridge.routeFiles(route)) : new Set();
22
- const chosen = files.size ? results.filter((r) => files.has(r.file)) : results;
23
- return chosen
24
- .flatMap((r) => [...r.errors, ...r.warnings])
25
- .map(({ code, file, line, column, message }) => ({ code, file, line, column, message }))
26
- .slice(0, LIMITS.findings);
27
- }
28
-
29
- function environmentOf(info, input) {
30
- const env = input && typeof input === "object" ? input : {};
31
- const viewport = env.viewport;
32
- return {
33
- framework: { name: info.framework?.name ?? null, version: info.framework?.version ?? null },
34
- react: info.react ?? null,
35
- browser: shortString(env.browser, 60) ?? null,
36
- viewport: viewport && Number.isInteger(viewport.width) && Number.isInteger(viewport.height)
37
- ? { width: viewport.width, height: viewport.height }
38
- : null,
39
- colorMode: env.colorMode === "light" || env.colorMode === "dark" ? env.colorMode : null,
40
- };
41
- }
42
-
43
- /**
44
- * The complete snapshot from the page's part. Returns { snapshot, problems }:
45
- * the overlay shows `snapshot` as the exact JSON that would be sent.
46
- */
47
- export function assembleSnapshot(bridge, input = {}) {
48
- const include = input.include ?? {};
49
- const route = cleanRoute(typeof input.route === "string" ? input.route : null);
50
- const description = { title: shortString(input.description?.title, LIMITS.title) ?? "" };
51
- const happened = shortString(input.description?.happened, LIMITS.body);
52
- const expected = shortString(input.description?.expected, LIMITS.body);
53
- if (happened) description.happened = happened;
54
- if (expected) description.expected = expected;
55
-
56
- const snapshot = {
57
- format: SNAPSHOT_FORMAT,
58
- source: "devtools",
59
- library: { ...bridge.info.library },
60
- environment: environmentOf(bridge.info, input.environment),
61
- route,
62
- tree: narrowTree(bridge.spec, input.tree),
63
- ...(include.findings === false ? {} : { findings: findingsFor(bridge, route) }),
64
- ...(include.console && Array.isArray(input.console)
65
- ? { console: input.console.slice(-LIMITS.console).map((m) => cleanConsoleMessage(m)) }
66
- : {}),
67
- ...(include.screenshot && typeof input.screenshot === "string" ? { screenshot: input.screenshot } : {}),
68
- description,
69
- };
70
- return { snapshot, problems: checkSnapshot(snapshot) };
71
- }
1
+ import {
2
+ LIMITS,
3
+ SNAPSHOT_FORMAT,
4
+ checkSnapshot,
5
+ cleanConsoleMessage,
6
+ cleanRoute,
7
+ narrowTree,
8
+ } from "astralis-cli/snapshot";
9
+
10
+ /* The bridge's half of an issue snapshot. The page sends what only the page
11
+ knows (the component tree, the browser, the reporter's words); the bridge
12
+ adds what only the dev server knows (versions, the spec's hash, the
13
+ validator's findings) and checks the whole. Nothing here makes a network
14
+ request; github.mjs sends a snapshot once the reporter confirms. */
15
+
16
+ const shortString = (value, max) => (typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined);
17
+
18
+ /** Findings for the files that render `route` (Next.js), else for the whole project. */
19
+ function findingsFor(bridge, route) {
20
+ const { results = [] } = bridge.findings();
21
+ const files = route ? new Set(bridge.routeFiles(route)) : new Set();
22
+ const chosen = files.size ? results.filter((r) => files.has(r.file)) : results;
23
+ return chosen
24
+ .flatMap((r) => [...r.errors, ...r.warnings])
25
+ .map(({ code, file, line, column, message }) => ({ code, file, line, column, message }))
26
+ .slice(0, LIMITS.findings);
27
+ }
28
+
29
+ function environmentOf(info, input) {
30
+ const env = input && typeof input === "object" ? input : {};
31
+ const viewport = env.viewport;
32
+ return {
33
+ framework: { name: info.framework?.name ?? null, version: info.framework?.version ?? null },
34
+ react: info.react ?? null,
35
+ browser: shortString(env.browser, 60) ?? null,
36
+ viewport: viewport && Number.isInteger(viewport.width) && Number.isInteger(viewport.height)
37
+ ? { width: viewport.width, height: viewport.height }
38
+ : null,
39
+ colorMode: env.colorMode === "light" || env.colorMode === "dark" ? env.colorMode : null,
40
+ };
41
+ }
42
+
43
+ /**
44
+ * The complete snapshot from the page's part. Returns { snapshot, problems }:
45
+ * the overlay shows `snapshot` as the exact JSON that would be sent.
46
+ */
47
+ export function assembleSnapshot(bridge, input = {}) {
48
+ const include = input.include ?? {};
49
+ const route = cleanRoute(typeof input.route === "string" ? input.route : null);
50
+ const description = { title: shortString(input.description?.title, LIMITS.title) ?? "" };
51
+ const happened = shortString(input.description?.happened, LIMITS.body);
52
+ const expected = shortString(input.description?.expected, LIMITS.body);
53
+ if (happened) description.happened = happened;
54
+ if (expected) description.expected = expected;
55
+
56
+ const snapshot = {
57
+ format: SNAPSHOT_FORMAT,
58
+ source: "devtools",
59
+ library: { ...bridge.info.library },
60
+ environment: environmentOf(bridge.info, input.environment),
61
+ route,
62
+ tree: narrowTree(bridge.spec, input.tree),
63
+ ...(include.findings === false ? {} : { findings: findingsFor(bridge, route) }),
64
+ ...(include.console && Array.isArray(input.console)
65
+ ? { console: input.console.slice(-LIMITS.console).map((m) => cleanConsoleMessage(m)) }
66
+ : {}),
67
+ description,
68
+ };
69
+ return { snapshot, problems: checkSnapshot(snapshot) };
70
+ }
@@ -1,44 +1,43 @@
1
- /* The overlay's icons: 24px line icons, drawn in currentColor, and the
2
- Astralis mark. */
3
-
4
- const line = (paths) =>
5
- `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">${paths}</svg>`;
6
-
7
- /** The Astralis mark: four blocks, the last one tipped. */
8
- export const MARK = `<svg viewBox="0 0 32 32" fill="currentColor">
9
- <rect class="b1" x="3" y="17" width="12" height="12" rx="3"/>
10
- <rect class="b2" x="17" y="17" width="12" height="12" rx="3"/>
11
- <rect class="b3" x="3" y="3" width="12" height="12" rx="3"/>
12
- <rect class="b4" x="19" y="1" width="12" height="12" rx="3" opacity="0.6" transform="rotate(14 25 7)"/>
13
- </svg>`;
14
-
15
- export const ICONS = {
16
- home: line('<path d="M3.5 10.2 12 3.5l8.5 6.7V19a1.5 1.5 0 0 1-1.5 1.5h-4v-6h-6v6H5A1.5 1.5 0 0 1 3.5 19z"/>'),
17
- components: line('<path d="m12 3 8.5 4.5L12 12 3.5 7.5z"/><path d="m3.5 12 8.5 4.5 8.5-4.5"/><path d="m3.5 16.5 8.5 4.5 8.5-4.5"/>'),
18
- findings: line('<path d="M8.2 3h7.6L21 8.2v7.6L15.8 21H8.2L3 15.8V8.2z"/><path d="M12 8v4.5"/><path d="M12 16h.01"/>'),
19
- recommendations: line('<path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.7.5 1.1 1.3 1.1 2.2h5c0-.9.4-1.7 1.1-2.2A6 6 0 0 0 12 3z"/>'),
20
- report: line('<path d="M5 21V4"/><path d="M5 4h11l-2 4 2 4H5"/>'),
21
- pick: line('<circle cx="12" cy="12" r="7.5"/><path d="M12 2v4"/><path d="M12 18v4"/><path d="M2 12h4"/><path d="M18 12h4"/><circle cx="12" cy="12" r="1.2" fill="currentColor"/>'),
22
- close: line('<path d="M6 6l12 12"/><path d="M18 6 6 18"/>'),
23
- back: line('<path d="M15 5l-7 7 7 7"/>'),
24
- chevron: line('<path d="m9 5 7 7-7 7"/>'),
25
- arrow: line('<path d="M5 12h14"/><path d="m13 6 6 6-6 6"/>'),
26
- external: line('<path d="M14 4h6v6"/><path d="M20 4 11 13"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/>'),
27
- search: line('<circle cx="11" cy="11" r="6.5"/><path d="m16 16 4.5 4.5"/>'),
28
- locate: line('<path d="M4 9V5a1 1 0 0 1 1-1h4"/><path d="M15 4h4a1 1 0 0 1 1 1v4"/><path d="M20 15v4a1 1 0 0 1-1 1h-4"/><path d="M9 20H5a1 1 0 0 1-1-1v-4"/><circle cx="12" cy="12" r="2.5"/>'),
29
- check: line('<path d="m5 12.5 4.5 4.5L19 7.5"/>'),
30
- copy: line('<rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h3"/>'),
31
- download: line('<path d="M12 4v11"/><path d="m7 10 5 5 5-5"/><path d="M5 20h14"/>'),
32
- camera: line('<path d="M4 8.5A1.5 1.5 0 0 1 5.5 7H8l1.5-2.5h5L16 7h2.5A1.5 1.5 0 0 1 20 8.5v9a1.5 1.5 0 0 1-1.5 1.5h-13A1.5 1.5 0 0 1 4 17.5z"/><circle cx="12" cy="12.5" r="3.5"/>'),
33
- file: line('<path d="M14 3H7a1.5 1.5 0 0 0-1.5 1.5v15A1.5 1.5 0 0 0 7 21h10a1.5 1.5 0 0 0 1.5-1.5V7.5z"/><path d="M14 3v4.5h4.5"/>'),
34
- sparkle: line('<path d="M12 3.5 13.8 10.2 20.5 12l-6.7 1.8L12 20.5l-1.8-6.7L3.5 12l6.7-1.8z"/>'),
35
- refresh: line('<path d="M20 11a8 8 0 0 0-14.3-4.9L4 8"/><path d="M4 4v4h4"/><path d="M4 13a8 8 0 0 0 14.3 4.9L20 16"/><path d="M20 20v-4h-4"/>'),
36
- issueOpen: line('<circle cx="12" cy="12" r="8.5"/><circle cx="12" cy="12" r="1.6" fill="currentColor"/>'),
37
- issueClosed: line('<circle cx="12" cy="12" r="8.5"/><path d="m8.5 12.2 2.4 2.4 4.6-4.8"/>'),
38
- signOut: line('<path d="M15 4h3a1.5 1.5 0 0 1 1.5 1.5v13A1.5 1.5 0 0 1 18 20h-3"/><path d="M10 16l-4-4 4-4"/><path d="M6 12h10"/>'),
39
- send: line('<path d="M20.5 3.5 10 14"/><path d="m20.5 3.5-6.5 17-4-6.5-6.5-4z"/>'),
40
- };
41
-
42
- /** GitHub's mark, for signing in with GitHub. */
43
- export const GITHUB_MARK =
44
- '<svg viewBox="0 0 16 16" fill="currentColor"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0 0 16 8c0-4.42-3.58-8-8-8z"/></svg>';
1
+ /* The overlay's icons: 24px line icons, drawn in currentColor, and the
2
+ Astralis mark. */
3
+
4
+ const line = (paths) =>
5
+ `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">${paths}</svg>`;
6
+
7
+ /** The Astralis mark: four blocks, the last one tipped. */
8
+ export const MARK = `<svg viewBox="0 0 32 32" fill="currentColor">
9
+ <rect class="b1" x="3" y="17" width="12" height="12" rx="3"/>
10
+ <rect class="b2" x="17" y="17" width="12" height="12" rx="3"/>
11
+ <rect class="b3" x="3" y="3" width="12" height="12" rx="3"/>
12
+ <rect class="b4" x="19" y="1" width="12" height="12" rx="3" opacity="0.6" transform="rotate(14 25 7)"/>
13
+ </svg>`;
14
+
15
+ export const ICONS = {
16
+ home: line('<path d="M3.5 10.2 12 3.5l8.5 6.7V19a1.5 1.5 0 0 1-1.5 1.5h-4v-6h-6v6H5A1.5 1.5 0 0 1 3.5 19z"/>'),
17
+ components: line('<path d="m12 3 8.5 4.5L12 12 3.5 7.5z"/><path d="m3.5 12 8.5 4.5 8.5-4.5"/><path d="m3.5 16.5 8.5 4.5 8.5-4.5"/>'),
18
+ findings: line('<path d="M8.2 3h7.6L21 8.2v7.6L15.8 21H8.2L3 15.8V8.2z"/><path d="M12 8v4.5"/><path d="M12 16h.01"/>'),
19
+ recommendations: line('<path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.7.5 1.1 1.3 1.1 2.2h5c0-.9.4-1.7 1.1-2.2A6 6 0 0 0 12 3z"/>'),
20
+ report: line('<path d="M5 21V4"/><path d="M5 4h11l-2 4 2 4H5"/>'),
21
+ pick: line('<circle cx="12" cy="12" r="7.5"/><path d="M12 2v4"/><path d="M12 18v4"/><path d="M2 12h4"/><path d="M18 12h4"/><circle cx="12" cy="12" r="1.2" fill="currentColor"/>'),
22
+ minimize: line('<path d="M6 17.5h12"/>'),
23
+ back: line('<path d="M15 5l-7 7 7 7"/>'),
24
+ chevron: line('<path d="m9 5 7 7-7 7"/>'),
25
+ arrow: line('<path d="M5 12h14"/><path d="m13 6 6 6-6 6"/>'),
26
+ external: line('<path d="M14 4h6v6"/><path d="M20 4 11 13"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/>'),
27
+ search: line('<circle cx="11" cy="11" r="6.5"/><path d="m16 16 4.5 4.5"/>'),
28
+ locate: line('<path d="M4 9V5a1 1 0 0 1 1-1h4"/><path d="M15 4h4a1 1 0 0 1 1 1v4"/><path d="M20 15v4a1 1 0 0 1-1 1h-4"/><path d="M9 20H5a1 1 0 0 1-1-1v-4"/><circle cx="12" cy="12" r="2.5"/>'),
29
+ check: line('<path d="m5 12.5 4.5 4.5L19 7.5"/>'),
30
+ copy: line('<rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h3"/>'),
31
+ download: line('<path d="M12 4v11"/><path d="m7 10 5 5 5-5"/><path d="M5 20h14"/>'),
32
+ file: line('<path d="M14 3H7a1.5 1.5 0 0 0-1.5 1.5v15A1.5 1.5 0 0 0 7 21h10a1.5 1.5 0 0 0 1.5-1.5V7.5z"/><path d="M14 3v4.5h4.5"/>'),
33
+ sparkle: line('<path d="M12 3.5 13.8 10.2 20.5 12l-6.7 1.8L12 20.5l-1.8-6.7L3.5 12l6.7-1.8z"/>'),
34
+ refresh: line('<path d="M20 11a8 8 0 0 0-14.3-4.9L4 8"/><path d="M4 4v4h4"/><path d="M4 13a8 8 0 0 0 14.3 4.9L20 16"/><path d="M20 20v-4h-4"/>'),
35
+ issueOpen: line('<circle cx="12" cy="12" r="8.5"/><circle cx="12" cy="12" r="1.6" fill="currentColor"/>'),
36
+ issueClosed: line('<circle cx="12" cy="12" r="8.5"/><path d="m8.5 12.2 2.4 2.4 4.6-4.8"/>'),
37
+ signOut: line('<path d="M15 4h3a1.5 1.5 0 0 1 1.5 1.5v13A1.5 1.5 0 0 1 18 20h-3"/><path d="M10 16l-4-4 4-4"/><path d="M6 12h10"/>'),
38
+ send: line('<path d="M20.5 3.5 10 14"/><path d="m20.5 3.5-6.5 17-4-6.5-6.5-4z"/>'),
39
+ };
40
+
41
+ /** GitHub's mark, for signing in with GitHub. */
42
+ export const GITHUB_MARK =
43
+ '<svg viewBox="0 0 16 16" fill="currentColor"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0 0 16 8c0-4.42-3.58-8-8-8z"/></svg>';
@@ -1,142 +1,143 @@
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
- }
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
+ focus: () => markButton.focus(),
128
+ setPicking(picking) {
129
+ pickButton.setAttribute("aria-pressed", String(picking));
130
+ },
131
+ setCounts({ errors, warnings }) {
132
+ dot.hidden = errors + warnings === 0;
133
+ dot.className = errors > 0 ? "dot" : "dot advice";
134
+ dot.textContent = String(errors > 0 ? errors : warnings);
135
+ markButton.setAttribute(
136
+ "aria-label",
137
+ errors + warnings > 0
138
+ ? `Astralis DevTools: ${errors} ${errors === 1 ? "error" : "errors"}, ${warnings} ${warnings === 1 ? "recommendation" : "recommendations"}`
139
+ : "Astralis DevTools",
140
+ );
141
+ },
142
+ };
143
+ }