@tokenoftrust/storefront-runner 7.2.1 → 7.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/feedback/package.json +5 -1
- package/packages/feedback/scripts/build-player.mjs +28 -0
- package/packages/feedback/src/tour/player/browser.ts +5 -0
- package/packages/feedback/src/tour/player/host.ts +12 -0
- package/packages/feedback/src/tour/player/index.ts +205 -0
- package/packages/feedback/src/tour/player/manifest.ts +76 -0
- package/packages/feedback/src/tour/player/player.css +130 -0
- package/packages/feedback/src/tour/player/resolver.ts +12 -0
- package/packages/feedback/src/tour/player/surface.ts +295 -0
- package/packages/feedback/src/tour/player/types.ts +8 -0
- package/packages/feedback/vitest.config.cts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "7.2.
|
|
3
|
+
"version": "7.2.2",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"./v1": "./src/v1/index.ts",
|
|
13
13
|
"./tour": "./src/tour/index.ts",
|
|
14
14
|
"./tour/resolve": "./src/tour/resolve/index.ts",
|
|
15
|
+
"./tour/player": "./src/tour/player/index.ts",
|
|
15
16
|
"./primitives": "./src/primitives/index.ts"
|
|
16
17
|
},
|
|
17
18
|
"files": [
|
|
@@ -21,7 +22,10 @@
|
|
|
21
22
|
"scripts": {
|
|
22
23
|
"typecheck": "tsc --noEmit",
|
|
23
24
|
"build": "tsc",
|
|
24
|
-
"
|
|
25
|
+
"build:tour-player": "node scripts/build-player.mjs",
|
|
26
|
+
"test:tour-player-browser": "node --test tests/player.browser.test.mjs",
|
|
27
|
+
"test:tour-player-static": "node --test tests/player-static.test.mjs",
|
|
28
|
+
"test": "tot-gate vitest run && node --test tests/player-static.test.mjs"
|
|
25
29
|
},
|
|
26
30
|
"devDependencies": {
|
|
27
31
|
"happy-dom": "20.11.2",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const here = fileURLToPath(new URL("../src/tour/player/", import.meta.url));
|
|
8
|
+
const require = createRequire(new URL('../../../apps/storefront/package.json', import.meta.url));
|
|
9
|
+
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
10
|
+
|
|
11
|
+
/** Produces immutable pool-ready bytes; publication owns copying them into each provider. */
|
|
12
|
+
export async function buildPlayer(outDir) {
|
|
13
|
+
const { build } = require('esbuild');
|
|
14
|
+
const result = await build({ entryPoints: [path.join(here, 'browser.ts')], bundle: true,
|
|
15
|
+
write: false, format: 'esm', platform: 'browser', target: 'es2022', minify: true, legalComments: 'none' });
|
|
16
|
+
const js = result.outputFiles[0].contents, css = await readFile(path.join(here, 'player.css'));
|
|
17
|
+
const jsSha = hash(js), cssSha = hash(css);
|
|
18
|
+
await mkdir(outDir, { recursive: true });
|
|
19
|
+
const jsPath = path.join(outDir, `${jsSha}.js`), cssPath = path.join(outDir, `${cssSha}.css`);
|
|
20
|
+
await writeFile(jsPath, js); await writeFile(cssPath, css);
|
|
21
|
+
return { module: { path: jsPath, sha256: jsSha, url: `/_a/${jsSha}.js`,
|
|
22
|
+
integrity: `sha256-${createHash('sha256').update(js).digest('base64')}` },
|
|
23
|
+
stylesheet: { path: cssPath, sha256: cssSha, url: `/_a/${cssSha}.css` } };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
27
|
+
console.log(JSON.stringify(await buildPlayer(path.resolve(process.argv[2] ?? 'tmp/tour-player-assets'))));
|
|
28
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { bootTourPlayer } from "./index.js";
|
|
2
|
+
|
|
3
|
+
const afterPaint = () => requestAnimationFrame(() => requestAnimationFrame(() => { void bootTourPlayer(); }));
|
|
4
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", afterPaint, { once: true });
|
|
5
|
+
else afterPaint();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Hosts mount an inert overlay; tour-1 never navigates on a visitor's behalf. */
|
|
2
|
+
export interface TourHost {
|
|
3
|
+
mount(root: HTMLElement): void;
|
|
4
|
+
navigate(path: string): never;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function browserHost(doc: Document): TourHost {
|
|
8
|
+
return {
|
|
9
|
+
mount(root) { doc.body.append(root); },
|
|
10
|
+
navigate() { throw new Error("tour-navigation-refused"); },
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { ViewportSnapshot } from "../../v1/types.js";
|
|
2
|
+
import type { PublishedTour, TourStep, TourTarget } from "../schema.js";
|
|
3
|
+
import { readManifest, loadTour, poolUrl } from "./manifest.js";
|
|
4
|
+
import { createSurface } from "./surface.js";
|
|
5
|
+
import type { Resolver, TargetResolution } from "./types.js";
|
|
6
|
+
import type { TourHost } from "./host.js";
|
|
7
|
+
import { bandOf } from "../resolve/index.js";
|
|
8
|
+
import { resolveDocument, resolveFallback } from "./resolver.js";
|
|
9
|
+
|
|
10
|
+
export type { TourHost } from "./host.js";
|
|
11
|
+
export type { TargetResolution, Resolver } from "./types.js";
|
|
12
|
+
export interface PlayerOptions { resolve?: Resolver; fallback?: Resolver; host?: TourHost }
|
|
13
|
+
export interface TourPlayer { start(id: string): Promise<void>; end(): void; destroy(): void }
|
|
14
|
+
interface ShownStep { step: TourStep; resolution: TargetResolution; target?: TourTarget; node: Element | null }
|
|
15
|
+
const absent: TargetResolution = { state: "absent", reason: "path-miss" };
|
|
16
|
+
|
|
17
|
+
function viewport(win: Window): ViewportSnapshot {
|
|
18
|
+
return { width: win.innerWidth, height: win.innerHeight, scrollX: win.scrollX, scrollY: win.scrollY,
|
|
19
|
+
scale: win.visualViewport?.scale ?? 1, devicePixelRatio: win.devicePixelRatio };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Resolves before offering a tour, so an all-skipped tour has no launcher. */
|
|
23
|
+
async function shownSteps(tour: PublishedTour, doc: Document, resolve: Resolver, fallback: Resolver): Promise<ShownStep[]> {
|
|
24
|
+
const win = doc.defaultView!;
|
|
25
|
+
const vp = viewport(win), band = bandOf(vp.width);
|
|
26
|
+
const shown = await Promise.all(tour.steps.map(async (step): Promise<ShownStep | null> => {
|
|
27
|
+
if (step.hideIn?.includes(band)) return null;
|
|
28
|
+
const target = step.bandTargets?.[band] ?? step.target;
|
|
29
|
+
let outcome = target ? await resolve(target, doc, vp, tour.authoredViewport) : absent;
|
|
30
|
+
let resolution = "resolution" in outcome ? outcome.resolution : outcome;
|
|
31
|
+
let node = "resolution" in outcome ? outcome.node : null;
|
|
32
|
+
if (resolution.state !== "resolved" && step.kind === "highlight" && step.onUnresolved !== "skip" && target) {
|
|
33
|
+
outcome = await fallback(target, doc, vp, tour.authoredViewport);
|
|
34
|
+
resolution = "resolution" in outcome ? outcome.resolution : outcome;
|
|
35
|
+
node = "resolution" in outcome ? outcome.node : null;
|
|
36
|
+
}
|
|
37
|
+
if (resolution.state !== "resolved" && (step.kind === "highlight" || (step.kind === "text" && step.onUnresolved === "skip"))) return null;
|
|
38
|
+
return { step, resolution, target, node };
|
|
39
|
+
}));
|
|
40
|
+
return shown.filter((s): s is ShownStep => s !== null);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A non-executable manifest is the only entry point. Invalid data mounts nothing. */
|
|
44
|
+
export async function bootTourPlayer(doc: Document = document, options: PlayerOptions = {}): Promise<TourPlayer | null> {
|
|
45
|
+
const win = doc.defaultView!;
|
|
46
|
+
const manifestNode = doc.getElementById("tot-tour-manifest");
|
|
47
|
+
if (!win || !doc.body || !manifestNode || manifestNode.localName !== "script" || manifestNode.getAttribute("type") !== "application/json") return null;
|
|
48
|
+
let input: unknown;
|
|
49
|
+
try { input = JSON.parse(manifestNode.textContent ?? ""); }
|
|
50
|
+
catch (error) { if (error instanceof SyntaxError) return null; throw error; }
|
|
51
|
+
const manifest = readManifest(input, win.location.href);
|
|
52
|
+
if (!manifest || manifest.tours.length === 0) return null;
|
|
53
|
+
const resolve = options.resolve ?? resolveDocument;
|
|
54
|
+
const fallback = options.fallback ?? options.resolve ?? resolveFallback;
|
|
55
|
+
const loaded = await Promise.all(manifest.tours.map(ref => loadTour(ref, doc)));
|
|
56
|
+
const tours: Array<{ tour: PublishedTour; shown: ShownStep[] }> = [];
|
|
57
|
+
for (const outcome of loaded) {
|
|
58
|
+
if (outcome.status !== "loaded") continue;
|
|
59
|
+
const tour = outcome.tour;
|
|
60
|
+
const shown = await shownSteps(tour, doc, resolve, fallback);
|
|
61
|
+
if (shown.length) tours.push({ tour, shown });
|
|
62
|
+
}
|
|
63
|
+
if (!tours.length) return null;
|
|
64
|
+
const surface = createSurface(doc, poolUrl(manifest.player.css, "css", win.location.href)!);
|
|
65
|
+
if (options.host) options.host.mount(surface.root);
|
|
66
|
+
const launcher = doc.createElement("nav");
|
|
67
|
+
launcher.setAttribute("aria-label", "Guided tours");
|
|
68
|
+
launcher.className = "tour-launcher";
|
|
69
|
+
surface.shadow.append(launcher);
|
|
70
|
+
let current: (typeof tours)[number] | null = null, index = 0, epoch = 0, resizeGeneration = 0, destroyed = false;
|
|
71
|
+
let timer: number | undefined, lastTick = 0, remaining = 0, hovered = false, focused = false;
|
|
72
|
+
let wasHidden = doc.hidden;
|
|
73
|
+
let returnFocus: Element | null = null;
|
|
74
|
+
const motion = win.matchMedia("(prefers-reduced-motion: reduce)");
|
|
75
|
+
const stopTimer = () => { if (timer !== undefined) win.clearInterval(timer); timer = undefined; };
|
|
76
|
+
const end = () => {
|
|
77
|
+
epoch++; resizeGeneration++; stopTimer(); current = null; launcher.hidden = false; surface.clear();
|
|
78
|
+
if (returnFocus && "focus" in returnFocus && returnFocus.isConnected) (returnFocus as HTMLElement).focus();
|
|
79
|
+
returnFocus = null;
|
|
80
|
+
};
|
|
81
|
+
const tick = () => {
|
|
82
|
+
const now = win.performance.now();
|
|
83
|
+
if (!doc.hidden && !hovered && !focused) remaining -= now - lastTick;
|
|
84
|
+
lastTick = now; wasHidden = doc.hidden;
|
|
85
|
+
if (remaining <= 0) next();
|
|
86
|
+
};
|
|
87
|
+
const render = (refresh = false) => {
|
|
88
|
+
if (!refresh) { resizeGeneration++; stopTimer(); }
|
|
89
|
+
const shown = current?.shown[index];
|
|
90
|
+
if (!shown || !current) { end(); return; }
|
|
91
|
+
surface.show(shown.step, shown.resolution, index, current.shown.length,
|
|
92
|
+
{ back: () => { if (index > 0) { index--; render(); } }, next, end }, motion.matches);
|
|
93
|
+
if (refresh) return;
|
|
94
|
+
hovered = false;
|
|
95
|
+
focused = surface.shadow.activeElement !== null;
|
|
96
|
+
if (shown.step.advance.on === "dwell") {
|
|
97
|
+
remaining = shown.step.advance.ms; lastTick = win.performance.now(); wasHidden = doc.hidden; timer = win.setInterval(tick, 50);
|
|
98
|
+
}
|
|
99
|
+
const rect = shown.resolution.state === "resolved" ? shown.resolution.rect : null;
|
|
100
|
+
if (rect && (rect.y < win.scrollY || rect.y + rect.height > win.scrollY + win.innerHeight ||
|
|
101
|
+
rect.x < win.scrollX || rect.x + rect.width > win.scrollX + win.innerWidth)) {
|
|
102
|
+
win.scrollTo({ top: Math.max(0, rect.y - 32), left: Math.max(0, rect.x - 32), behavior: motion.matches ? "instant" : "smooth" });
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
function next() { if (current) { index++; render(); } }
|
|
106
|
+
async function start(id: string) {
|
|
107
|
+
if (destroyed) return;
|
|
108
|
+
const found = tours.find(t => t.tour.id === id);
|
|
109
|
+
if (!found) return;
|
|
110
|
+
end();
|
|
111
|
+
const generation = ++epoch;
|
|
112
|
+
const shown = await shownSteps(found.tour, doc, resolve, fallback);
|
|
113
|
+
if (destroyed || generation !== epoch || !shown.length) return;
|
|
114
|
+
returnFocus = surface.shadow.activeElement ?? doc.activeElement;
|
|
115
|
+
current = { tour: found.tour, shown }; index = 0; launcher.hidden = true; render();
|
|
116
|
+
if (found.tour.trigger.kind === "first-visit") {
|
|
117
|
+
try { win.localStorage.setItem(`tot-tour:${id}:${found.tour.revision}`, "seen"); } catch { /* Storage denial does not affect manual launch. */ }
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const { tour } of tours) {
|
|
121
|
+
if (tour.trigger.kind === "link") continue;
|
|
122
|
+
const button = doc.createElement("button"); button.type = "button"; button.textContent = tour.title;
|
|
123
|
+
button.addEventListener("click", () => { void start(tour.id); }); launcher.append(button);
|
|
124
|
+
}
|
|
125
|
+
const hashStart = () => {
|
|
126
|
+
const hash = /^#tour=(tour_[a-z0-9][a-z0-9-]{1,62})$/.exec(win.location.hash);
|
|
127
|
+
if (hash?.[1]) { void start(hash[1]); return true; }
|
|
128
|
+
return false;
|
|
129
|
+
};
|
|
130
|
+
const keydown = (event: KeyboardEvent) => { if (current && event.key === "Escape") { event.preventDefault(); end(); } };
|
|
131
|
+
const targetClick = (event: MouseEvent) => {
|
|
132
|
+
const shown = current?.shown[index];
|
|
133
|
+
if (!shown || shown.step.advance.on !== "target-click" || shown.resolution.state !== "resolved" ||
|
|
134
|
+
event.composedPath().includes(surface.root)) return;
|
|
135
|
+
const path = event.composedPath();
|
|
136
|
+
if (shown.resolution.match === "page") { next(); return; }
|
|
137
|
+
if (shown.resolution.match === "region") {
|
|
138
|
+
if (shown.node && !path.includes(shown.node)) return;
|
|
139
|
+
let x = event.clientX + win.scrollX, y = event.clientY + win.scrollY;
|
|
140
|
+
if (event.detail === 0 && event.clientX === 0 && event.clientY === 0) {
|
|
141
|
+
const activated = path.find((node): node is Element => "getBoundingClientRect" in (node as object));
|
|
142
|
+
if (!activated) return;
|
|
143
|
+
const rect = activated.getBoundingClientRect();
|
|
144
|
+
x = rect.left + rect.width / 2 + win.scrollX; y = rect.top + rect.height / 2 + win.scrollY;
|
|
145
|
+
}
|
|
146
|
+
const rect = shown.resolution.rect;
|
|
147
|
+
if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) next();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (shown.node && path.includes(shown.node)) next();
|
|
151
|
+
};
|
|
152
|
+
const reposition = async () => {
|
|
153
|
+
if (!current) return;
|
|
154
|
+
const own = ++resizeGeneration, active = current, stepId = current.shown[index]?.step.id;
|
|
155
|
+
const shown = await shownSteps(active.tour, doc, resolve, fallback);
|
|
156
|
+
if (own !== resizeGeneration || current !== active || destroyed) return;
|
|
157
|
+
active.shown = shown;
|
|
158
|
+
const at = shown.findIndex(s => s.step.id === stepId);
|
|
159
|
+
const originalIndex = active.tour.steps.findIndex(s => s.id === stepId);
|
|
160
|
+
const nextAt = shown.findIndex(s => active.tour.steps.findIndex(original => original.id === s.step.id) > originalIndex);
|
|
161
|
+
index = at >= 0 ? at : nextAt >= 0 ? nextAt : shown.length;
|
|
162
|
+
render(at >= 0);
|
|
163
|
+
};
|
|
164
|
+
const pointerEnter = () => { if (current) hovered = true; };
|
|
165
|
+
const pointerLeave = () => { hovered = false; lastTick = win.performance.now(); };
|
|
166
|
+
const focusIn = () => { focused = true; };
|
|
167
|
+
const focusOut = () => { win.queueMicrotask(() => { focused = surface.shadow.activeElement !== null; lastTick = win.performance.now(); }); };
|
|
168
|
+
const visibilityChange = () => {
|
|
169
|
+
const now = win.performance.now();
|
|
170
|
+
if (timer !== undefined && !wasHidden && !hovered && !focused) remaining -= now - lastTick;
|
|
171
|
+
lastTick = now; wasHidden = doc.hidden;
|
|
172
|
+
if (timer !== undefined && remaining <= 0) next();
|
|
173
|
+
};
|
|
174
|
+
surface.root.addEventListener("pointerenter", pointerEnter);
|
|
175
|
+
surface.root.addEventListener("pointerleave", pointerLeave);
|
|
176
|
+
surface.shadow.addEventListener("focusin", focusIn);
|
|
177
|
+
surface.shadow.addEventListener("focusout", focusOut);
|
|
178
|
+
doc.addEventListener("keydown", keydown, true);
|
|
179
|
+
doc.addEventListener("click", targetClick, true);
|
|
180
|
+
doc.addEventListener("visibilitychange", visibilityChange);
|
|
181
|
+
win.addEventListener("hashchange", hashStart);
|
|
182
|
+
win.addEventListener("resize", reposition);
|
|
183
|
+
win.addEventListener("scroll", reposition, { passive: true });
|
|
184
|
+
win.visualViewport?.addEventListener("resize", reposition);
|
|
185
|
+
win.visualViewport?.addEventListener("scroll", reposition);
|
|
186
|
+
motion.addEventListener("change", reposition);
|
|
187
|
+
if (!hashStart()) {
|
|
188
|
+
for (const { tour } of tours) if (tour.trigger.kind === "first-visit") {
|
|
189
|
+
try { if (!win.localStorage.getItem(`tot-tour:${tour.id}:${tour.revision}`)) { void start(tour.id); break; } }
|
|
190
|
+
catch { /* No automatic launch when storage is unavailable. */ }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { start, end, destroy() {
|
|
194
|
+
if (destroyed) return;
|
|
195
|
+
destroyed = true; end(); resizeGeneration++;
|
|
196
|
+
doc.removeEventListener("keydown", keydown, true); doc.removeEventListener("click", targetClick, true);
|
|
197
|
+
doc.removeEventListener("visibilitychange", visibilityChange);
|
|
198
|
+
win.removeEventListener("hashchange", hashStart); win.removeEventListener("resize", reposition); win.removeEventListener("scroll", reposition);
|
|
199
|
+
win.visualViewport?.removeEventListener("resize", reposition); win.visualViewport?.removeEventListener("scroll", reposition);
|
|
200
|
+
motion.removeEventListener("change", reposition);
|
|
201
|
+
surface.root.removeEventListener("pointerenter", pointerEnter); surface.root.removeEventListener("pointerleave", pointerLeave);
|
|
202
|
+
surface.shadow.removeEventListener("focusin", focusIn); surface.shadow.removeEventListener("focusout", focusOut);
|
|
203
|
+
surface.destroy();
|
|
204
|
+
} };
|
|
205
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { validateTour, type PublishedTour } from "../schema.js";
|
|
2
|
+
|
|
3
|
+
export interface TourReference {
|
|
4
|
+
id: string;
|
|
5
|
+
revision: number;
|
|
6
|
+
title: string;
|
|
7
|
+
trigger: { kind: "launcher" | "first-visit" | "link" };
|
|
8
|
+
url: string;
|
|
9
|
+
sha256: string;
|
|
10
|
+
}
|
|
11
|
+
export interface TourManifest { v: 1; player: { css: string }; tours: TourReference[] }
|
|
12
|
+
const object = (v: unknown): v is Record<string, unknown> => !!v && typeof v === "object" && !Array.isArray(v);
|
|
13
|
+
const keys = (v: Record<string, unknown>, allowed: string[]) => Object.keys(v).every(k => allowed.includes(k));
|
|
14
|
+
|
|
15
|
+
/** Pool paths are canonical and same-origin, with no query, credentials or redirects. */
|
|
16
|
+
export function poolUrl(value: unknown, extension: "json" | "css", base: string): string | null {
|
|
17
|
+
if (typeof value !== "string" || !new RegExp(`^/_a/[a-f0-9]{64}\\.${extension}$`).test(value)) return null;
|
|
18
|
+
return new URL(value, base).href;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function readManifest(value: unknown, base: string): TourManifest | null {
|
|
22
|
+
if (!object(value) || !keys(value, ["v", "player", "tours"]) || value.v !== 1 || !object(value.player) ||
|
|
23
|
+
!keys(value.player, ["css"]) || !poolUrl(value.player.css, "css", base) ||
|
|
24
|
+
!Array.isArray(value.tours) || value.tours.length > 3) return null;
|
|
25
|
+
const seen = new Set<string>();
|
|
26
|
+
for (const t of value.tours) {
|
|
27
|
+
if (!object(t) || !keys(t, ["id", "revision", "title", "trigger", "url", "sha256"]) ||
|
|
28
|
+
typeof t.id !== "string" || !/^tour_[a-z0-9][a-z0-9-]{1,62}$/.test(t.id) || seen.has(t.id) ||
|
|
29
|
+
!Number.isInteger(t.revision) || Number(t.revision) < 1 || Number(t.revision) > 2 ** 31 - 1 ||
|
|
30
|
+
typeof t.title !== "string" || !object(t.trigger) || !keys(t.trigger, ["kind"]) ||
|
|
31
|
+
!["launcher", "first-visit", "link"].includes(String(t.trigger.kind)) ||
|
|
32
|
+
typeof t.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(t.sha256) ||
|
|
33
|
+
!poolUrl(t.url, "json", base) || t.url !== `/_a/${t.sha256}.json`) return null;
|
|
34
|
+
seen.add(t.id);
|
|
35
|
+
}
|
|
36
|
+
return value as unknown as TourManifest;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type TourLoadOutcome = { status: "loaded"; tour: PublishedTour } | { status: "refused" | "unavailable" };
|
|
40
|
+
|
|
41
|
+
export async function loadTour(ref: TourReference, doc: Document): Promise<TourLoadOutcome> {
|
|
42
|
+
const win = doc.defaultView;
|
|
43
|
+
if (!win) return { status: "refused" };
|
|
44
|
+
try {
|
|
45
|
+
const url = poolUrl(ref.url, "json", win.location.href);
|
|
46
|
+
if (!url || new URL(url).origin !== win.location.origin) return { status: "refused" };
|
|
47
|
+
const response = await win.fetch(url, { credentials: "omit", redirect: "error", mode: "same-origin" });
|
|
48
|
+
if (!response.ok) return { status: "refused" };
|
|
49
|
+
if (!response.body) return { status: "refused" };
|
|
50
|
+
const reader = response.body.getReader();
|
|
51
|
+
const chunks: Uint8Array[] = [];
|
|
52
|
+
let size = 0;
|
|
53
|
+
try {
|
|
54
|
+
for (;;) {
|
|
55
|
+
const { value, done } = await reader.read();
|
|
56
|
+
if (done) break;
|
|
57
|
+
size += value.byteLength;
|
|
58
|
+
if (size > 32768) { await reader.cancel(); return { status: "refused" }; }
|
|
59
|
+
chunks.push(value);
|
|
60
|
+
}
|
|
61
|
+
} finally { reader.releaseLock(); }
|
|
62
|
+
const bytes = new Uint8Array(size);
|
|
63
|
+
let offset = 0;
|
|
64
|
+
for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.byteLength; }
|
|
65
|
+
const digest = await win.crypto.subtle.digest("SHA-256", bytes);
|
|
66
|
+
const hex = Array.from(new Uint8Array(digest), n => n.toString(16).padStart(2, "0")).join("");
|
|
67
|
+
if (hex !== ref.sha256) return { status: "refused" };
|
|
68
|
+
const result = validateTour(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)), "published");
|
|
69
|
+
if (!result.ok) return { status: "refused" };
|
|
70
|
+
const tour = result.doc;
|
|
71
|
+
if (tour.id !== ref.id || tour.revision !== ref.revision || tour.title !== ref.title ||
|
|
72
|
+
tour.trigger.kind !== ref.trigger.kind || tour.page.path !== win.location.pathname ||
|
|
73
|
+
(tour.locale !== undefined && tour.locale !== doc.documentElement.lang)) return { status: "refused" };
|
|
74
|
+
return { status: "loaded", tour };
|
|
75
|
+
} catch { return { status: "unavailable" }; }
|
|
76
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
:host {
|
|
2
|
+
all: initial;
|
|
3
|
+
position: fixed;
|
|
4
|
+
inset: 0;
|
|
5
|
+
z-index: 2147483646;
|
|
6
|
+
pointer-events: none;
|
|
7
|
+
color: #182033;
|
|
8
|
+
font: 400 16px/1.5 system-ui, sans-serif;
|
|
9
|
+
text-align: start;
|
|
10
|
+
color-scheme: light;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
*, *::before, *::after { box-sizing: border-box; }
|
|
14
|
+
|
|
15
|
+
.tour-content { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
|
|
16
|
+
|
|
17
|
+
.tour-backdrop {
|
|
18
|
+
position: fixed;
|
|
19
|
+
inset: 0;
|
|
20
|
+
background: rgb(16 24 40 / 45%);
|
|
21
|
+
pointer-events: auto;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.tour-panel {
|
|
25
|
+
position: fixed;
|
|
26
|
+
margin: 0;
|
|
27
|
+
max-width: calc(100vw - 32px);
|
|
28
|
+
max-height: calc(100vh - 32px);
|
|
29
|
+
max-height: calc(100dvh - 32px);
|
|
30
|
+
overflow: visible;
|
|
31
|
+
padding: 20px;
|
|
32
|
+
border: 1px solid #b7c1d2;
|
|
33
|
+
border-radius: 12px;
|
|
34
|
+
background: #fff;
|
|
35
|
+
color: #182033;
|
|
36
|
+
box-shadow: 0 8px 32px rgb(16 24 40 / 22%);
|
|
37
|
+
pointer-events: auto;
|
|
38
|
+
overflow-wrap: anywhere;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.tour-panel h2 { margin: 0 0 8px; font-size: 20px; line-height: 1.3; font-weight: 700; }
|
|
42
|
+
.tour-panel-body { max-height: calc(100vh - 74px); max-height: calc(100dvh - 74px); overflow: auto; }
|
|
43
|
+
.tour-copy { margin: 0 0 16px; white-space: pre-wrap; }
|
|
44
|
+
.tour-counter { margin: 0 0 12px; font-size: 14px; color: #46536b; }
|
|
45
|
+
.tour-controls { display: flex; flex-wrap: wrap; gap: 8px; }
|
|
46
|
+
|
|
47
|
+
.tour-controls button {
|
|
48
|
+
min-width: 44px;
|
|
49
|
+
min-height: 44px;
|
|
50
|
+
padding: 8px 12px;
|
|
51
|
+
border: 1px solid #718099;
|
|
52
|
+
border-radius: 6px;
|
|
53
|
+
background: #fff;
|
|
54
|
+
color: #182033;
|
|
55
|
+
font: inherit;
|
|
56
|
+
line-height: 1.25;
|
|
57
|
+
cursor: pointer;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.tour-controls button:hover { background: #edf1f8; }
|
|
61
|
+
.tour-controls .tour-primary { background: #234bc8; border-color: #234bc8; color: #fff; }
|
|
62
|
+
.tour-controls .tour-primary:hover { background: #193ba3; }
|
|
63
|
+
.tour-controls button:focus-visible, .tour-panel:focus-visible { outline: 3px solid #1447d5; outline-offset: 3px; }
|
|
64
|
+
|
|
65
|
+
.tour-launcher {
|
|
66
|
+
position: fixed;
|
|
67
|
+
right: 16px;
|
|
68
|
+
bottom: 16px;
|
|
69
|
+
display: flex;
|
|
70
|
+
flex-direction: column;
|
|
71
|
+
align-items: flex-end;
|
|
72
|
+
gap: 8px;
|
|
73
|
+
max-width: calc(100vw - 32px);
|
|
74
|
+
max-height: calc(100vh - 32px);
|
|
75
|
+
overflow: auto;
|
|
76
|
+
pointer-events: auto;
|
|
77
|
+
}
|
|
78
|
+
.tour-launcher[hidden] { display: none; }
|
|
79
|
+
.tour-launcher button {
|
|
80
|
+
min-height: 44px;
|
|
81
|
+
max-width: 100%;
|
|
82
|
+
padding: 10px 16px;
|
|
83
|
+
border: 1px solid #234bc8;
|
|
84
|
+
border-radius: 8px;
|
|
85
|
+
background: #234bc8;
|
|
86
|
+
color: #fff;
|
|
87
|
+
font: inherit;
|
|
88
|
+
overflow-wrap: anywhere;
|
|
89
|
+
cursor: pointer;
|
|
90
|
+
}
|
|
91
|
+
.tour-launcher button:hover { background: #193ba3; }
|
|
92
|
+
.tour-launcher button:focus-visible { outline: 3px solid #1447d5; outline-offset: 3px; }
|
|
93
|
+
|
|
94
|
+
.tour-highlight {
|
|
95
|
+
position: fixed;
|
|
96
|
+
border: 3px solid #315cdb;
|
|
97
|
+
border-radius: 8px;
|
|
98
|
+
pointer-events: none;
|
|
99
|
+
animation: tour-glow 1.8s ease-in-out infinite alternate;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.tour-highlight-neutral { border-color: #46536b; }
|
|
103
|
+
|
|
104
|
+
.tour-arrow {
|
|
105
|
+
position: absolute;
|
|
106
|
+
width: 14px;
|
|
107
|
+
height: 14px;
|
|
108
|
+
background: #fff;
|
|
109
|
+
transform: rotate(45deg);
|
|
110
|
+
pointer-events: none;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.tour-text[data-placement="bottom"] .tour-arrow { border-top: 1px solid #b7c1d2; border-left: 1px solid #b7c1d2; }
|
|
114
|
+
.tour-text[data-placement="top"] .tour-arrow { border-bottom: 1px solid #b7c1d2; border-right: 1px solid #b7c1d2; }
|
|
115
|
+
.tour-text[data-placement="right"] .tour-arrow { border-bottom: 1px solid #b7c1d2; border-left: 1px solid #b7c1d2; }
|
|
116
|
+
.tour-text[data-placement="left"] .tour-arrow { border-top: 1px solid #b7c1d2; border-right: 1px solid #b7c1d2; }
|
|
117
|
+
|
|
118
|
+
@keyframes tour-glow {
|
|
119
|
+
from { box-shadow: 0 0 0 2px rgb(49 92 219 / 16%); }
|
|
120
|
+
to { box-shadow: 0 0 0 7px rgb(49 92 219 / 32%); }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
:host([data-reduced-motion="true"]) .tour-highlight { animation: none; }
|
|
124
|
+
@media (prefers-reduced-motion: reduce) {
|
|
125
|
+
.tour-highlight { animation: none; }
|
|
126
|
+
}
|
|
127
|
+
@media (forced-colors: active) {
|
|
128
|
+
.tour-highlight { border-color: Highlight; animation: none; }
|
|
129
|
+
.tour-panel, .tour-controls button { border-color: ButtonText; }
|
|
130
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { regionFallbackInDocument, settleTarget } from "../resolve/index.js";
|
|
2
|
+
import type { Resolver } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/** The shared DOM adapter settles absent targets; it never acts on the page. */
|
|
5
|
+
export const resolveDocument: Resolver = (target, doc, viewport, authored) => new Promise(done => {
|
|
6
|
+
settleTarget(doc, target, { viewport, authored }, (resolution, node) => done({ resolution, node }));
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export const resolveFallback: Resolver = (target, doc, viewport, authored) => ({
|
|
10
|
+
resolution: regionFallbackInDocument(doc, target, { viewport, authored }) ?? { state: "absent", reason: "path-miss" },
|
|
11
|
+
node: null,
|
|
12
|
+
});
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import type { Placement, TourStep } from "../schema.js";
|
|
2
|
+
import type { TargetResolution } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export interface SurfaceCallbacks {
|
|
5
|
+
back(): void;
|
|
6
|
+
next(): void;
|
|
7
|
+
end(): void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PlayerSurface {
|
|
11
|
+
readonly root: HTMLElement;
|
|
12
|
+
readonly shadow: ShadowRoot;
|
|
13
|
+
show(step: TourStep, resolution: TargetResolution, index: number, total: number, callbacks: SurfaceCallbacks, reducedMotion: boolean): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
destroy(): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Side = "top" | "right" | "bottom" | "left";
|
|
19
|
+
type Rect = { x: number; y: number; width: number; height: number };
|
|
20
|
+
const GUTTER = 16;
|
|
21
|
+
const GAP = 16;
|
|
22
|
+
const SIDES: readonly Side[] = ["top", "right", "bottom", "left"];
|
|
23
|
+
const OPPOSITE: Record<Side, Side> = { top: "bottom", right: "left", bottom: "top", left: "right" };
|
|
24
|
+
|
|
25
|
+
function clamp(value: number, min: number, max: number): number {
|
|
26
|
+
return Math.max(min, Math.min(value, Math.max(min, max)));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function activeElement(doc: Document): HTMLElement | null {
|
|
30
|
+
let active = doc.activeElement;
|
|
31
|
+
while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
|
|
32
|
+
return active && "focus" in active ? active as HTMLElement : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The host occupies no document flow; all authored copy is inserted as plain text. */
|
|
36
|
+
export function createSurface(doc: Document, cssUrl: string): PlayerSurface {
|
|
37
|
+
const win = doc.defaultView;
|
|
38
|
+
const root = doc.createElement("div");
|
|
39
|
+
root.setAttribute("data-tot-tour-player", "");
|
|
40
|
+
// Fixed before the stylesheet loads, so attaching the host never reserves flow space.
|
|
41
|
+
root.style.position = "fixed";
|
|
42
|
+
root.style.inset = "0";
|
|
43
|
+
root.style.pointerEvents = "none";
|
|
44
|
+
const shadow = root.attachShadow({ mode: "open" });
|
|
45
|
+
const stylesheet = doc.createElement("link");
|
|
46
|
+
stylesheet.rel = "stylesheet";
|
|
47
|
+
stylesheet.href = cssUrl;
|
|
48
|
+
const content = doc.createElement("div");
|
|
49
|
+
content.className = "tour-content";
|
|
50
|
+
shadow.append(stylesheet, content);
|
|
51
|
+
doc.body.append(root);
|
|
52
|
+
|
|
53
|
+
let current: { step: TourStep; resolution: TargetResolution; index: number; total: number; callbacks: SurfaceCallbacks } | null = null;
|
|
54
|
+
let panel: HTMLElement | null = null;
|
|
55
|
+
let border: HTMLElement | null = null;
|
|
56
|
+
let arrow: HTMLElement | null = null;
|
|
57
|
+
let primary: HTMLButtonElement | null = null;
|
|
58
|
+
let modal = false;
|
|
59
|
+
let destroyed = false;
|
|
60
|
+
let origin: HTMLElement | null = null;
|
|
61
|
+
let modalOrigin: HTMLElement | null = null;
|
|
62
|
+
|
|
63
|
+
function focus(target: HTMLElement | null): void {
|
|
64
|
+
if (target?.isConnected) target.focus({ preventScroll: true });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function restoreModal(): void {
|
|
68
|
+
modal = false;
|
|
69
|
+
const target = modalOrigin;
|
|
70
|
+
modalOrigin = null;
|
|
71
|
+
focus(target);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function viewport(): { width: number; height: number; x: number; y: number } {
|
|
75
|
+
const visual = win?.visualViewport;
|
|
76
|
+
return { width: visual?.width ?? win?.innerWidth ?? doc.documentElement.clientWidth,
|
|
77
|
+
height: visual?.height ?? win?.innerHeight ?? doc.documentElement.clientHeight,
|
|
78
|
+
x: visual?.offsetLeft ?? 0, y: visual?.offsetTop ?? 0 };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function position(): void {
|
|
82
|
+
if (!current || !panel) return;
|
|
83
|
+
const { step, resolution } = current;
|
|
84
|
+
const view = viewport();
|
|
85
|
+
panel.style.width = `${Math.max(0, Math.min(step.kind === "modal" ? 400 : 320, view.width - GUTTER * 2))}px`;
|
|
86
|
+
panel.style.maxHeight = `${Math.max(0, view.height - GUTTER * 2)}px`;
|
|
87
|
+
const body = panel.querySelector<HTMLElement>(".tour-panel-body");
|
|
88
|
+
if (body) body.style.maxHeight = `${Math.max(0, view.height - 74)}px`;
|
|
89
|
+
const measured = panel.getBoundingClientRect();
|
|
90
|
+
const width = measured.width || Number.parseFloat(panel.style.width);
|
|
91
|
+
const height = Math.min(measured.height, Math.max(0, view.height - GUTTER * 2));
|
|
92
|
+
const rect: Rect | null = resolution.state === "resolved" ? {
|
|
93
|
+
x: resolution.rect.x - (win?.scrollX ?? 0) - view.x,
|
|
94
|
+
y: resolution.rect.y - (win?.scrollY ?? 0) - view.y,
|
|
95
|
+
width: resolution.rect.width,
|
|
96
|
+
height: resolution.rect.height,
|
|
97
|
+
} : null;
|
|
98
|
+
if (border && rect && step.kind === "highlight") {
|
|
99
|
+
const padding = step.padding ?? 6;
|
|
100
|
+
border.style.left = `${rect.x - padding + view.x}px`;
|
|
101
|
+
border.style.top = `${rect.y - padding + view.y}px`;
|
|
102
|
+
border.style.width = `${rect.width + padding * 2}px`;
|
|
103
|
+
border.style.height = `${rect.height + padding * 2}px`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let x = (view.width - width) / 2;
|
|
107
|
+
let y = view.height - GUTTER - height;
|
|
108
|
+
let side: Side = "bottom";
|
|
109
|
+
const placement: Placement = step.kind === "toast" ? "auto" : (step.placement ?? (step.kind === "modal" ? "center" : "auto"));
|
|
110
|
+
if (step.kind === "modal" && (!rect || placement === "center")) {
|
|
111
|
+
y = (view.height - height) / 2;
|
|
112
|
+
} else if (rect) {
|
|
113
|
+
const padding = step.kind === "highlight" ? step.padding ?? 6 : 0;
|
|
114
|
+
const gap = GAP + padding;
|
|
115
|
+
const preferred: Side = placement === "auto" || placement === "center" ? "bottom" : placement;
|
|
116
|
+
const order = [...new Set([preferred, OPPOSITE[preferred], ...SIDES])];
|
|
117
|
+
for (const candidate of order) {
|
|
118
|
+
const point = candidate === "top" ? { x: rect.x + (rect.width - width) / 2, y: rect.y - gap - height }
|
|
119
|
+
: candidate === "right" ? { x: rect.x + rect.width + gap, y: rect.y + (rect.height - height) / 2 }
|
|
120
|
+
: candidate === "bottom" ? { x: rect.x + (rect.width - width) / 2, y: rect.y + rect.height + gap }
|
|
121
|
+
: { x: rect.x - gap - width, y: rect.y + (rect.height - height) / 2 };
|
|
122
|
+
if (point.x >= GUTTER && point.y >= GUTTER && point.x + width <= view.width - GUTTER && point.y + height <= view.height - GUTTER) {
|
|
123
|
+
x = point.x;
|
|
124
|
+
y = point.y;
|
|
125
|
+
side = candidate;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} else if (step.kind === "toast") {
|
|
130
|
+
const corner = step.position ?? "bottom-end";
|
|
131
|
+
const rtl = doc.documentElement.dir === "rtl";
|
|
132
|
+
const start = corner.endsWith("start");
|
|
133
|
+
x = start !== rtl ? GUTTER : view.width - GUTTER - width;
|
|
134
|
+
y = corner.startsWith("top") ? GUTTER : view.height - GUTTER - height;
|
|
135
|
+
}
|
|
136
|
+
x = clamp(x, GUTTER, view.width - GUTTER - width);
|
|
137
|
+
y = clamp(y, GUTTER, view.height - GUTTER - height);
|
|
138
|
+
panel.style.left = `${x + view.x}px`;
|
|
139
|
+
panel.style.top = `${y + view.y}px`;
|
|
140
|
+
panel.dataset.placement = side;
|
|
141
|
+
if (arrow && rect) {
|
|
142
|
+
arrow.style.left = `${side === "left" ? width - 7 : side === "right" ? -7 : clamp(rect.x + rect.width / 2 - x - 7, 16, width - 30)}px`;
|
|
143
|
+
arrow.style.top = `${side === "top" ? height - 7 : side === "bottom" ? -7 : clamp(rect.y + rect.height / 2 - y - 7, 16, height - 30)}px`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function onKeyDown(event: KeyboardEvent): void {
|
|
148
|
+
if (!current || !panel) return;
|
|
149
|
+
if (event.key === "Escape" && !event.defaultPrevented) {
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
current.callbacks.end();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (!modal || event.key !== "Tab") return;
|
|
155
|
+
const buttons = [...panel.querySelectorAll<HTMLButtonElement>("button")].filter((button) => !button.disabled);
|
|
156
|
+
const first = buttons[0];
|
|
157
|
+
const last = buttons[buttons.length - 1];
|
|
158
|
+
const active = shadow.activeElement;
|
|
159
|
+
if (event.shiftKey && (active === first || !active || !panel.contains(active))) {
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
focus(last ?? panel);
|
|
162
|
+
} else if (!event.shiftKey && (active === last || !active || !panel.contains(active))) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
focus(first ?? panel);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function onFocusIn(event: FocusEvent): void {
|
|
169
|
+
if (modal && panel && !event.composedPath().includes(panel)) focus(primary ?? panel);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
doc.addEventListener("keydown", onKeyDown, true);
|
|
173
|
+
doc.addEventListener("focusin", onFocusIn, true);
|
|
174
|
+
stylesheet.addEventListener("load", position);
|
|
175
|
+
|
|
176
|
+
function clear(): void {
|
|
177
|
+
const wasModal = modal;
|
|
178
|
+
current = null;
|
|
179
|
+
if (modal) restoreModal();
|
|
180
|
+
content.replaceChildren();
|
|
181
|
+
panel = null;
|
|
182
|
+
border = null;
|
|
183
|
+
arrow = null;
|
|
184
|
+
primary = null;
|
|
185
|
+
if (!wasModal) focus(origin);
|
|
186
|
+
origin = null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
root,
|
|
191
|
+
shadow,
|
|
192
|
+
show(step, resolution, index, total, callbacks, reducedMotion) {
|
|
193
|
+
if (destroyed) return;
|
|
194
|
+
root.dataset.reducedMotion = String(reducedMotion);
|
|
195
|
+
if (current?.step === step && current.index === index && current.total === total && current.resolution.state === resolution.state) {
|
|
196
|
+
current.resolution = resolution;
|
|
197
|
+
current.callbacks = callbacks;
|
|
198
|
+
position();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (!current) origin = activeElement(doc);
|
|
202
|
+
if (modal && step.kind !== "modal") restoreModal();
|
|
203
|
+
if (step.kind === "modal" && !modal) {
|
|
204
|
+
const active = activeElement(doc);
|
|
205
|
+
modalOrigin = active && !shadow.contains(active) && active !== root ? active : origin;
|
|
206
|
+
modal = true;
|
|
207
|
+
}
|
|
208
|
+
current = { step, resolution, index, total, callbacks };
|
|
209
|
+
content.replaceChildren();
|
|
210
|
+
border = null;
|
|
211
|
+
arrow = null;
|
|
212
|
+
if ((step.kind === "highlight" || (step.kind === "text" && step.onUnresolved === "skip")) && resolution.state !== "resolved") {
|
|
213
|
+
panel = null;
|
|
214
|
+
primary = null;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (modal) {
|
|
218
|
+
const backdrop = doc.createElement("div");
|
|
219
|
+
backdrop.className = "tour-backdrop";
|
|
220
|
+
backdrop.setAttribute("aria-hidden", "true");
|
|
221
|
+
content.append(backdrop);
|
|
222
|
+
}
|
|
223
|
+
if (step.kind === "highlight") {
|
|
224
|
+
border = doc.createElement("div");
|
|
225
|
+
border.className = `tour-highlight tour-highlight-${step.tone ?? "accent"}`;
|
|
226
|
+
border.setAttribute("aria-hidden", "true");
|
|
227
|
+
content.append(border);
|
|
228
|
+
}
|
|
229
|
+
panel = doc.createElement("section");
|
|
230
|
+
panel.className = `tour-panel tour-${step.kind}`;
|
|
231
|
+
panel.tabIndex = -1;
|
|
232
|
+
panel.setAttribute("role", modal ? "dialog" : "region");
|
|
233
|
+
if (modal) panel.setAttribute("aria-modal", "true");
|
|
234
|
+
else panel.setAttribute("aria-label", "Tour step");
|
|
235
|
+
const panelBody = doc.createElement("div");
|
|
236
|
+
panelBody.className = "tour-panel-body";
|
|
237
|
+
panel.append(panelBody);
|
|
238
|
+
if (step.kind === "modal" || (step.kind === "toast" && step.title)) {
|
|
239
|
+
const title = doc.createElement("h2");
|
|
240
|
+
title.id = "tour-step-title";
|
|
241
|
+
title.textContent = step.title ?? "";
|
|
242
|
+
panel.setAttribute("aria-labelledby", title.id);
|
|
243
|
+
panelBody.append(title);
|
|
244
|
+
}
|
|
245
|
+
const copy = doc.createElement("p");
|
|
246
|
+
copy.id = "tour-step-copy";
|
|
247
|
+
copy.className = "tour-copy";
|
|
248
|
+
copy.textContent = step.kind === "highlight" ? step.label ?? "" : step.body;
|
|
249
|
+
if (copy.textContent) {
|
|
250
|
+
panelBody.append(copy);
|
|
251
|
+
panel.setAttribute("aria-describedby", copy.id);
|
|
252
|
+
}
|
|
253
|
+
const counter = doc.createElement("p");
|
|
254
|
+
counter.className = "tour-counter";
|
|
255
|
+
counter.setAttribute("role", "status");
|
|
256
|
+
counter.setAttribute("aria-live", "polite");
|
|
257
|
+
counter.setAttribute("aria-atomic", "true");
|
|
258
|
+
counter.textContent = `Step ${index + 1} of ${total}`;
|
|
259
|
+
const controls = doc.createElement("div");
|
|
260
|
+
controls.className = "tour-controls";
|
|
261
|
+
const button = (label: string, action: keyof SurfaceCallbacks): HTMLButtonElement => {
|
|
262
|
+
const element = doc.createElement("button");
|
|
263
|
+
element.type = "button";
|
|
264
|
+
element.textContent = label;
|
|
265
|
+
element.addEventListener("click", () => current?.callbacks[action]());
|
|
266
|
+
controls.append(element);
|
|
267
|
+
return element;
|
|
268
|
+
};
|
|
269
|
+
if (index > 0) button("Back", "back");
|
|
270
|
+
primary = button(step.kind === "modal" ? step.primaryLabel : "Next", "next");
|
|
271
|
+
primary.className = "tour-primary";
|
|
272
|
+
button(step.kind === "modal" ? step.dismissLabel ?? "End tour" : "End tour", "end");
|
|
273
|
+
panelBody.append(counter, controls);
|
|
274
|
+
if (step.kind === "text" && resolution.state === "resolved") {
|
|
275
|
+
arrow = doc.createElement("span");
|
|
276
|
+
arrow.className = "tour-arrow";
|
|
277
|
+
arrow.setAttribute("aria-hidden", "true");
|
|
278
|
+
panel.append(arrow);
|
|
279
|
+
}
|
|
280
|
+
content.append(panel);
|
|
281
|
+
position();
|
|
282
|
+
focus(primary);
|
|
283
|
+
},
|
|
284
|
+
clear,
|
|
285
|
+
destroy() {
|
|
286
|
+
if (destroyed) return;
|
|
287
|
+
clear();
|
|
288
|
+
destroyed = true;
|
|
289
|
+
doc.removeEventListener("keydown", onKeyDown, true);
|
|
290
|
+
doc.removeEventListener("focusin", onFocusIn, true);
|
|
291
|
+
stylesheet.removeEventListener("load", position);
|
|
292
|
+
root.remove();
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ViewportSnapshot } from "../../v1/types.js";
|
|
2
|
+
import type { TourTarget } from "../schema.js";
|
|
3
|
+
import type { TargetResolution } from "../resolve/index.js";
|
|
4
|
+
|
|
5
|
+
export type { TargetResolution } from "../resolve/index.js";
|
|
6
|
+
export interface ResolvedTarget { resolution: TargetResolution; node: Element | null }
|
|
7
|
+
export type Resolver = (target: TourTarget, doc: Document, viewport: ViewportSnapshot,
|
|
8
|
+
authoredViewport?: ViewportSnapshot) => TargetResolution | ResolvedTarget | Promise<TargetResolution | ResolvedTarget>;
|