deictic 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +167 -0
- package/dist/annotations/client.d.ts +4 -0
- package/dist/annotations/client.d.ts.map +1 -0
- package/dist/annotations/client.js +497 -0
- package/dist/annotations/client.js.map +1 -0
- package/dist/annotations/index.d.ts +110 -0
- package/dist/annotations/index.d.ts.map +1 -0
- package/dist/annotations/index.js +22 -0
- package/dist/annotations/index.js.map +1 -0
- package/dist/astro.d.ts +29 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +24 -0
- package/dist/astro.js.map +1 -0
- package/dist/bun.d.ts +12 -0
- package/dist/bun.d.ts.map +1 -0
- package/dist/bun.js +67 -0
- package/dist/bun.js.map +1 -0
- package/dist/client/index.d.ts +95 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +1333 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +15 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +17 -0
- package/dist/loader.js.map +1 -0
- package/dist/next.d.ts +17 -0
- package/dist/next.d.ts.map +1 -0
- package/dist/next.js +150 -0
- package/dist/next.js.map +1 -0
- package/dist/recommended/index.d.ts +25 -0
- package/dist/recommended/index.d.ts.map +1 -0
- package/dist/recommended/index.js +19 -0
- package/dist/recommended/index.js.map +1 -0
- package/dist/recordings/client.d.ts +4 -0
- package/dist/recordings/client.d.ts.map +1 -0
- package/dist/recordings/client.js +376 -0
- package/dist/recordings/client.js.map +1 -0
- package/dist/recordings/index.d.ts +2 -0
- package/dist/recordings/index.js +3 -0
- package/dist/screenshots/client.d.ts +23 -0
- package/dist/screenshots/client.d.ts.map +1 -0
- package/dist/screenshots/client.js +76 -0
- package/dist/screenshots/client.js.map +1 -0
- package/dist/screenshots/dom.d.ts +4 -0
- package/dist/screenshots/dom.d.ts.map +1 -0
- package/dist/screenshots/dom.js +92 -0
- package/dist/screenshots/dom.js.map +1 -0
- package/dist/screenshots/index.d.ts +2 -0
- package/dist/screenshots/index.js +98 -0
- package/dist/screenshots/index.js.map +1 -0
- package/dist/screenshots/rrweb.d.ts +9 -0
- package/dist/screenshots/rrweb.d.ts.map +1 -0
- package/dist/screenshots/rrweb.js +60 -0
- package/dist/screenshots/rrweb.js.map +1 -0
- package/dist/shared/endpoints.js +354 -0
- package/dist/shared/endpoints.js.map +1 -0
- package/dist/shared/index.d.ts +226 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index2.d.ts +98 -0
- package/dist/shared/index2.d.ts.map +1 -0
- package/dist/shared/index3.d.ts +86 -0
- package/dist/shared/index3.d.ts.map +1 -0
- package/dist/shared/index4.d.ts +184 -0
- package/dist/shared/index4.d.ts.map +1 -0
- package/dist/shared/index5.d.ts +70 -0
- package/dist/shared/index5.d.ts.map +1 -0
- package/dist/shared/integration.d.ts +20 -0
- package/dist/shared/integration.d.ts.map +1 -0
- package/dist/shared/integration.js +279 -0
- package/dist/shared/integration.js.map +1 -0
- package/dist/shared/kept.js +23 -0
- package/dist/shared/kept.js.map +1 -0
- package/dist/shared/marks.js +14 -0
- package/dist/shared/marks.js.map +1 -0
- package/dist/shared/playwright.js +105 -0
- package/dist/shared/playwright.js.map +1 -0
- package/dist/shared/recordings.js +246 -0
- package/dist/shared/recordings.js.map +1 -0
- package/dist/shared/shortcut.js +63 -0
- package/dist/shared/shortcut.js.map +1 -0
- package/dist/shared/tasks.js +521 -0
- package/dist/shared/tasks.js.map +1 -0
- package/dist/shared/waveform.js +93 -0
- package/dist/shared/waveform.js.map +1 -0
- package/dist/tailwind/client.d.ts +4 -0
- package/dist/tailwind/client.d.ts.map +1 -0
- package/dist/tailwind/client.js +613 -0
- package/dist/tailwind/client.js.map +1 -0
- package/dist/tailwind/index.d.ts +2 -0
- package/dist/tailwind/index.js +557 -0
- package/dist/tailwind/index.js.map +1 -0
- package/dist/tasks/client.d.ts +6 -0
- package/dist/tasks/client.d.ts.map +1 -0
- package/dist/tasks/client.js +642 -0
- package/dist/tasks/client.js.map +1 -0
- package/dist/tasks/index.d.ts +2 -0
- package/dist/tasks/index.js +1 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +61 -0
- package/dist/vite.js.map +1 -0
- package/dist/voice/client.d.ts +4 -0
- package/dist/voice/client.d.ts.map +1 -0
- package/dist/voice/client.js +66 -0
- package/dist/voice/client.js.map +1 -0
- package/dist/voice/index.d.ts +47 -0
- package/dist/voice/index.d.ts.map +1 -0
- package/dist/voice/index.js +74 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/webpack.d.ts +50 -0
- package/dist/webpack.d.ts.map +1 -0
- package/dist/webpack.js +78 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +307 -7
- package/skills/deictic/SKILL.md +47 -0
- package/skills/deictic/references/install.md +96 -0
- package/skills/deictic/references/recordings.md +83 -0
- package/skills/deictic/references/tasks.md +79 -0
- package/skills/deictic/references/watch.md +43 -0
- package/skills/deictic/scripts/frame.mjs +62 -0
- package/skills/deictic/scripts/lib.mjs +211 -0
- package/skills/deictic/scripts/recording.mjs +186 -0
- package/skills/deictic/scripts/task.mjs +84 -0
- package/skills/deictic/scripts/transcribe.mjs +93 -0
- package/skills/deictic/scripts/voice.mjs +50 -0
- package/skills/deictic/scripts/watch.mjs +51 -0
- package/src/astro.ts +34 -0
- package/src/bun.ts +79 -0
- package/src/client/index.ts +846 -0
- package/src/client/lightbox.ts +256 -0
- package/src/client/style.ts +243 -0
- package/src/client/tooltip.ts +78 -0
- package/src/client/waveform.ts +98 -0
- package/src/index.ts +247 -0
- package/src/loader.ts +28 -0
- package/src/next.ts +133 -0
- package/src/plugins/annotations/client.ts +301 -0
- package/src/plugins/annotations/draw.ts +169 -0
- package/src/plugins/annotations/index.ts +105 -0
- package/src/plugins/annotations/svg.ts +61 -0
- package/src/plugins/recommended/index.ts +35 -0
- package/src/plugins/recordings/client.ts +316 -0
- package/src/plugins/recordings/index.ts +175 -0
- package/src/plugins/recordings/marks.ts +11 -0
- package/src/plugins/recordings/ours.ts +70 -0
- package/src/plugins/recordings/replay.ts +165 -0
- package/src/plugins/screenshots/client.ts +64 -0
- package/src/plugins/screenshots/dom.ts +79 -0
- package/src/plugins/screenshots/index.ts +194 -0
- package/src/plugins/screenshots/playwright.ts +119 -0
- package/src/plugins/screenshots/rrweb.ts +45 -0
- package/src/plugins/tailwind/catalog.ts +396 -0
- package/src/plugins/tailwind/client.ts +669 -0
- package/src/plugins/tailwind/index.ts +267 -0
- package/src/plugins/tasks/client.ts +622 -0
- package/src/plugins/tasks/index.ts +557 -0
- package/src/plugins/tasks/local.ts +170 -0
- package/src/plugins/voice/client.ts +58 -0
- package/src/plugins/voice/index.ts +114 -0
- package/src/shortcut.ts +83 -0
- package/src/tools/astro.ts +81 -0
- package/src/tools/editor.ts +22 -0
- package/src/tools/endpoints.ts +163 -0
- package/src/tools/history.ts +203 -0
- package/src/tools/integration.ts +108 -0
- package/src/tools/kept.ts +21 -0
- package/src/tools/locate.ts +94 -0
- package/src/tools/plugins.ts +41 -0
- package/src/tools/source.ts +72 -0
- package/src/vite.ts +81 -0
- package/src/webpack.ts +107 -0
- package/index.js +0 -1
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The replay page, `replay/<id>`: rrweb's own player (`rrweb-player`), its timeline, speeds, skipping idle
|
|
3
|
+
* stretches and fullscreen, sized to the window, with what it doesn't do drawn over it:
|
|
4
|
+
*
|
|
5
|
+
* - The page shows in the colour scheme it was recorded in, whatever the viewer's: the replay's iframe
|
|
6
|
+
* answers `prefers-color-scheme` as the viewer's browser does, so the recorded CSS's queries are rewritten
|
|
7
|
+
* before it's replayed, the recorded scheme's to always match and the other's never.
|
|
8
|
+
* - The marks drawn while recording come as custom events, an SVG in page coordinates each; they're drawn
|
|
9
|
+
* over the replay in the player's own scaled wrapper, moved with its scroll, rather than into its document,
|
|
10
|
+
* which the replayer rebuilds. Each is a marker on the timeline.
|
|
11
|
+
* - The voice clips play in step, at the player's speed. A recording with voice starts paused, as a browser
|
|
12
|
+
* plays sound only after a click.
|
|
13
|
+
*
|
|
14
|
+
* `?t=<seconds>` opens it paused there, and `&bare` without the controls at the recorded size, 1:1, for a
|
|
15
|
+
* screenshot; `?embed` is it filling a frame, with no header of its own (the lightbox's); `body[data-ready]` says
|
|
16
|
+
* it's in place.
|
|
17
|
+
*
|
|
18
|
+
* The plugin's routes serve it, the player and what it plays. A page with no server behind it (a `standalone` build)
|
|
19
|
+
* publishes it as a file instead, `replayPage(sources)`: which recording is `?id=`, and a script of its own says
|
|
20
|
+
* where one is.
|
|
21
|
+
*/
|
|
22
|
+
import { MARKS } from './marks.ts';
|
|
23
|
+
|
|
24
|
+
/** Where the replay page finds what the plugin's routes would serve it, for a page published as a file. */
|
|
25
|
+
export interface ReplaySources {
|
|
26
|
+
/** The folder `player.js` and `player.css` are in (`rrweb-player`'s `rrweb-player.umd.min.cjs` and `style.css`), from the page: `..`, the routes', by default. */
|
|
27
|
+
player?: string;
|
|
28
|
+
/**
|
|
29
|
+
* A script, from the page, that sets `globalThis.deicticReplay` before the page asks: `recording(id)` answers the
|
|
30
|
+
* recording (a `Kept`) and `clip(id)` a voice clip's URL, either as a promise. The routes by default.
|
|
31
|
+
*/
|
|
32
|
+
source?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The replay page's HTML: the routes' as it is, or one for a page published as a file. */
|
|
36
|
+
export const replayPage = ({ player = '..', source }: ReplaySources = {}) => `<!doctype html>
|
|
37
|
+
<html lang="en">
|
|
38
|
+
<meta charset="utf-8">
|
|
39
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
40
|
+
<title>Recording · deictic</title>
|
|
41
|
+
<link rel="stylesheet" href="${player}/player.css">
|
|
42
|
+
<style>
|
|
43
|
+
:root { color-scheme: dark; --accent: #12b358; }
|
|
44
|
+
body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; background: #18181b; color: #fafafa; font: 13px/1.4 system-ui, sans-serif; }
|
|
45
|
+
header { display: flex; gap: 10px; align-items: center; padding: 10px 16px; border-bottom: 1px solid #3f3f46; }
|
|
46
|
+
header a { color: #a1a1aa; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
47
|
+
main { flex: 1; display: flex; justify-content: center; align-items: flex-start; padding: 16px; }
|
|
48
|
+
body.bare header { display: none; }
|
|
49
|
+
body.bare main { padding: 0; }
|
|
50
|
+
body.embed header { display: none; }
|
|
51
|
+
body.embed main { padding: 0; }
|
|
52
|
+
body.embed #main .rr-player { box-shadow: none; border-radius: 0; }
|
|
53
|
+
#marks { position: absolute; left: 0; top: 0; pointer-events: none; z-index: 3; }
|
|
54
|
+
#marks svg { position: absolute; left: 0; top: 0; overflow: visible; }
|
|
55
|
+
.problem { color: #f87171; padding: 16px; }
|
|
56
|
+
/* rrweb-player, in the dark of the rest of deictic. Its rules repeat Svelte's scoping class, so these start
|
|
57
|
+
from #main, which outweighs any number of classes. */
|
|
58
|
+
#main .rr-player { background: #27272a; box-shadow: 0 12px 32px rgb(0 0 0 / 0.5); border-radius: 8px; overflow: hidden; }
|
|
59
|
+
body.bare #main .rr-player { box-shadow: none; border-radius: 0; }
|
|
60
|
+
#main .rr-player .rr-player__frame { background: #fff; }
|
|
61
|
+
#main .rr-player .rr-controller { background: #18181b; border-top: 1px solid #3f3f46; border-radius: 0; }
|
|
62
|
+
#main .rr-player .rr-timeline__time { color: #a1a1aa; font: 12px ui-monospace, monospace; }
|
|
63
|
+
#main .rr-player .rr-progress { background: #3f3f46; border-color: #18181b; }
|
|
64
|
+
#main .rr-player .rr-progress__step { background: color-mix(in srgb, var(--accent) 45%, transparent); }
|
|
65
|
+
#main .rr-player .rr-progress__handler { background: var(--accent); }
|
|
66
|
+
#main .rr-player .rr-controller__btns button { color: #e4e4e7; }
|
|
67
|
+
#main .rr-player .rr-controller__btns button:disabled { color: #52525b; }
|
|
68
|
+
#main .rr-player .rr-controller__btns button svg { fill: currentColor; }
|
|
69
|
+
#main .rr-player .rr-controller__btns button.active { color: #04130b; background: var(--accent); }
|
|
70
|
+
#main .rr-player .rr-controller__btns button:active { background: color-mix(in srgb, var(--accent) 60%, transparent); }
|
|
71
|
+
#main .rr-player .switch .label { color: #e4e4e7; }
|
|
72
|
+
#main .rr-player .switch.disabled .label { color: #52525b; }
|
|
73
|
+
#main .rr-player .switch label:before { background: #52525b; }
|
|
74
|
+
#main .rr-player .switch input:checked + label:before { background: var(--accent); }
|
|
75
|
+
</style>
|
|
76
|
+
<header>
|
|
77
|
+
<strong>Recording</strong>
|
|
78
|
+
<a id="page" target="_blank" rel="noreferrer"></a>
|
|
79
|
+
</header>
|
|
80
|
+
<main id="main"></main>
|
|
81
|
+
<script src="${player}/player.js"></script>${
|
|
82
|
+
source === undefined
|
|
83
|
+
? ''
|
|
84
|
+
: `
|
|
85
|
+
<script src="${source}"></script>`
|
|
86
|
+
}
|
|
87
|
+
<script>
|
|
88
|
+
const params = new URLSearchParams(location.search);
|
|
89
|
+
const bare = params.has('bare');
|
|
90
|
+
const embed = params.has('embed');
|
|
91
|
+
if (bare) document.body.classList.add('bare');
|
|
92
|
+
if (embed) document.body.classList.add('embed');
|
|
93
|
+
const id = params.get('id') ?? location.pathname.split('/').at(-1);
|
|
94
|
+
// The plugin's routes, unless the page was given a source of its own (\`replayPage\`).
|
|
95
|
+
const from = globalThis.deicticReplay ?? {
|
|
96
|
+
recording: (id) => fetch('../recording/' + id).then((r) => (r.ok ? r.json() : r.text().then((t) => Promise.reject(new Error(t))))),
|
|
97
|
+
clip: (id) => '../../voice/clip/' + id,
|
|
98
|
+
};
|
|
99
|
+
Promise.resolve(from.recording(id)).then(async (kept) => {
|
|
100
|
+
const scheme = kept.colorScheme ?? 'light';
|
|
101
|
+
const events = JSON.parse(
|
|
102
|
+
JSON.stringify(kept.events).replace(/\\(\\s*prefers-color-scheme\\s*:\\s*(light|dark)\\s*\\)/g, (_, s) =>
|
|
103
|
+
s === scheme ? '(min-width: 0px)' : '(max-width: 0px) and (min-width: 1px)',
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
page.textContent = kept.page;
|
|
107
|
+
page.href = kept.page;
|
|
108
|
+
|
|
109
|
+
// A recording with the mic starts paused: a browser plays sound only after a click, which Play is.
|
|
110
|
+
const clips = await Promise.all((kept.voice ?? []).map(async (c) => ({ ...c, audio: Object.assign(new Audio(await from.clip(c.id)), { preload: 'auto' }) })));
|
|
111
|
+
const t = Number.parseFloat(params.get('t') ?? '');
|
|
112
|
+
// The player's frame, at most the window's room, the controls (80px) under it; 1:1 when bare.
|
|
113
|
+
const size = () => bare
|
|
114
|
+
? { width: kept.viewport.width, height: kept.viewport.height }
|
|
115
|
+
: embed
|
|
116
|
+
? { width: innerWidth, height: innerHeight - 80 }
|
|
117
|
+
: { width: Math.min(kept.viewport.width, innerWidth - 32), height: Math.min(kept.viewport.height, innerHeight - 90 - 80 - 16) };
|
|
118
|
+
const Player = rrwebPlayer.default ?? rrwebPlayer.Player ?? rrwebPlayer;
|
|
119
|
+
const player = new Player({
|
|
120
|
+
target: main,
|
|
121
|
+
props: {
|
|
122
|
+
events,
|
|
123
|
+
...size(),
|
|
124
|
+
autoPlay: !Number.isFinite(t) && clips.length === 0,
|
|
125
|
+
showController: !bare,
|
|
126
|
+
speedOption: [1, 2, 4, 8],
|
|
127
|
+
tags: { ${JSON.stringify(MARKS)}: '#12b358' },
|
|
128
|
+
inactiveColor: '#3f3f46',
|
|
129
|
+
mouseTail: false,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
addEventListener('resize', () => { player.$set(size()); player.triggerResize(); });
|
|
133
|
+
const replayer = player.getReplayer();
|
|
134
|
+
const total = player.getMetaData().totalTime;
|
|
135
|
+
|
|
136
|
+
// The marks as they were at this point: the latest custom event's, in the player's scaled wrapper, moved with the replay's scroll.
|
|
137
|
+
const marks = Object.assign(document.createElement('div'), { id: 'marks' });
|
|
138
|
+
replayer.wrapper.append(marks);
|
|
139
|
+
replayer.on('custom-event', (e) => { if (e.data.tag === ${JSON.stringify(MARKS)}) marks.innerHTML = e.data.payload.svg; });
|
|
140
|
+
|
|
141
|
+
let playing = false;
|
|
142
|
+
player.addEventListener('ui-update-player-state', (e) => { playing = e.payload === 'playing'; });
|
|
143
|
+
const follow = () => {
|
|
144
|
+
const win = replayer.iframe.contentWindow;
|
|
145
|
+
if (win) marks.style.transform = 'translate(' + -win.scrollX + 'px,' + -win.scrollY + 'px)';
|
|
146
|
+
const now = Math.min(replayer.getCurrentTime(), total);
|
|
147
|
+
const speed = replayer.config.speed ?? 1;
|
|
148
|
+
// Each clip plays while the replay is inside it, at its speed, kept within a few frames of the replay's time.
|
|
149
|
+
for (const c of clips) {
|
|
150
|
+
const into = now - c.at;
|
|
151
|
+
if (!playing || into < 0 || into >= c.duration) { if (!c.audio.paused) c.audio.pause(); continue; }
|
|
152
|
+
if (c.audio.playbackRate !== speed) c.audio.playbackRate = speed;
|
|
153
|
+
if (Math.abs(c.audio.currentTime * 1000 - into) > 250 * speed) c.audio.currentTime = into / 1000;
|
|
154
|
+
if (c.audio.paused) c.audio.play().catch(() => {});
|
|
155
|
+
}
|
|
156
|
+
requestAnimationFrame(follow);
|
|
157
|
+
};
|
|
158
|
+
follow();
|
|
159
|
+
|
|
160
|
+
if (Number.isFinite(t)) player.goto(Math.min(Math.max(0, t * 1000), total), false);
|
|
161
|
+
document.body.dataset.ready = '';
|
|
162
|
+
}).catch((e) => { document.querySelector('main').innerHTML = '<p class="problem"></p>'; document.querySelector('.problem').textContent = e.message; });
|
|
163
|
+
</script>
|
|
164
|
+
</html>
|
|
165
|
+
`;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The screenshots plugin's page half, around a renderer's capture: the renderer makes the shot (a PNG,
|
|
3
|
+
* or what its server half makes one of), and this sends it to the plugin's `add` route and answers the
|
|
4
|
+
* `Shot`. A renderer's page module default-exports `screenshotsClient(itsCapture)`.
|
|
5
|
+
*/
|
|
6
|
+
import { defineClientPlugin } from '#src/index.ts';
|
|
7
|
+
import type { Annotations } from '#src/plugins/annotations/index.ts';
|
|
8
|
+
|
|
9
|
+
import type { Box, Capture, CaptureOptions, ResolvedMasking, Shot } from './index.ts';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What a renderer's page half makes of the element: a body for `add`, its type, and where the element is
|
|
13
|
+
* in the shot. `overlay(area)` is what's drawn over the page in that part of the viewport, in viewport CSS
|
|
14
|
+
* pixels, as an SVG that size, or nothing: the annotations plugin's marks, where it's loaded. The renderer
|
|
15
|
+
* draws it over the shot, as deictic's own layers are left out.
|
|
16
|
+
*/
|
|
17
|
+
export type PageCapture = (
|
|
18
|
+
element: Element,
|
|
19
|
+
options: {
|
|
20
|
+
area: NonNullable<CaptureOptions['area']>;
|
|
21
|
+
masking: ResolvedMasking;
|
|
22
|
+
renderer: unknown;
|
|
23
|
+
overlay: (area: Box) => string | undefined;
|
|
24
|
+
},
|
|
25
|
+
) => Promise<{ body: Blob | string; type: string; box: Box }>;
|
|
26
|
+
|
|
27
|
+
/** Leaves out deictic's own UI: the overlay with its hover outline and any open panel. */
|
|
28
|
+
export const notOurs = (node: Node) => !(node instanceof Element && node.localName === 'deictic-overlay');
|
|
29
|
+
|
|
30
|
+
/** The element's box, in CSS pixels from the viewport's top left, or from its own for a shot of it alone. */
|
|
31
|
+
export function boxOf(element: Element, area: NonNullable<CaptureOptions['area']>): Box {
|
|
32
|
+
const { x, y, width, height } = element.getBoundingClientRect();
|
|
33
|
+
return area === 'element' ? { x: 0, y: 0, width, height } : { x, y, width, height };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function screenshotsClient(capture: PageCapture) {
|
|
37
|
+
return defineClientPlugin<never, never, Capture>({
|
|
38
|
+
name: 'screenshots',
|
|
39
|
+
setup: ({ base, request, options, use }) => {
|
|
40
|
+
const { masking, renderer } = options as { masking: ResolvedMasking; renderer: unknown };
|
|
41
|
+
// Asked at each shot: the annotations plugin may be set up after this one.
|
|
42
|
+
const overlay = (area: Box) => {
|
|
43
|
+
const marks: Annotations | undefined = use('annotations');
|
|
44
|
+
return marks?.svg(area);
|
|
45
|
+
};
|
|
46
|
+
const add = async (body: Blob | string, type: string, { box, size }: Pick<Shot, 'box' | 'size'>): Promise<Shot> => {
|
|
47
|
+
const res = await request('add', { method: 'POST', headers: { 'content-type': type }, body });
|
|
48
|
+
// `url`: where the shot is, said by what keeps it with no server to serve it from (a `standalone` build).
|
|
49
|
+
const { id, url, problem } = (await res.json()) as { id?: string; url?: string; problem?: string };
|
|
50
|
+
if (id === undefined) throw new Error(`screenshots: ${problem ?? res.statusText}`);
|
|
51
|
+
return { id, url: url ?? `${base}__deictic/plugins/screenshots/shot/${id}`, box, size };
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
capture: async (element, { area = 'viewport' } = {}) => {
|
|
55
|
+
const { body, type, box } = await capture(element, { area, masking, renderer, overlay });
|
|
56
|
+
const { clientWidth, clientHeight } = document.documentElement;
|
|
57
|
+
const size = area === 'element' ? { width: box.width, height: box.height } : { width: clientWidth, height: clientHeight };
|
|
58
|
+
return add(body, type, { box, size });
|
|
59
|
+
},
|
|
60
|
+
upload: (png, of) => add(png, 'image/png', of),
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `dom()` renderer's page half: draws the page in the page from its DOM with `modern-screenshot`
|
|
3
|
+
* (imported on the first shot), so it needs no server, but text may lay out a little differently than
|
|
4
|
+
* the page's. Masking changes only the copy that's drawn, never the page.
|
|
5
|
+
*/
|
|
6
|
+
import { boxOf, notOurs, screenshotsClient } from './client.ts';
|
|
7
|
+
import type { ResolvedMasking } from './index.ts';
|
|
8
|
+
|
|
9
|
+
const BLANK = '#d4d4d8';
|
|
10
|
+
|
|
11
|
+
const selector = (className: string, extra: string | undefined) => [`.${CSS.escape(className)}`, ...(extra ? [extra] : [])].join(', ');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Hides in the copy what rrweb would: blocked elements become a blank box their size (the copy keeps each
|
|
15
|
+
* element's size as its styles), masked text keeps its spaces but shows `*`, and so do passwords' values,
|
|
16
|
+
* or every field's.
|
|
17
|
+
*/
|
|
18
|
+
function mask(copy: Node, m: ResolvedMasking): void {
|
|
19
|
+
if (!(copy instanceof Element)) return;
|
|
20
|
+
const all = (sel: string) => [...(copy.matches(sel) ? [copy] : []), ...copy.querySelectorAll(sel)];
|
|
21
|
+
const stars = (text: string) => text.replace(/\S/g, '*');
|
|
22
|
+
for (const el of all(selector(m.blockClass, m.blockSelector))) {
|
|
23
|
+
el.replaceChildren();
|
|
24
|
+
if (el instanceof HTMLElement || el instanceof SVGElement) el.style.background = BLANK;
|
|
25
|
+
}
|
|
26
|
+
for (const el of all(selector(m.maskTextClass, m.maskTextSelector))) {
|
|
27
|
+
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
|
28
|
+
for (let text = walker.nextNode(); text !== null; text = walker.nextNode()) text.textContent = stars(text.textContent ?? '');
|
|
29
|
+
}
|
|
30
|
+
for (const field of all('input, textarea')) {
|
|
31
|
+
if (!(field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement)) continue;
|
|
32
|
+
if (!m.maskAllInputs && !(field instanceof HTMLInputElement && field.type === 'password')) continue;
|
|
33
|
+
field.value = stars(field.value);
|
|
34
|
+
field.setAttribute('value', field.value);
|
|
35
|
+
if (field instanceof HTMLTextAreaElement) field.textContent = field.value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Draws an SVG over the whole canvas, which is the area it was made for at the canvas's scale. */
|
|
40
|
+
async function drawOver(canvas: HTMLCanvasElement, svg: string | undefined): Promise<HTMLCanvasElement> {
|
|
41
|
+
if (svg === undefined) return canvas;
|
|
42
|
+
const image = new Image();
|
|
43
|
+
image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
|
|
44
|
+
await image.decode();
|
|
45
|
+
canvas.getContext('2d')?.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
46
|
+
return canvas;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default screenshotsClient(async (element, { area, masking, overlay }) => {
|
|
50
|
+
const { domToCanvas } = await import('modern-screenshot').catch((e: unknown) => {
|
|
51
|
+
throw new Error('the dom() renderer needs modern-screenshot: add it to your devDependencies', { cause: e });
|
|
52
|
+
});
|
|
53
|
+
// Sharp on a high-density screen, without drawing a long page at more than twice its size.
|
|
54
|
+
const scale = Math.min(devicePixelRatio, 2);
|
|
55
|
+
const options = { filter: notOurs, scale, onCloneNode: (copy: Node) => mask(copy, masking) };
|
|
56
|
+
const box = boxOf(element, area);
|
|
57
|
+
const toPng = (canvas: HTMLCanvasElement) =>
|
|
58
|
+
new Promise<Blob>((done, fail) =>
|
|
59
|
+
canvas.toBlob((blob) => (blob === null ? fail(new Error('screenshots: the page drew nothing')) : done(blob)), 'image/png'),
|
|
60
|
+
);
|
|
61
|
+
if (area === 'element') {
|
|
62
|
+
const { x, y, width, height } = element.getBoundingClientRect();
|
|
63
|
+
const shot = await drawOver(await domToCanvas(element, options), overlay({ x, y, width, height }));
|
|
64
|
+
return { body: await toPng(shot), type: 'image/png', box };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The whole document, then the part the viewport shows: the document's box is where it scrolled to.
|
|
68
|
+
const page = await domToCanvas(document.documentElement, options);
|
|
69
|
+
const { clientWidth: width, clientHeight: height } = document.documentElement;
|
|
70
|
+
const at = document.documentElement.getBoundingClientRect();
|
|
71
|
+
const canvas = document.createElement('canvas');
|
|
72
|
+
canvas.width = width * scale;
|
|
73
|
+
canvas.height = height * scale;
|
|
74
|
+
const ctx = canvas.getContext('2d');
|
|
75
|
+
if (ctx === null) throw new Error("screenshots: this browser won't draw to a canvas");
|
|
76
|
+
ctx.drawImage(page, -at.left * scale, -at.top * scale, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
|
|
77
|
+
await drawOver(canvas, overlay({ x: 0, y: 0, width, height }));
|
|
78
|
+
return { body: await toPng(canvas), type: 'image/png', box };
|
|
79
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic/screenshots`: a plugin with no panel, for others to build on. In the page,
|
|
3
|
+
* `use('screenshots').capture(element)` takes what the viewport shows (or the element alone), with
|
|
4
|
+
* nothing of deictic's in it, and says where the element is; on the server, `use('screenshots').read(id)`
|
|
5
|
+
* hands the PNG to another plugin, which the tasks plugin attaches to a task.
|
|
6
|
+
*
|
|
7
|
+
* How a shot is made is its renderer's: `rrweb()` by default, which snapshots the page's DOM and renders it
|
|
8
|
+
* in a browser beside the dev server, so text lays out as the page's does; or `dom()`, which draws the page
|
|
9
|
+
* in the page itself, for a target with no server to render on. A renderer is a page module and, where it
|
|
10
|
+
* renders elsewhere, a `render` here (`Renderer`): another may take pixels from screen sharing or devtools.
|
|
11
|
+
*
|
|
12
|
+
* Shots are kept under `.deictic/screenshots`, the latest `keep` of them, so they outlive a
|
|
13
|
+
* restart of the dev server and stay out of git.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { join, resolve } from 'node:path';
|
|
17
|
+
|
|
18
|
+
import { definePlugin } from '#src/index.ts';
|
|
19
|
+
import { keptIn } from '#src/tools/kept.ts';
|
|
20
|
+
|
|
21
|
+
/** A rectangle in CSS pixels, from the shot's top left. */
|
|
22
|
+
export interface Box {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A shot as the page knows it: its ID, where the dev server serves its PNG, and where the element is in it. */
|
|
30
|
+
export interface Shot {
|
|
31
|
+
id: string;
|
|
32
|
+
url: string;
|
|
33
|
+
/** The element, in the shot: nothing marks it there, as nothing of deictic's is drawn. */
|
|
34
|
+
box: Box;
|
|
35
|
+
/** The shot's size in CSS pixels; its PNG may have more, on a high-density screen. */
|
|
36
|
+
size: { width: number; height: number };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface CaptureOptions {
|
|
40
|
+
/** `viewport`, what the page shows, by default; or `element`, the element alone. */
|
|
41
|
+
area?: 'viewport' | 'element';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The page's half, for other plugins' page halves. */
|
|
45
|
+
export interface Capture {
|
|
46
|
+
capture(element: Element, options?: CaptureOptions): Promise<Shot>;
|
|
47
|
+
/** Keeps a PNG made of a shot (the same one with marks drawn over it, say) as a shot of its own. */
|
|
48
|
+
upload(png: Blob, of: Pick<Shot, 'box' | 'size'>): Promise<Shot>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The server's half, for other plugins' server halves. */
|
|
52
|
+
export interface Screenshots {
|
|
53
|
+
/** A shot's PNG, or nothing for one not kept (any more). */
|
|
54
|
+
read(id: string): { data: Uint8Array<ArrayBuffer>; name: string; type: 'image/png' } | undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
declare module 'deictic' {
|
|
58
|
+
interface ServerApis {
|
|
59
|
+
screenshots: Screenshots;
|
|
60
|
+
}
|
|
61
|
+
interface ClientApis {
|
|
62
|
+
screenshots: Capture;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What a shot hides, as rrweb names it and by its defaults, so one marking in the app hides the same
|
|
68
|
+
* things from a screenshot as from an rrweb recording or snapshot.
|
|
69
|
+
*/
|
|
70
|
+
export interface Masking {
|
|
71
|
+
/** Elements drawn as a blank box their size: `rr-block` by default. */
|
|
72
|
+
blockClass?: string;
|
|
73
|
+
blockSelector?: string;
|
|
74
|
+
/** Elements whose text is drawn as `*`: `rr-mask` by default. */
|
|
75
|
+
maskTextClass?: string;
|
|
76
|
+
maskTextSelector?: string;
|
|
77
|
+
/** Every field's value as `*`, not only passwords': false by default. */
|
|
78
|
+
maskAllInputs?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Masking with its defaults, as a renderer's page half gets it. */
|
|
82
|
+
export type ResolvedMasking = Required<Pick<Masking, 'blockClass' | 'maskTextClass' | 'maskAllInputs'>> & Masking;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* How shots are made. `client` is the page module that captures, default-exporting `screenshotsClient(…)`
|
|
86
|
+
* from `deictic/screenshots/client`. A page half that sends a PNG needs nothing here; one that sends
|
|
87
|
+
* something else, as JSON (a DOM snapshot), needs `render` to make the PNG of it.
|
|
88
|
+
*/
|
|
89
|
+
export interface Renderer {
|
|
90
|
+
client: string;
|
|
91
|
+
/** What the page half gets, besides the masking. */
|
|
92
|
+
clientOptions?: unknown;
|
|
93
|
+
render?(input: unknown): Promise<Uint8Array>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Where `rrweb()` finds a browser: any Chromium renders the snapshot as the page's own would. */
|
|
97
|
+
export interface BrowserOptions {
|
|
98
|
+
/** A Chromium-based browser's executable. */
|
|
99
|
+
executablePath?: string;
|
|
100
|
+
/** An installed browser by Playwright's name: `chrome`, `msedge`. By default Playwright's own Chromium, then Chrome, then Edge. */
|
|
101
|
+
channel?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Draws the page in the page, from its DOM (`modern-screenshot`): no server needed, but text may lay out a little differently. */
|
|
105
|
+
export const dom = (): Renderer => ({ client: 'deictic/screenshots/dom' });
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Snapshots the page's DOM in the page (`rrweb-snapshot`, masking as rrweb does) and renders it in a
|
|
109
|
+
* browser beside the dev server (`playwright-core`), so text lays out as the page's does.
|
|
110
|
+
*/
|
|
111
|
+
export const rrweb = (options: BrowserOptions = {}): Renderer => ({
|
|
112
|
+
client: 'deictic/screenshots/rrweb',
|
|
113
|
+
// Loaded on the first shot, so a project rendering in the page needn't install Playwright.
|
|
114
|
+
render: async (input) => (await import('./playwright.ts')).render(input, options),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
export interface ScreenshotsOptions extends Masking {
|
|
118
|
+
/** How shots are made: `rrweb()` by default, or `dom()` where there's no server to render on. */
|
|
119
|
+
renderer?: Renderer;
|
|
120
|
+
/** How many shots to keep, the latest: 100 by default. */
|
|
121
|
+
keep?: number;
|
|
122
|
+
/** Where they're kept, from the project's root: `.deictic/screenshots` by default. */
|
|
123
|
+
dir?: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const ID = /^[a-z0-9]+-[a-z0-9]+$/;
|
|
127
|
+
/** What one PNG, or one snapshot to render, may weigh: a viewport at twice its pixels is a few MB. */
|
|
128
|
+
const MAX = 20 * 1024 * 1024;
|
|
129
|
+
|
|
130
|
+
export function screenshots(options: ScreenshotsOptions = {}) {
|
|
131
|
+
const renderer = options.renderer ?? rrweb();
|
|
132
|
+
const keep = options.keep ?? 100;
|
|
133
|
+
let dir = '';
|
|
134
|
+
let taken = 0;
|
|
135
|
+
const pathOf = (id: string) => join(dir, `${id}.png`);
|
|
136
|
+
const read: Screenshots['read'] = (id) => {
|
|
137
|
+
if (!ID.test(id) || !existsSync(pathOf(id))) return undefined;
|
|
138
|
+
return { data: new Uint8Array(readFileSync(pathOf(id))), name: `${id}.png`, type: 'image/png' };
|
|
139
|
+
};
|
|
140
|
+
const keepShot = (data: Uint8Array) => {
|
|
141
|
+
// Sorts by when it was taken, which is what pruning keeps by: the count orders two in one millisecond.
|
|
142
|
+
const id = `${Date.now().toString(36).padStart(9, '0')}${(taken++ % 1296).toString(36).padStart(2, '0')}-${crypto.randomUUID().slice(0, 8)}`;
|
|
143
|
+
mkdirSync(dir, { recursive: true });
|
|
144
|
+
writeFileSync(pathOf(id), data);
|
|
145
|
+
const kept = readdirSync(dir)
|
|
146
|
+
.filter((f) => f.endsWith('.png'))
|
|
147
|
+
.toSorted();
|
|
148
|
+
for (const old of kept.slice(0, Math.max(0, kept.length - keep))) rmSync(join(dir, old));
|
|
149
|
+
return id;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return definePlugin<never, never, never, Screenshots>({
|
|
153
|
+
name: 'screenshots',
|
|
154
|
+
client: renderer.client,
|
|
155
|
+
clientOptions: {
|
|
156
|
+
masking: {
|
|
157
|
+
blockClass: options.blockClass ?? 'rr-block',
|
|
158
|
+
blockSelector: options.blockSelector,
|
|
159
|
+
maskTextClass: options.maskTextClass ?? 'rr-mask',
|
|
160
|
+
maskTextSelector: options.maskTextSelector,
|
|
161
|
+
maskAllInputs: options.maskAllInputs ?? false,
|
|
162
|
+
} satisfies ResolvedMasking,
|
|
163
|
+
renderer: renderer.clientOptions,
|
|
164
|
+
},
|
|
165
|
+
setup: ({ root }) => {
|
|
166
|
+
dir = options.dir === undefined ? keptIn(root, 'screenshots') : resolve(root, options.dir);
|
|
167
|
+
return { read };
|
|
168
|
+
},
|
|
169
|
+
routes: {
|
|
170
|
+
// A PNG the page made, or what the renderer makes one of.
|
|
171
|
+
add: async (req) => {
|
|
172
|
+
const type = req.headers.get('content-type') ?? '';
|
|
173
|
+
const body = new Uint8Array(await req.arrayBuffer());
|
|
174
|
+
if (body.byteLength > MAX) return Response.json({ problem: `a shot may weigh ${MAX / 1024 / 1024} MB at most` }, { status: 413 });
|
|
175
|
+
if (type === 'image/png') return Response.json({ id: keepShot(body) });
|
|
176
|
+
if (renderer.render === undefined || !type.startsWith('application/json'))
|
|
177
|
+
return Response.json({ problem: 'send the shot as image/png' }, { status: 415 });
|
|
178
|
+
try {
|
|
179
|
+
return Response.json({ id: keepShot(await renderer.render(JSON.parse(new TextDecoder().decode(body)))) });
|
|
180
|
+
} catch (e) {
|
|
181
|
+
return Response.json({ problem: e instanceof Error ? e.message : String(e) }, { status: 500 });
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
// `shot/<id>`: the PNG, for a panel to show.
|
|
185
|
+
shot: (req) => {
|
|
186
|
+
const shot = read(new URL(req.url).pathname.split('/').at(-1) ?? '');
|
|
187
|
+
if (shot === undefined) return new Response('no such shot, or not kept any more', { status: 404 });
|
|
188
|
+
return new Response(shot.data, { headers: { 'content-type': shot.type, 'cache-control': 'no-store' } });
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export default screenshots;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `rrweb()` renderer's server half: rebuilds a page's snapshot in a browser beside the dev server and
|
|
3
|
+
* screenshots it at the page's viewport, scroll and pixel density, so text lays out as the page's does. The
|
|
4
|
+
* browser is launched on the first shot and kept for the next; Playwright closes it with the process.
|
|
5
|
+
*
|
|
6
|
+
* The snapshot is rebuilt into a sandboxed iframe filling a blank page on the page's own origin, which
|
|
7
|
+
* Playwright answers itself, so nothing asks the dev server for it: rrweb has made the snapshot's URLs
|
|
8
|
+
* whole, and its stylesheets' fonts and images load from the dev server as the page's did.
|
|
9
|
+
*/
|
|
10
|
+
import { createRequire } from 'node:module';
|
|
11
|
+
|
|
12
|
+
import type { Browser } from 'playwright-core';
|
|
13
|
+
import type * as Rrweb from 'rrweb-snapshot';
|
|
14
|
+
|
|
15
|
+
import type { Box, BrowserOptions } from './index.ts';
|
|
16
|
+
|
|
17
|
+
/** What the page sends: its snapshot and what's needed to show it as the page does. */
|
|
18
|
+
/** A node of an rrweb snapshot, as `snapshot()` answers it. */
|
|
19
|
+
export type SnapshotNode = NonNullable<ReturnType<typeof Rrweb.snapshot>>;
|
|
20
|
+
|
|
21
|
+
export interface Snapshot {
|
|
22
|
+
snapshot: SnapshotNode;
|
|
23
|
+
url: string;
|
|
24
|
+
viewport: { width: number; height: number };
|
|
25
|
+
scroll: { x: number; y: number };
|
|
26
|
+
scale: number;
|
|
27
|
+
/** The part of the viewport to keep, for a shot of an element alone. */
|
|
28
|
+
clip?: Box;
|
|
29
|
+
/** An SVG the viewport's size, drawn over the page: the annotations, where there are any. */
|
|
30
|
+
overlay?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const HOST = '<!doctype html><html><body style="margin:0"></body></html>';
|
|
34
|
+
|
|
35
|
+
const browsers = new Map<string, Promise<Browser>>();
|
|
36
|
+
|
|
37
|
+
/** A browser by the options, launched once: the one named, or Playwright's own Chromium, then Chrome, then Edge. */
|
|
38
|
+
export function browserOf(options: BrowserOptions): Promise<Browser> {
|
|
39
|
+
const key = JSON.stringify(options);
|
|
40
|
+
const known = browsers.get(key);
|
|
41
|
+
if (known !== undefined) return known;
|
|
42
|
+
const launched = launch(options);
|
|
43
|
+
browsers.set(key, launched);
|
|
44
|
+
// A launch that failed is tried again on the next shot, after the user installs what it said to.
|
|
45
|
+
launched.catch(() => browsers.delete(key));
|
|
46
|
+
return launched;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function launch({ executablePath, channel }: BrowserOptions): Promise<Browser> {
|
|
50
|
+
const { chromium } = await import('playwright-core').catch((e: unknown) => {
|
|
51
|
+
throw new Error('the rrweb() renderer needs playwright-core: add it to your devDependencies', { cause: e });
|
|
52
|
+
});
|
|
53
|
+
if (executablePath !== undefined || channel !== undefined) return chromium.launch({ executablePath, channel });
|
|
54
|
+
const tried: string[] = [];
|
|
55
|
+
for (const attempt of [{}, { channel: 'chrome' }, { channel: 'msedge' }]) {
|
|
56
|
+
try {
|
|
57
|
+
return await chromium.launch(attempt);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
tried.push(
|
|
60
|
+
`${'channel' in attempt ? attempt.channel : "Playwright's Chromium"}: ${(e instanceof Error ? e.message : String(e)).split('\n', 1)[0]}`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw new Error(
|
|
65
|
+
`the rrweb() renderer found no browser to render in (${tried.join('; ')}): run \`npx playwright install chromium\`, or pass rrweb({ executablePath })`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The rrweb-snapshot bundle that defines `rrwebSnapshot` in a page: its CommonJS entry is that bundle. */
|
|
70
|
+
const bundle = () => createRequire(import.meta.url).resolve('rrweb-snapshot');
|
|
71
|
+
|
|
72
|
+
export async function render(input: unknown, options: BrowserOptions): Promise<Uint8Array> {
|
|
73
|
+
const shot = input as Snapshot;
|
|
74
|
+
if (typeof shot?.url !== 'string' || typeof shot.snapshot !== 'object' || typeof shot.viewport?.width !== 'number')
|
|
75
|
+
throw new Error('screenshots: that is not a snapshot the rrweb() renderer took');
|
|
76
|
+
const browser = await browserOf(options);
|
|
77
|
+
const context = await browser.newContext({ viewport: shot.viewport, deviceScaleFactor: shot.scale });
|
|
78
|
+
try {
|
|
79
|
+
const page = await context.newPage();
|
|
80
|
+
const host = new URL('/__deictic/plugins/screenshots/host', shot.url).href;
|
|
81
|
+
await page.route(host, (route) => route.fulfill({ contentType: 'text/html', body: HOST }));
|
|
82
|
+
await page.goto(host);
|
|
83
|
+
await page.addScriptTag({ path: bundle() });
|
|
84
|
+
await page.evaluate(async ({ snapshot, scroll, overlay }) => {
|
|
85
|
+
const { rebuildIntoSandboxedIframe, createCache, createMirror } = (globalThis as unknown as { rrwebSnapshot: typeof Rrweb })
|
|
86
|
+
.rrwebSnapshot;
|
|
87
|
+
const { iframe } = rebuildIntoSandboxedIframe(snapshot, {
|
|
88
|
+
root: document.body,
|
|
89
|
+
cache: createCache(),
|
|
90
|
+
mirror: createMirror(),
|
|
91
|
+
iframeAttributes: { style: 'border:0;position:fixed;inset:0;width:100vw;height:100vh' },
|
|
92
|
+
});
|
|
93
|
+
const doc = iframe.contentDocument;
|
|
94
|
+
if (doc === null) throw new Error('screenshots: the snapshot was rebuilt where it cannot be read');
|
|
95
|
+
await doc.fonts.ready;
|
|
96
|
+
await Promise.all([...doc.images].map((img) => img.decode().catch(() => {})));
|
|
97
|
+
iframe.contentWindow?.scrollTo(scroll.x, scroll.y);
|
|
98
|
+
if (overlay === undefined) return;
|
|
99
|
+
// Over the page and its scroll: the SVG is the viewport's, already where it scrolled to.
|
|
100
|
+
const marks = document.createElement('div');
|
|
101
|
+
marks.style.cssText = 'position:fixed;inset:0;z-index:1;pointer-events:none';
|
|
102
|
+
marks.innerHTML = overlay;
|
|
103
|
+
document.body.append(marks);
|
|
104
|
+
}, shot);
|
|
105
|
+
const clip = shot.clip === undefined ? undefined : clamp(shot.clip, shot.viewport);
|
|
106
|
+
return new Uint8Array(await page.screenshot({ type: 'png', ...(clip === undefined ? {} : { clip }) }));
|
|
107
|
+
} finally {
|
|
108
|
+
await context.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A box kept within the viewport: an element partly scrolled out of it shows only the part that's in. */
|
|
113
|
+
function clamp(box: Box, viewport: { width: number; height: number }): Box | undefined {
|
|
114
|
+
const x = Math.max(0, box.x);
|
|
115
|
+
const y = Math.max(0, box.y);
|
|
116
|
+
const width = Math.min(viewport.width, box.x + box.width) - x;
|
|
117
|
+
const height = Math.min(viewport.height, box.y + box.height) - y;
|
|
118
|
+
return width > 0 && height > 0 ? { x, y, width, height } : undefined;
|
|
119
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `rrweb()` renderer's page half: a snapshot of the page's DOM with `rrweb-snapshot` (imported on the
|
|
3
|
+
* first shot), masked as rrweb masks, without deictic's overlay, sent with what the server's browser needs
|
|
4
|
+
* to show it as the page does: the viewport, where it's scrolled to, the pixel density, the page's URL.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { boxOf, screenshotsClient } from './client.ts';
|
|
8
|
+
import type { Snapshot, SnapshotNode } from './playwright.ts';
|
|
9
|
+
|
|
10
|
+
/** The snapshot without deictic's overlay, which rrweb has no option to leave out. */
|
|
11
|
+
function withoutOurs(node: SnapshotNode): SnapshotNode {
|
|
12
|
+
if (!('childNodes' in node)) return node;
|
|
13
|
+
const childNodes = node.childNodes.filter((c: SnapshotNode) => !('tagName' in c && c.tagName === 'deictic-overlay')).map(withoutOurs);
|
|
14
|
+
return { ...node, childNodes };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default screenshotsClient(async (element, { area, masking, overlay }) => {
|
|
18
|
+
const { snapshot } = await import('rrweb-snapshot').catch((e: unknown) => {
|
|
19
|
+
throw new Error('the rrweb() renderer needs rrweb-snapshot: add it to your devDependencies', { cause: e });
|
|
20
|
+
});
|
|
21
|
+
const taken = snapshot(document, {
|
|
22
|
+
inlineStylesheet: true,
|
|
23
|
+
blockClass: masking.blockClass,
|
|
24
|
+
blockSelector: masking.blockSelector ?? null,
|
|
25
|
+
maskTextClass: masking.maskTextClass,
|
|
26
|
+
maskTextSelector: masking.maskTextSelector ?? null,
|
|
27
|
+
maskAllInputs: masking.maskAllInputs,
|
|
28
|
+
});
|
|
29
|
+
if (taken === null) throw new Error('screenshots: rrweb took no snapshot of this page');
|
|
30
|
+
const box = boxOf(element, area);
|
|
31
|
+
const viewport = { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight };
|
|
32
|
+
// Over the viewport, which the server renders before cutting out an element's part.
|
|
33
|
+
const marks = overlay({ x: 0, y: 0, ...viewport });
|
|
34
|
+
const body: Snapshot = {
|
|
35
|
+
snapshot: withoutOurs(taken),
|
|
36
|
+
url: location.href,
|
|
37
|
+
viewport,
|
|
38
|
+
...(marks === undefined ? {} : { overlay: marks }),
|
|
39
|
+
scroll: { x: scrollX, y: scrollY },
|
|
40
|
+
scale: Math.min(devicePixelRatio, 2),
|
|
41
|
+
// A shot of the element alone is the part of the viewport it fills.
|
|
42
|
+
...(area === 'element' ? { clip: element.getBoundingClientRect().toJSON() as Snapshot['clip'] } : {}),
|
|
43
|
+
};
|
|
44
|
+
return { body: JSON.stringify(body), type: 'application/json', box };
|
|
45
|
+
});
|