@stefan.inc/feedback-engine 1.2.0 → 1.2.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 CHANGED
@@ -8,7 +8,7 @@ Alt+Meta+click anything in the browser, click any component on the frozen screen
8
8
 
9
9
  | The editor in the browser | The `feedback.png` it exports |
10
10
  | :---: | :---: |
11
- | <img src="https://raw.githubusercontent.com/smirea/feedback-engine/master/docs/web-editor.png" alt="The Feedback editor in Google Chrome over the React example: the page's screenshot on the left with four notes numbered 1 to 4 and an @5 tag on the New List button, and the panel on the right with the Groceries row, a ListRow, selected and its note, Show a progress bar here." width="320"> | <img src="https://raw.githubusercontent.com/smirea/feedback-engine/master/docs/web-export.png" alt="The feedback.png exported from the browser: the page with four numbered boxes and the New List button tagged @5. Each box links to a card with its note, the component or element, and its file:line, and the app's details sit at the top right. The note on Settings reads Move this next to @5_Button." width="520"> |
11
+ | <img src="https://raw.githubusercontent.com/smirea/feedback-engine/master/docs/web-editor.png" alt="The Feedback editor in Google Chrome over the React example: the page's screenshot on the left with four notes numbered 1 to 4 and an @5 tag on the New List button, and the panel on the right with the Groceries row, a ListRow, selected and its note, Show a progress bar here." width="320"> | <img src="https://raw.githubusercontent.com/smirea/feedback-engine/master/docs/web-export.png" alt="The feedback.png exported from the browser: the page with four numbered boxes and the New List button tagged @5. One column on the left holds the app's details as Name: value lines, a line naming @5 with its file:line, and a card per note, each joined to its box by a curve that crosses no other box. Each card has its note, the component or element, and its file:line. The note on Settings reads Move this next to @5_Button." width="520"> |
12
12
 
13
13
  Four notes on the React example in Google Chrome. The last one points at a second element. Type `@`, click the element, and the note reads `Move this next to @5_Button`. The export draws that element's box and highlights the token in the same color, so an agent knows which button you meant. Each card names its component or element and the `file:line` it comes from, such as `src/components/ListRow.tsx:4`.
14
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stefan.inc/feedback-engine",
3
- "version": "1.2.0",
3
+ "version": "1.2.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",
@@ -8,13 +8,18 @@ export const cards = {
8
8
  badge: 20,
9
9
  padding: 10,
10
10
  rightColumnPenalty: 80,
11
+ crossingPenalty: 500,
11
12
  };
12
13
 
13
14
  export type Card = { isLeft: boolean; origin: Point };
14
15
  export type Layout = { shot: Frame; cards: Card[]; environment: Point; size: Size };
16
+ export type Curve = { start: Point; control1: Point; control2: Point; end: Point };
15
17
 
16
- /** From the card's badge to the nearest side of the target, at the badge's height when the target spans it. */
17
- export function line(card: Card, target: Frame): { start: Point; end: Point } {
18
+ /**
19
+ * An S curve from the card's badge to the nearest side of the target, at the badge's height when the target spans it.
20
+ * It leaves the card and meets the target level.
21
+ */
22
+ export function connector(card: Card, target: Frame): Curve {
18
23
  const start = {
19
24
  x: card.isLeft ? card.origin.x + cards.column : card.origin.x,
20
25
  y: card.origin.y + cards.padding + cards.badge / 2,
@@ -23,47 +28,122 @@ export function line(card: Card, target: Frame): { start: Point; end: Point } {
23
28
  x: card.isLeft ? target.x : target.x + target.width,
24
29
  y: Math.min(Math.max(start.y, target.y + 2), target.y + target.height - 2),
25
30
  };
26
- return { start, end };
31
+ const bend = (end.x - start.x) / 2;
32
+ return { start, control1: { x: start.x + bend, y: start.y }, control2: { x: end.x - bend, y: end.y }, end };
27
33
  }
28
34
 
29
- const length = (card: Card, target: Frame) => {
30
- const { start, end } = line(card, target);
31
- return Math.hypot(end.x - start.x, end.y - start.y);
32
- };
35
+ const samples = 16;
33
36
 
34
- /**
35
- * Where the image export puts each item's card. A card sits in the left column, as level with its target as the cards
36
- * above it allow. It moves to the right column when that makes its line to the target at least `rightColumnPenalty`
37
- * points shorter, as for a button at the right edge. The environment always sits at the top of the right column, and
38
- * right-column cards start below it. A tall capture is cut half the height that was on screen below the lowest box or
39
- * card, and never shorter than that height, and `shot` is the part that is drawn. `targets` and
40
- * `references` are frames in the capture. The layout's frames are in the image.
41
- */
42
- export function place(
37
+ function polyline({ start, control1, control2, end }: Curve): Point[] {
38
+ return Array.from({ length: samples + 1 }, (_, step) => {
39
+ const t = step / samples;
40
+ const u = 1 - t;
41
+ const at = (a: number, b: number, c: number, d: number) =>
42
+ u * u * u * a + 3 * u * u * t * b + 3 * u * t * t * c + t * t * t * d;
43
+ return { x: at(start.x, control1.x, control2.x, end.x), y: at(start.y, control1.y, control2.y, end.y) };
44
+ });
45
+ }
46
+
47
+ function segmentCrossesBox(a: Point, b: Point, box: Frame) {
48
+ let low = 0;
49
+ let high = 1;
50
+ const dx = b.x - a.x;
51
+ const dy = b.y - a.y;
52
+ const edges: [number, number][] = [
53
+ [-dx, a.x - box.x],
54
+ [dx, box.x + box.width - a.x],
55
+ [-dy, a.y - box.y],
56
+ [dy, box.y + box.height - a.y],
57
+ ];
58
+ for (const [direction, distance] of edges) {
59
+ if (direction === 0) {
60
+ if (distance < 0) return false;
61
+ continue;
62
+ }
63
+ const t = distance / direction;
64
+ if (direction < 0) low = Math.max(low, t);
65
+ else high = Math.min(high, t);
66
+ if (low > high) return false;
67
+ }
68
+ return true;
69
+ }
70
+
71
+ const turn = (a: Point, b: Point, c: Point) => Math.sign((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));
72
+ const segmentsCross = (a: Point, b: Point, c: Point, d: Point) =>
73
+ turn(a, b, c) * turn(a, b, d) < 0 && turn(c, d, a) * turn(c, d, b) < 0;
74
+
75
+ const contains = (box: Frame, point: Point) =>
76
+ point.x >= box.x && point.x <= box.x + box.width && point.y >= box.y && point.y <= box.y + box.height;
77
+
78
+ /** Boxes the path passes through, other than its target and the boxes around it, plus the earlier paths it crosses. */
79
+ function crossings(path: Point[], target: Frame, boxes: Frame[], earlier: Point[][]) {
80
+ const end = path.at(-1)!;
81
+ const pairs = path.slice(1).map((point, index) => [path[index]!, point] as const);
82
+ const box = boxes.filter(
83
+ box =>
84
+ !contains(box, end) &&
85
+ !(box.x === target.x && box.y === target.y && box.width === target.width && box.height === target.height) &&
86
+ pairs.some(([a, b]) => segmentCrossesBox(a, b, box)),
87
+ ).length;
88
+ const paths = earlier.filter(other =>
89
+ other.slice(1).some((d, index) => pairs.some(([a, b]) => segmentsCross(a, b, other[index]!, d))),
90
+ ).length;
91
+ return box + paths;
92
+ }
93
+
94
+ type Columns = 'both' | 'left' | 'right';
95
+ type Arrangement = Layout & { cost: number; crossings: number };
96
+
97
+ function arrange(
98
+ columns: Columns,
43
99
  targets: Frame[],
44
100
  heights: number[],
45
101
  screen: Size,
46
102
  environmentHeight: number,
47
103
  viewport: number,
48
- references: Frame[] = [],
49
- ): Layout {
50
- const { margin, column, gutter, spacing, rightColumnPenalty } = cards;
51
- const shot = { x: margin + column + gutter, y: margin, width: screen.width, height: screen.height };
104
+ references: Frame[],
105
+ ): Arrangement {
106
+ const { margin, column, gutter, spacing, rightColumnPenalty, crossingPenalty } = cards;
107
+ const shot = {
108
+ x: columns === 'right' ? margin : margin + column + gutter,
109
+ y: margin,
110
+ width: screen.width,
111
+ height: screen.height,
112
+ };
52
113
  const rightX = shot.x + shot.width + gutter;
53
- const moved = targets.map(target => ({ ...target, x: target.x + shot.x, y: target.y + shot.y }));
114
+ const inShot = (frame: Frame) => ({ ...frame, x: frame.x + shot.x, y: frame.y + shot.y });
115
+ const moved = targets.map(inShot);
116
+ const boxes = [...moved, ...references.map(inShot)];
54
117
  const midY = (index: number) => moved[index]!.y + moved[index]!.height / 2;
55
118
  const placed: Card[] = moved.map(() => ({ isLeft: true, origin: { x: 0, y: 0 } }));
56
- let nextLeft = margin;
57
- let nextRight = margin + environmentHeight + spacing * 2;
119
+ const paths: Point[][] = [];
120
+ const underEnvironment = margin + environmentHeight + spacing * 2;
121
+ let nextLeft = columns === 'left' ? underEnvironment : margin;
122
+ let nextRight = underEnvironment;
123
+ let cost = 0;
124
+ let crossed = 0;
58
125
  const order = moved.map((_, index) => index).toSorted((a, b) => midY(a) - midY(b) || a - b);
59
126
  for (const index of order) {
127
+ const target = moved[index]!;
60
128
  const desired = Math.max(midY(index) - heights[index]! / 2, margin);
61
- const left: Card = { isLeft: true, origin: { x: margin, y: Math.max(desired, nextLeft) } };
62
- const right: Card = { isLeft: false, origin: { x: rightX, y: Math.max(desired, nextRight) } };
63
- const card = length(left, moved[index]!) - length(right, moved[index]!) >= rightColumnPenalty ? right : left;
64
- placed[index] = card;
65
- if (card.isLeft) nextLeft = card.origin.y + heights[index]! + spacing;
66
- else nextRight = card.origin.y + heights[index]! + spacing;
129
+ const options: Card[] = [];
130
+ if (columns !== 'right') options.push({ isLeft: true, origin: { x: margin, y: Math.max(desired, nextLeft) } });
131
+ if (columns !== 'left') options.push({ isLeft: false, origin: { x: rightX, y: Math.max(desired, nextRight) } });
132
+ const scored = options.map(card => {
133
+ const curve = connector(card, target);
134
+ const path = polyline(curve);
135
+ const count = crossings(path, target, boxes, paths);
136
+ const length = Math.hypot(curve.end.x - curve.start.x, curve.end.y - curve.start.y);
137
+ const penalty = columns === 'both' && !card.isLeft ? rightColumnPenalty : 0;
138
+ return { card, path, count, cost: length + penalty + count * crossingPenalty };
139
+ });
140
+ const best = scored.reduce((chosen, option) => (option.cost <= chosen.cost ? option : chosen));
141
+ placed[index] = best.card;
142
+ paths.push(best.path);
143
+ cost += best.cost;
144
+ crossed += best.count;
145
+ if (best.card.isLeft) nextLeft = best.card.origin.y + heights[index]! + spacing;
146
+ else nextRight = best.card.origin.y + heights[index]! + spacing;
67
147
  }
68
148
 
69
149
  const lowest = Math.max(
@@ -76,7 +156,39 @@ export function place(
76
156
  return {
77
157
  shot,
78
158
  cards: placed,
79
- environment: { x: rightX, y: margin },
80
- size: { width: rightX + column + margin, height },
159
+ environment: { x: columns === 'left' ? margin : rightX, y: margin },
160
+ size: { width: columns === 'left' ? shot.x + shot.width + margin : rightX + column + margin, height },
161
+ cost,
162
+ crossings: crossed,
81
163
  };
82
164
  }
165
+
166
+ /**
167
+ * Where the image export puts each item's card. Cards go in the column whose path to the target costs least: its
168
+ * length, `rightColumnPenalty` points for the right column, and `crossingPenalty` for each other box or earlier path
169
+ * it crosses, as level with the target as the cards above allow. The environment sits at the top of the right column,
170
+ * and right-column cards start below it. When one column holds the environment and every card with no crossing and no
171
+ * taller image, the image drops the other column and keeps only a margin on that side. A tall capture is cut half the
172
+ * height that was on screen below the lowest box or card, and never shorter than that height, and `shot` is the part
173
+ * that is drawn. `targets` and `references` are frames in the capture. The layout's frames are in the image.
174
+ */
175
+ export function place(
176
+ targets: Frame[],
177
+ heights: number[],
178
+ screen: Size,
179
+ environmentHeight: number,
180
+ viewport: number,
181
+ references: Frame[] = [],
182
+ ): Layout {
183
+ const build = (columns: Columns) =>
184
+ arrange(columns, targets, heights, screen, environmentHeight, viewport, references);
185
+ const both = build('both');
186
+ const single = [build('left'), build('right')]
187
+ .filter(option => option.crossings === 0 && option.size.height <= both.size.height)
188
+ .reduce<Arrangement | undefined>(
189
+ (chosen, option) => (chosen && chosen.cost <= option.cost ? chosen : option),
190
+ undefined,
191
+ );
192
+ const { shot, cards: placed, environment, size } = single ?? both;
193
+ return { shot, cards: placed, environment, size };
194
+ }
package/src/dom/image.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  cards as metrics,
3
- line,
3
+ connector,
4
4
  nearestSource,
5
5
  paletteColor,
6
6
  place,
@@ -174,18 +174,38 @@ export function annotatedImage({ capture, image, scale }: Screen, annotations: A
174
174
  const context = canvas.getContext('2d')!;
175
175
 
176
176
  const app = capture.environment.find(entry => entry.name === 'App')?.value ?? 'App';
177
+ const flagged = new Set(annotations.items.map(item => item.target));
177
178
  const environment = layout(
178
179
  context,
179
180
  [
180
- { runs: [{ text: `Feedback: ${app}`, font: `700 16px ${sans}`, color: primary }], gap: 6 },
181
- ...capture.environment.flatMap(entry =>
182
- entry.name === 'App'
183
- ? []
184
- : [
185
- { runs: [{ text: entry.name.toUpperCase(), font: `600 10px ${sans}`, color: secondary, spacing: 0.5 }] },
186
- { runs: [{ text: entry.value, font: `12.5px ${sans}`, color: primary }], gap: 6 },
181
+ { runs: [{ text: `Feedback: ${app}`, font: `700 16px ${sans}`, color: primary }], gap: 8 },
182
+ ...capture.environment
183
+ .filter(entry => entry.name !== 'App')
184
+ .map(entry => ({
185
+ runs: [
186
+ { text: `${entry.name}: `, font: `600 12.5px ${sans}`, color: secondary },
187
+ { text: entry.value, font: `12.5px ${sans}`, color: primary },
188
+ ],
189
+ gap: 4,
190
+ })),
191
+ ...annotations.used
192
+ .filter(reference => !flagged.has(reference.target))
193
+ .map(reference => {
194
+ const source = nearestSource(capture, reference.target);
195
+ return {
196
+ runs: [
197
+ {
198
+ text: ` ${tag(reference)} `,
199
+ font: `700 11px ${sans}`,
200
+ color: 'white',
201
+ background: paletteColor(annotations.colorIndex(reference.target)),
202
+ },
203
+ ...(source ? [{ text: ` ${source}`, font: `11px ${mono}`, color: primary }] : []),
204
+ { text: ` ${title(capture.nodes[reference.target]!)}`, font: `12.5px ${sans}`, color: primary },
187
205
  ],
188
- ),
206
+ gap: 4,
207
+ };
208
+ }),
189
209
  ],
190
210
  column,
191
211
  );
@@ -263,10 +283,10 @@ export function annotatedImage({ capture, image, scale }: Screen, annotations: A
263
283
  for (const [index, item] of items.entries()) {
264
284
  const target = inShot(item.frame);
265
285
  dashedBox(context, target, item.color);
266
- const { start, end } = line(placed.cards[index]!, target);
286
+ const { start, control1, control2, end } = connector(placed.cards[index]!, target);
267
287
  context.beginPath();
268
288
  context.moveTo(start.x, start.y);
269
- context.lineTo(end.x, end.y);
289
+ context.bezierCurveTo(control1.x, control1.y, control2.x, control2.y, end.x, end.y);
270
290
  context.lineWidth = 1.25;
271
291
  context.strokeStyle = withAlpha(item.color, 0.6);
272
292
  context.stroke();
@@ -280,7 +300,6 @@ export function annotatedImage({ capture, image, scale }: Screen, annotations: A
280
300
  });
281
301
  }
282
302
 
283
- const flagged = new Set(annotations.items.map(item => item.target));
284
303
  for (const reference of annotations.used) {
285
304
  const color = paletteColor(annotations.colorIndex(reference.target));
286
305
  const target = inShot(capture.nodes[reference.target]!.frame);