@risinghq/lienzo 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -2
- package/dist/shell/assets/index-Cna8wlzF.js +4 -0
- package/dist/shell/assets/index-CoJSASvW.css +2 -0
- package/dist/shell/index.html +13 -0
- package/package.json +76 -4
- package/src/bench/bench.ts +132 -0
- package/src/bridge/page.ts +443 -0
- package/src/bridge/protocol.ts +124 -0
- package/src/bridge/record.ts +75 -0
- package/src/bridge/script.ts +24 -0
- package/src/bridge/state.ts +143 -0
- package/src/capture/capture.ts +125 -0
- package/src/capture/compare.ts +46 -0
- package/src/capture/diff.ts +16 -0
- package/src/capture/manifest.ts +60 -0
- package/src/capture/refs.ts +20 -0
- package/src/capture/replay.ts +25 -0
- package/src/capture/review.ts +78 -0
- package/src/capture/serve.ts +32 -0
- package/src/capture/snap.ts +62 -0
- package/src/capture/store.ts +28 -0
- package/src/cli.ts +287 -0
- package/src/cloud/checkout.ts +33 -0
- package/src/cloud/credentials.ts +25 -0
- package/src/cloud/link.ts +60 -0
- package/src/cloud/login.ts +85 -0
- package/src/commands/canvas.ts +46 -0
- package/src/commands/change.ts +218 -0
- package/src/commands/class.ts +66 -0
- package/src/commands/command.ts +27 -0
- package/src/commands/create.ts +95 -0
- package/src/commands/element.ts +118 -0
- package/src/commands/file.ts +96 -0
- package/src/commands/frame.ts +58 -0
- package/src/commands/override.ts +38 -0
- package/src/commands/prop.ts +18 -0
- package/src/commands/registry.ts +76 -0
- package/src/commands/rule.ts +131 -0
- package/src/commands/stage.ts +52 -0
- package/src/commands/story.ts +112 -0
- package/src/commands/style.ts +123 -0
- package/src/commands/text.ts +17 -0
- package/src/commands/token.ts +60 -0
- package/src/commands/transaction.ts +66 -0
- package/src/job/docker.ts +109 -0
- package/src/job/job.ts +125 -0
- package/src/job/server.ts +62 -0
- package/src/mirror/apply.ts +121 -0
- package/src/mirror/entry.ts +7 -0
- package/src/mirror/host.ts +68 -0
- package/src/mirror/observe.ts +271 -0
- package/src/mirror/script.ts +18 -0
- package/src/mirror/transport.ts +7 -0
- package/src/mirror/wire.ts +25 -0
- package/src/project/config.ts +98 -0
- package/src/project/design.ts +59 -0
- package/src/project/detect.ts +184 -0
- package/src/project/files.ts +30 -0
- package/src/project/git.ts +68 -0
- package/src/project/github.ts +42 -0
- package/src/project/history.ts +89 -0
- package/src/project/project.ts +106 -0
- package/src/project/spawn.ts +23 -0
- package/src/project/step.ts +15 -0
- package/src/protocol.ts +125 -0
- package/src/sandbox/forward.ts +73 -0
- package/src/sandbox/install.ts +44 -0
- package/src/sandbox/sandbox.ts +57 -0
- package/src/serve/build.ts +49 -0
- package/src/serve/canvases.ts +65 -0
- package/src/serve/dev.ts +296 -0
- package/src/serve/env.ts +16 -0
- package/src/serve/mcp.ts +65 -0
- package/src/serve/middleware.ts +172 -0
- package/src/serve/next/config.ts +68 -0
- package/src/serve/next/hook.ts +74 -0
- package/src/serve/next/loader.ts +37 -0
- package/src/serve/nuxt.ts +14 -0
- package/src/serve/plugin.ts +95 -0
- package/src/serve/preload.ts +167 -0
- package/src/serve/resend.ts +29 -0
- package/src/serve/running.ts +115 -0
- package/src/serve/screenshot.ts +26 -0
- package/src/serve/sources.ts +46 -0
- package/src/serve/uncached.ts +25 -0
- package/src/serve/vite-build.ts +15 -0
- package/src/serve/vite.ts +19 -0
- package/src/session/session.ts +96 -0
- package/src/share/ask.ts +24 -0
- package/src/share/client.ts +140 -0
- package/src/share/consent.ts +39 -0
- package/src/share/frame.ts +26 -0
- package/src/source/astro/astro.ts +10 -0
- package/src/source/astro/elements.ts +60 -0
- package/src/source/blocks.ts +42 -0
- package/src/source/css.ts +48 -0
- package/src/source/estree.ts +55 -0
- package/src/source/extract.ts +68 -0
- package/src/source/insert.ts +189 -0
- package/src/source/jsx/elements.ts +136 -0
- package/src/source/jsx/jsx.ts +5 -0
- package/src/source/language.ts +176 -0
- package/src/source/location.ts +34 -0
- package/src/source/modules.ts +41 -0
- package/src/source/overrides.ts +35 -0
- package/src/source/page.ts +69 -0
- package/src/source/patch.ts +79 -0
- package/src/source/prop.ts +34 -0
- package/src/source/refusal.ts +7 -0
- package/src/source/rules.ts +298 -0
- package/src/source/stamp.ts +28 -0
- package/src/source/story-file.ts +40 -0
- package/src/source/story.ts +162 -0
- package/src/source/structure.ts +253 -0
- package/src/source/style.ts +143 -0
- package/src/source/svelte/elements.ts +154 -0
- package/src/source/svelte/svelte.ts +11 -0
- package/src/source/tag.ts +99 -0
- package/src/source/tailwind.ts +46 -0
- package/src/source/tokens.ts +56 -0
- package/src/source/vue/elements.ts +236 -0
- package/src/source/vue/vue.ts +11 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { ATTRIBUTE, OVERRIDES } from "../protocol.ts";
|
|
2
|
+
import { record } from "./record.ts";
|
|
3
|
+
import { animationsOf, force, playback, refreshForced, scrub } from "./state.ts";
|
|
4
|
+
import {
|
|
5
|
+
CHANNEL,
|
|
6
|
+
isCall,
|
|
7
|
+
type Box,
|
|
8
|
+
type Call,
|
|
9
|
+
type Events,
|
|
10
|
+
type Matched,
|
|
11
|
+
type Methods,
|
|
12
|
+
type PageLayer,
|
|
13
|
+
type Ref,
|
|
14
|
+
} from "./protocol.ts";
|
|
15
|
+
|
|
16
|
+
// Runs inside a framed page. It answers the canvas that frames it and tells
|
|
17
|
+
// the canvas what the person does in the page. It hears only its parent window,
|
|
18
|
+
// and only when the message comes from the canvas origin the server names.
|
|
19
|
+
|
|
20
|
+
declare global {
|
|
21
|
+
// Set by the script that carries this bundle: the origin of the canvas.
|
|
22
|
+
var __lienzoCanvas: string | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const canvas = globalThis.__lienzoCanvas ?? location.origin;
|
|
26
|
+
|
|
27
|
+
function tell<K extends keyof Events>(event: K, data: Events[K]) {
|
|
28
|
+
parent.postMessage({ channel: CHANNEL, event, data }, canvas);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const slot = (at: string) => `[${ATTRIBUTE}="${CSS.escape(at)}"]`;
|
|
32
|
+
|
|
33
|
+
// A node the source does not stamp has `at` "" and its position among every
|
|
34
|
+
// element of the body, in document order.
|
|
35
|
+
const UNSTAMPED = "";
|
|
36
|
+
|
|
37
|
+
const domNodes = () => [...document.body.querySelectorAll("*")];
|
|
38
|
+
|
|
39
|
+
function nodesOf(at: string): Element[] {
|
|
40
|
+
return at === UNSTAMPED ? domNodes() : [...document.querySelectorAll(slot(at))];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function nodeOf(ref: Ref): Element | undefined {
|
|
44
|
+
return nodesOf(ref.at)[ref.index];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The nearest stamped node at or above the element, else the element itself
|
|
48
|
+
// as an unstamped node.
|
|
49
|
+
function refOf(node: Element): Ref | null {
|
|
50
|
+
const stamped = node.closest(`[${ATTRIBUTE}]`);
|
|
51
|
+
const at = stamped?.getAttribute(ATTRIBUTE);
|
|
52
|
+
if (stamped && at) return { at, index: nodesOf(at).indexOf(stamped) };
|
|
53
|
+
|
|
54
|
+
const index = domNodes().indexOf(node);
|
|
55
|
+
return index < 0 ? null : { at: UNSTAMPED, index };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A page the source stamps shows its stamped nodes. A page with none shows
|
|
59
|
+
// every element, so a person can still pick and read one.
|
|
60
|
+
function layers(): PageLayer[] {
|
|
61
|
+
return document.body.querySelector(`[${ATTRIBUTE}]`) ? stampedLayers() : domLayers();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const HIDDEN_TAGS = new Set(["script", "style", "link", "meta", "noscript", "template"]);
|
|
65
|
+
const MAX_DOM_LAYERS = 3000;
|
|
66
|
+
|
|
67
|
+
function domLayers(): PageLayer[] {
|
|
68
|
+
const roots: PageLayer[] = [];
|
|
69
|
+
const built = new Map<Element, PageLayer>();
|
|
70
|
+
|
|
71
|
+
domNodes()
|
|
72
|
+
.slice(0, MAX_DOM_LAYERS)
|
|
73
|
+
.forEach((node, index) => {
|
|
74
|
+
if (HIDDEN_TAGS.has(node.localName)) return;
|
|
75
|
+
|
|
76
|
+
const layer: PageLayer = { ref: { at: UNSTAMPED, index }, tag: node.localName, children: [] };
|
|
77
|
+
built.set(node, layer);
|
|
78
|
+
|
|
79
|
+
const parent = node.parentElement && built.get(node.parentElement);
|
|
80
|
+
(parent?.children ?? roots).push(layer);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return roots;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function stampedLayers(): PageLayer[] {
|
|
87
|
+
const roots: PageLayer[] = [];
|
|
88
|
+
const built = new Map<Element, PageLayer>();
|
|
89
|
+
const counts = new Map<string, number>();
|
|
90
|
+
|
|
91
|
+
for (const node of document.body.querySelectorAll(`[${ATTRIBUTE}]`)) {
|
|
92
|
+
const at = node.getAttribute(ATTRIBUTE) ?? "";
|
|
93
|
+
const index = counts.get(at) ?? 0;
|
|
94
|
+
counts.set(at, index + 1);
|
|
95
|
+
|
|
96
|
+
const layer: PageLayer = { ref: { at, index }, tag: node.localName, children: [] };
|
|
97
|
+
built.set(node, layer);
|
|
98
|
+
|
|
99
|
+
// Document order puts every ancestor in `built` before its descendants.
|
|
100
|
+
const parent = node.parentElement?.closest(`[${ATTRIBUTE}]`);
|
|
101
|
+
const siblings = (parent && built.get(parent)?.children) || roots;
|
|
102
|
+
siblings.push(layer);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return roots;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function computed(ref: Ref, properties: string[]): Record<string, string> | null {
|
|
109
|
+
const node = nodeOf(ref);
|
|
110
|
+
if (!node) return null;
|
|
111
|
+
|
|
112
|
+
const style = getComputedStyle(node);
|
|
113
|
+
|
|
114
|
+
return Object.fromEntries(properties.map((name) => [name, style.getPropertyValue(name)]));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Vite names the module of each `<style>` it injects, `/src/App.svelte?svelte&
|
|
118
|
+
// type=style&lang.css`; the path before the query is the file.
|
|
119
|
+
function fileOf(sheet: CSSStyleSheet): string | undefined {
|
|
120
|
+
const id = (sheet.ownerNode as Element | null)?.getAttribute?.("data-vite-dev-id");
|
|
121
|
+
|
|
122
|
+
return id?.startsWith("/") ? id.split("?")[0] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A `<style>` holds its text. A linked sheet is fetched, from the cache the
|
|
126
|
+
// page loaded it into.
|
|
127
|
+
async function textOf(sheet: CSSStyleSheet): Promise<string | undefined> {
|
|
128
|
+
const owner = sheet.ownerNode;
|
|
129
|
+
if (owner?.nodeName === "STYLE") return owner.textContent ?? undefined;
|
|
130
|
+
if (!sheet.href) return undefined;
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
return await (await fetch(sheet.href)).text();
|
|
134
|
+
} catch {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
type Found = { index: string; selector: string };
|
|
140
|
+
|
|
141
|
+
// The rules of a group that hold in this page: a `@media` query the viewport
|
|
142
|
+
// meets, a `@supports` condition the browser meets, and every `@layer` block.
|
|
143
|
+
// Whether a `@container` query holds depends on a container the CSSOM does not
|
|
144
|
+
// name, so its rules are left out.
|
|
145
|
+
function heldRules(rule: CSSRule): CSSRuleList | undefined {
|
|
146
|
+
if (rule instanceof CSSMediaRule) {
|
|
147
|
+
return matchMedia(rule.conditionText).matches ? rule.cssRules : undefined;
|
|
148
|
+
}
|
|
149
|
+
if (rule instanceof CSSSupportsRule) {
|
|
150
|
+
return CSS.supports(rule.conditionText) ? rule.cssRules : undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return rule instanceof CSSLayerBlockRule ? rule.cssRules : undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// The style rules that match the node, at an index `rule.find` takes: their
|
|
157
|
+
// position in `rules`, after the positions of the groups they sit in.
|
|
158
|
+
function matchingIn(rules: CSSRuleList, node: Element, path: number[]): Found[] {
|
|
159
|
+
return [...rules].flatMap((rule, at) => {
|
|
160
|
+
const index = [...path, at];
|
|
161
|
+
|
|
162
|
+
if (!(rule instanceof CSSStyleRule)) {
|
|
163
|
+
const held = heldRules(rule);
|
|
164
|
+
return held ? matchingIn(held, node, index) : [];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
return node.matches(rule.selectorText)
|
|
169
|
+
? [{ index: index.join("."), selector: rule.selectorText }]
|
|
170
|
+
: [];
|
|
171
|
+
} catch {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function matchingInSheet(sheet: CSSStyleSheet, node: Element): Found[] {
|
|
178
|
+
try {
|
|
179
|
+
return matchingIn(sheet.cssRules, node, []);
|
|
180
|
+
} catch {
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function matched(ref: Ref): Promise<Matched[] | null> {
|
|
186
|
+
const node = nodeOf(ref);
|
|
187
|
+
if (!node) return null;
|
|
188
|
+
|
|
189
|
+
const found = await Promise.all(
|
|
190
|
+
[...document.styleSheets].map(async (sheet) => {
|
|
191
|
+
const rules = matchingInSheet(sheet, node);
|
|
192
|
+
const css = rules.length ? await textOf(sheet) : undefined;
|
|
193
|
+
const from = fileOf(sheet);
|
|
194
|
+
|
|
195
|
+
return css === undefined
|
|
196
|
+
? []
|
|
197
|
+
: rules.map((rule) => ({ css, ...rule, ...(from && { from }) }));
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
return found.flat();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function boxOf(ref: Ref | null): Box | null {
|
|
205
|
+
const node = ref && nodeOf(ref);
|
|
206
|
+
if (!node) return null;
|
|
207
|
+
|
|
208
|
+
const { x, y, width, height } = node.getBoundingClientRect();
|
|
209
|
+
|
|
210
|
+
return { x, y, width, height, tag: node.localName };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const same = (a: Box | null, b: Box | null) =>
|
|
214
|
+
a === b ||
|
|
215
|
+
(a !== null &&
|
|
216
|
+
b !== null &&
|
|
217
|
+
a.x === b.x &&
|
|
218
|
+
a.y === b.y &&
|
|
219
|
+
a.width === b.width &&
|
|
220
|
+
a.height === b.height &&
|
|
221
|
+
a.tag === b.tag);
|
|
222
|
+
|
|
223
|
+
// The boxes of the tracked nodes, told whenever one changes. Pages scroll,
|
|
224
|
+
// reflow and hot reload on their own, so they are measured every animation
|
|
225
|
+
// frame while a node is tracked, instead of on a list of events that could
|
|
226
|
+
// miss one.
|
|
227
|
+
let tracked: { hover: Ref | null; selected: Ref | null } = { hover: null, selected: null };
|
|
228
|
+
let last: { hover: Box | null; selected: Box | null } | null = null;
|
|
229
|
+
let measuring = false;
|
|
230
|
+
|
|
231
|
+
function measure() {
|
|
232
|
+
const now = { hover: boxOf(tracked.hover), selected: boxOf(tracked.selected) };
|
|
233
|
+
|
|
234
|
+
if (!last || !same(now.hover, last.hover) || !same(now.selected, last.selected)) {
|
|
235
|
+
last = now;
|
|
236
|
+
tell("boxes", now);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
measuring = Boolean(tracked.hover || tracked.selected);
|
|
240
|
+
if (measuring) requestAnimationFrame(measure);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function track({ hover, selected }: Methods["track"]["params"]): null {
|
|
244
|
+
tracked = { hover, selected };
|
|
245
|
+
// The canvas drops what it knew when it names other nodes, so the first
|
|
246
|
+
// measure of them is always told.
|
|
247
|
+
last = null;
|
|
248
|
+
if (!measuring) {
|
|
249
|
+
measuring = true;
|
|
250
|
+
requestAnimationFrame(measure);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// A rule in the page's own head outlives a hot update that redraws the node.
|
|
257
|
+
const hiding = new Set<string>();
|
|
258
|
+
const veil = document.createElement("style");
|
|
259
|
+
|
|
260
|
+
function hide({ at, hidden }: Methods["hide"]["params"]): null {
|
|
261
|
+
if (at === UNSTAMPED) throw new Error("only a stamped element can be hidden");
|
|
262
|
+
if (hidden) hiding.add(at);
|
|
263
|
+
else hiding.delete(at);
|
|
264
|
+
|
|
265
|
+
veil.textContent = [...hiding].map((one) => `${slot(one)}{visibility:hidden!important}`).join("");
|
|
266
|
+
if (!veil.isConnected) document.head.append(veil);
|
|
267
|
+
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const recording = record((step) => tell("step", step));
|
|
272
|
+
|
|
273
|
+
const METHODS: { [K in keyof Methods]: (params: Methods[K]["params"]) => unknown } = {
|
|
274
|
+
record: ({ on }) => {
|
|
275
|
+
recording.set(on);
|
|
276
|
+
|
|
277
|
+
return null;
|
|
278
|
+
},
|
|
279
|
+
count: ({ at }) => nodesOf(at).length,
|
|
280
|
+
layers: () => layers(),
|
|
281
|
+
computed: ({ ref, properties }) => computed(ref, properties),
|
|
282
|
+
matched: ({ ref }) => matched(ref),
|
|
283
|
+
track,
|
|
284
|
+
hide,
|
|
285
|
+
pseudo: ({ ref, states }) => {
|
|
286
|
+
force(ref && (nodeOf(ref) ?? null), states);
|
|
287
|
+
|
|
288
|
+
return null;
|
|
289
|
+
},
|
|
290
|
+
animations: () => animationsOf(refOf),
|
|
291
|
+
scrub: ({ index, time }) => {
|
|
292
|
+
scrub(index, time);
|
|
293
|
+
|
|
294
|
+
return null;
|
|
295
|
+
},
|
|
296
|
+
playback: ({ paused }) => {
|
|
297
|
+
playback(paused);
|
|
298
|
+
|
|
299
|
+
return null;
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
async function answer(event: MessageEvent) {
|
|
304
|
+
if (event.source !== parent || event.origin !== canvas || !isCall(event.data)) return;
|
|
305
|
+
|
|
306
|
+
const { id, method, params } = event.data as Call;
|
|
307
|
+
const handler = METHODS[method] as ((params: unknown) => unknown) | undefined;
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
if (!handler) throw new Error(`no method "${method}"`);
|
|
311
|
+
const result = await handler(params);
|
|
312
|
+
parent.postMessage({ channel: CHANNEL, id, result }, canvas);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
315
|
+
parent.postMessage({ channel: CHANNEL, id, error: message }, canvas);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// In the canvas a click selects, so the page's own handlers must not see it.
|
|
320
|
+
// While recording, the page acts as it does for a visitor.
|
|
321
|
+
const SWALLOWED = ["mousedown", "pointerdown", "submit", "dblclick"];
|
|
322
|
+
|
|
323
|
+
const isMiddle = (event: Event) => (event as MouseEvent).button === 1;
|
|
324
|
+
|
|
325
|
+
// The middle button pans the canvas wherever it is pressed, as in every editor.
|
|
326
|
+
// Screen coordinates ignore the scale the canvas draws the frame at. It reads
|
|
327
|
+
// pointer events: `swallow` cancels `pointerdown`, which suppresses the mouse
|
|
328
|
+
// events that follow it.
|
|
329
|
+
function panWithMiddle() {
|
|
330
|
+
let last: { x: number; y: number } | undefined;
|
|
331
|
+
|
|
332
|
+
document.addEventListener(
|
|
333
|
+
"pointerdown",
|
|
334
|
+
(event) => {
|
|
335
|
+
if (event.button === 1) last = { x: event.screenX, y: event.screenY };
|
|
336
|
+
},
|
|
337
|
+
true,
|
|
338
|
+
);
|
|
339
|
+
document.addEventListener("pointermove", (event) => {
|
|
340
|
+
if (!last) return;
|
|
341
|
+
if (!(event.buttons & 4)) {
|
|
342
|
+
last = undefined;
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
tell("pan", { dx: event.screenX - last.x, dy: event.screenY - last.y });
|
|
347
|
+
last = { x: event.screenX, y: event.screenY };
|
|
348
|
+
});
|
|
349
|
+
document.addEventListener("pointerup", () => (last = undefined));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function swallow(event: Event) {
|
|
353
|
+
if (recording.on && !isMiddle(event)) return;
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
event.stopPropagation();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// A key in a framed page belongs to the canvas, so the canvas hears it and the
|
|
359
|
+
// page does not act on the keys the canvas uses: space scrolls, ctrl+d
|
|
360
|
+
// bookmarks.
|
|
361
|
+
function key(event: KeyboardEvent) {
|
|
362
|
+
const { key, shiftKey, ctrlKey, metaKey, altKey } = event;
|
|
363
|
+
tell("key", { type: event.type as "keydown" | "keyup", key, shiftKey, ctrlKey, metaKey, altKey });
|
|
364
|
+
|
|
365
|
+
const command = ctrlKey || metaKey;
|
|
366
|
+
const lower = key.toLowerCase();
|
|
367
|
+
if (key === " " || (command && (lower === "z" || lower === "d"))) event.preventDefault();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function wire() {
|
|
371
|
+
addEventListener("message", (event) => void answer(event));
|
|
372
|
+
|
|
373
|
+
const watch = new MutationObserver(() => notify());
|
|
374
|
+
// The nodes and their classes, and the stylesheets a hot update swaps.
|
|
375
|
+
watch.observe(document.documentElement, {
|
|
376
|
+
childList: true,
|
|
377
|
+
subtree: true,
|
|
378
|
+
attributes: true,
|
|
379
|
+
attributeFilter: [ATTRIBUTE, "class", "style"],
|
|
380
|
+
});
|
|
381
|
+
watch.observe(document.head ?? document.documentElement, { characterData: true, subtree: true });
|
|
382
|
+
|
|
383
|
+
document.addEventListener("mousemove", (event) =>
|
|
384
|
+
tell("hover", event.target instanceof Element ? refOf(event.target) : null),
|
|
385
|
+
);
|
|
386
|
+
document.addEventListener("mouseleave", () => tell("hover", null));
|
|
387
|
+
|
|
388
|
+
document.addEventListener(
|
|
389
|
+
"click",
|
|
390
|
+
(event) => {
|
|
391
|
+
if (recording.on) return;
|
|
392
|
+
swallow(event);
|
|
393
|
+
tell("select", event.target instanceof Element ? refOf(event.target) : null);
|
|
394
|
+
},
|
|
395
|
+
true,
|
|
396
|
+
);
|
|
397
|
+
for (const type of SWALLOWED) document.addEventListener(type, swallow, true);
|
|
398
|
+
|
|
399
|
+
document.addEventListener("keydown", key);
|
|
400
|
+
document.addEventListener("keyup", key);
|
|
401
|
+
|
|
402
|
+
panWithMiddle();
|
|
403
|
+
|
|
404
|
+
// A plain wheel scrolls the page. A pinch, or ctrl and wheel, zooms the canvas.
|
|
405
|
+
document.addEventListener(
|
|
406
|
+
"wheel",
|
|
407
|
+
(event) => {
|
|
408
|
+
if (!event.ctrlKey && !event.metaKey) return;
|
|
409
|
+
event.preventDefault();
|
|
410
|
+
tell("wheel", { x: event.clientX, y: event.clientY, deltaY: event.deltaY });
|
|
411
|
+
},
|
|
412
|
+
{ passive: false },
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// A hot reload rewrites many nodes at once; one notice per animation frame is
|
|
417
|
+
// enough.
|
|
418
|
+
let pending = false;
|
|
419
|
+
|
|
420
|
+
function notify() {
|
|
421
|
+
if (pending) return;
|
|
422
|
+
pending = true;
|
|
423
|
+
|
|
424
|
+
requestAnimationFrame(() => {
|
|
425
|
+
pending = false;
|
|
426
|
+
refreshForced();
|
|
427
|
+
tell("changed", null);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function loadOverrides() {
|
|
432
|
+
const link = document.createElement("link");
|
|
433
|
+
link.rel = "stylesheet";
|
|
434
|
+
link.href = OVERRIDES;
|
|
435
|
+
document.head.append(link);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (parent !== window) {
|
|
439
|
+
wire();
|
|
440
|
+
loadOverrides();
|
|
441
|
+
tell("ready", null);
|
|
442
|
+
notify();
|
|
443
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// What the canvas and the bridge in a framed page say to each other, by
|
|
2
|
+
// `postMessage`. The page may sit on another origin than the canvas, so the
|
|
3
|
+
// canvas never reads the page's document: it asks the bridge.
|
|
4
|
+
|
|
5
|
+
import type { Step } from "../project/step.ts";
|
|
6
|
+
|
|
7
|
+
export const CHANNEL = "lienzo";
|
|
8
|
+
|
|
9
|
+
// One node of the page: the element's stamped location, and which of the nodes
|
|
10
|
+
// that element draws.
|
|
11
|
+
export type Ref = { at: string; index: number };
|
|
12
|
+
|
|
13
|
+
// A node's box in the page's viewport, in px, and its tag.
|
|
14
|
+
export type Box = { x: number; y: number; width: number; height: number; tag: string };
|
|
15
|
+
|
|
16
|
+
// A stamped node and the stamped nodes nearest below it.
|
|
17
|
+
export type PageLayer = { ref: Ref; tag: string; children: PageLayer[] };
|
|
18
|
+
|
|
19
|
+
// A rule the browser applies to a node: the text of its stylesheet, its index
|
|
20
|
+
// in the sheet and its selector, which `rule.find` takes, and the file the
|
|
21
|
+
// sheet was served from, when the page names it.
|
|
22
|
+
export type Matched = { css: string; index: string; selector: string; from?: string };
|
|
23
|
+
|
|
24
|
+
export const FORCED_STATES = ["hover", "focus", "focus-visible", "active"] as const;
|
|
25
|
+
export type ForcedState = (typeof FORCED_STATES)[number] | "focus-within";
|
|
26
|
+
|
|
27
|
+
// One running animation of the page. `index` is its place in the page's list
|
|
28
|
+
// now, which `scrub` takes; `time` and `duration` are in ms.
|
|
29
|
+
export type AnimationInfo = {
|
|
30
|
+
index: number;
|
|
31
|
+
name: string;
|
|
32
|
+
kind: "css-animation" | "css-transition" | "script";
|
|
33
|
+
target: Ref | null;
|
|
34
|
+
duration: number;
|
|
35
|
+
delay: number;
|
|
36
|
+
iterations: number;
|
|
37
|
+
time: number;
|
|
38
|
+
paused: boolean;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// A key the page heard, as much of it as a shortcut reads.
|
|
42
|
+
export type Key = {
|
|
43
|
+
type: "keydown" | "keyup";
|
|
44
|
+
key: string;
|
|
45
|
+
shiftKey: boolean;
|
|
46
|
+
ctrlKey: boolean;
|
|
47
|
+
metaKey: boolean;
|
|
48
|
+
altKey: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// What the canvas asks. `track` names the nodes whose boxes the bridge reports
|
|
52
|
+
// as they move.
|
|
53
|
+
export type Methods = {
|
|
54
|
+
count: { params: { at: string }; result: number };
|
|
55
|
+
layers: { params: Record<string, never>; result: PageLayer[] };
|
|
56
|
+
computed: {
|
|
57
|
+
params: { ref: Ref; properties: string[] };
|
|
58
|
+
result: Record<string, string> | null;
|
|
59
|
+
};
|
|
60
|
+
matched: { params: { ref: Ref }; result: Matched[] | null };
|
|
61
|
+
track: { params: { hover: Ref | null; selected: Ref | null }; result: null };
|
|
62
|
+
// While on, the page acts on clicks and keys as it does for a visitor, and
|
|
63
|
+
// tells each one as a `step` instead of selecting what was clicked.
|
|
64
|
+
record: { params: { on: boolean }; result: null };
|
|
65
|
+
// Hides or shows every node a stamped element draws. Only the page's look
|
|
66
|
+
// changes: the source and the layout stay.
|
|
67
|
+
hide: { params: { at: string; hidden: boolean }; result: null };
|
|
68
|
+
pseudo: { params: { ref: Ref | null; states: ForcedState[] }; result: null };
|
|
69
|
+
animations: { params: Record<string, never>; result: AnimationInfo[] };
|
|
70
|
+
scrub: { params: { index: number; time: number }; result: null };
|
|
71
|
+
playback: { params: { paused: boolean }; result: null };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// What the page tells the canvas without being asked.
|
|
75
|
+
export type Events = {
|
|
76
|
+
ready: null;
|
|
77
|
+
changed: null;
|
|
78
|
+
hover: Ref | null;
|
|
79
|
+
select: Ref | null;
|
|
80
|
+
step: Step;
|
|
81
|
+
boxes: { hover: Box | null; selected: Box | null };
|
|
82
|
+
key: Key;
|
|
83
|
+
wheel: { x: number; y: number; deltaY: number };
|
|
84
|
+
// How far the middle button dragged since the last notice, in screen px.
|
|
85
|
+
pan: { dx: number; dy: number };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export type Call = {
|
|
89
|
+
channel: typeof CHANNEL;
|
|
90
|
+
id: number;
|
|
91
|
+
method: keyof Methods;
|
|
92
|
+
params: unknown;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export type Reply = {
|
|
96
|
+
channel: typeof CHANNEL;
|
|
97
|
+
id: number;
|
|
98
|
+
result?: unknown;
|
|
99
|
+
error?: string;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export type Notice = {
|
|
103
|
+
channel: typeof CHANNEL;
|
|
104
|
+
event: keyof Events;
|
|
105
|
+
data: unknown;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export function isCall(value: unknown): value is Call {
|
|
109
|
+
const call = value as Call | null;
|
|
110
|
+
|
|
111
|
+
return call?.channel === CHANNEL && typeof call.id === "number" && "method" in call;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function isReply(value: unknown): value is Reply {
|
|
115
|
+
const reply = value as Reply | null;
|
|
116
|
+
|
|
117
|
+
return reply?.channel === CHANNEL && typeof reply.id === "number" && !("method" in reply);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function isNotice(value: unknown): value is Notice {
|
|
121
|
+
const notice = value as Notice | null;
|
|
122
|
+
|
|
123
|
+
return notice?.channel === CHANNEL && typeof notice.event === "string";
|
|
124
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { Step } from "../project/step.ts";
|
|
2
|
+
import { ATTRIBUTE } from "../protocol.ts";
|
|
3
|
+
|
|
4
|
+
// The keys a flow presses. Typing is a fill, so it is not listed here.
|
|
5
|
+
const KEYS = new Set(["Enter", "Escape", "Tab"]);
|
|
6
|
+
|
|
7
|
+
const TEXT =
|
|
8
|
+
"input:not([type=checkbox],[type=radio],[type=button],[type=submit],[type=file]),textarea";
|
|
9
|
+
|
|
10
|
+
// Hears a visitor's clicks, typing and keys in the page and turns each into the
|
|
11
|
+
// step that repeats it. `told` receives the steps in order.
|
|
12
|
+
//
|
|
13
|
+
// Typing records as one `fill` with the field's final value. It is told before
|
|
14
|
+
// whatever ends it (a click, a key, leaving the field), so the order of the
|
|
15
|
+
// steps is the order of the visitor's actions.
|
|
16
|
+
export function record(told: (step: Step) => void) {
|
|
17
|
+
let on = false;
|
|
18
|
+
let typing: HTMLInputElement | HTMLTextAreaElement | null = null;
|
|
19
|
+
|
|
20
|
+
const target = (event: Event) =>
|
|
21
|
+
event.target instanceof Element ? event.target.closest(`[${ATTRIBUTE}]`) : null;
|
|
22
|
+
|
|
23
|
+
// The stamp, and the element's index among those with it when more than one
|
|
24
|
+
// has it, which is the only time a replay needs an index.
|
|
25
|
+
function where(node: Element): { at: string; nth?: number } {
|
|
26
|
+
const at = node.getAttribute(ATTRIBUTE) ?? "";
|
|
27
|
+
const all = [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(at)}"]`)];
|
|
28
|
+
|
|
29
|
+
return all.length > 1 ? { at, nth: all.indexOf(node) } : { at };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function typed() {
|
|
33
|
+
const field = typing;
|
|
34
|
+
typing = null;
|
|
35
|
+
const node = field?.closest(`[${ATTRIBUTE}]`);
|
|
36
|
+
if (field && node) told({ do: "fill", ...where(node), value: field.value });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function click(event: Event) {
|
|
40
|
+
const node = target(event);
|
|
41
|
+
if (!on || !node) return;
|
|
42
|
+
typed();
|
|
43
|
+
told({ do: "click", ...where(node) });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function input(event: Event) {
|
|
47
|
+
if (!on || !(event.target instanceof Element) || !event.target.matches(TEXT)) return;
|
|
48
|
+
if (typing && typing !== event.target) typed();
|
|
49
|
+
typing = event.target as HTMLInputElement;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function key(event: KeyboardEvent) {
|
|
53
|
+
const node = target(event);
|
|
54
|
+
if (!on || !node || !KEYS.has(event.key)) return;
|
|
55
|
+
typed();
|
|
56
|
+
told({ do: "press", ...where(node), key: event.key });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
addEventListener("click", click, true);
|
|
60
|
+
addEventListener("input", input, true);
|
|
61
|
+
addEventListener("change", typed, true);
|
|
62
|
+
addEventListener("focusout", typed, true);
|
|
63
|
+
addEventListener("keydown", key, true);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
get on() {
|
|
67
|
+
return on;
|
|
68
|
+
},
|
|
69
|
+
set(next: boolean) {
|
|
70
|
+
if (on && !next) typed();
|
|
71
|
+
on = next;
|
|
72
|
+
typing = null;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
const ENTRY = path.join(import.meta.dir, "page.ts");
|
|
4
|
+
|
|
5
|
+
let bundled: Promise<string> | undefined;
|
|
6
|
+
|
|
7
|
+
// The bridge as one script that runs in any page: its source, bundled once.
|
|
8
|
+
// `canvas` is the origin the page takes its messages from, when it is not the
|
|
9
|
+
// page's own.
|
|
10
|
+
export async function bridgeScript(canvas?: string): Promise<string> {
|
|
11
|
+
bundled ??= Bun.build({ entrypoints: [ENTRY], target: "browser", format: "iife" }).then(
|
|
12
|
+
async ({ outputs, success, logs }) => {
|
|
13
|
+
if (!success || !outputs[0]) throw new Error(`the bridge did not bundle: ${logs.join("\n")}`);
|
|
14
|
+
|
|
15
|
+
return outputs[0].text();
|
|
16
|
+
},
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const code = await bundled;
|
|
20
|
+
|
|
21
|
+
return canvas === undefined
|
|
22
|
+
? code
|
|
23
|
+
: `globalThis.__lienzoCanvas=${JSON.stringify(canvas)};\n${code}`;
|
|
24
|
+
}
|