@stefan.inc/feedback-engine 1.7.0 → 1.7.1
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 +3 -3
- package/package.json +1 -1
- package/src/cli/index.js +43 -10
- package/src/core/extent.ts +37 -0
- package/src/core/index.ts +1 -0
- package/src/dom/capture.ts +51 -12
- package/src/dom/image.ts +3 -3
- package/src/dom/screenshot.ts +147 -52
- package/src/editor/Editor.svelte +73 -5
- package/src/editor/engine.ts +4 -2
package/README.md
CHANGED
|
@@ -214,7 +214,7 @@ The package ships the [`feedback-accessibility`](https://github.com/smirea/feedb
|
|
|
214
214
|
bunx feedback-engine init
|
|
215
215
|
```
|
|
216
216
|
|
|
217
|
-
It links the skill into `.agents/skills/feedback-accessibility`, where agents that read the `.agents` folder find it by name, and adds a Feedback section to `AGENTS.md`, between `feedback-engine` markers. The section tells every agent what the editor is, how to read an export someone hands it, which skill to follow when it changes a screen, and how to check a screen with [`capture()`](#drive-it-from-a-script). When the app also has a `CLAUDE.md` that does not mention `AGENTS.md`, the section goes there too. Commit both: the link points into `node_modules`, so the skill always matches the installed version. Run `init` again after an upgrade to refresh the section.
|
|
217
|
+
It links the skill into `.agents/skills/feedback-accessibility`, where agents that read the `.agents` folder find it by name, and adds a Feedback section to `AGENTS.md`, between `feedback-engine` markers. The section tells every agent what the editor is, how to read an export someone hands it, which skill to follow when it changes a screen, and how to check a screen with [`capture()`](#drive-it-from-a-script). When the app also has a `CLAUDE.md` that does not mention `AGENTS.md`, the section goes there too. Commit both: the link points into `node_modules`, so the skill always matches the installed version. Run `init` again after an upgrade to refresh the section. In a monorepo, run it from the repository's root: it finds the package in the workspace that installed it, so the skill and the section sit where every agent in the repository reads them.
|
|
218
218
|
|
|
219
219
|
## Give feedback
|
|
220
220
|
|
|
@@ -248,7 +248,7 @@ The token is the switch. With `PROCRASTIMATE_TOKEN` set, **ProcrastiMate** shows
|
|
|
248
248
|
## How capture works
|
|
249
249
|
|
|
250
250
|
- The browser gives a page no way to read its own pixels without a permission prompt, so Feedback draws the DOM again into a canvas with [modern-screenshot](https://github.com/qq15725/modern-screenshot). On the example app, a scrolled page differs from the browser's own screenshot in under 0.5% of pixels, and a page with an open dialog in about 1%.
|
|
251
|
-
- The
|
|
251
|
+
- The document is copied in one pass, as vector images of the DOM, so the editor draws the part in view sharp at any zoom and the image export draws at the screen's pixel ratio. Nothing scrolls on screen. The window jumps to its origin and back inside one task, so a sticky bar is read where the document has it, and scroll listeners run.
|
|
252
252
|
- Content that stays put while the page scrolls, such as a `position: fixed` tab bar, is drawn where it was on screen, over the visible part of the capture.
|
|
253
253
|
- A modal `<dialog>`, an open popover, and an `aria-modal` dialog are separate layers. Clicks on a layer only select what is inside it, and whatever it hides completely is left out of the tree.
|
|
254
254
|
- Roles and names come from the DOM with [dom-accessibility-api](https://github.com/eps1lon/dom-accessibility-api). There is nothing to switch on first, and reading a screen takes a few milliseconds.
|
|
@@ -256,7 +256,7 @@ The token is the switch. With `PROCRASTIMATE_TOKEN` set, **ProcrastiMate** shows
|
|
|
256
256
|
## Limits
|
|
257
257
|
|
|
258
258
|
- The screenshot is a re-draw, not the screen's pixels. A `<canvas>`, an inline SVG, and an image from another origin that allows CORS are drawn. An image from an origin that does not allow CORS, a `<video>`, and an iframe can come out blank, and a field's placeholder is drawn in its text color. It is tested in Chromium, in Google Chrome, and in Playwright's WebKit, not in Safari itself.
|
|
259
|
-
- A
|
|
259
|
+
- A capture stops at 16,000 device pixels tall, 8,000 CSS pixels on a 2x screen, as on iOS, and ends between two rows. When the page was scrolled deep, it starts partway down with the part on screen near its middle.
|
|
260
260
|
- A scroll container inside the page shows what was on screen in it.
|
|
261
261
|
- A minified production build shows a class's minified name in component data, such as `a(count: 2)`.
|
|
262
262
|
- Element locations for untagged content exist only in development builds, unless a React app turns on `elementSources`. Components keep theirs in every build.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stefan.inc/feedback-engine",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"description": "In-app feedback for Svelte and React: point at a component, write what should change, and export its name, file:line, and data for an agent.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"accessibility",
|
package/src/cli/index.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
lstatSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
symlinkSync,
|
|
9
|
+
unlinkSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'node:fs';
|
|
3
12
|
import { dirname, join, relative, resolve } from 'node:path';
|
|
4
13
|
|
|
5
14
|
const name = '@stefan.inc/feedback-engine';
|
|
@@ -9,7 +18,8 @@ const end = '<!-- feedback-engine:end -->';
|
|
|
9
18
|
|
|
10
19
|
const usage = `Usage: feedback-engine init
|
|
11
20
|
|
|
12
|
-
Run it from the root of an app that has ${name} installed
|
|
21
|
+
Run it from the root of an app that has ${name} installed, or from the root of a
|
|
22
|
+
workspace whose app has it. It links the package's
|
|
13
23
|
${skill} skill into .agents/skills, and adds a section to AGENTS.md that tells the app's
|
|
14
24
|
agents about the skill, the editor, and __feedbackEngine.capture(). Run it again after an
|
|
15
25
|
upgrade: the skill follows the installed version through the link, and the section is replaced.`;
|
|
@@ -19,13 +29,35 @@ function fail(message) {
|
|
|
19
29
|
process.exit(1);
|
|
20
30
|
}
|
|
21
31
|
|
|
22
|
-
/** The
|
|
32
|
+
/** The workspace folders a root `package.json` lists, for the `dir/*` and plain-folder patterns npm, Bun, and Yarn take. */
|
|
33
|
+
function workspaces(root) {
|
|
34
|
+
const manifest = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
35
|
+
const patterns = Array.isArray(manifest.workspaces) ? manifest.workspaces : (manifest.workspaces?.packages ?? []);
|
|
36
|
+
return patterns.flatMap(pattern => {
|
|
37
|
+
if (!pattern.endsWith('/*')) return [join(root, pattern)];
|
|
38
|
+
const parent = join(root, pattern.slice(0, -2));
|
|
39
|
+
if (!existsSync(parent)) return [];
|
|
40
|
+
return readdirSync(parent, { withFileTypes: true })
|
|
41
|
+
.filter(entry => entry.isDirectory())
|
|
42
|
+
.map(entry => join(parent, entry.name));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The installed package, through node_modules rather than its real path, so the link survives an upgrade, and the app
|
|
48
|
+
* that depends on it. In a workspace whose packages each keep their own node_modules, as Bun's isolated installs do,
|
|
49
|
+
* that app is the workspace package that has it.
|
|
50
|
+
*/
|
|
23
51
|
function installed(root) {
|
|
52
|
+
const at = folder => join(folder, 'node_modules', name);
|
|
53
|
+
const has = folder => existsSync(join(at(folder), 'package.json'));
|
|
24
54
|
for (let folder = root; ; folder = dirname(folder)) {
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
if (dirname(folder) === folder) fail(`${name} is not installed here. Add it first: bun add ${name}`);
|
|
55
|
+
if (has(folder)) return { source: at(folder), app: root };
|
|
56
|
+
if (dirname(folder) === folder) break;
|
|
28
57
|
}
|
|
58
|
+
const app = workspaces(root).find(has);
|
|
59
|
+
if (app) return { source: at(app), app };
|
|
60
|
+
fail(`${name} is not installed here. Add it first: bun add ${name}`);
|
|
29
61
|
}
|
|
30
62
|
|
|
31
63
|
function link(root, source) {
|
|
@@ -42,8 +74,8 @@ function link(root, source) {
|
|
|
42
74
|
return target;
|
|
43
75
|
}
|
|
44
76
|
|
|
45
|
-
function section(root) {
|
|
46
|
-
const manifest = JSON.parse(readFileSync(join(
|
|
77
|
+
function section(root, app) {
|
|
78
|
+
const manifest = JSON.parse(readFileSync(join(app, 'package.json'), 'utf8'));
|
|
47
79
|
const dependencies = { ...manifest.dependencies, ...manifest.devDependencies };
|
|
48
80
|
const framework = dependencies.svelte && !dependencies.react ? 'Svelte' : 'React';
|
|
49
81
|
const run = existsSync(join(root, 'bun.lock')) || existsSync(join(root, 'bun.lockb')) ? 'bun' : 'node';
|
|
@@ -81,13 +113,14 @@ if (command !== 'init' || rest.length > 0) fail(`unknown command.\n\n${usage}`);
|
|
|
81
113
|
|
|
82
114
|
const root = resolve('.');
|
|
83
115
|
if (!existsSync(join(root, 'package.json'))) fail('run init from the folder that holds the app’s package.json.');
|
|
84
|
-
const source =
|
|
116
|
+
const { source: installation, app } = installed(root);
|
|
117
|
+
const source = join(installation, 'skills', skill);
|
|
85
118
|
if (!existsSync(join(source, 'SKILL.md'))) fail(`${source} has no SKILL.md. Upgrade ${name}.`);
|
|
86
119
|
|
|
87
120
|
const linked = link(root, source);
|
|
88
121
|
console.log(`Linked ${relative(root, linked)} to ${relative(root, source)}`);
|
|
89
122
|
|
|
90
|
-
const text = section(root);
|
|
123
|
+
const text = section(root, app);
|
|
91
124
|
const files = [join(root, 'AGENTS.md')];
|
|
92
125
|
// Claude Code reads CLAUDE.md and not AGENTS.md, unless CLAUDE.md points to it.
|
|
93
126
|
const claude = join(root, 'CLAUDE.md');
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Frame } from './model.ts';
|
|
2
|
+
|
|
3
|
+
/** The tallest capture in device pixels, as on iOS, where Core Animation stops drawing a taller image. */
|
|
4
|
+
export const captureLimit = 16_000;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Where a capture that reaches its height limit should end: the lowest y from `floor` down to `limit` that no frame
|
|
8
|
+
* crosses, so the last row is whole. `undefined` when frames cover the whole range. iOS's `Stitch.cut`.
|
|
9
|
+
*/
|
|
10
|
+
export function cut(limit: number, floor: number, frames: Frame[]): number | undefined {
|
|
11
|
+
const candidates = [limit, ...frames.flatMap(frame => [frame.y, frame.y + frame.height])].filter(
|
|
12
|
+
y => y === limit || (y > floor && y < limit),
|
|
13
|
+
);
|
|
14
|
+
return candidates
|
|
15
|
+
.toSorted((a, b) => b - a)
|
|
16
|
+
.find(y => !frames.some(frame => frame.y < y - 0.5 && frame.y + frame.height > y + 0.5));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The part of a page a capture covers, from `top` down `height` points, by the iOS rules. A page up to `limit` tall is
|
|
21
|
+
* captured whole. A longer one is captured `limit` tall from its top, or, when the window is scrolled more than half
|
|
22
|
+
* the room the limit leaves below the screen, with the part on screen near the middle. A capture that stops before
|
|
23
|
+
* the page's end ends between two rows in its last screen, where none of `rows` crosses the cut.
|
|
24
|
+
*/
|
|
25
|
+
export function captureRange(page: number, screen: number, scroll: number, limit: number, rows: Frame[]) {
|
|
26
|
+
if (page <= limit) return { top: 0, height: page };
|
|
27
|
+
const reach = limit - screen;
|
|
28
|
+
const top = scroll > reach / 2 ? Math.max(0, Math.min(scroll - reach / 2, page - limit)) : 0;
|
|
29
|
+
const end = top + limit;
|
|
30
|
+
if (end >= page) return { top, height: page - top };
|
|
31
|
+
const ending = cut(
|
|
32
|
+
end,
|
|
33
|
+
end - screen,
|
|
34
|
+
rows.filter(row => row.height < screen),
|
|
35
|
+
);
|
|
36
|
+
return { top, height: (ending ?? end) - top };
|
|
37
|
+
}
|
package/src/core/index.ts
CHANGED
package/src/dom/capture.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
buildTree,
|
|
3
|
+
captureLimit,
|
|
4
|
+
captureRange,
|
|
5
|
+
contextLimit,
|
|
6
|
+
describe,
|
|
7
|
+
type Capture,
|
|
8
|
+
type ContextEntry,
|
|
9
|
+
type Frame,
|
|
10
|
+
} from '../core/index.ts';
|
|
11
|
+
import { readScene, type Scene } from './scene.ts';
|
|
12
|
+
import { takePicture, type Picture } from './screenshot.ts';
|
|
4
13
|
|
|
5
|
-
|
|
14
|
+
/**
|
|
15
|
+
* A capture and its picture. `image` is the whole picture on one canvas, at the screen's pixel ratio or lower when the
|
|
16
|
+
* capture is too large for one, which the editor shows until it draws the part in view sharp. `pixelRatio` is the
|
|
17
|
+
* screen's, the resolution an export aims for.
|
|
18
|
+
*/
|
|
19
|
+
export type Screen = { capture: Capture; picture: Picture; image: HTMLCanvasElement; pixelRatio: number };
|
|
6
20
|
|
|
7
21
|
export type CaptureOptions = {
|
|
8
22
|
app?: string;
|
|
@@ -39,9 +53,25 @@ function environment(options: CaptureOptions, date: Date): ContextEntry[] {
|
|
|
39
53
|
return entries.filter(entry => !!entry);
|
|
40
54
|
}
|
|
41
55
|
|
|
56
|
+
/** Moves the scene up by `top` and ends it `height` points down. The tree leaves out what is outside. */
|
|
57
|
+
function cropped(scene: Scene, top: number, height: number): Scene {
|
|
58
|
+
const up = (frame: Frame) => ({ ...frame, y: frame.y - top });
|
|
59
|
+
return {
|
|
60
|
+
size: { width: scene.size.width, height },
|
|
61
|
+
layers: scene.layers.map((layer, index) =>
|
|
62
|
+
index === 0
|
|
63
|
+
? { ...layer, frame: { x: 0, y: 0, width: scene.size.width, height } }
|
|
64
|
+
: { ...layer, frame: up(layer.frame) },
|
|
65
|
+
),
|
|
66
|
+
nodes: scene.nodes.map(node => ({ ...node, frame: up(node.frame) })),
|
|
67
|
+
fixed: scene.fixed.map(root => ({ ...root, frame: up(root.frame) })),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
42
71
|
/**
|
|
43
72
|
* Reads the page at the moment of the trigger. The window scrolls to its origin while the page is read, so a sticky
|
|
44
|
-
* bar is where the
|
|
73
|
+
* bar is where the picture draws it, and then returns to where it was. A page taller than iOS's limit of 16,000 pixels
|
|
74
|
+
* is captured by the iOS rules, which `captureRange` holds, and `top` is where the capture starts in the document.
|
|
45
75
|
*/
|
|
46
76
|
function readPage(options: CaptureOptions) {
|
|
47
77
|
const date = new Date();
|
|
@@ -57,27 +87,36 @@ function readPage(options: CaptureOptions) {
|
|
|
57
87
|
// Reading and scrolling back happen in one task, so the browser never paints the jump.
|
|
58
88
|
const moved = scroll.x !== 0 || scroll.y !== 0;
|
|
59
89
|
if (moved) scrollTo({ left: 0, top: 0, behavior: 'instant' });
|
|
60
|
-
const
|
|
90
|
+
const page = readScene(scroll);
|
|
61
91
|
if (moved) scrollTo({ left: scroll.x, top: scroll.y, behavior: 'instant' });
|
|
62
92
|
|
|
93
|
+
const { top, height } = captureRange(
|
|
94
|
+
page.size.height,
|
|
95
|
+
innerHeight,
|
|
96
|
+
scroll.y,
|
|
97
|
+
Math.floor(captureLimit / devicePixelRatio),
|
|
98
|
+
page.nodes.filter(node => node.layer === 0).map(node => node.frame),
|
|
99
|
+
);
|
|
100
|
+
const scene = cropped(page, top, height);
|
|
63
101
|
const capture: Capture = {
|
|
64
102
|
date,
|
|
65
103
|
size: scene.size,
|
|
66
|
-
visible: { x: scroll.x, y: scroll.y, width: innerWidth, height: innerHeight },
|
|
104
|
+
visible: { x: scroll.x, y: scroll.y - top, width: innerWidth, height: innerHeight },
|
|
67
105
|
nodes: buildTree(scene),
|
|
68
106
|
environment: environment(options, date),
|
|
69
107
|
context,
|
|
70
108
|
screenName: document.title || undefined,
|
|
71
109
|
};
|
|
72
|
-
return { capture, scene };
|
|
110
|
+
return { capture, scene, top };
|
|
73
111
|
}
|
|
74
112
|
|
|
75
113
|
/** Reads the page without drawing it, for automation that needs only the tree. */
|
|
76
114
|
export const captureTree = (options: CaptureOptions) => readPage(options).capture;
|
|
77
115
|
|
|
78
|
-
/** Reads the page and
|
|
116
|
+
/** Reads the page and copies it. */
|
|
79
117
|
export async function captureScreen(options: CaptureOptions): Promise<Screen> {
|
|
80
|
-
const { capture, scene } = readPage(options);
|
|
81
|
-
const
|
|
82
|
-
|
|
118
|
+
const { capture, scene, top } = readPage(options);
|
|
119
|
+
const picture = await takePicture(scene.size, top, capture.visible, scene.fixed);
|
|
120
|
+
const pixelRatio = devicePixelRatio;
|
|
121
|
+
return { capture, picture, image: picture.canvas(pixelRatio), pixelRatio };
|
|
83
122
|
}
|
package/src/dom/image.ts
CHANGED
|
@@ -204,7 +204,7 @@ function watermark(context: CanvasRenderingContext2D, { isLeft, origin }: Card)
|
|
|
204
204
|
* The image export: the screenshot with numbered boxes, each item's text and code location in a card beside it, a
|
|
205
205
|
* tagged box for each referenced node, and the capture's environment, so the one file is enough on its own.
|
|
206
206
|
*/
|
|
207
|
-
export function annotatedImage({ capture,
|
|
207
|
+
export function annotatedImage({ capture, picture, pixelRatio }: Screen, annotations: Annotations): HTMLCanvasElement {
|
|
208
208
|
const { column, padding, badge: badgeSize } = metrics;
|
|
209
209
|
const textWidth = column - padding * 3 - badgeSize;
|
|
210
210
|
const canvas = document.createElement('canvas');
|
|
@@ -307,7 +307,7 @@ export function annotatedImage({ capture, image, scale }: Screen, annotations: A
|
|
|
307
307
|
annotations.used.map(reference => capture.nodes[reference.target]!.frame),
|
|
308
308
|
);
|
|
309
309
|
const { shot, size } = placed;
|
|
310
|
-
const exportScale = Math.min(
|
|
310
|
+
const exportScale = Math.min(pixelRatio, Math.sqrt(pixelBudget / (size.width * size.height)));
|
|
311
311
|
canvas.width = Math.round(size.width * exportScale);
|
|
312
312
|
canvas.height = Math.round(size.height * exportScale);
|
|
313
313
|
context.scale(exportScale, exportScale);
|
|
@@ -317,7 +317,7 @@ export function annotatedImage({ capture, image, scale }: Screen, annotations: A
|
|
|
317
317
|
context.save();
|
|
318
318
|
roundedRect(context, shot, 12);
|
|
319
319
|
context.clip();
|
|
320
|
-
|
|
320
|
+
picture.draw(context, { x: 0, y: 0, width: shot.width, height: shot.height }, shot, 1);
|
|
321
321
|
context.restore();
|
|
322
322
|
roundedRect(context, shot, 12);
|
|
323
323
|
context.lineWidth = 1;
|
package/src/dom/screenshot.ts
CHANGED
|
@@ -1,33 +1,101 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Frame, Size } from '../core/index.ts';
|
|
1
|
+
import { createContext, destroyContext, domToForeignObjectSvg, loadMedia, type Options } from 'modern-screenshot';
|
|
2
|
+
import type { Frame, Point, Size } from '../core/index.ts';
|
|
3
3
|
import type { FixedRoot } from './scene.ts';
|
|
4
4
|
import { hostAttribute } from './tags.ts';
|
|
5
5
|
|
|
6
|
-
/** iOS Safari refuses a larger canvas
|
|
6
|
+
/** iOS Safari refuses a larger canvas. */
|
|
7
7
|
const pixelBudget = 16_000_000;
|
|
8
8
|
const maxSide = 16_384;
|
|
9
9
|
|
|
10
|
-
/** The
|
|
11
|
-
export function
|
|
10
|
+
/** The largest scale up to `wanted` at which `size` fits one canvas. */
|
|
11
|
+
export function canvasScale(size: Size, wanted: number) {
|
|
12
12
|
return Math.min(
|
|
13
|
-
|
|
13
|
+
wanted,
|
|
14
14
|
Math.sqrt(pixelBudget / (size.width * size.height)),
|
|
15
15
|
maxSide / size.width,
|
|
16
16
|
maxSide / size.height,
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/** One copy of the DOM as an SVG image, at its place in the capture, after `backdrop` covers the part that was on screen. */
|
|
21
|
+
type Part = { image?: HTMLImageElement; frame: Frame; backdrop?: { color: string; frame: Frame } };
|
|
22
|
+
|
|
23
|
+
const intersect = (a: Frame, b: Frame): Frame | undefined => {
|
|
24
|
+
const x = Math.max(a.x, b.x);
|
|
25
|
+
const y = Math.max(a.y, b.y);
|
|
26
|
+
const width = Math.min(a.x + a.width, b.x + b.width) - x;
|
|
27
|
+
const height = Math.min(a.y + a.height, b.y + b.height) - y;
|
|
28
|
+
return width > 0 && height > 0 ? { x, y, width, height } : undefined;
|
|
29
|
+
};
|
|
30
|
+
|
|
20
31
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* is then drawn where it was on screen, over a modal's backdrop, which a re-draw of the DOM leaves out.
|
|
32
|
+
* The frozen page, kept as vector copies of the DOM, so any part of it can be drawn sharp at any scale long after the
|
|
33
|
+
* page moved on: the editor draws what is in view at the screen's resolution, and an export draws at its own.
|
|
24
34
|
*/
|
|
25
|
-
export
|
|
26
|
-
|
|
35
|
+
export class Picture {
|
|
36
|
+
constructor(
|
|
37
|
+
readonly size: Size,
|
|
38
|
+
private readonly parts: Part[],
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Draws `region` of the capture into `context` with its corner at `at` and `scale` units per point. Each copy is
|
|
43
|
+
* drawn only where it meets the region, so the browser rasterizes no more of it than shows.
|
|
44
|
+
*/
|
|
45
|
+
draw(context: CanvasRenderingContext2D, region: Frame, at: Point, scale: number) {
|
|
46
|
+
const place = (frame: Frame) => ({
|
|
47
|
+
x: at.x + (frame.x - region.x) * scale,
|
|
48
|
+
y: at.y + (frame.y - region.y) * scale,
|
|
49
|
+
width: frame.width * scale,
|
|
50
|
+
height: frame.height * scale,
|
|
51
|
+
});
|
|
52
|
+
for (const { image, frame, backdrop } of this.parts) {
|
|
53
|
+
const tinted = backdrop && intersect(backdrop.frame, region);
|
|
54
|
+
if (tinted) {
|
|
55
|
+
const target = place(tinted);
|
|
56
|
+
context.fillStyle = backdrop.color;
|
|
57
|
+
context.fillRect(target.x, target.y, target.width, target.height);
|
|
58
|
+
}
|
|
59
|
+
const shown = image && intersect(frame, region);
|
|
60
|
+
if (!shown) continue;
|
|
61
|
+
const target = place(shown);
|
|
62
|
+
context.drawImage(
|
|
63
|
+
image,
|
|
64
|
+
shown.x - frame.x,
|
|
65
|
+
shown.y - frame.y,
|
|
66
|
+
shown.width,
|
|
67
|
+
shown.height,
|
|
68
|
+
target.x,
|
|
69
|
+
target.y,
|
|
70
|
+
target.width,
|
|
71
|
+
target.height,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The whole capture on a canvas, at `scale` or as close to it as one canvas allows. */
|
|
77
|
+
canvas(scale: number) {
|
|
78
|
+
const fitted = canvasScale(this.size, scale);
|
|
79
|
+
const canvas = document.createElement('canvas');
|
|
80
|
+
canvas.width = Math.round(this.size.width * fitted);
|
|
81
|
+
canvas.height = Math.round(this.size.height * fitted);
|
|
82
|
+
this.draw(canvas.getContext('2d')!, { x: 0, y: 0, ...this.size }, { x: 0, y: 0 }, fitted);
|
|
83
|
+
return canvas;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Copies the page, from `top` down `size.height` points. The browser gives a page no way to read its own pixels, so the
|
|
89
|
+
* DOM is copied into an SVG image. The document goes first, without the content that stays put while the page
|
|
90
|
+
* scrolls. Each such root is then copied on its own and drawn where it was on screen, over a modal's backdrop, which a
|
|
91
|
+
* copy of the DOM leaves out. `visible` and the roots' frames are in capture coordinates.
|
|
92
|
+
*/
|
|
93
|
+
export function takePicture(size: Size, top: number, visible: Frame, fixed: FixedRoot[]) {
|
|
94
|
+
return withFieldState(() => copy(size, top, visible, fixed));
|
|
27
95
|
}
|
|
28
96
|
|
|
29
97
|
/**
|
|
30
|
-
* The
|
|
98
|
+
* The copy carries attributes, and what a field holds is a property. This writes the text of each text area and the
|
|
31
99
|
* state of each checkbox and radio button into their attributes while `work` runs, then puts the attributes back.
|
|
32
100
|
*/
|
|
33
101
|
async function withFieldState<T>(work: () => Promise<T>) {
|
|
@@ -52,7 +120,7 @@ async function withFieldState<T>(work: () => Promise<T>) {
|
|
|
52
120
|
|
|
53
121
|
/**
|
|
54
122
|
* What the browser paints behind the page: the root's background, else the body's, which the root takes over, else the
|
|
55
|
-
* canvas color of the page's color scheme. The
|
|
123
|
+
* canvas color of the page's color scheme. The copy is transparent where the page sets no background.
|
|
56
124
|
*/
|
|
57
125
|
function pageBackground() {
|
|
58
126
|
for (const element of [document.documentElement, document.body]) {
|
|
@@ -67,52 +135,79 @@ function pageBackground() {
|
|
|
67
135
|
return color;
|
|
68
136
|
}
|
|
69
137
|
|
|
70
|
-
|
|
138
|
+
/** What modern-screenshot's `svgToDataUrl` does, which it does not export. */
|
|
139
|
+
function svgImageURL(svg: SVGElement) {
|
|
140
|
+
// XML 1.0 has no place for control characters or lone surrogates, and a page's text can hold them.
|
|
141
|
+
// oxlint-disable-next-line no-control-regex
|
|
142
|
+
const invalid = /[\u0000-\u0008\v\f\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/gu;
|
|
143
|
+
const markup = new XMLSerializer().serializeToString(svg).replace(invalid, '');
|
|
144
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(markup)}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Copies `node` into a decoded SVG image. modern-screenshot draws a copy with images again after an interval, for
|
|
149
|
+
* browsers that decode the images inside an SVG image only once it is first drawn. It waits once per image, which held
|
|
150
|
+
* Safari 2.4 s on a page with 20 icons, so the copy is drawn once and waits one interval in all.
|
|
151
|
+
*/
|
|
152
|
+
async function svgImage(node: Element, options: Options) {
|
|
153
|
+
const context = await createContext(node, options);
|
|
154
|
+
try {
|
|
155
|
+
const svg = await domToForeignObjectSvg(context);
|
|
156
|
+
const image = await loadMedia(svgImageURL(svg), { timeout: context.timeout });
|
|
157
|
+
if (context.drawImageCount > 0) {
|
|
158
|
+
document.createElement('canvas').getContext('2d')!.drawImage(image, 0, 0, 1, 1);
|
|
159
|
+
await new Promise(resolve => setTimeout(resolve, context.drawImageInterval));
|
|
160
|
+
}
|
|
161
|
+
return image;
|
|
162
|
+
} finally {
|
|
163
|
+
destroyContext(context);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function copy(size: Size, top: number, visible: Frame, fixed: FixedRoot[]) {
|
|
71
168
|
const roots = new Set<Node>(fixed.map(root => root.element));
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
169
|
+
const parts: Part[] = [
|
|
170
|
+
{
|
|
171
|
+
image: await svgImage(document.documentElement, {
|
|
172
|
+
width: size.width,
|
|
173
|
+
height: top + size.height,
|
|
174
|
+
backgroundColor: pageBackground(),
|
|
175
|
+
filter: node => !(roots.has(node) || (node instanceof Element && node.hasAttribute(hostAttribute))),
|
|
176
|
+
}),
|
|
177
|
+
frame: { x: 0, y: -top, width: size.width, height: top + size.height },
|
|
178
|
+
},
|
|
179
|
+
];
|
|
83
180
|
// A modal dialog or popover sits in the browser's top layer, above every other fixed root, whatever the DOM order.
|
|
84
181
|
const layered = [
|
|
85
182
|
...fixed.filter(root => root.backdrop === undefined),
|
|
86
183
|
...fixed.filter(root => root.backdrop !== undefined),
|
|
87
184
|
];
|
|
88
185
|
for (const root of layered) {
|
|
89
|
-
if (root.backdrop) {
|
|
90
|
-
context.fillStyle = root.backdrop;
|
|
91
|
-
context.fillRect(visible.x, visible.y, visible.width, visible.height);
|
|
92
|
-
}
|
|
93
186
|
const { frame } = root;
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
width
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
187
|
+
const backdrop = root.backdrop ? { color: root.backdrop, frame: visible } : undefined;
|
|
188
|
+
const image =
|
|
189
|
+
frame.width && frame.height
|
|
190
|
+
? await svgImage(root.element, {
|
|
191
|
+
width: frame.width,
|
|
192
|
+
height: frame.height,
|
|
193
|
+
// A layer inside this root is drawn by its own turn.
|
|
194
|
+
filter: node => node === root.element || !roots.has(node),
|
|
195
|
+
style: {
|
|
196
|
+
// A copy of a popover is not open, and a closed popover is `display: none`.
|
|
197
|
+
display: getComputedStyle(root.element).display,
|
|
198
|
+
position: 'static',
|
|
199
|
+
inset: 'auto',
|
|
200
|
+
margin: '0',
|
|
201
|
+
transform: 'none',
|
|
202
|
+
translate: 'none',
|
|
203
|
+
width: `${frame.width}px`,
|
|
204
|
+
height: `${frame.height}px`,
|
|
205
|
+
maxWidth: 'none',
|
|
206
|
+
maxHeight: 'none',
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
: undefined;
|
|
210
|
+
parts.push({ image, frame, backdrop });
|
|
116
211
|
}
|
|
117
|
-
return
|
|
212
|
+
return new Picture(size, parts);
|
|
118
213
|
}
|
package/src/editor/Editor.svelte
CHANGED
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
|
|
89
89
|
let dialog = $state<HTMLDialogElement>();
|
|
90
90
|
let pane = $state<HTMLElement>();
|
|
91
|
+
let sharp = $state<HTMLCanvasElement>();
|
|
91
92
|
let field = $state<HTMLTextAreaElement>();
|
|
92
93
|
let paneSize = $state({ width: 0, height: 0 });
|
|
93
94
|
let zoom = $state(1);
|
|
@@ -149,6 +150,53 @@
|
|
|
149
150
|
return () => removeEventListener('resize', measure);
|
|
150
151
|
});
|
|
151
152
|
|
|
153
|
+
/**
|
|
154
|
+
* The base image has as many pixels as one canvas can hold, which a zoom or a tall capture can outgrow. Then the part
|
|
155
|
+
* in view is drawn again from the picture at the screen's resolution, over the base image, once scrolling settles
|
|
156
|
+
* into a frame. A zoom hides it at once, since its place and scale no longer match.
|
|
157
|
+
*/
|
|
158
|
+
let sharpening = 0;
|
|
159
|
+
function sharpen() {
|
|
160
|
+
cancelAnimationFrame(sharpening);
|
|
161
|
+
sharpening = requestAnimationFrame(() => {
|
|
162
|
+
const canvas = sharp;
|
|
163
|
+
if (!pane || !canvas) return;
|
|
164
|
+
const ratio = devicePixelRatio;
|
|
165
|
+
if (scale * ratio <= screen.image.width / capture.size.width + 0.01) {
|
|
166
|
+
canvas.hidden = true;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const shown = pane.getBoundingClientRect();
|
|
170
|
+
const whole = canvas.parentElement!.getBoundingClientRect();
|
|
171
|
+
const left = Math.floor(Math.max(0, shown.left - whole.left));
|
|
172
|
+
const top = Math.floor(Math.max(0, shown.top - whole.top));
|
|
173
|
+
const width = Math.ceil(Math.min(whole.width, shown.right - whole.left)) - left;
|
|
174
|
+
const height = Math.ceil(Math.min(whole.height, shown.bottom - whole.top)) - top;
|
|
175
|
+
if (width <= 0 || height <= 0) {
|
|
176
|
+
canvas.hidden = true;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
canvas.width = Math.round(width * ratio);
|
|
180
|
+
canvas.height = Math.round(height * ratio);
|
|
181
|
+
canvas.style.cssText = `left:${left}px;top:${top}px;width:${width}px;height:${height}px`;
|
|
182
|
+
screen.picture.draw(
|
|
183
|
+
canvas.getContext('2d')!,
|
|
184
|
+
{ x: left / scale, y: top / scale, width: width / scale, height: height / scale },
|
|
185
|
+
{ x: 0, y: 0 },
|
|
186
|
+
scale * ratio,
|
|
187
|
+
);
|
|
188
|
+
canvas.hidden = false;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
$effect(() => {
|
|
193
|
+
void scale;
|
|
194
|
+
void paneSize;
|
|
195
|
+
if (sharp) sharp.hidden = true;
|
|
196
|
+
sharpen();
|
|
197
|
+
return () => cancelAnimationFrame(sharpening);
|
|
198
|
+
});
|
|
199
|
+
|
|
152
200
|
$effect(() => {
|
|
153
201
|
const toast = view.toast;
|
|
154
202
|
if (!toast || toast.problem) return;
|
|
@@ -364,6 +412,7 @@
|
|
|
364
412
|
aria-label="Screenshot"
|
|
365
413
|
bind:this={pane}
|
|
366
414
|
onwheel={wheel}
|
|
415
|
+
onscroll={sharpen}
|
|
367
416
|
onpointerdown={pointer}
|
|
368
417
|
onpointermove={pointer}
|
|
369
418
|
onpointerup={pointer}
|
|
@@ -379,10 +428,15 @@
|
|
|
379
428
|
onclick={event => act(() => session.pick(pointAt(event)))}
|
|
380
429
|
onmousemove={event => (hover = hitTest(capture, pointAt(event)))}
|
|
381
430
|
onmouseleave={() => (hover = undefined)}
|
|
382
|
-
{@attach node => {
|
|
383
|
-
node.prepend(screen.image);
|
|
384
|
-
}}
|
|
385
431
|
>
|
|
432
|
+
<div
|
|
433
|
+
class="picture"
|
|
434
|
+
{@attach node => {
|
|
435
|
+
node.prepend(screen.image);
|
|
436
|
+
}}
|
|
437
|
+
>
|
|
438
|
+
<canvas class="sharp" hidden bind:this={sharp}></canvas>
|
|
439
|
+
</div>
|
|
386
440
|
{#each view.similar as id (id)}
|
|
387
441
|
<span class="box similar" style={box(capture.nodes[id]!.frame)}></span>
|
|
388
442
|
{/each}
|
|
@@ -861,11 +915,25 @@
|
|
|
861
915
|
}
|
|
862
916
|
|
|
863
917
|
/* The picture is clipped, not the boxes over it, so a number badge on a box at the edge stays whole. */
|
|
864
|
-
.
|
|
918
|
+
.picture {
|
|
919
|
+
position: absolute;
|
|
920
|
+
inset: 0;
|
|
921
|
+
overflow: hidden;
|
|
922
|
+
border-radius: 10px;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
.picture :global(canvas) {
|
|
865
926
|
display: block;
|
|
866
927
|
width: 100%;
|
|
867
928
|
height: 100%;
|
|
868
|
-
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
.picture .sharp {
|
|
932
|
+
position: absolute;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
.sharp[hidden] {
|
|
936
|
+
display: none;
|
|
869
937
|
}
|
|
870
938
|
|
|
871
939
|
.box {
|
package/src/editor/engine.ts
CHANGED
|
@@ -125,13 +125,15 @@ export class Engine {
|
|
|
125
125
|
this.remember();
|
|
126
126
|
this.hidePill();
|
|
127
127
|
this.capturing = true;
|
|
128
|
-
this.pointed = at && { x: at.x + scrollX, y: at.y + scrollY };
|
|
129
128
|
try {
|
|
130
|
-
|
|
129
|
+
const screen = await captureScreen({
|
|
131
130
|
app: this.options.app,
|
|
132
131
|
version: this.options.version,
|
|
133
132
|
context: this.providers,
|
|
134
133
|
});
|
|
134
|
+
const { visible } = screen.capture;
|
|
135
|
+
this.pointed = at && { x: at.x + visible.x, y: at.y + visible.y };
|
|
136
|
+
this.screen = screen;
|
|
135
137
|
} finally {
|
|
136
138
|
this.capturing = false;
|
|
137
139
|
this.holding = false;
|