@tokenoftrust/storefront-runner 6.12.16 → 6.12.18
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/apps/storefront/package.json +1 -0
- package/package.json +1 -1
- package/packages/feedback/fixtures/v1/expected-findings.json +16 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/carries-transport-credential.json +14 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/control-char-in-description.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/legacy-schema-version.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/page-path-with-query.json +14 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/title-too-short.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/element-problem.json +33 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/page-comment.json +20 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/region-improvement.json +24 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/text-selection-feature.json +30 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/accepted-at-not-utc.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/bad-feedback-id.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/legacy-contract-version.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/missing-replayed.json +8 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/accepted.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/fractional-seconds.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/replayed.json +9 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/page-kind-claims-structure.json +6 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/path-too-deep.json +17 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/relative-region-out-of-range.json +11 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/selector-uses-class.json +7 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/source-precision-without-anchor.json +6 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/element-structure.json +21 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/host-iframe.json +14 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/source-anchor.json +12 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/text-selection.json +26 -0
- package/packages/feedback/package.json +29 -0
- package/packages/feedback/src/index.ts +1 -0
- package/packages/feedback/src/primitives/composer.ts +639 -0
- package/packages/feedback/src/primitives/dialog.ts +263 -0
- package/packages/feedback/src/primitives/hostAdapter.ts +116 -0
- package/packages/feedback/src/primitives/index.ts +10 -0
- package/packages/feedback/src/primitives/launcher.ts +81 -0
- package/packages/feedback/src/primitives/picker.ts +663 -0
- package/packages/feedback/src/primitives/redaction.ts +204 -0
- package/packages/feedback/src/primitives/selection.ts +49 -0
- package/packages/feedback/src/primitives/shiftAccelerator.ts +160 -0
- package/packages/feedback/src/primitives/styles.ts +64 -0
- package/packages/feedback/src/primitives/surface.ts +124 -0
- package/packages/feedback/src/primitives/target.ts +293 -0
- package/packages/feedback/src/v1/guards.ts +318 -0
- package/packages/feedback/src/v1/index.ts +2 -0
- package/packages/feedback/src/v1/types.ts +177 -0
- package/packages/feedback/tsconfig.json +9 -0
- package/packages/feedback/vitest.config.cts +20 -0
- package/pnpm-lock.runner.yaml +15 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selection: hover glow, click-to-select, Larger/Smaller area, region drag, two-corner taps, a
|
|
3
|
+
* keyboard target navigator, and text-selection picking.
|
|
4
|
+
*
|
|
5
|
+
* While picking an element or region, a full-viewport shield inside the primitive's shadow root
|
|
6
|
+
* receives every pointer event, and capture-phase window listeners swallow any
|
|
7
|
+
* click/pointer/touch/submit event aimed at the page, so links, buttons, forms, cart and checkout
|
|
8
|
+
* controls never activate during selection. When the picker is inactive none of these listeners
|
|
9
|
+
* exist. Text mode needs the page's own selection gesture, so it installs no shield and no
|
|
10
|
+
* swallowing; the reviewer confirms with "Use selection".
|
|
11
|
+
*
|
|
12
|
+
* Hit testing uses `document.elementsFromPoint` with the picker host filtered out. A point over an
|
|
13
|
+
* iframe, canvas or shadow host resolves to that host element (see target.ts).
|
|
14
|
+
*/
|
|
15
|
+
import type { FeedbackTarget, Rect, TargetEvidenceV1, TextSelection } from "../v1/types.js";
|
|
16
|
+
import { isSurfacePermitted, type HostAdapter } from "./hostAdapter.js";
|
|
17
|
+
import { readTextSelection } from "./selection.js";
|
|
18
|
+
import { createSurface, eventFromHost, h } from "./surface.js";
|
|
19
|
+
import {
|
|
20
|
+
buildTargetEvidence,
|
|
21
|
+
describeTarget,
|
|
22
|
+
elementTarget,
|
|
23
|
+
isHostBoundary,
|
|
24
|
+
pageTarget,
|
|
25
|
+
rectFromCorners,
|
|
26
|
+
regionTarget,
|
|
27
|
+
safeLabel,
|
|
28
|
+
} from "./target.js";
|
|
29
|
+
|
|
30
|
+
export type PickMode = "element" | "region";
|
|
31
|
+
|
|
32
|
+
export interface PickResult {
|
|
33
|
+
kind: "element" | "region";
|
|
34
|
+
element: Element | null;
|
|
35
|
+
rect?: Rect;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface PickerCallbacks {
|
|
39
|
+
onHover(el: Element | null, label: string | null): void;
|
|
40
|
+
onRegionDraft(rect: Rect | null): void;
|
|
41
|
+
onPick(result: PickResult): void;
|
|
42
|
+
onInstruction(text: string): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const SWALLOW = [
|
|
46
|
+
"click",
|
|
47
|
+
"dblclick",
|
|
48
|
+
"auxclick",
|
|
49
|
+
"contextmenu",
|
|
50
|
+
"pointerdown",
|
|
51
|
+
"pointerup",
|
|
52
|
+
"mousedown",
|
|
53
|
+
"mouseup",
|
|
54
|
+
"touchstart",
|
|
55
|
+
"touchend",
|
|
56
|
+
"submit",
|
|
57
|
+
] as const;
|
|
58
|
+
|
|
59
|
+
const TAP_SLOP = 6;
|
|
60
|
+
/**
|
|
61
|
+
* After a pick, the browser still synthesizes the tap's compatibility mouse events and click — at the
|
|
62
|
+
* page element now under the point, because the shield is gone. Keep swallowing for this long.
|
|
63
|
+
*/
|
|
64
|
+
const GHOST_CLICK_GRACE_MS = 500;
|
|
65
|
+
|
|
66
|
+
export class Picker {
|
|
67
|
+
private active = false;
|
|
68
|
+
private mode: PickMode = "element";
|
|
69
|
+
private hovered: Element | null = null;
|
|
70
|
+
/** Element stack for Larger/Smaller: [original, parent, grandparent, ...]. */
|
|
71
|
+
private stack: Element[] = [];
|
|
72
|
+
private stackIndex = 0;
|
|
73
|
+
private dragStart: { x: number; y: number } | null = null;
|
|
74
|
+
private firstCorner: { x: number; y: number } | null = null;
|
|
75
|
+
private attached = false;
|
|
76
|
+
private graceUntil = 0;
|
|
77
|
+
/** A keyboard or toolbar pick synthesizes no click, so it has no ghost click to swallow. */
|
|
78
|
+
private pickWithoutGhostClick = false;
|
|
79
|
+
private detachTimer: ReturnType<typeof setTimeout> | null = null;
|
|
80
|
+
private readonly swallow = (e: Event) => {
|
|
81
|
+
if (!this.active && performance.now() > this.graceUntil) return;
|
|
82
|
+
if (this.isOwn(e)) return;
|
|
83
|
+
// After the pick the ghost click lands on the page; the package's own dialog and launcher are never its target.
|
|
84
|
+
if (!this.active && e.composedPath().some((n) => n instanceof Element && n.localName.startsWith("feedback-"))) return;
|
|
85
|
+
e.preventDefault();
|
|
86
|
+
e.stopImmediatePropagation();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
private readonly win: Window,
|
|
91
|
+
private readonly doc: Document,
|
|
92
|
+
private readonly host: Element,
|
|
93
|
+
private readonly shield: HTMLElement,
|
|
94
|
+
private readonly cb: PickerCallbacks,
|
|
95
|
+
) {
|
|
96
|
+
shield.addEventListener("pointermove", (e) => this.onMove(e));
|
|
97
|
+
shield.addEventListener("pointerdown", (e) => this.onDown(e));
|
|
98
|
+
shield.addEventListener("pointerup", (e) => this.onUp(e));
|
|
99
|
+
shield.addEventListener("click", (e) => {
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
e.stopPropagation();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get isActive(): boolean {
|
|
106
|
+
return this.active;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get currentMode(): PickMode {
|
|
110
|
+
return this.mode;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get current(): Element | null {
|
|
114
|
+
return this.stack[this.stackIndex] ?? this.hovered;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param seed the page element the keyboard navigator starts on (the element that had focus when
|
|
119
|
+
* selection began); falls back to the element at the viewport centre, then the first visible
|
|
120
|
+
* element of the page.
|
|
121
|
+
*/
|
|
122
|
+
start(mode: PickMode, seed: Element | null = null): void {
|
|
123
|
+
this.mode = mode;
|
|
124
|
+
this.firstCorner = null;
|
|
125
|
+
this.dragStart = null;
|
|
126
|
+
this.cb.onRegionDraft(null);
|
|
127
|
+
if (this.detachTimer) {
|
|
128
|
+
clearTimeout(this.detachTimer);
|
|
129
|
+
this.detachTimer = null;
|
|
130
|
+
}
|
|
131
|
+
this.active = true;
|
|
132
|
+
this.pickWithoutGhostClick = false;
|
|
133
|
+
if (!this.attached) {
|
|
134
|
+
this.attached = true;
|
|
135
|
+
for (const type of SWALLOW) this.win.addEventListener(type, this.swallow, { capture: true });
|
|
136
|
+
}
|
|
137
|
+
this.shield.hidden = false;
|
|
138
|
+
this.shield.dataset.mode = mode;
|
|
139
|
+
if (!this.current) {
|
|
140
|
+
const usableSeed = seed && seed.isConnected && seed !== this.doc.body && seed !== this.doc.documentElement && !this.host.contains(seed) ? seed : null;
|
|
141
|
+
this.setStackFrom(
|
|
142
|
+
usableSeed ??
|
|
143
|
+
this.hit(this.win.innerWidth / 2, this.win.innerHeight / 2) ??
|
|
144
|
+
(this.doc.body ? this.firstVisible(Array.from(this.doc.body.children)) : null),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
this.cb.onInstruction(
|
|
148
|
+
mode === "element"
|
|
149
|
+
? "Select something to comment on. Esc to cancel."
|
|
150
|
+
: "Drag to draw a region, or tap two corners. Esc to cancel.",
|
|
151
|
+
);
|
|
152
|
+
this.emitHover();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
stop(): void {
|
|
156
|
+
const ghostClick = !this.pickWithoutGhostClick;
|
|
157
|
+
this.pickWithoutGhostClick = false;
|
|
158
|
+
if (this.active && this.attached && !ghostClick) this.detach();
|
|
159
|
+
else if (this.active && this.attached) {
|
|
160
|
+
this.graceUntil = performance.now() + GHOST_CLICK_GRACE_MS;
|
|
161
|
+
if (this.detachTimer) clearTimeout(this.detachTimer);
|
|
162
|
+
this.detachTimer = setTimeout(() => this.detach(), GHOST_CLICK_GRACE_MS + 50);
|
|
163
|
+
}
|
|
164
|
+
this.active = false;
|
|
165
|
+
this.shield.hidden = true;
|
|
166
|
+
this.dragStart = null;
|
|
167
|
+
this.firstCorner = null;
|
|
168
|
+
this.hovered = null;
|
|
169
|
+
this.stack = [];
|
|
170
|
+
this.stackIndex = 0;
|
|
171
|
+
this.cb.onHover(null, null);
|
|
172
|
+
this.cb.onRegionDraft(null);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The next stop() follows a pick that came from a control, not a pointer tap, so no click is synthesized. */
|
|
176
|
+
pickedByControl(): void {
|
|
177
|
+
this.pickWithoutGhostClick = true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Remove every page listener now (teardown, or when the ghost-click grace has elapsed). */
|
|
181
|
+
detach(): void {
|
|
182
|
+
if (this.detachTimer) {
|
|
183
|
+
clearTimeout(this.detachTimer);
|
|
184
|
+
this.detachTimer = null;
|
|
185
|
+
}
|
|
186
|
+
if (this.attached) {
|
|
187
|
+
for (const type of SWALLOW) this.win.removeEventListener(type, this.swallow, { capture: true });
|
|
188
|
+
this.attached = false;
|
|
189
|
+
}
|
|
190
|
+
this.graceUntil = 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Larger area: promote to the containing element. */
|
|
194
|
+
larger(): void {
|
|
195
|
+
const cur = this.current;
|
|
196
|
+
if (!cur) return;
|
|
197
|
+
if (this.stackIndex < this.stack.length - 1) this.stackIndex += 1;
|
|
198
|
+
else {
|
|
199
|
+
const parent = cur.parentElement;
|
|
200
|
+
if (parent && parent !== this.doc.documentElement) {
|
|
201
|
+
this.stack.push(parent);
|
|
202
|
+
this.stackIndex = this.stack.length - 1;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
this.emitHover();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Smaller area: step back toward the originally hovered element, or into the first child. */
|
|
209
|
+
smaller(): void {
|
|
210
|
+
if (this.stackIndex > 0) {
|
|
211
|
+
this.stackIndex -= 1;
|
|
212
|
+
this.emitHover();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.child();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
parent(): void {
|
|
219
|
+
this.larger();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
child(): void {
|
|
223
|
+
const cur = this.current;
|
|
224
|
+
if (!cur || isHostBoundary(cur)) return;
|
|
225
|
+
const next = this.firstVisible(Array.from(cur.children));
|
|
226
|
+
if (next) this.setStackFrom(next);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
next(): void {
|
|
230
|
+
this.sibling(1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
previous(): void {
|
|
234
|
+
this.sibling(-1);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Select the navigator's current element (keyboard path). */
|
|
238
|
+
selectCurrent(): void {
|
|
239
|
+
const el = this.current;
|
|
240
|
+
if (!el) return;
|
|
241
|
+
this.pickWithoutGhostClick = true;
|
|
242
|
+
if (this.mode === "region") {
|
|
243
|
+
const r = el.getBoundingClientRect();
|
|
244
|
+
const rect = rectFromCorners(
|
|
245
|
+
{ x: r.left, y: r.top },
|
|
246
|
+
{ x: r.right, y: r.bottom },
|
|
247
|
+
{ width: this.win.innerWidth, height: this.win.innerHeight },
|
|
248
|
+
);
|
|
249
|
+
this.cb.onPick({ kind: "region", element: el.parentElement ?? el, rect });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
this.cb.onPick({ kind: "element", element: el });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private sibling(step: 1 | -1): void {
|
|
256
|
+
const cur = this.current;
|
|
257
|
+
const parent = cur?.parentElement;
|
|
258
|
+
if (!cur || !parent) return;
|
|
259
|
+
const siblings = Array.from(parent.children).filter((c) => c !== this.host && this.visible(c));
|
|
260
|
+
const i = siblings.indexOf(cur);
|
|
261
|
+
const next = siblings[(i + step + siblings.length) % siblings.length];
|
|
262
|
+
if (next) this.setStackFrom(next);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private setStackFrom(el: Element | null): void {
|
|
266
|
+
this.stack = el ? [el] : [];
|
|
267
|
+
this.stackIndex = 0;
|
|
268
|
+
this.hovered = el;
|
|
269
|
+
this.emitHover();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private emitHover(): void {
|
|
273
|
+
const el = this.current;
|
|
274
|
+
this.cb.onHover(el, el ? safeLabel(el) : null);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private isOwn(e: Event): boolean {
|
|
278
|
+
const path = typeof e.composedPath === "function" ? e.composedPath() : [];
|
|
279
|
+
return path.includes(this.host);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private visible(el: Element): boolean {
|
|
283
|
+
const r = el.getBoundingClientRect();
|
|
284
|
+
return r.width > 0 || r.height > 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private firstVisible(list: Element[]): Element | null {
|
|
288
|
+
return list.find((c) => c !== this.host && this.visible(c)) ?? list.find((c) => c !== this.host) ?? null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Topmost page element at a viewport point, skipping the annotation host. */
|
|
292
|
+
hit(x: number, y: number): Element | null {
|
|
293
|
+
const list =
|
|
294
|
+
typeof this.doc.elementsFromPoint === "function"
|
|
295
|
+
? this.doc.elementsFromPoint(x, y)
|
|
296
|
+
: [this.doc.elementFromPoint(x, y)].filter((e): e is Element => !!e);
|
|
297
|
+
for (const el of list) {
|
|
298
|
+
if (el === this.host || this.host.contains(el)) continue;
|
|
299
|
+
if (el === this.doc.documentElement) continue;
|
|
300
|
+
return el;
|
|
301
|
+
}
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private onMove(e: PointerEvent): void {
|
|
306
|
+
if (!this.active) return;
|
|
307
|
+
if (this.mode === "region") {
|
|
308
|
+
if (this.dragStart) {
|
|
309
|
+
this.cb.onRegionDraft(this.rect(this.dragStart, { x: e.clientX, y: e.clientY }));
|
|
310
|
+
} else if (this.firstCorner) {
|
|
311
|
+
this.cb.onRegionDraft(this.rect(this.firstCorner, { x: e.clientX, y: e.clientY }));
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const el = this.hit(e.clientX, e.clientY);
|
|
316
|
+
if (el !== this.hovered) this.setStackFrom(el);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private onDown(e: PointerEvent): void {
|
|
320
|
+
if (!this.active) return;
|
|
321
|
+
e.preventDefault();
|
|
322
|
+
e.stopPropagation();
|
|
323
|
+
if (this.mode === "region") this.dragStart = { x: e.clientX, y: e.clientY };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private onUp(e: PointerEvent): void {
|
|
327
|
+
if (!this.active) return;
|
|
328
|
+
e.preventDefault();
|
|
329
|
+
e.stopPropagation();
|
|
330
|
+
const point = { x: e.clientX, y: e.clientY };
|
|
331
|
+
if (this.mode === "element") {
|
|
332
|
+
const el = this.hit(point.x, point.y);
|
|
333
|
+
if (el) {
|
|
334
|
+
if (el !== this.hovered) this.setStackFrom(el);
|
|
335
|
+
this.cb.onPick({ kind: "element", element: this.current });
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const start = this.dragStart;
|
|
340
|
+
this.dragStart = null;
|
|
341
|
+
if (start && (Math.abs(start.x - point.x) > TAP_SLOP || Math.abs(start.y - point.y) > TAP_SLOP)) {
|
|
342
|
+
this.finishRegion(start, point);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
// A tap: two separate taps choose two corners.
|
|
346
|
+
if (!this.firstCorner) {
|
|
347
|
+
this.firstCorner = point;
|
|
348
|
+
this.cb.onInstruction("First corner set. Tap the opposite corner. Esc to cancel.");
|
|
349
|
+
this.cb.onRegionDraft(this.rect(point, point));
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const first = this.firstCorner;
|
|
353
|
+
this.firstCorner = null;
|
|
354
|
+
this.finishRegion(first, point);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private rect(a: { x: number; y: number }, b: { x: number; y: number }): Rect {
|
|
358
|
+
return rectFromCorners(a, b, { width: this.win.innerWidth, height: this.win.innerHeight });
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private finishRegion(a: { x: number; y: number }, b: { x: number; y: number }): void {
|
|
362
|
+
const rect = this.rect(a, b);
|
|
363
|
+
if (rect.width < 4 || rect.height < 4) {
|
|
364
|
+
this.cb.onInstruction("That region is too small. Drag a larger area or tap two corners.");
|
|
365
|
+
this.cb.onRegionDraft(null);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const container = this.containerFor(rect);
|
|
369
|
+
this.cb.onRegionDraft(rect);
|
|
370
|
+
this.cb.onPick({ kind: "region", element: container, rect });
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Deepest page element at the region centre whose box contains the whole region. */
|
|
374
|
+
private containerFor(rect: Rect): Element | null {
|
|
375
|
+
let el = this.hit(rect.x + rect.width / 2, rect.y + rect.height / 2);
|
|
376
|
+
while (el && el !== this.doc.body) {
|
|
377
|
+
const r = el.getBoundingClientRect();
|
|
378
|
+
if (r.left <= rect.x && r.top <= rect.y && r.right >= rect.x + rect.width && r.bottom >= rect.y + rect.height) {
|
|
379
|
+
return el;
|
|
380
|
+
}
|
|
381
|
+
el = el.parentElement;
|
|
382
|
+
}
|
|
383
|
+
return this.doc.body;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ---------------------------------------------------------------------------------------------
|
|
388
|
+
// Picker surface: shield, highlight, region box and toolbar around the Picker engine.
|
|
389
|
+
|
|
390
|
+
export type PickerSurfaceMode = PickMode | "text";
|
|
391
|
+
|
|
392
|
+
/** What a finished pick produces: the evidence, plus a safe label for the UI. */
|
|
393
|
+
export interface PickOutcome {
|
|
394
|
+
kind: "element" | "region" | "page" | "text";
|
|
395
|
+
/** The picked element, kept only so the UI can highlight it; never serialize it. */
|
|
396
|
+
element: Element | null;
|
|
397
|
+
/** Orientation label: a tag/role word, "Whole page", "Region WxH", or "Selected text". Never page text. */
|
|
398
|
+
label: string;
|
|
399
|
+
target: FeedbackTarget;
|
|
400
|
+
selection?: TextSelection;
|
|
401
|
+
evidence: TargetEvidenceV1;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export interface PickerSurfaceOptions {
|
|
405
|
+
win: Window;
|
|
406
|
+
doc: Document;
|
|
407
|
+
adapter?: HostAdapter;
|
|
408
|
+
/** Pick modes the toolbar offers. Default: element, region and text. A host whose intake cannot carry a text selection omits `text`. */
|
|
409
|
+
modes?: readonly PickerSurfaceMode[];
|
|
410
|
+
onPick(outcome: PickOutcome): void;
|
|
411
|
+
/** Called after the reviewer cancels (Esc or the Cancel button). */
|
|
412
|
+
onCancel?(): void;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export interface PickerSurface {
|
|
416
|
+
mount(): boolean;
|
|
417
|
+
/** Stops any active pick, then removes the surface. Idempotent. */
|
|
418
|
+
unmount(): void;
|
|
419
|
+
/** Begin picking. `seed` is the page element the keyboard navigator starts on. Returns false when not permitted. */
|
|
420
|
+
start(mode?: PickerSurfaceMode, seed?: Element | null): boolean;
|
|
421
|
+
/** End picking without reporting a result. */
|
|
422
|
+
stop(): void;
|
|
423
|
+
/**
|
|
424
|
+
* The outcome a pick of `element` would report, without starting a pick or calling `onPick`. For a
|
|
425
|
+
* host that re-targets a draft (for example, widening it to the containing element). Null for an
|
|
426
|
+
* element the picker never offers (the document element, or one of the package's own surfaces).
|
|
427
|
+
*/
|
|
428
|
+
outcomeFor(element: Element): PickOutcome | null;
|
|
429
|
+
readonly isActive: boolean;
|
|
430
|
+
readonly mode: PickerSurfaceMode;
|
|
431
|
+
readonly host: HTMLElement;
|
|
432
|
+
/** Diagnostic access to the shadow root (a closed root hides it from page script). */
|
|
433
|
+
readonly root: ShadowRoot;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function createPicker(options: PickerSurfaceOptions): PickerSurface {
|
|
437
|
+
const { win, doc, adapter } = options;
|
|
438
|
+
const surface = createSurface({ win, doc, adapter, tag: "feedback-picker" });
|
|
439
|
+
const live = h(doc, "div", { class: "sr", role: "status", "aria-live": "polite" });
|
|
440
|
+
const shield = h(doc, "div", { class: "shield", hidden: true, "aria-hidden": "true" });
|
|
441
|
+
const highlight = h(doc, "div", { class: "highlight", hidden: true, "aria-hidden": "true" });
|
|
442
|
+
const highlightTag = h(doc, "span", { class: "tag" });
|
|
443
|
+
highlight.append(highlightTag);
|
|
444
|
+
const regionBox = h(doc, "div", { class: "region", hidden: true, "aria-hidden": "true" });
|
|
445
|
+
const toolbar = h(doc, "div", { class: "toolbar", role: "toolbar", "aria-label": "Feedback selection", hidden: true });
|
|
446
|
+
const instr = h(doc, "span", { class: "instr" });
|
|
447
|
+
surface.root.append(shield, highlight, regionBox, toolbar, live);
|
|
448
|
+
|
|
449
|
+
let active = false;
|
|
450
|
+
let mode: PickerSurfaceMode = "element";
|
|
451
|
+
|
|
452
|
+
const announce = (text: string) => {
|
|
453
|
+
instr.textContent = text;
|
|
454
|
+
live.textContent = text;
|
|
455
|
+
};
|
|
456
|
+
const place = (el: HTMLElement, r: { left: number; top: number; width: number; height: number }) => {
|
|
457
|
+
el.style.setProperty("left", `${r.left}px`);
|
|
458
|
+
el.style.setProperty("top", `${r.top}px`);
|
|
459
|
+
el.style.setProperty("width", `${Math.max(0, r.width)}px`);
|
|
460
|
+
el.style.setProperty("height", `${Math.max(0, r.height)}px`);
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
const engine = new Picker(win, doc, surface.host, shield, {
|
|
464
|
+
onHover(el, label) {
|
|
465
|
+
if (!el) {
|
|
466
|
+
highlight.hidden = true;
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
place(highlight, el.getBoundingClientRect());
|
|
470
|
+
highlightTag.textContent = label ?? "";
|
|
471
|
+
highlight.hidden = false;
|
|
472
|
+
},
|
|
473
|
+
onRegionDraft(rect) {
|
|
474
|
+
if (!rect) {
|
|
475
|
+
regionBox.hidden = true;
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
place(regionBox, { left: rect.x, top: rect.y, width: rect.width, height: rect.height });
|
|
479
|
+
regionBox.hidden = false;
|
|
480
|
+
},
|
|
481
|
+
onPick(result) {
|
|
482
|
+
finish(result.kind, result.element, result.rect);
|
|
483
|
+
},
|
|
484
|
+
onInstruction: announce,
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
const evidenceFor = (target: FeedbackTarget, selection?: TextSelection) => {
|
|
488
|
+
const page = adapter?.context?.();
|
|
489
|
+
return buildTargetEvidence({
|
|
490
|
+
win,
|
|
491
|
+
target,
|
|
492
|
+
...(page ? { page } : {}),
|
|
493
|
+
...(selection ? { selection } : {}),
|
|
494
|
+
});
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
function finish(kind: PickOutcome["kind"], element: Element | null, rect?: Rect, selection?: TextSelection) {
|
|
498
|
+
let target: FeedbackTarget;
|
|
499
|
+
let label: string;
|
|
500
|
+
if (kind === "page") {
|
|
501
|
+
target = pageTarget();
|
|
502
|
+
label = "Whole page";
|
|
503
|
+
} else if (kind === "region" && rect) {
|
|
504
|
+
target = regionTarget(rect, element);
|
|
505
|
+
label = describeTarget(target);
|
|
506
|
+
} else if (kind === "text") {
|
|
507
|
+
target = element ? elementTarget(element) : pageTarget();
|
|
508
|
+
label = "Selected text";
|
|
509
|
+
} else if (element) {
|
|
510
|
+
target = elementTarget(element);
|
|
511
|
+
label = safeLabel(element);
|
|
512
|
+
} else return;
|
|
513
|
+
const outcome: PickOutcome = {
|
|
514
|
+
kind,
|
|
515
|
+
element: kind === "page" || kind === "region" ? null : element,
|
|
516
|
+
label,
|
|
517
|
+
target,
|
|
518
|
+
evidence: evidenceFor(target, selection),
|
|
519
|
+
...(selection ? { selection } : {}),
|
|
520
|
+
};
|
|
521
|
+
stop();
|
|
522
|
+
options.onPick(outcome);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const button = (label: string, onClick: () => void, extra: Record<string, string> = {}) => {
|
|
526
|
+
const b = h(doc, "button", { type: "button", text: label, ...extra });
|
|
527
|
+
b.addEventListener("click", (e) => {
|
|
528
|
+
e.preventDefault();
|
|
529
|
+
onClick();
|
|
530
|
+
});
|
|
531
|
+
return b;
|
|
532
|
+
};
|
|
533
|
+
const modeButtons: Record<PickerSurfaceMode, HTMLButtonElement> = {
|
|
534
|
+
element: button("Element", () => begin("element"), { "aria-pressed": "true" }),
|
|
535
|
+
region: button("Region", () => begin("region"), { "aria-pressed": "false" }),
|
|
536
|
+
text: button("Text", () => begin("text"), { "aria-pressed": "false" }),
|
|
537
|
+
};
|
|
538
|
+
const useSelection = button("Use selection", () => {
|
|
539
|
+
const pick = readTextSelection(win, surface.host);
|
|
540
|
+
if (!pick) {
|
|
541
|
+
announce("Select some text on the page first.");
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
finish("text", pick.container, undefined, pick.selection);
|
|
545
|
+
}, { class: "primary" });
|
|
546
|
+
const keyed = (fn: () => void) => () => {
|
|
547
|
+
if (mode !== "text") fn();
|
|
548
|
+
};
|
|
549
|
+
const offered = new Set<PickerSurfaceMode>(options.modes ?? ["element", "region", "text"]);
|
|
550
|
+
toolbar.append(
|
|
551
|
+
instr,
|
|
552
|
+
...(["element", "region", "text"] as const).filter((m) => offered.has(m)).map((m) => modeButtons[m]),
|
|
553
|
+
button("Whole page", () => {
|
|
554
|
+
engine.pickedByControl();
|
|
555
|
+
finish("page", null);
|
|
556
|
+
}),
|
|
557
|
+
button("Larger area", keyed(() => engine.larger())),
|
|
558
|
+
button("Smaller area", keyed(() => engine.smaller())),
|
|
559
|
+
button("Previous", keyed(() => engine.previous())),
|
|
560
|
+
button("Next", keyed(() => engine.next())),
|
|
561
|
+
button("Parent", keyed(() => engine.parent())),
|
|
562
|
+
button("Child", keyed(() => engine.child())),
|
|
563
|
+
button("Select highlighted", keyed(() => engine.selectCurrent()), { class: "primary" }),
|
|
564
|
+
useSelection,
|
|
565
|
+
button("Cancel", () => cancel()),
|
|
566
|
+
);
|
|
567
|
+
|
|
568
|
+
/** Keep the toolbar off what the pointer is aiming at: it docks at the edge farther from the pointer. */
|
|
569
|
+
const dockAwayFrom = (y: number) => {
|
|
570
|
+
const clearance = (toolbar.offsetHeight || 64) + 12 + 16;
|
|
571
|
+
const docked = toolbar.classList.contains("bottom");
|
|
572
|
+
if (!docked && y < clearance) toolbar.classList.add("bottom");
|
|
573
|
+
else if (docked && y > win.innerHeight - clearance) toolbar.classList.remove("bottom");
|
|
574
|
+
};
|
|
575
|
+
shield.addEventListener("pointermove", (e) => dockAwayFrom((e as PointerEvent).clientY));
|
|
576
|
+
shield.addEventListener("pointerdown", (e) => dockAwayFrom((e as PointerEvent).clientY));
|
|
577
|
+
const touchFirst = () => !!win.matchMedia?.("(pointer: coarse)").matches || (win.navigator?.maxTouchPoints ?? 0) > 0;
|
|
578
|
+
|
|
579
|
+
let seedEl: Element | null = null;
|
|
580
|
+
function begin(next: PickerSurfaceMode) {
|
|
581
|
+
mode = next;
|
|
582
|
+
active = true;
|
|
583
|
+
for (const [m, b] of Object.entries(modeButtons)) b.setAttribute("aria-pressed", String(m === next));
|
|
584
|
+
toolbar.hidden = false;
|
|
585
|
+
toolbar.classList.toggle("bottom", touchFirst());
|
|
586
|
+
useSelection.hidden = next !== "text";
|
|
587
|
+
if (next === "text") {
|
|
588
|
+
engine.stop();
|
|
589
|
+
shield.hidden = true;
|
|
590
|
+
announce("Select text on the page, then press Use selection. Esc to cancel.");
|
|
591
|
+
} else {
|
|
592
|
+
engine.start(next, seedEl);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function stop() {
|
|
597
|
+
if (!active) return;
|
|
598
|
+
active = false;
|
|
599
|
+
engine.stop();
|
|
600
|
+
toolbar.hidden = true;
|
|
601
|
+
highlight.hidden = true;
|
|
602
|
+
regionBox.hidden = true;
|
|
603
|
+
win.removeEventListener("keydown", onKey, true);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function cancel() {
|
|
607
|
+
if (!active) return;
|
|
608
|
+
stop();
|
|
609
|
+
options.onCancel?.();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const onKey = (e: Event) => {
|
|
613
|
+
const ke = e as KeyboardEvent;
|
|
614
|
+
if (!active) return;
|
|
615
|
+
if (ke.key === "Escape") {
|
|
616
|
+
ke.preventDefault();
|
|
617
|
+
ke.stopImmediatePropagation();
|
|
618
|
+
cancel();
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
// Keep keyboard activation of page controls from firing while picking.
|
|
622
|
+
if (mode !== "text" && !eventFromHost(ke, surface.host) && (ke.key === "Enter" || ke.key === " ")) {
|
|
623
|
+
ke.preventDefault();
|
|
624
|
+
ke.stopImmediatePropagation();
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
return {
|
|
629
|
+
mount() {
|
|
630
|
+
if (!isSurfacePermitted(adapter, "picker")) return false;
|
|
631
|
+
surface.attach();
|
|
632
|
+
return true;
|
|
633
|
+
},
|
|
634
|
+
unmount() {
|
|
635
|
+
stop();
|
|
636
|
+
engine.detach();
|
|
637
|
+
surface.detach();
|
|
638
|
+
},
|
|
639
|
+
start(next = "element", seed = null) {
|
|
640
|
+
if (!isSurfacePermitted(adapter, "picker") || !offered.has(next)) return false;
|
|
641
|
+
surface.attach();
|
|
642
|
+
seedEl = seed;
|
|
643
|
+
win.removeEventListener("keydown", onKey, true);
|
|
644
|
+
win.addEventListener("keydown", onKey, true);
|
|
645
|
+
begin(next);
|
|
646
|
+
return true;
|
|
647
|
+
},
|
|
648
|
+
stop,
|
|
649
|
+
outcomeFor(element) {
|
|
650
|
+
if (element === doc.documentElement || element === surface.host || surface.host.contains(element)) return null;
|
|
651
|
+
const target = elementTarget(element);
|
|
652
|
+
return { kind: "element", element, label: safeLabel(element), target, evidence: evidenceFor(target) };
|
|
653
|
+
},
|
|
654
|
+
get isActive() {
|
|
655
|
+
return active;
|
|
656
|
+
},
|
|
657
|
+
get mode() {
|
|
658
|
+
return mode;
|
|
659
|
+
},
|
|
660
|
+
host: surface.host,
|
|
661
|
+
root: surface.shadow,
|
|
662
|
+
};
|
|
663
|
+
}
|