@tokenoftrust/storefront-runner 6.12.16 → 6.12.17
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 +252 -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,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local redaction of reviewer prose before it leaves the browser.
|
|
3
|
+
*
|
|
4
|
+
* This is the first of two passes: the host's intake runs its own canonical redactor
|
|
5
|
+
* independently.
|
|
6
|
+
*
|
|
7
|
+
* Supported classes: email addresses, phone numbers in common formats, credentials (bearer/basic
|
|
8
|
+
* tokens, JWTs, cookie/authorization header pastes, key=value secrets, provider key prefixes,
|
|
9
|
+
* long opaque tokens), URLs, and markup. Arbitrary names, street addresses and free-form
|
|
10
|
+
* identifiers are NOT reliably detectable by pattern; the composer shows the exact outgoing text so
|
|
11
|
+
* the reviewer can remove them. `PII_LIMITATION_NOTICE` is the user-facing statement of that.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { RedactionCategory, RedactionCounts } from "../v1/types.js";
|
|
15
|
+
|
|
16
|
+
export type { RedactionCategory, RedactionCounts };
|
|
17
|
+
|
|
18
|
+
export const PII_LIMITATION_NOTICE =
|
|
19
|
+
"Automatic filters remove email addresses, phone numbers, links, markup and secrets they " +
|
|
20
|
+
"recognise. They cannot reliably find every name, address or other personal detail, so check " +
|
|
21
|
+
"the text below before sending.";
|
|
22
|
+
|
|
23
|
+
interface Rule {
|
|
24
|
+
category: RedactionCategory;
|
|
25
|
+
pattern: RegExp;
|
|
26
|
+
replacement: string;
|
|
27
|
+
/** Leave a match as written when this returns true (a shape that is prose, not a secret). */
|
|
28
|
+
keep?(match: string): boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A `/`-separated run with an ordinary word segment is a path the reviewer is naming. */
|
|
32
|
+
const WORD_SEGMENT = /^[A-Z]?[a-z]{3,}$/;
|
|
33
|
+
function isPathLike(match: string): boolean {
|
|
34
|
+
return match.includes("/") && match.split("/").some((segment) => WORD_SEGMENT.test(segment));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Longest input any rule ever sees. Every outgoing field is far shorter (the description is capped
|
|
39
|
+
* at 4000), so this only bounds the work done on an oversized paste before it is refused.
|
|
40
|
+
*/
|
|
41
|
+
export const REDACT_INPUT_MAX = 20_000;
|
|
42
|
+
|
|
43
|
+
// Order matters: key blocks, header pastes and URLs first so their contents are not counted twice.
|
|
44
|
+
// Every rule is linear in its input: unbounded runs are only started at a run boundary (a
|
|
45
|
+
// lookbehind), never at every position inside the run.
|
|
46
|
+
const RULES: Rule[] = [
|
|
47
|
+
{
|
|
48
|
+
// PEM / PGP armoured blocks, through their END line (or the end of the text if it was cut off).
|
|
49
|
+
category: "credential",
|
|
50
|
+
pattern: /-----BEGIN [A-Z0-9 ]{1,40}-----[\s\S]*?(?:-----END [A-Z0-9 ]{1,40}-----|$)/g,
|
|
51
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
category: "credential",
|
|
55
|
+
pattern: /\b(?:set-)?cookie\s*:\s*[^\n]*/gi,
|
|
56
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
category: "credential",
|
|
60
|
+
pattern: /\b(?:proxy-)?authorization\s*:\s*[^\n]*/gi,
|
|
61
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
category: "credential",
|
|
65
|
+
pattern: /\b(?:bearer|basic|token)\s+[A-Za-z0-9._~+/=-]{12,}/gi,
|
|
66
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
category: "credential",
|
|
70
|
+
pattern: /\beyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{0,}/g,
|
|
71
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
category: "url",
|
|
75
|
+
pattern: /\b(?:[a-z][a-z0-9+.-]{1,15}:\/\/|www\.)[^\s<>"'`]+/gi,
|
|
76
|
+
replacement: "[REDACTED_URL]",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
category: "credential",
|
|
80
|
+
pattern:
|
|
81
|
+
/\b(?:api[_-]?key|access[_-]?token|refresh[_-]?token|secret|password|passwd|pwd|session(?:[_-]?id)?|sid|token|preview[_-]?token|csrf(?:[_-]?token)?|client[_-]?secret)\s*[:=]\s*["']?[^\s"'&;,]{4,}["']?/gi,
|
|
82
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
category: "credential",
|
|
86
|
+
pattern:
|
|
87
|
+
/\b(?:sk|pk|rk|ghp|gho|ghu|ghs|github_pat|xox[abposr]|AKIA|ASIA|AIza|glpat|npm)[_-]?[A-Za-z0-9_-]{12,}/g,
|
|
88
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
// Base64 secrets that contain + or / (cloud secret keys, key material): mixed case and a digit.
|
|
92
|
+
category: "credential",
|
|
93
|
+
pattern:
|
|
94
|
+
/(?<![A-Za-z0-9+/=_-])(?=[A-Za-z0-9+/]*[A-Z])(?=[A-Za-z0-9+/]*[a-z])(?=[A-Za-z0-9+/]*\d)(?=[A-Za-z0-9+/]*[+/])[A-Za-z0-9+/]{32,}={0,2}(?![A-Za-z0-9+/=_-])/g,
|
|
95
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
96
|
+
keep: isPathLike,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
category: "email",
|
|
100
|
+
pattern: /(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,
|
|
101
|
+
replacement: "[REDACTED_EMAIL]",
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
category: "markup",
|
|
105
|
+
pattern: /<\/?[A-Za-z][^<>]*>|<!--[\s\S]*?-->/g,
|
|
106
|
+
replacement: "",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
category: "phone",
|
|
110
|
+
pattern: /(?:\+\d{1,3}[\s.-]?)?(?:\(\d{2,4}\)[\s.-]?|\d{2,4}[\s.-])\d{3,4}[\s.-]?\d{3,4}\b/g,
|
|
111
|
+
replacement: "[REDACTED_PHONE]",
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
category: "phone",
|
|
115
|
+
pattern: /\+\d{8,15}\b/g,
|
|
116
|
+
replacement: "[REDACTED_PHONE]",
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
// Long opaque tokens (hex / base64url runs) that look like secrets or identifiers.
|
|
120
|
+
category: "credential",
|
|
121
|
+
pattern: /(?<![A-Za-z0-9_-])(?=[A-Za-z0-9_-]*\d)(?=[A-Za-z0-9_-]*[A-Za-z])[A-Za-z0-9_-]{32,}(?![A-Za-z0-9_-])/g,
|
|
122
|
+
replacement: "[REDACTED_CREDENTIAL]",
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
// Control characters other than tab/newline; bidi marks, overrides and isolates that can disguise
|
|
127
|
+
// text; and invisible format characters (soft hyphen, zero-width space/joiners, word joiner, BOM)
|
|
128
|
+
// that would otherwise split an address or key so no rule recognises it.
|
|
129
|
+
const CONTROL =
|
|
130
|
+
/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F\u00AD\u061C\u180E\u200B\u200E\u200F\u2028-\u202E\u2060-\u2064\u2066-\u206F\uFEFF]/g;
|
|
131
|
+
|
|
132
|
+
// Zero-width non-joiner / joiner are real text in Persian, Indic scripts and emoji sequences; strip
|
|
133
|
+
// them only between printable ASCII characters, where they can only split an address or key.
|
|
134
|
+
const ASCII_JOINER = /(?<=[\x21-\x7E])[\u200C\u200D]+(?=[\x21-\x7E])/g;
|
|
135
|
+
|
|
136
|
+
export interface RedactionResult {
|
|
137
|
+
text: string;
|
|
138
|
+
counts: RedactionCounts;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Redact one string. Pure and deterministic. */
|
|
142
|
+
export function redactText(input: string): RedactionResult {
|
|
143
|
+
const counts: RedactionCounts = {};
|
|
144
|
+
let text = String(input ?? "").slice(0, REDACT_INPUT_MAX).normalize("NFC");
|
|
145
|
+
const controlMatches = text.match(CONTROL);
|
|
146
|
+
if (controlMatches) {
|
|
147
|
+
counts.other = (counts.other ?? 0) + controlMatches.length;
|
|
148
|
+
text = text.replace(CONTROL, "");
|
|
149
|
+
}
|
|
150
|
+
const joiners = text.match(ASCII_JOINER);
|
|
151
|
+
if (joiners) {
|
|
152
|
+
counts.other = (counts.other ?? 0) + joiners.reduce((n, m) => n + m.length, 0);
|
|
153
|
+
text = text.replace(ASCII_JOINER, "");
|
|
154
|
+
}
|
|
155
|
+
for (const rule of RULES) {
|
|
156
|
+
text = text.replace(rule.pattern, (match) => {
|
|
157
|
+
if (rule.keep?.(match)) return match;
|
|
158
|
+
counts[rule.category] = (counts[rule.category] ?? 0) + 1;
|
|
159
|
+
return rule.replacement;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
// Any stray angle brackets left after markup removal cannot form a tag, but keep the outgoing
|
|
163
|
+
// text plain: render-side escaping still applies everywhere it is displayed.
|
|
164
|
+
text = text.replace(/[ \t]+\n/g, "\n").replace(/\n{3,}/g, "\n\n").trim();
|
|
165
|
+
return { text, counts };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Merge several count maps. */
|
|
169
|
+
export function mergeCounts(...maps: RedactionCounts[]): RedactionCounts {
|
|
170
|
+
const out: RedactionCounts = {};
|
|
171
|
+
for (const map of maps) {
|
|
172
|
+
for (const [key, value] of Object.entries(map) as [RedactionCategory, number][]) {
|
|
173
|
+
if (value > 0) out[key] = Math.min(10000, (out[key] ?? 0) + value);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function totalRedactions(counts: RedactionCounts): number {
|
|
180
|
+
return Object.values(counts).reduce((sum, n) => sum + (n ?? 0), 0);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const LABELS: Record<RedactionCategory, [string, string]> = {
|
|
184
|
+
email: ["email address", "email addresses"],
|
|
185
|
+
phone: ["phone number", "phone numbers"],
|
|
186
|
+
credential: ["secret", "secrets"],
|
|
187
|
+
url: ["link", "links"],
|
|
188
|
+
markup: ["markup tag", "markup tags"],
|
|
189
|
+
other: ["hidden character", "hidden characters"],
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** "Removed 2 email addresses and 1 link." — or null when nothing was removed. */
|
|
193
|
+
export function describeRedactions(counts: RedactionCounts): string | null {
|
|
194
|
+
const parts = (Object.keys(LABELS) as RedactionCategory[])
|
|
195
|
+
.filter((key) => (counts[key] ?? 0) > 0)
|
|
196
|
+
.map((key) => {
|
|
197
|
+
const n = counts[key] as number;
|
|
198
|
+
return `${n} ${LABELS[key][n === 1 ? 0 : 1]}`;
|
|
199
|
+
});
|
|
200
|
+
if (parts.length === 0) return null;
|
|
201
|
+
const list =
|
|
202
|
+
parts.length === 1 ? parts[0] : `${parts.slice(0, -1).join(", ")} and ${parts[parts.length - 1]}`;
|
|
203
|
+
return `Removed ${list}.`;
|
|
204
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text-selection picking. The reviewer's own selected words are the one page text the primitives
|
|
3
|
+
* read, and only an excerpt that has passed local redaction ever leaves this module.
|
|
4
|
+
*/
|
|
5
|
+
import type { TextSelection } from "../v1/types.js";
|
|
6
|
+
import { mergeCounts, redactText } from "./redaction.js";
|
|
7
|
+
import { safeRect } from "./target.js";
|
|
8
|
+
|
|
9
|
+
export const EXCERPT_MAX = 500;
|
|
10
|
+
const CHAR_COUNT_MAX = 100_000;
|
|
11
|
+
|
|
12
|
+
export interface TextPick {
|
|
13
|
+
selection: TextSelection;
|
|
14
|
+
/** Closest element containing the whole selection; for structural evidence. */
|
|
15
|
+
container: Element | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Read the current window selection. Returns null when nothing is selected, or when the selection
|
|
20
|
+
* lies inside the feedback UI itself (`ownHost`).
|
|
21
|
+
*/
|
|
22
|
+
export function readTextSelection(win: Window, ownHost?: Element | null): TextPick | null {
|
|
23
|
+
const sel = typeof win.getSelection === "function" ? win.getSelection() : null;
|
|
24
|
+
if (!sel || sel.rangeCount === 0 || sel.isCollapsed) return null;
|
|
25
|
+
const range = sel.getRangeAt(0);
|
|
26
|
+
const common = range.commonAncestorContainer;
|
|
27
|
+
const container = common.nodeType === 1 ? (common as Element) : common.parentElement;
|
|
28
|
+
if (ownHost && (ownHost === container || (container && ownHost.contains(container)))) return null;
|
|
29
|
+
const raw = sel.toString();
|
|
30
|
+
if (raw.trim().length === 0) return null;
|
|
31
|
+
|
|
32
|
+
const selection: TextSelection = { kind: "text", charCount: Math.min(CHAR_COUNT_MAX, raw.length) };
|
|
33
|
+
const r = range.getBoundingClientRect();
|
|
34
|
+
if (r && (r.width > 0 || r.height > 0)) selection.bounds = safeRect(r);
|
|
35
|
+
|
|
36
|
+
// Redact before truncating so a secret straddling the cut is still caught; bound the work first.
|
|
37
|
+
const redacted = redactText(raw.slice(0, 4000));
|
|
38
|
+
const flat = redacted.text.replace(/\s+/g, " ").trim();
|
|
39
|
+
if (flat.length > 0) {
|
|
40
|
+
const truncated = flat.length > EXCERPT_MAX;
|
|
41
|
+
const counts = mergeCounts(redacted.counts);
|
|
42
|
+
selection.excerpt = {
|
|
43
|
+
text: truncated ? `${flat.slice(0, EXCERPT_MAX - 1)}…` : flat,
|
|
44
|
+
truncated: truncated || raw.length > 4000,
|
|
45
|
+
...(Object.keys(counts).length > 0 ? { redactionCounts: counts } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return { selection, container };
|
|
49
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Double-Shift accelerator: two complete, unmodified Shift down/up taps within 450 ms.
|
|
3
|
+
*
|
|
4
|
+
* Ignored: key repeats, IME composition, events from editable controls, Shift held with another
|
|
5
|
+
* modifier, any intervening key, a pointer action, focus loss, and taps during the post-activation
|
|
6
|
+
* cooldown. It never calls preventDefault, so ordinary shortcuts are untouched.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const DOUBLE_SHIFT_WINDOW_MS = 450;
|
|
10
|
+
export const DOUBLE_SHIFT_COOLDOWN_MS = 800;
|
|
11
|
+
/** A Shift held longer than this is not a tap. */
|
|
12
|
+
const MAX_TAP_MS = 300;
|
|
13
|
+
|
|
14
|
+
export interface KeyLike {
|
|
15
|
+
type: "keydown" | "keyup";
|
|
16
|
+
key: string;
|
|
17
|
+
repeat?: boolean;
|
|
18
|
+
isComposing?: boolean;
|
|
19
|
+
altKey?: boolean;
|
|
20
|
+
ctrlKey?: boolean;
|
|
21
|
+
metaKey?: boolean;
|
|
22
|
+
/** True when the event target is an editable control (input, textarea, select, contenteditable). */
|
|
23
|
+
editable?: boolean;
|
|
24
|
+
timeStamp: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class DoubleShiftDetector {
|
|
28
|
+
private downAt: number | null = null;
|
|
29
|
+
private taps: number[] = [];
|
|
30
|
+
private cooldownUntil = 0;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
private readonly windowMs = DOUBLE_SHIFT_WINDOW_MS,
|
|
34
|
+
private readonly cooldownMs = DOUBLE_SHIFT_COOLDOWN_MS,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
/** Clear any partial sequence (pointer action, blur, visibility change, intervening key). */
|
|
38
|
+
reset(): void {
|
|
39
|
+
this.downAt = null;
|
|
40
|
+
this.taps = [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Feed a key event. Returns true exactly when the accelerator fires. */
|
|
44
|
+
feed(e: KeyLike): boolean {
|
|
45
|
+
const isShift = e.key === "Shift";
|
|
46
|
+
if (!isShift || e.repeat || e.isComposing || e.editable || e.altKey || e.ctrlKey || e.metaKey) {
|
|
47
|
+
// Holding Shift through a repeat is not a tap; any other key breaks the sequence.
|
|
48
|
+
this.reset();
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (e.timeStamp < this.cooldownUntil) {
|
|
52
|
+
this.reset();
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
if (e.type === "keydown") {
|
|
56
|
+
if (this.downAt !== null) {
|
|
57
|
+
this.reset();
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
this.downAt = e.timeStamp;
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
// keyup
|
|
64
|
+
if (this.downAt === null || e.timeStamp - this.downAt > MAX_TAP_MS) {
|
|
65
|
+
this.reset();
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
this.downAt = null;
|
|
69
|
+
this.taps = this.taps.filter((t) => e.timeStamp - t <= this.windowMs);
|
|
70
|
+
this.taps.push(e.timeStamp);
|
|
71
|
+
if (this.taps.length >= 2) {
|
|
72
|
+
this.reset();
|
|
73
|
+
this.cooldownUntil = e.timeStamp + this.cooldownMs;
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface DoubleShiftOptions {
|
|
81
|
+
/** Called exactly when the guarded double-Shift fires. */
|
|
82
|
+
onActivate(): void;
|
|
83
|
+
/** True while activation must be ignored (for example, a dialog is already open). */
|
|
84
|
+
isSuppressed?(): boolean;
|
|
85
|
+
/** True when the event came from the feedback UI itself; such events are treated as editable. */
|
|
86
|
+
isOwnEvent?(event: Event): boolean;
|
|
87
|
+
windowMs?: number;
|
|
88
|
+
cooldownMs?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DoubleShiftBinding {
|
|
92
|
+
detach(): void;
|
|
93
|
+
/** Clear any partial Shift sequence. */
|
|
94
|
+
reset(): void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Listen on `win` (capture phase, passive: never calls preventDefault) for the guarded double-Shift.
|
|
99
|
+
* Pointer actions and window blur reset a partial sequence. Safe to detach more than once.
|
|
100
|
+
*/
|
|
101
|
+
export function attachDoubleShift(win: Window, options: DoubleShiftOptions): DoubleShiftBinding {
|
|
102
|
+
const detector = new DoubleShiftDetector(options.windowMs, options.cooldownMs);
|
|
103
|
+
let attached = true;
|
|
104
|
+
const onKey = (e: Event) => {
|
|
105
|
+
const ke = e as KeyboardEvent;
|
|
106
|
+
const suppressed = options.isSuppressed?.() ?? false;
|
|
107
|
+
const fired = detector.feed({
|
|
108
|
+
type: ke.type as "keydown" | "keyup",
|
|
109
|
+
key: ke.key,
|
|
110
|
+
repeat: ke.repeat,
|
|
111
|
+
isComposing: ke.isComposing,
|
|
112
|
+
altKey: ke.altKey,
|
|
113
|
+
ctrlKey: ke.ctrlKey,
|
|
114
|
+
metaKey: ke.metaKey,
|
|
115
|
+
editable: (options.isOwnEvent?.(ke) ?? false) || isEditableTarget(ke),
|
|
116
|
+
timeStamp: ke.timeStamp,
|
|
117
|
+
});
|
|
118
|
+
if (suppressed) {
|
|
119
|
+
detector.reset();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (fired) options.onActivate();
|
|
123
|
+
};
|
|
124
|
+
const reset = () => detector.reset();
|
|
125
|
+
win.addEventListener("keydown", onKey, { capture: true, passive: true });
|
|
126
|
+
win.addEventListener("keyup", onKey, { capture: true, passive: true });
|
|
127
|
+
win.addEventListener("pointerdown", reset, { capture: true, passive: true });
|
|
128
|
+
win.addEventListener("blur", reset);
|
|
129
|
+
return {
|
|
130
|
+
reset,
|
|
131
|
+
detach() {
|
|
132
|
+
if (!attached) return;
|
|
133
|
+
attached = false;
|
|
134
|
+
win.removeEventListener("keydown", onKey, { capture: true });
|
|
135
|
+
win.removeEventListener("keyup", onKey, { capture: true });
|
|
136
|
+
win.removeEventListener("pointerdown", reset, { capture: true });
|
|
137
|
+
win.removeEventListener("blur", reset);
|
|
138
|
+
detector.reset();
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** True when an event target is an editable control. Composed path aware (shadow DOM inputs). */
|
|
144
|
+
export function isEditableTarget(event: Event): boolean {
|
|
145
|
+
const path = typeof event.composedPath === "function" ? event.composedPath() : [event.target];
|
|
146
|
+
for (const node of path) {
|
|
147
|
+
if (!node || typeof (node as Element).tagName !== "string") continue;
|
|
148
|
+
const el = node as HTMLElement;
|
|
149
|
+
const tag = el.tagName.toLowerCase();
|
|
150
|
+
if (tag === "textarea" || tag === "select") return true;
|
|
151
|
+
if (tag === "input") {
|
|
152
|
+
const type = ((el as HTMLInputElement).type || "text").toLowerCase();
|
|
153
|
+
if (!["button", "submit", "reset", "checkbox", "radio", "range", "color", "file", "image"].includes(type)) return true;
|
|
154
|
+
}
|
|
155
|
+
if (el.isContentEditable || el.getAttribute?.("contenteditable") === "true") return true;
|
|
156
|
+
const role = el.getAttribute?.("role");
|
|
157
|
+
if (role === "textbox" || role === "combobox" || role === "searchbox") return true;
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isolated styles for the primitives' shadow roots. Colors, font and launcher stacking come from
|
|
3
|
+
* `--fb-*` custom properties, which a host overrides through `HostAdapter.theme.tokens`.
|
|
4
|
+
*/
|
|
5
|
+
export const PRIMITIVE_CSS = `
|
|
6
|
+
:host { all: initial; }
|
|
7
|
+
* { box-sizing: border-box; }
|
|
8
|
+
[hidden] { display: none !important; }
|
|
9
|
+
.root { --fb-acc: #4f46e5; --fb-acc-ink: #fff; --fb-ink: #111827; --fb-muted: #4b5563; --fb-bg: #fff; --fb-line: #d1d5db;
|
|
10
|
+
--fb-err: #b91c1c; --fb-ok: #047857; --fb-font: system-ui, -apple-system, "Segoe UI", sans-serif; --fb-z: 2147483646;
|
|
11
|
+
font: 14px/1.45 var(--fb-font); color: var(--fb-ink); }
|
|
12
|
+
button, input, select, textarea { font: inherit; color: inherit; }
|
|
13
|
+
button { cursor: pointer; border: 1px solid var(--fb-line); background: var(--fb-bg); border-radius: 6px; padding: 6px 10px; min-height: 32px; }
|
|
14
|
+
button.primary { background: var(--fb-acc); border-color: var(--fb-acc); color: var(--fb-acc-ink); }
|
|
15
|
+
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible {
|
|
16
|
+
outline: 3px solid var(--fb-acc); outline-offset: 2px; }
|
|
17
|
+
button[disabled] { opacity: .55; cursor: not-allowed; }
|
|
18
|
+
.fab { position: fixed; right: 16px; bottom: 16px; z-index: var(--fb-z); pointer-events: auto;
|
|
19
|
+
background: var(--fb-acc); color: var(--fb-acc-ink); border: 0; border-radius: 999px; padding: 10px 16px; min-height: 44px;
|
|
20
|
+
box-shadow: 0 6px 20px rgba(17,24,39,.25); font-weight: 600; }
|
|
21
|
+
.fab .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #fbbf24; margin-left: 6px; vertical-align: middle; }
|
|
22
|
+
.dialog { position: fixed; right: 16px; top: 16px; z-index: calc(var(--fb-z) + 1); pointer-events: auto; width: 360px;
|
|
23
|
+
max-width: calc(100vw - 16px); max-height: calc(100vh - 16px); overflow: auto; background: var(--fb-bg);
|
|
24
|
+
border: 1px solid var(--fb-line); border-radius: 12px; box-shadow: 0 16px 40px rgba(17,24,39,.28); }
|
|
25
|
+
@media (max-width: 600px) {
|
|
26
|
+
.dialog { left: 0; right: 0; top: auto; bottom: 0; width: 100%; max-width: 100%; max-height: 85vh; border-radius: 14px 14px 0 0; }
|
|
27
|
+
}
|
|
28
|
+
.titlebar { display: flex; align-items: center; gap: 4px; padding: 8px 10px; border-bottom: 1px solid var(--fb-line); }
|
|
29
|
+
.titlebar h2 { font-size: 15px; margin: 0; flex: 1; }
|
|
30
|
+
.titlebar button { padding: 2px 6px; min-height: 28px; font-size: 12px; }
|
|
31
|
+
.body { padding: 10px 12px 12px; display: grid; gap: 10px; }
|
|
32
|
+
.shield { position: fixed; inset: 0; z-index: calc(var(--fb-z) - 2); pointer-events: auto; cursor: crosshair; background: transparent;
|
|
33
|
+
touch-action: none; }
|
|
34
|
+
.highlight { position: fixed; z-index: calc(var(--fb-z) - 1); pointer-events: none; border: 2px solid var(--fb-acc); border-radius: 3px;
|
|
35
|
+
box-shadow: 0 0 0 4px rgba(79,70,229,.25), 0 0 18px 4px rgba(79,70,229,.35); }
|
|
36
|
+
.highlight .tag { position: absolute; left: -2px; top: -24px; background: var(--fb-acc); color: var(--fb-acc-ink); font-size: 12px;
|
|
37
|
+
padding: 2px 6px; border-radius: 4px; white-space: nowrap; }
|
|
38
|
+
.region { position: fixed; z-index: calc(var(--fb-z) - 1); pointer-events: none; border: 2px dashed var(--fb-acc); background: rgba(79,70,229,.10); }
|
|
39
|
+
.toolbar { position: fixed; left: 50%; top: 12px; transform: translateX(-50%); z-index: calc(var(--fb-z) + 2); pointer-events: auto;
|
|
40
|
+
background: var(--fb-bg); border: 1px solid var(--fb-line); border-radius: 10px; padding: 8px; box-shadow: 0 8px 28px rgba(17,24,39,.22);
|
|
41
|
+
display: flex; flex-wrap: wrap; gap: 6px; align-items: center; max-width: calc(100vw - 24px); }
|
|
42
|
+
.toolbar.bottom { top: auto; bottom: 12px; }
|
|
43
|
+
.toolbar .instr { flex-basis: 100%; color: var(--fb-muted); font-size: 13px; }
|
|
44
|
+
.toolbar [aria-pressed="true"] { background: var(--fb-acc); color: var(--fb-acc-ink); border-color: var(--fb-acc); }
|
|
45
|
+
.composer label, .composer legend { font-weight: 600; font-size: 13px; }
|
|
46
|
+
.composer input[type=text], .composer textarea, .composer select { width: 100%; border: 1px solid var(--fb-line); border-radius: 6px; padding: 6px 8px; background: #fff; }
|
|
47
|
+
.composer textarea { min-height: 64px; resize: vertical; }
|
|
48
|
+
.composer fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 4px; }
|
|
49
|
+
.intents { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
|
|
50
|
+
.intents label { font-weight: 400; display: flex; gap: 6px; align-items: center; border: 1px solid var(--fb-line); border-radius: 6px; padding: 4px 6px; }
|
|
51
|
+
.muted { color: var(--fb-muted); font-size: 12px; }
|
|
52
|
+
.error { color: var(--fb-err); font-size: 13px; }
|
|
53
|
+
.ok { color: var(--fb-ok); }
|
|
54
|
+
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
|
|
55
|
+
.target { display: flex; gap: 8px; align-items: center; justify-content: space-between; background: #f3f4f6; border-radius: 6px; padding: 6px 8px; }
|
|
56
|
+
.review dl { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 2px 8px; font-size: 13px; }
|
|
57
|
+
.review dt { color: var(--fb-muted); }
|
|
58
|
+
.review dd { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
59
|
+
.composer pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; background: #f9fafb; padding: 6px; border-radius: 6px; max-height: 200px; overflow: auto; }
|
|
60
|
+
.notice { background: #fffbeb; border: 1px solid #fcd34d; border-radius: 6px; padding: 6px 8px; font-size: 13px; }
|
|
61
|
+
.copybox { width: 100%; min-height: 80px; font-size: 12px; }
|
|
62
|
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
63
|
+
@media (prefers-reduced-motion: reduce) { .fab, .dialog, .highlight { transition: none !important; animation: none !important; } }
|
|
64
|
+
`;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One isolated surface: a host element carrying a shadow root, themed and styled, attached to the
|
|
3
|
+
* page through the host adapter. Every primitive owns exactly one. All dynamic text is written with
|
|
4
|
+
* `textContent`; the shadow root gives style isolation only, never confidentiality.
|
|
5
|
+
*/
|
|
6
|
+
import { applyTheme, type HostAdapter } from "./hostAdapter.js";
|
|
7
|
+
import { PRIMITIVE_CSS } from "./styles.js";
|
|
8
|
+
|
|
9
|
+
type Child = Node | string | null | undefined | false;
|
|
10
|
+
|
|
11
|
+
/** Create an element with attributes (`text` sets textContent) and children. */
|
|
12
|
+
export function h<K extends keyof HTMLElementTagNameMap>(
|
|
13
|
+
doc: Document,
|
|
14
|
+
tag: K,
|
|
15
|
+
attrs: Record<string, string | boolean | undefined> = {},
|
|
16
|
+
...children: Child[]
|
|
17
|
+
): HTMLElementTagNameMap[K] {
|
|
18
|
+
const el = doc.createElement(tag);
|
|
19
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
20
|
+
if (v === undefined || v === false) continue;
|
|
21
|
+
if (k === "text") el.textContent = String(v);
|
|
22
|
+
else el.setAttribute(k, v === true ? "" : v);
|
|
23
|
+
}
|
|
24
|
+
for (const c of children) {
|
|
25
|
+
if (c === null || c === undefined || c === false) continue;
|
|
26
|
+
el.append(typeof c === "string" ? doc.createTextNode(c) : c);
|
|
27
|
+
}
|
|
28
|
+
return el;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface Surface {
|
|
32
|
+
/** The element placed in the page. */
|
|
33
|
+
readonly host: HTMLElement;
|
|
34
|
+
readonly shadow: ShadowRoot;
|
|
35
|
+
/** Container inside the shadow root; primitives append their UI here. */
|
|
36
|
+
readonly root: HTMLElement;
|
|
37
|
+
readonly isAttached: boolean;
|
|
38
|
+
/** Idempotent. */
|
|
39
|
+
attach(): void;
|
|
40
|
+
/** Idempotent. Runs the adapter's mount cleanup, then removes the host element. */
|
|
41
|
+
detach(): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SurfaceOptions {
|
|
45
|
+
win: Window;
|
|
46
|
+
doc: Document;
|
|
47
|
+
adapter: HostAdapter | undefined;
|
|
48
|
+
/** Custom element name for the host; must contain a hyphen. */
|
|
49
|
+
tag: string;
|
|
50
|
+
shadowMode?: "open" | "closed";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createSurface(options: SurfaceOptions): Surface {
|
|
54
|
+
const { win, doc, adapter, tag } = options;
|
|
55
|
+
const host = doc.createElement(tag);
|
|
56
|
+
host.setAttribute("data-feedback-surface", "");
|
|
57
|
+
for (const [k, v] of [
|
|
58
|
+
["all", "initial"],
|
|
59
|
+
["position", "fixed"],
|
|
60
|
+
["top", "0"],
|
|
61
|
+
["left", "0"],
|
|
62
|
+
["width", "0"],
|
|
63
|
+
["height", "0"],
|
|
64
|
+
] as const) {
|
|
65
|
+
host.style.setProperty(k, v);
|
|
66
|
+
}
|
|
67
|
+
const shadow = host.attachShadow({ mode: options.shadowMode ?? "closed" });
|
|
68
|
+
let adopted = false;
|
|
69
|
+
const SheetCtor = (win as Window & { CSSStyleSheet?: typeof CSSStyleSheet }).CSSStyleSheet;
|
|
70
|
+
try {
|
|
71
|
+
if (SheetCtor && "adoptedStyleSheets" in shadow) {
|
|
72
|
+
const sheet = new SheetCtor();
|
|
73
|
+
sheet.replaceSync(PRIMITIVE_CSS);
|
|
74
|
+
(shadow as ShadowRoot & { adoptedStyleSheets: CSSStyleSheet[] }).adoptedStyleSheets = [sheet];
|
|
75
|
+
adopted = true;
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
adopted = false;
|
|
79
|
+
}
|
|
80
|
+
if (!adopted) {
|
|
81
|
+
const style = doc.createElement("style");
|
|
82
|
+
const nonce = adapter?.theme?.nonce;
|
|
83
|
+
if (nonce) style.setAttribute("nonce", nonce);
|
|
84
|
+
style.textContent = PRIMITIVE_CSS;
|
|
85
|
+
shadow.append(style);
|
|
86
|
+
}
|
|
87
|
+
const root = h(doc, "div", { class: "root" });
|
|
88
|
+
applyTheme(root, adapter?.theme);
|
|
89
|
+
shadow.append(root);
|
|
90
|
+
|
|
91
|
+
let attached = false;
|
|
92
|
+
let cleanup: void | (() => void);
|
|
93
|
+
return {
|
|
94
|
+
host,
|
|
95
|
+
shadow,
|
|
96
|
+
root,
|
|
97
|
+
get isAttached() {
|
|
98
|
+
return attached;
|
|
99
|
+
},
|
|
100
|
+
attach() {
|
|
101
|
+
if (attached) return;
|
|
102
|
+
attached = true;
|
|
103
|
+
if (adapter?.mount) cleanup = adapter.mount(host);
|
|
104
|
+
else (doc.documentElement || doc.body).append(host);
|
|
105
|
+
},
|
|
106
|
+
detach() {
|
|
107
|
+
if (!attached) return;
|
|
108
|
+
attached = false;
|
|
109
|
+
const run = cleanup;
|
|
110
|
+
cleanup = undefined;
|
|
111
|
+
try {
|
|
112
|
+
if (typeof run === "function") run();
|
|
113
|
+
} finally {
|
|
114
|
+
host.remove();
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** True when `event` was dispatched from inside (or on) the given host element. */
|
|
121
|
+
export function eventFromHost(event: Event, host: Element): boolean {
|
|
122
|
+
const path = typeof event.composedPath === "function" ? event.composedPath() : [];
|
|
123
|
+
return path.includes(host);
|
|
124
|
+
}
|