context-drop 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/MIT-LICENSE +21 -0
  2. package/README.md +709 -2
  3. package/app/assets/javascripts/context_drop/context_drop.js +8936 -0
  4. package/dist/app/javascript/context_drop/api-client.d.ts +41 -0
  5. package/dist/app/javascript/context_drop/api-client.js +279 -0
  6. package/dist/app/javascript/context_drop/element-picker.d.ts +111 -0
  7. package/dist/app/javascript/context_drop/element-picker.js +1428 -0
  8. package/dist/app/javascript/context_drop/icons.d.ts +32 -0
  9. package/dist/app/javascript/context_drop/icons.js +32 -0
  10. package/dist/app/javascript/context_drop/index.d.ts +7 -0
  11. package/dist/app/javascript/context_drop/index.js +9 -0
  12. package/dist/app/javascript/context_drop/markdown.d.ts +7 -0
  13. package/dist/app/javascript/context_drop/markdown.js +129 -0
  14. package/dist/app/javascript/context_drop/sanitizer.d.ts +38 -0
  15. package/dist/app/javascript/context_drop/sanitizer.js +769 -0
  16. package/dist/app/javascript/context_drop/screenshot.d.ts +21 -0
  17. package/dist/app/javascript/context_drop/screenshot.js +245 -0
  18. package/dist/app/javascript/context_drop/styles.d.ts +1 -0
  19. package/dist/app/javascript/context_drop/styles.js +2015 -0
  20. package/dist/app/javascript/context_drop/types.d.ts +239 -0
  21. package/dist/app/javascript/context_drop/types.js +1 -0
  22. package/dist/app/javascript/context_drop/widget.d.ts +188 -0
  23. package/dist/app/javascript/context_drop/widget.js +3227 -0
  24. package/dist/src/cli.d.ts +3 -0
  25. package/dist/src/cli.js +207 -0
  26. package/dist/src/index.d.ts +5 -0
  27. package/dist/src/index.js +5 -0
  28. package/dist/src/mcp/dispatcher.d.ts +42 -0
  29. package/dist/src/mcp/dispatcher.js +869 -0
  30. package/dist/src/mcp/environment-router.d.ts +38 -0
  31. package/dist/src/mcp/environment-router.js +174 -0
  32. package/dist/src/mcp/index.d.ts +24 -0
  33. package/dist/src/mcp/index.js +75 -0
  34. package/dist/src/mcp/remote-client.d.ts +25 -0
  35. package/dist/src/mcp/remote-client.js +98 -0
  36. package/dist/src/next/index.d.ts +13 -0
  37. package/dist/src/next/index.js +25 -0
  38. package/dist/src/next/server.d.ts +1 -0
  39. package/dist/src/next/server.js +1 -0
  40. package/dist/src/react/index.d.ts +90 -0
  41. package/dist/src/react/index.js +124 -0
  42. package/dist/src/server/handler.d.ts +42 -0
  43. package/dist/src/server/handler.js +677 -0
  44. package/dist/src/server/index.d.ts +8 -0
  45. package/dist/src/server/index.js +8 -0
  46. package/dist/src/server/sanitizer.d.ts +15 -0
  47. package/dist/src/server/sanitizer.js +201 -0
  48. package/dist/src/server/storage/local.d.ts +22 -0
  49. package/dist/src/server/storage/local.js +574 -0
  50. package/dist/src/server/storage/memory.d.ts +5 -0
  51. package/dist/src/server/storage/memory.js +221 -0
  52. package/dist/src/server/storage/postgres.d.ts +27 -0
  53. package/dist/src/server/storage/postgres.js +446 -0
  54. package/dist/src/server/types.d.ts +190 -0
  55. package/dist/src/server/types.js +7 -0
  56. package/dist/src/server/upload-validator.d.ts +16 -0
  57. package/dist/src/server/upload-validator.js +196 -0
  58. package/dist/src/widget/index.d.ts +1 -0
  59. package/dist/src/widget/index.js +1 -0
  60. package/package.json +99 -4
@@ -0,0 +1,1428 @@
1
+ import { captureElementSnapshot, computeSelector, describeElementCandidate, isSelectableElement, resolveAncestorChain, resolveCandidateStack, resolveSmartComponent } from "./sanitizer.js";
2
+ import { ICON_CHECK, ICON_LAYERS, ICON_PIN, ICON_SCROLL_VERTICAL } from "./icons.js";
3
+ const WHEEL_STEP_THRESHOLD = 26;
4
+ const WHEEL_STEP_COOLDOWN_MS = 80;
5
+ const SCRUB_JITTER_LOCK_PX = 28;
6
+ const MAX_VISIBLE_STACK_ROWS = 5;
7
+ function isDomElementNode(node) {
8
+ if (!node || typeof node !== "object")
9
+ return false;
10
+ if (typeof window !== "undefined" && node === window)
11
+ return false;
12
+ if (node.nodeType !== 1)
13
+ return false;
14
+ const tag = (node.tagName || "").toUpperCase();
15
+ return tag.length > 0 && tag !== "WINDOW";
16
+ }
17
+ function formatCandidateLabel(tagName, selector) {
18
+ if (selector.startsWith(tagName)) {
19
+ return selector;
20
+ }
21
+ if (selector.startsWith("#") || selector.startsWith(".")) {
22
+ return `${tagName}${selector}`;
23
+ }
24
+ return selector;
25
+ }
26
+ function isPointInsideRect(x, y, rect, tolerance = 2) {
27
+ const left = (rect.left ?? rect.x ?? 0) - tolerance;
28
+ const top = (rect.top ?? rect.y ?? 0) - tolerance;
29
+ return x >= left && x <= left + rect.width + tolerance * 2 && y >= top && y <= top + rect.height + tolerance * 2;
30
+ }
31
+ export class ElementPicker {
32
+ active = false;
33
+ suppressCursorPin = false;
34
+ isScrubbingLayers = false;
35
+ scrubOriginX = 0;
36
+ scrubOriginY = 0;
37
+ lastPointerX = 0;
38
+ lastPointerY = 0;
39
+ wheelAccumulator = 0;
40
+ lastWheelStepAt = 0;
41
+ callbacks;
42
+ overlayEl = null;
43
+ leafOutlineEl = null;
44
+ hudEl = null;
45
+ pinMarkerEl = null;
46
+ pinnedMarkersContainerEl = null;
47
+ toastEl = null;
48
+ previewOverlayEl = null;
49
+ pulseTimer = null;
50
+ nudgeTimer = null;
51
+ toastTimer = null;
52
+ candidateStack = [];
53
+ activeCandidateIndex = 0;
54
+ leafElement = null;
55
+ lastAction = null;
56
+ previousCursor = "";
57
+ boundMouseMove;
58
+ boundClickCapture;
59
+ boundPointerCapture;
60
+ boundBlurSuppression;
61
+ boundWheelCapture;
62
+ boundKeyDown;
63
+ boundScrollOrResize;
64
+ constructor(callbacks) {
65
+ this.callbacks = callbacks;
66
+ this.boundMouseMove = this.handleMouseMove.bind(this);
67
+ this.boundClickCapture = this.handleClickCapture.bind(this);
68
+ this.boundPointerCapture = this.handlePointerSuppression.bind(this);
69
+ this.boundBlurSuppression = this.handleBlurSuppression.bind(this);
70
+ this.boundWheelCapture = this.handleWheelCapture.bind(this);
71
+ this.boundKeyDown = this.handleKeyDown.bind(this);
72
+ this.boundScrollOrResize = this.handleViewportScrollOrResize.bind(this);
73
+ }
74
+ isActive() {
75
+ return this.active;
76
+ }
77
+ isAnchored() {
78
+ return false;
79
+ }
80
+ isScrubbing() {
81
+ return this.isScrubbingLayers;
82
+ }
83
+ isSmartMode() {
84
+ return (this.callbacks.getSelectionMode ? this.callbacks.getSelectionMode() : "verbose") === "smart";
85
+ }
86
+ getActiveCandidate() {
87
+ return this.candidateStack[this.activeCandidateIndex] || null;
88
+ }
89
+ getCandidateStack() {
90
+ return [...this.candidateStack];
91
+ }
92
+ start(options) {
93
+ if (this.active || typeof document === "undefined" || typeof window === "undefined") {
94
+ return;
95
+ }
96
+ this.active = true;
97
+ this.suppressCursorPin = Boolean(options?.suppressCursorPin);
98
+ this.isScrubbingLayers = false;
99
+ this.wheelAccumulator = 0;
100
+ this.lastWheelStepAt = 0;
101
+ this.candidateStack = [];
102
+ this.activeCandidateIndex = 0;
103
+ this.leafElement = null;
104
+ this.lastAction = null;
105
+ this.hideExternalPreview();
106
+ this.previousCursor = document.body ? document.body.style.cursor : "";
107
+ if (document.body) {
108
+ document.body.style.cursor = this.suppressCursorPin ? "grabbing" : "crosshair";
109
+ }
110
+ this.mountSelectionUi();
111
+ this.refreshPinnedMarkers();
112
+ this.notifyStateChange();
113
+ window.addEventListener("mousemove", this.boundMouseMove, { capture: true, passive: true });
114
+ window.addEventListener("pointerdown", this.boundPointerCapture, { capture: true });
115
+ window.addEventListener("mousedown", this.boundPointerCapture, { capture: true });
116
+ window.addEventListener("mouseup", this.boundPointerCapture, { capture: true });
117
+ window.addEventListener("click", this.boundClickCapture, { capture: true });
118
+ window.addEventListener("blur", this.boundBlurSuppression, { capture: true });
119
+ window.addEventListener("focusout", this.boundBlurSuppression, { capture: true });
120
+ window.addEventListener("wheel", this.boundWheelCapture, { capture: true, passive: false });
121
+ window.addEventListener("keydown", this.boundKeyDown, { capture: true });
122
+ window.addEventListener("scroll", this.boundScrollOrResize, { capture: true, passive: true });
123
+ window.addEventListener("resize", this.boundScrollOrResize, { passive: true });
124
+ }
125
+ stop(options) {
126
+ if (!this.active)
127
+ return;
128
+ this.active = false;
129
+ this.suppressCursorPin = false;
130
+ this.isScrubbingLayers = false;
131
+ this.wheelAccumulator = 0;
132
+ this.candidateStack = [];
133
+ this.activeCandidateIndex = 0;
134
+ this.leafElement = null;
135
+ this.lastAction = null;
136
+ this.clearTimers();
137
+ if (typeof document !== "undefined" && document.body) {
138
+ document.body.style.cursor = this.previousCursor;
139
+ }
140
+ if (typeof window !== "undefined") {
141
+ window.removeEventListener("mousemove", this.boundMouseMove, { capture: true });
142
+ window.removeEventListener("pointerdown", this.boundPointerCapture, { capture: true });
143
+ window.removeEventListener("mousedown", this.boundPointerCapture, { capture: true });
144
+ window.removeEventListener("mouseup", this.boundPointerCapture, { capture: true });
145
+ window.removeEventListener("click", this.boundClickCapture, { capture: true });
146
+ window.removeEventListener("blur", this.boundBlurSuppression, { capture: true });
147
+ window.removeEventListener("focusout", this.boundBlurSuppression, { capture: true });
148
+ window.removeEventListener("wheel", this.boundWheelCapture, { capture: true });
149
+ window.removeEventListener("keydown", this.boundKeyDown, { capture: true });
150
+ window.removeEventListener("scroll", this.boundScrollOrResize, { capture: true });
151
+ window.removeEventListener("resize", this.boundScrollOrResize);
152
+ }
153
+ this.unmountSelectionUi();
154
+ this.notifyStateChange();
155
+ if (!options?.silent) {
156
+ this.callbacks.onExit();
157
+ }
158
+ }
159
+ highlightAtPoint(x, y, event) {
160
+ if (!this.active || typeof document === "undefined")
161
+ return;
162
+ this.lastPointerX = x;
163
+ this.lastPointerY = y;
164
+ this.updateCandidatesFromPoint(x, y, event);
165
+ this.updateOverlayPosition();
166
+ this.notifyStateChange();
167
+ }
168
+ clearCandidateHighlight() {
169
+ if (!this.active)
170
+ return;
171
+ this.leafElement = null;
172
+ this.candidateStack = [];
173
+ this.activeCandidateIndex = 0;
174
+ this.isScrubbingLayers = false;
175
+ if (this.overlayEl)
176
+ this.overlayEl.style.display = "none";
177
+ if (this.hudEl)
178
+ this.hudEl.style.display = "none";
179
+ if (this.leafOutlineEl)
180
+ this.leafOutlineEl.style.display = "none";
181
+ if (this.pinMarkerEl)
182
+ this.pinMarkerEl.style.display = "none";
183
+ this.notifyStateChange();
184
+ }
185
+ commitActiveCandidate(x, y) {
186
+ if (!this.active)
187
+ return false;
188
+ const candidate = this.getActiveCandidate();
189
+ if (!candidate)
190
+ return false;
191
+ this.commitCandidateSelection(candidate.element, x ?? this.lastPointerX ?? 80, y ?? this.lastPointerY ?? 80);
192
+ return true;
193
+ }
194
+ focusCandidateElement(el) {
195
+ if (!this.active)
196
+ return;
197
+ const excluded = this.callbacks.getExcludedSelectors();
198
+ if (!isSelectableElement(el, excluded))
199
+ return;
200
+ const existingIdx = this.candidateStack.findIndex((c) => c.element === el);
201
+ if (existingIdx !== -1) {
202
+ this.activeCandidateIndex = existingIdx;
203
+ }
204
+ else {
205
+ const info = describeElementCandidate(el, "ancestor", this.candidateStack.length, excluded);
206
+ this.candidateStack.push(info);
207
+ this.activeCandidateIndex = this.candidateStack.length - 1;
208
+ }
209
+ this.isScrubbingLayers = this.candidateStack.length > 1;
210
+ this.scrubOriginX = this.lastPointerX;
211
+ this.scrubOriginY = this.lastPointerY;
212
+ this.updateOverlayPosition();
213
+ this.notifyStateChange();
214
+ }
215
+ selectElementDirectly(el) {
216
+ const excluded = this.callbacks.getExcludedSelectors();
217
+ const snapshot = captureElementSnapshot(el, excluded);
218
+ if (!snapshot)
219
+ return false;
220
+ this.callbacks.onSelect(snapshot, el);
221
+ this.lastAction = { kind: "pinned", selector: snapshot.selector };
222
+ this.isScrubbingLayers = false;
223
+ this.refreshPinnedMarkers();
224
+ if (!this.callbacks.canSelectMore()) {
225
+ this.stop();
226
+ }
227
+ else {
228
+ this.updateOverlayPosition();
229
+ this.notifyStateChange();
230
+ }
231
+ return true;
232
+ }
233
+ refreshPinnedMarkers() {
234
+ if (!this.active || !this.pinnedMarkersContainerEl)
235
+ return;
236
+ this.pinnedMarkersContainerEl.innerHTML = "";
237
+ const pinnedEntries = this.callbacks.getPinnedElements ? this.callbacks.getPinnedElements() : [];
238
+ pinnedEntries.forEach((entry, index) => {
239
+ if (!entry.element || typeof entry.element.getBoundingClientRect !== "function")
240
+ return;
241
+ const rect = entry.element.getBoundingClientRect();
242
+ if (rect.width <= 0 || rect.height <= 0)
243
+ return;
244
+ const markerFrame = document.createElement("div");
245
+ markerFrame.setAttribute("data-context-drop-ui", "pinned-badge");
246
+ markerFrame.style.cssText = [
247
+ "position: fixed",
248
+ "pointer-events: none",
249
+ `left: ${rect.left}px`,
250
+ `top: ${rect.top}px`,
251
+ `width: ${rect.width}px`,
252
+ `height: ${rect.height}px`,
253
+ "border: 1.5px dashed rgba(194, 65, 12, 0.72)",
254
+ "background: rgba(194, 65, 12, 0.04)",
255
+ "border-radius: 6px"
256
+ ].join(";");
257
+ const badge = document.createElement("div");
258
+ const badgeTop = rect.top < 26 ? "4px" : "-22px";
259
+ const smartMode = this.isSmartMode();
260
+ badge.style.cssText = [
261
+ "position: absolute",
262
+ `top: ${badgeTop}`,
263
+ "right: -1px",
264
+ "display: inline-flex",
265
+ "align-items: center",
266
+ "gap: 4px",
267
+ "padding: 2px 7px",
268
+ "border-radius: 9999px",
269
+ "background: #c2410c",
270
+ "color: #faf9f6",
271
+ "border: 1px solid #faf9f6",
272
+ smartMode
273
+ ? "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
274
+ : "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
275
+ "font-size: 10px",
276
+ "font-weight: 600",
277
+ "font-variant-numeric: tabular-nums",
278
+ "line-height: 1.2",
279
+ "max-width: 180px",
280
+ "overflow: hidden",
281
+ "text-overflow: ellipsis",
282
+ "white-space: nowrap",
283
+ "box-shadow: 0 4px 10px -2px rgba(17, 19, 24, 0.25)"
284
+ ].join(";");
285
+ badge.textContent = smartMode
286
+ ? entry.friendlyLabel || "Selected area"
287
+ : `#${index + 1} ${entry.selector}`;
288
+ markerFrame.appendChild(badge);
289
+ this.pinnedMarkersContainerEl?.appendChild(markerFrame);
290
+ });
291
+ }
292
+ showExternalPreview(el) {
293
+ if (typeof document === "undefined" || !document.body)
294
+ return;
295
+ if (!el || this.active) {
296
+ this.hideExternalPreview();
297
+ return;
298
+ }
299
+ const rect = el.getBoundingClientRect();
300
+ if (rect.width <= 0 || rect.height <= 0) {
301
+ this.hideExternalPreview();
302
+ return;
303
+ }
304
+ if (!this.previewOverlayEl) {
305
+ const preview = document.createElement("div");
306
+ preview.setAttribute("data-context-drop-ui", "chip-preview");
307
+ preview.style.cssText = [
308
+ "position: fixed",
309
+ "pointer-events: none",
310
+ "z-index: 2147483644",
311
+ "border: 2px solid #c2410c",
312
+ "background: rgba(234, 88, 12, 0.10)",
313
+ "border-radius: 6px",
314
+ "transition: all 0.12s cubic-bezier(0.22, 1, 0.36, 1)"
315
+ ].join(";");
316
+ document.body.appendChild(preview);
317
+ this.previewOverlayEl = preview;
318
+ }
319
+ this.previewOverlayEl.style.display = "block";
320
+ this.previewOverlayEl.style.left = `${rect.left}px`;
321
+ this.previewOverlayEl.style.top = `${rect.top}px`;
322
+ this.previewOverlayEl.style.width = `${rect.width}px`;
323
+ this.previewOverlayEl.style.height = `${rect.height}px`;
324
+ }
325
+ hideExternalPreview() {
326
+ if (this.previewOverlayEl && this.previewOverlayEl.parentNode) {
327
+ this.previewOverlayEl.parentNode.removeChild(this.previewOverlayEl);
328
+ }
329
+ this.previewOverlayEl = null;
330
+ }
331
+ cycleDepth(delta, revealStack = true) {
332
+ if (!this.active || this.candidateStack.length <= 1)
333
+ return;
334
+ const wasScrubbing = this.isScrubbingLayers;
335
+ if (revealStack) {
336
+ this.isScrubbingLayers = true;
337
+ this.scrubOriginX = this.lastPointerX;
338
+ this.scrubOriginY = this.lastPointerY;
339
+ }
340
+ const next = Math.max(0, Math.min(this.candidateStack.length - 1, this.activeCandidateIndex + delta));
341
+ if (next !== this.activeCandidateIndex) {
342
+ this.activeCandidateIndex = next;
343
+ this.updateOverlayPosition();
344
+ this.notifyStateChange();
345
+ }
346
+ else if (!wasScrubbing && this.isScrubbingLayers) {
347
+ this.updateOverlayPosition();
348
+ this.triggerHudBoundaryNudge(delta < 0 ? -3 : 3);
349
+ this.notifyStateChange();
350
+ }
351
+ else {
352
+ this.triggerHudBoundaryNudge(delta < 0 ? -3 : 3);
353
+ }
354
+ }
355
+ clearTimers() {
356
+ if (this.pulseTimer !== null) {
357
+ clearTimeout(this.pulseTimer);
358
+ this.pulseTimer = null;
359
+ }
360
+ if (this.nudgeTimer !== null) {
361
+ clearTimeout(this.nudgeTimer);
362
+ this.nudgeTimer = null;
363
+ }
364
+ if (this.toastTimer !== null) {
365
+ clearTimeout(this.toastTimer);
366
+ this.toastTimer = null;
367
+ }
368
+ }
369
+ findPinnedIndex(el) {
370
+ if (!this.callbacks.getPinnedElements)
371
+ return null;
372
+ const pinned = this.callbacks.getPinnedElements();
373
+ for (let i = 0; i < pinned.length; i += 1) {
374
+ if (pinned[i].element === el) {
375
+ return { index: i, entry: pinned[i] };
376
+ }
377
+ }
378
+ return null;
379
+ }
380
+ notifyStateChange() {
381
+ if (!this.callbacks.onStateChange)
382
+ return;
383
+ const activeCandidate = this.getActiveCandidate();
384
+ const ancestors = activeCandidate
385
+ ? resolveAncestorChain(activeCandidate.element, this.callbacks.getExcludedSelectors())
386
+ : [];
387
+ this.callbacks.onStateChange({
388
+ active: this.active,
389
+ anchored: false,
390
+ scrubbing: this.isScrubbingLayers,
391
+ activeCandidate,
392
+ candidateStack: [...this.candidateStack],
393
+ ancestors,
394
+ lastAction: this.lastAction
395
+ });
396
+ }
397
+ mountSelectionUi() {
398
+ if (typeof document === "undefined" || !document.body)
399
+ return;
400
+ const pinnedMarkers = document.createElement("div");
401
+ pinnedMarkers.setAttribute("data-context-drop-ui", "pinned-markers");
402
+ pinnedMarkers.style.cssText = [
403
+ "position: fixed",
404
+ "inset: 0",
405
+ "pointer-events: none",
406
+ "z-index: 2147483643"
407
+ ].join(";");
408
+ // Secondary ghost outline showing the innermost leaf element when cycling up to parent/overlapping layers
409
+ const leafOutline = document.createElement("div");
410
+ leafOutline.setAttribute("data-context-drop-ui", "leaf-outline");
411
+ leafOutline.style.cssText = [
412
+ "position: fixed",
413
+ "pointer-events: none",
414
+ "z-index: 2147483644",
415
+ "border: 1px dashed rgba(29, 78, 216, 0.55)",
416
+ "background: rgba(29, 78, 216, 0.03)",
417
+ "border-radius: 4px",
418
+ "display: none"
419
+ ].join(";");
420
+ // Primary active element highlight box
421
+ const overlay = document.createElement("div");
422
+ overlay.setAttribute("data-context-drop-ui", "overlay");
423
+ overlay.style.cssText = [
424
+ "position: fixed",
425
+ "pointer-events: none",
426
+ "z-index: 2147483645",
427
+ "border: 2px solid #1d4ed8",
428
+ "background: rgba(29, 78, 216, 0.10)",
429
+ "border-radius: 6px",
430
+ "display: none",
431
+ "transition: left 0.06s cubic-bezier(0.22, 1, 0.36, 1), top 0.06s cubic-bezier(0.22, 1, 0.36, 1), width 0.06s cubic-bezier(0.22, 1, 0.36, 1), height 0.06s cubic-bezier(0.22, 1, 0.36, 1), border-color 0.16s ease, background-color 0.16s ease"
432
+ ].join(";");
433
+ // Viewport-clamped Floating HUD (Compact 2-line coach on hover, inline 5-row wheel-scrubbed stack on scroll)
434
+ const hud = document.createElement("div");
435
+ hud.setAttribute("data-context-drop-ui", "label");
436
+ hud.style.cssText = [
437
+ "position: fixed",
438
+ "z-index: 2147483646",
439
+ "background: #111318",
440
+ "color: #faf9f6",
441
+ "border: 1px solid rgba(250, 249, 246, 0.16)",
442
+ "border-radius: 10px",
443
+ "pointer-events: none",
444
+ "display: none",
445
+ "overflow: hidden",
446
+ "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
447
+ "font-size: 11.5px",
448
+ "line-height: 1.35",
449
+ "box-shadow: 0 16px 34px -8px rgba(17, 19, 24, 0.40), 0 6px 14px -4px rgba(17, 19, 24, 0.22)",
450
+ "transition: transform 0.12s cubic-bezier(0.22, 1, 0.36, 1)"
451
+ ].join(";");
452
+ // Floating Pin marker that follows the pointer
453
+ const pinMarker = document.createElement("div");
454
+ pinMarker.setAttribute("data-context-drop-ui", "pin-marker");
455
+ pinMarker.style.cssText = [
456
+ "position: fixed",
457
+ "pointer-events: none",
458
+ "z-index: 2147483647",
459
+ "width: 28px",
460
+ "height: 28px",
461
+ "margin-left: -6px",
462
+ "margin-top: -24px",
463
+ "border-radius: 9999px 9999px 9999px 3px",
464
+ "background: #c2410c",
465
+ "color: #faf9f6",
466
+ "border: 1.5px solid #faf9f6",
467
+ "display: none",
468
+ "align-items: center",
469
+ "justify-content: center",
470
+ "box-shadow: 0 8px 16px -3px rgba(17, 19, 24, 0.32)",
471
+ "transition: transform 0.12s cubic-bezier(0.22, 1, 0.36, 1)"
472
+ ].join(";");
473
+ pinMarker.innerHTML = ICON_PIN;
474
+ // Tactile floating confirmation pill near cursor on pin / unpin
475
+ const toast = document.createElement("div");
476
+ toast.setAttribute("data-context-drop-ui", "pin-toast");
477
+ toast.style.cssText = [
478
+ "position: fixed",
479
+ "pointer-events: none",
480
+ "z-index: 2147483647",
481
+ "display: none",
482
+ "align-items: center",
483
+ "gap: 6px",
484
+ "padding: 6px 10px",
485
+ "border-radius: 9999px",
486
+ "background: #111318",
487
+ "color: #faf9f6",
488
+ "border: 1px solid rgba(250, 249, 246, 0.18)",
489
+ "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
490
+ "font-size: 11.5px",
491
+ "font-weight: 500",
492
+ "white-space: nowrap",
493
+ "box-shadow: 0 12px 24px -6px rgba(17, 19, 24, 0.34)"
494
+ ].join(";");
495
+ document.body.appendChild(pinnedMarkers);
496
+ document.body.appendChild(leafOutline);
497
+ document.body.appendChild(overlay);
498
+ document.body.appendChild(hud);
499
+ document.body.appendChild(pinMarker);
500
+ document.body.appendChild(toast);
501
+ this.pinnedMarkersContainerEl = pinnedMarkers;
502
+ this.leafOutlineEl = leafOutline;
503
+ this.overlayEl = overlay;
504
+ this.hudEl = hud;
505
+ this.pinMarkerEl = pinMarker;
506
+ this.toastEl = toast;
507
+ }
508
+ unmountSelectionUi() {
509
+ for (const el of [
510
+ this.pinnedMarkersContainerEl,
511
+ this.leafOutlineEl,
512
+ this.overlayEl,
513
+ this.hudEl,
514
+ this.pinMarkerEl,
515
+ this.toastEl
516
+ ]) {
517
+ if (el && el.parentNode) {
518
+ el.parentNode.removeChild(el);
519
+ }
520
+ }
521
+ this.pinnedMarkersContainerEl = null;
522
+ this.leafOutlineEl = null;
523
+ this.overlayEl = null;
524
+ this.hudEl = null;
525
+ this.pinMarkerEl = null;
526
+ this.toastEl = null;
527
+ }
528
+ isInternalUiEvent(event) {
529
+ const path = typeof event.composedPath === "function" ? event.composedPath() : [];
530
+ for (const node of path) {
531
+ if (isDomElementNode(node)) {
532
+ if (node.tagName.toLowerCase() === "context-drop-widget" ||
533
+ node.hasAttribute("data-context-drop-ui") ||
534
+ (typeof node.closest === "function" &&
535
+ (node.closest("context-drop-widget") || node.closest("[data-context-drop-ui]")))) {
536
+ return true;
537
+ }
538
+ }
539
+ }
540
+ const rawTarget = isDomElementNode(event.target) ? event.target : null;
541
+ if (rawTarget &&
542
+ typeof rawTarget.closest === "function" &&
543
+ (rawTarget.closest("context-drop-widget") || rawTarget.closest("[data-context-drop-ui]"))) {
544
+ return true;
545
+ }
546
+ return false;
547
+ }
548
+ collectPointElements(clientX, clientY, event) {
549
+ const results = [];
550
+ if (typeof document !== "undefined" && typeof document.elementsFromPoint === "function") {
551
+ try {
552
+ const fromPoint = document.elementsFromPoint(clientX, clientY);
553
+ for (const el of fromPoint) {
554
+ if (isDomElementNode(el)) {
555
+ results.push(el);
556
+ }
557
+ }
558
+ }
559
+ catch {
560
+ // Fallback to composedPath below
561
+ }
562
+ }
563
+ if (event && typeof event.composedPath === "function") {
564
+ for (const node of event.composedPath()) {
565
+ if (isDomElementNode(node) && !results.includes(node)) {
566
+ results.push(node);
567
+ }
568
+ }
569
+ }
570
+ if (event && isDomElementNode(event.target) && !results.includes(event.target)) {
571
+ results.unshift(event.target);
572
+ }
573
+ return results;
574
+ }
575
+ updateCandidatesFromPoint(clientX, clientY, event) {
576
+ const excluded = this.callbacks.getExcludedSelectors();
577
+ const pointElements = this.collectPointElements(clientX, clientY, event);
578
+ const primary = pointElements.find((el) => isSelectableElement(el, excluded)) || null;
579
+ if (!primary) {
580
+ this.leafElement = null;
581
+ this.candidateStack = [];
582
+ this.activeCandidateIndex = 0;
583
+ this.isScrubbingLayers = false;
584
+ return;
585
+ }
586
+ if (this.isSmartMode()) {
587
+ const smartComponent = resolveSmartComponent(primary, excluded) || primary;
588
+ if (this.leafElement === primary &&
589
+ this.candidateStack.length === 1 &&
590
+ this.candidateStack[0].element === smartComponent) {
591
+ return;
592
+ }
593
+ this.leafElement = primary;
594
+ this.candidateStack = [describeElementCandidate(smartComponent, "target", 0, excluded)];
595
+ this.activeCandidateIndex = 0;
596
+ this.isScrubbingLayers = false;
597
+ return;
598
+ }
599
+ // Preserve depth & inline stack when pointer stays inside the exact same primary leaf element
600
+ if (this.leafElement === primary && this.candidateStack.length > 0) {
601
+ return;
602
+ }
603
+ // Pointer-jitter lock: if user scrubbed to a specific layer and nudges the mouse slightly (<=28px)
604
+ // across child boundaries inside that selected element right before clicking, keep it locked.
605
+ if (this.isScrubbingLayers && this.candidateStack.length > 0) {
606
+ const lockedCandidate = this.getActiveCandidate();
607
+ const travel = Math.hypot(clientX - this.scrubOriginX, clientY - this.scrubOriginY);
608
+ if (lockedCandidate &&
609
+ travel <= SCRUB_JITTER_LOCK_PX &&
610
+ (pointElements.includes(lockedCandidate.element) ||
611
+ isPointInsideRect(clientX, clientY, lockedCandidate.element.getBoundingClientRect()))) {
612
+ return;
613
+ }
614
+ }
615
+ this.leafElement = primary;
616
+ this.candidateStack = resolveCandidateStack(primary, pointElements, excluded, 10);
617
+ this.activeCandidateIndex = 0;
618
+ this.isScrubbingLayers = false;
619
+ }
620
+ handleViewportScrollOrResize() {
621
+ this.refreshPinnedMarkers();
622
+ this.updateOverlayPosition();
623
+ }
624
+ handleMouseMove(event) {
625
+ if (!this.active)
626
+ return;
627
+ this.lastPointerX = event.clientX;
628
+ this.lastPointerY = event.clientY;
629
+ if (this.isInternalUiEvent(event)) {
630
+ if (this.overlayEl)
631
+ this.overlayEl.style.display = "none";
632
+ if (this.hudEl)
633
+ this.hudEl.style.display = "none";
634
+ if (this.leafOutlineEl)
635
+ this.leafOutlineEl.style.display = "none";
636
+ if (this.pinMarkerEl)
637
+ this.pinMarkerEl.style.display = "none";
638
+ return;
639
+ }
640
+ if (this.pinMarkerEl) {
641
+ if (this.suppressCursorPin) {
642
+ this.pinMarkerEl.style.display = "none";
643
+ }
644
+ else {
645
+ this.pinMarkerEl.style.display = "inline-flex";
646
+ this.pinMarkerEl.style.left = `${event.clientX}px`;
647
+ this.pinMarkerEl.style.top = `${event.clientY}px`;
648
+ }
649
+ }
650
+ this.updateCandidatesFromPoint(event.clientX, event.clientY, event);
651
+ this.updateOverlayPosition();
652
+ this.notifyStateChange();
653
+ }
654
+ createSpanEl(text) {
655
+ const span = document.createElement("span");
656
+ span.textContent = text;
657
+ return span;
658
+ }
659
+ createKbdEl(text) {
660
+ const kbd = document.createElement("kbd");
661
+ kbd.textContent = text;
662
+ kbd.style.cssText = [
663
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
664
+ "font-size: 10px",
665
+ "font-weight: 600",
666
+ "line-height: 1.1",
667
+ "padding: 1px 4px",
668
+ "border-radius: 4px",
669
+ "background: rgba(250, 249, 246, 0.14)",
670
+ "border: 1px solid rgba(250, 249, 246, 0.24)",
671
+ "color: #faf9f6"
672
+ ].join(";");
673
+ return kbd;
674
+ }
675
+ renderSmartHudContent(currentCandidate) {
676
+ if (!this.hudEl)
677
+ return;
678
+ this.hudEl.innerHTML = "";
679
+ this.hudEl.style.width = "auto";
680
+ this.hudEl.style.maxWidth = "min(360px, calc(100vw - 20px))";
681
+ this.hudEl.style.borderRadius = "9999px";
682
+ const pinnedInfo = this.findPinnedIndex(currentCandidate.element);
683
+ const pill = document.createElement("div");
684
+ pill.setAttribute("data-context-drop-ui", "smart-hud-pill");
685
+ pill.style.cssText = [
686
+ "display: inline-flex",
687
+ "align-items: center",
688
+ "gap: 7px",
689
+ "padding: 6px 12px",
690
+ "white-space: nowrap"
691
+ ].join(";");
692
+ const iconDot = document.createElement("span");
693
+ iconDot.style.cssText = [
694
+ "display: inline-flex",
695
+ "align-items: center",
696
+ "justify-content: center",
697
+ "width: 14px",
698
+ "height: 14px",
699
+ "color: #ffedd5",
700
+ "flex-shrink: 0"
701
+ ].join(";");
702
+ iconDot.innerHTML = ICON_PIN;
703
+ pill.appendChild(iconDot);
704
+ const nameSpan = document.createElement("span");
705
+ nameSpan.textContent = currentCandidate.friendlyLabel || "Page area";
706
+ nameSpan.style.cssText = [
707
+ "font-size: 12px",
708
+ "font-weight: 600",
709
+ "color: #faf9f6",
710
+ "max-width: 210px",
711
+ "overflow: hidden",
712
+ "text-overflow: ellipsis",
713
+ "white-space: nowrap"
714
+ ].join(";");
715
+ pill.appendChild(nameSpan);
716
+ const actionHint = document.createElement("span");
717
+ actionHint.textContent = this.suppressCursorPin
718
+ ? pinnedInfo
719
+ ? "· Release to remove"
720
+ : "· Release to select"
721
+ : pinnedInfo
722
+ ? "· Click to remove"
723
+ : "· Click to select";
724
+ actionHint.style.cssText = "font-size: 11px; color: #cbd5e1; flex-shrink: 0;";
725
+ pill.appendChild(actionHint);
726
+ this.hudEl.appendChild(pill);
727
+ }
728
+ renderCompactHudContent(currentCandidate, rect) {
729
+ if (!this.hudEl)
730
+ return;
731
+ this.hudEl.innerHTML = "";
732
+ this.hudEl.style.width = "auto";
733
+ this.hudEl.style.maxWidth = "min(440px, calc(100vw - 20px))";
734
+ this.hudEl.style.borderRadius = "10px";
735
+ const total = this.candidateStack.length;
736
+ const pos = this.activeCandidateIndex + 1;
737
+ const dims = `${Math.round(rect.width)}×${Math.round(rect.height)}`;
738
+ const pinnedInfo = this.findPinnedIndex(currentCandidate.element);
739
+ const hasOverlap = this.candidateStack.some((c) => c.relation === "overlap");
740
+ // Top Row: Identity + Dimensions + Layer Count
741
+ const topRow = document.createElement("div");
742
+ topRow.style.cssText = [
743
+ "display: flex",
744
+ "align-items: center",
745
+ "gap: 6px",
746
+ "padding: 6px 9px 5px",
747
+ "white-space: nowrap"
748
+ ].join(";");
749
+ if (pinnedInfo) {
750
+ const pinnedPill = document.createElement("span");
751
+ pinnedPill.textContent = `#${pinnedInfo.index + 1} Pinned`;
752
+ pinnedPill.style.cssText = [
753
+ "padding: 1px 5px",
754
+ "border-radius: 4px",
755
+ "background: #c2410c",
756
+ "color: #faf9f6",
757
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
758
+ "font-size: 10px",
759
+ "font-weight: 700",
760
+ "font-variant-numeric: tabular-nums",
761
+ "flex-shrink: 0"
762
+ ].join(";");
763
+ topRow.appendChild(pinnedPill);
764
+ }
765
+ const selectorSpan = document.createElement("span");
766
+ selectorSpan.textContent = formatCandidateLabel(currentCandidate.tagName, currentCandidate.selector);
767
+ selectorSpan.style.cssText = [
768
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
769
+ "font-size: 11.5px",
770
+ "font-weight: 600",
771
+ "color: #faf9f6",
772
+ "max-width: 230px",
773
+ "overflow: hidden",
774
+ "text-overflow: ellipsis",
775
+ "white-space: nowrap"
776
+ ].join(";");
777
+ topRow.appendChild(selectorSpan);
778
+ const dimsSpan = document.createElement("span");
779
+ dimsSpan.textContent = dims;
780
+ dimsSpan.style.cssText = [
781
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
782
+ "font-size: 10.5px",
783
+ "font-variant-numeric: tabular-nums",
784
+ "color: #cbd5e1",
785
+ "flex-shrink: 0"
786
+ ].join(";");
787
+ topRow.appendChild(dimsSpan);
788
+ if (total > 1) {
789
+ const layerPill = document.createElement("span");
790
+ layerPill.style.cssText = [
791
+ "display: inline-flex",
792
+ "align-items: center",
793
+ "gap: 3px",
794
+ "padding: 1px 6px",
795
+ "border-radius: 9999px",
796
+ "background: rgba(29, 78, 216, 0.36)",
797
+ "border: 1px solid rgba(96, 165, 250, 0.45)",
798
+ "color: #dbeafe",
799
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
800
+ "font-size: 10px",
801
+ "font-weight: 600",
802
+ "font-variant-numeric: tabular-nums",
803
+ "flex-shrink: 0"
804
+ ].join(";");
805
+ const iconWrap = document.createElement("span");
806
+ iconWrap.style.cssText = "display: inline-flex; width: 11px; height: 11px; align-items: center;";
807
+ iconWrap.innerHTML = ICON_LAYERS;
808
+ const countText = document.createElement("span");
809
+ countText.textContent = `${pos}/${total}`;
810
+ layerPill.appendChild(iconWrap);
811
+ layerPill.appendChild(countText);
812
+ topRow.appendChild(layerPill);
813
+ }
814
+ this.hudEl.appendChild(topRow);
815
+ // Bottom Row: Action Coaching Strip
816
+ const coachRow = document.createElement("div");
817
+ coachRow.style.cssText = [
818
+ "display: flex",
819
+ "align-items: center",
820
+ "gap: 8px",
821
+ "padding: 4px 9px 5px",
822
+ "background: rgba(250, 249, 246, 0.05)",
823
+ "border-top: 1px solid rgba(250, 249, 246, 0.10)",
824
+ "font-size: 10.5px",
825
+ "color: #cbd5e1",
826
+ "white-space: nowrap"
827
+ ].join(";");
828
+ const clickHint = document.createElement("span");
829
+ clickHint.style.cssText = "display: inline-flex; align-items: center; gap: 4px;";
830
+ if (this.suppressCursorPin) {
831
+ const releaseStrong = document.createElement("strong");
832
+ releaseStrong.style.cssText = pinnedInfo
833
+ ? "color: #ffedd5; font-weight: 600;"
834
+ : "color: #faf9f6; font-weight: 600;";
835
+ releaseStrong.textContent = "Release";
836
+ clickHint.appendChild(releaseStrong);
837
+ clickHint.appendChild(this.createSpanEl(pinnedInfo ? " to unpin" : " to pin"));
838
+ }
839
+ else {
840
+ clickHint.appendChild(this.createKbdEl("Space"));
841
+ clickHint.appendChild(this.createSpanEl("or"));
842
+ const clickStrong = document.createElement("strong");
843
+ clickStrong.style.cssText = pinnedInfo
844
+ ? "color: #ffedd5; font-weight: 600;"
845
+ : "color: #faf9f6; font-weight: 600;";
846
+ clickStrong.textContent = "Click";
847
+ clickHint.appendChild(clickStrong);
848
+ clickHint.appendChild(this.createSpanEl(pinnedInfo ? " to unpin" : " to pin"));
849
+ }
850
+ coachRow.appendChild(clickHint);
851
+ if (total > 1) {
852
+ const scrollHint = document.createElement("span");
853
+ scrollHint.style.cssText = "display: inline-flex; align-items: center; gap: 3px;";
854
+ const wheelIcon = document.createElement("span");
855
+ wheelIcon.style.cssText = "display: inline-flex; color: #dbeafe;";
856
+ wheelIcon.innerHTML = ICON_SCROLL_VERTICAL;
857
+ const scrollStrong = document.createElement("strong");
858
+ scrollStrong.style.cssText = hasOverlap
859
+ ? "color: #dbeafe; font-weight: 600;"
860
+ : "color: #faf9f6; font-weight: 600;";
861
+ scrollStrong.textContent = "Scroll wheel";
862
+ scrollHint.appendChild(wheelIcon);
863
+ scrollHint.appendChild(scrollStrong);
864
+ scrollHint.appendChild(this.createSpanEl(hasOverlap ? " for element behind" : ` for ${total} layers`));
865
+ coachRow.appendChild(scrollHint);
866
+ }
867
+ const escHint = document.createElement("span");
868
+ escHint.style.cssText = "display: inline-flex; align-items: center; gap: 4px;";
869
+ escHint.appendChild(this.createKbdEl("⌥⇧P"));
870
+ escHint.appendChild(this.createSpanEl("freeze · "));
871
+ escHint.appendChild(this.createKbdEl("Esc"));
872
+ escHint.appendChild(this.createSpanEl("done"));
873
+ coachRow.appendChild(escHint);
874
+ this.hudEl.appendChild(coachRow);
875
+ }
876
+ renderScrubbingHudContent(_currentCandidate, rect) {
877
+ if (!this.hudEl)
878
+ return;
879
+ this.hudEl.innerHTML = "";
880
+ this.hudEl.style.width = "320px";
881
+ this.hudEl.style.maxWidth = "calc(100vw - 20px)";
882
+ const total = this.candidateStack.length;
883
+ const pos = this.activeCandidateIndex + 1;
884
+ // Header Strip
885
+ const header = document.createElement("div");
886
+ header.style.cssText = [
887
+ "display: flex",
888
+ "align-items: center",
889
+ "justify-content: space-between",
890
+ "gap: 8px",
891
+ "padding: 6px 10px",
892
+ "background: rgba(250, 249, 246, 0.05)",
893
+ "border-bottom: 1px solid rgba(250, 249, 246, 0.12)"
894
+ ].join(";");
895
+ const headerLeft = document.createElement("div");
896
+ headerLeft.style.cssText =
897
+ "display: inline-flex; align-items: center; gap: 5px; font-size: 11px; font-weight: 600; color: #faf9f6;";
898
+ const layersIcon = document.createElement("span");
899
+ layersIcon.style.cssText = "display: inline-flex; color: #60a5fa;";
900
+ layersIcon.innerHTML = ICON_LAYERS;
901
+ const headerTitle = document.createElement("span");
902
+ headerTitle.textContent = "Select Layer";
903
+ headerLeft.appendChild(layersIcon);
904
+ headerLeft.appendChild(headerTitle);
905
+ const headerRight = document.createElement("div");
906
+ headerRight.style.cssText =
907
+ "display: inline-flex; align-items: center; gap: 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 10.5px; font-variant-numeric: tabular-nums; color: #cbd5e1;";
908
+ const wheelSpan = document.createElement("span");
909
+ wheelSpan.style.cssText = "display: inline-flex; color: #dbeafe;";
910
+ wheelSpan.innerHTML = ICON_SCROLL_VERTICAL;
911
+ const ratioSpan = document.createElement("span");
912
+ ratioSpan.textContent = `${pos}/${total}`;
913
+ headerRight.appendChild(wheelSpan);
914
+ headerRight.appendChild(ratioSpan);
915
+ header.appendChild(headerLeft);
916
+ header.appendChild(headerRight);
917
+ this.hudEl.appendChild(header);
918
+ // Non-scrolling Sliding Window of Layers (max 5 rows)
919
+ const start = total <= MAX_VISIBLE_STACK_ROWS
920
+ ? 0
921
+ : Math.max(0, Math.min(this.activeCandidateIndex - 2, total - MAX_VISIBLE_STACK_ROWS));
922
+ const end = Math.min(total, start + MAX_VISIBLE_STACK_ROWS);
923
+ const listWrap = document.createElement("div");
924
+ listWrap.setAttribute("data-context-drop-ui", "hud-layer-list");
925
+ listWrap.style.cssText = [
926
+ "display: flex",
927
+ "flex-direction: column",
928
+ "gap: 2px",
929
+ "padding: 5px"
930
+ ].join(";");
931
+ if (start > 0) {
932
+ const aboveHint = document.createElement("div");
933
+ aboveHint.textContent = `${start} inner layer${start > 1 ? "s" : ""} above`;
934
+ aboveHint.style.cssText =
935
+ "font-size: 10px; color: #cbd5e1; padding: 1px 7px; font-variant-numeric: tabular-nums;";
936
+ listWrap.appendChild(aboveHint);
937
+ }
938
+ const relationLabels = {
939
+ target: "Topmost",
940
+ overlap: "Behind",
941
+ child: "Child",
942
+ ancestor: "Parent"
943
+ };
944
+ for (let idx = start; idx < end; idx += 1) {
945
+ const candidate = this.candidateStack[idx];
946
+ const isSelected = idx === this.activeCandidateIndex;
947
+ const candPinned = this.findPinnedIndex(candidate.element);
948
+ const row = document.createElement("div");
949
+ row.setAttribute("data-context-drop-layer-index", String(idx));
950
+ if (isSelected) {
951
+ row.setAttribute("data-selected", "true");
952
+ }
953
+ row.style.cssText = [
954
+ "display: flex",
955
+ "align-items: center",
956
+ "justify-content: space-between",
957
+ "gap: 6px",
958
+ "padding: " + (isSelected ? "5px 7px" : "4px 7px"),
959
+ "border-radius: 7px",
960
+ "border: 1px solid " + (isSelected ? "#60a5fa" : "transparent"),
961
+ "background: " + (isSelected ? "rgba(29, 78, 216, 0.34)" : "transparent")
962
+ ].join(";");
963
+ const leftGroup = document.createElement("div");
964
+ leftGroup.style.cssText = "display: flex; align-items: center; gap: 5px; min-width: 0; flex: 1;";
965
+ const relBadge = document.createElement("span");
966
+ relBadge.textContent = relationLabels[candidate.relation] || "Layer";
967
+ const relBg = candidate.relation === "target"
968
+ ? "rgba(255, 237, 213, 0.16)"
969
+ : candidate.relation === "overlap"
970
+ ? "rgba(252, 231, 243, 0.20)"
971
+ : "rgba(226, 232, 240, 0.14)";
972
+ const relColor = candidate.relation === "target"
973
+ ? "#ffedd5"
974
+ : candidate.relation === "overlap"
975
+ ? "#fee2e2"
976
+ : "#cbd5e1";
977
+ relBadge.style.cssText = [
978
+ "font-size: 9.5px",
979
+ "font-weight: 600",
980
+ "padding: 1px 5px",
981
+ "border-radius: 4px",
982
+ `background: ${relBg}`,
983
+ `color: ${relColor}`,
984
+ "flex-shrink: 0"
985
+ ].join(";");
986
+ const selText = document.createElement("span");
987
+ selText.textContent = formatCandidateLabel(candidate.tagName, candidate.selector);
988
+ selText.style.cssText = [
989
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
990
+ "font-size: 11px",
991
+ "font-weight: " + (isSelected ? "600" : "500"),
992
+ "color: " + (isSelected ? "#faf9f6" : "#e2e8f0"),
993
+ "overflow: hidden",
994
+ "text-overflow: ellipsis",
995
+ "white-space: nowrap"
996
+ ].join(";");
997
+ leftGroup.appendChild(relBadge);
998
+ leftGroup.appendChild(selText);
999
+ if (candPinned) {
1000
+ const pinTag = document.createElement("span");
1001
+ pinTag.textContent = `#${candPinned.index + 1}`;
1002
+ pinTag.style.cssText = [
1003
+ "padding: 0 4px",
1004
+ "border-radius: 4px",
1005
+ "background: #c2410c",
1006
+ "color: #faf9f6",
1007
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
1008
+ "font-size: 9.5px",
1009
+ "font-weight: 700",
1010
+ "flex-shrink: 0"
1011
+ ].join(";");
1012
+ leftGroup.appendChild(pinTag);
1013
+ }
1014
+ row.appendChild(leftGroup);
1015
+ if (isSelected) {
1016
+ const actionBadge = document.createElement("span");
1017
+ actionBadge.textContent = candPinned ? "Click unpin" : "Click to pin";
1018
+ actionBadge.style.cssText = [
1019
+ "padding: 1px 6px",
1020
+ "border-radius: 4px",
1021
+ "background: " + (candPinned ? "rgba(250, 249, 246, 0.18)" : "#c2410c"),
1022
+ "color: #faf9f6",
1023
+ "font-size: 10px",
1024
+ "font-weight: 600",
1025
+ "flex-shrink: 0"
1026
+ ].join(";");
1027
+ row.appendChild(actionBadge);
1028
+ }
1029
+ else {
1030
+ const rowDims = document.createElement("span");
1031
+ rowDims.textContent = `${Math.round(candidate.rect.width)}×${Math.round(candidate.rect.height)}`;
1032
+ rowDims.style.cssText = [
1033
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace",
1034
+ "font-size: 10px",
1035
+ "font-variant-numeric: tabular-nums",
1036
+ "color: #cbd5e1",
1037
+ "flex-shrink: 0"
1038
+ ].join(";");
1039
+ row.appendChild(rowDims);
1040
+ }
1041
+ listWrap.appendChild(row);
1042
+ }
1043
+ if (end < total) {
1044
+ const belowHint = document.createElement("div");
1045
+ belowHint.textContent = `${total - end} parent layer${total - end > 1 ? "s" : ""} below`;
1046
+ belowHint.style.cssText =
1047
+ "font-size: 10px; color: #cbd5e1; padding: 1px 7px; font-variant-numeric: tabular-nums;";
1048
+ listWrap.appendChild(belowHint);
1049
+ }
1050
+ this.hudEl.appendChild(listWrap);
1051
+ // Footer Strip
1052
+ const footer = document.createElement("div");
1053
+ footer.style.cssText = [
1054
+ "display: flex",
1055
+ "align-items: center",
1056
+ "justify-content: space-between",
1057
+ "gap: 8px",
1058
+ "padding: 5px 10px",
1059
+ "background: rgba(250, 249, 246, 0.04)",
1060
+ "border-top: 1px solid rgba(250, 249, 246, 0.10)",
1061
+ "font-size: 10.5px",
1062
+ "color: #cbd5e1"
1063
+ ].join(";");
1064
+ const dimsLabel = document.createElement("span");
1065
+ dimsLabel.textContent = `${Math.round(rect.width)}×${Math.round(rect.height)}`;
1066
+ dimsLabel.style.cssText =
1067
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 10px; font-variant-numeric: tabular-nums;";
1068
+ const keysHint = document.createElement("span");
1069
+ keysHint.style.cssText = "display: inline-flex; align-items: center; gap: 4px;";
1070
+ keysHint.appendChild(this.createKbdEl("Space"));
1071
+ keysHint.appendChild(this.createSpanEl("pin · "));
1072
+ keysHint.appendChild(this.createKbdEl("⌥⇧P"));
1073
+ keysHint.appendChild(this.createSpanEl("freeze · "));
1074
+ keysHint.appendChild(this.createKbdEl("Esc"));
1075
+ keysHint.appendChild(this.createSpanEl("done"));
1076
+ footer.appendChild(dimsLabel);
1077
+ footer.appendChild(keysHint);
1078
+ this.hudEl.appendChild(footer);
1079
+ }
1080
+ positionHudElement(rect) {
1081
+ if (!this.hudEl || typeof window === "undefined")
1082
+ return;
1083
+ const vw = window.innerWidth || 1024;
1084
+ const vh = window.innerHeight || 768;
1085
+ const fallbackWidth = this.isScrubbingLayers ? 320 : 300;
1086
+ const visibleRows = Math.min(this.candidateStack.length, MAX_VISIBLE_STACK_ROWS);
1087
+ const fallbackHeight = this.isScrubbingLayers ? 66 + visibleRows * 30 : 54;
1088
+ const hudWidth = this.hudEl.offsetWidth || fallbackWidth;
1089
+ const hudHeight = this.hudEl.offsetHeight || fallbackHeight;
1090
+ const clampedLeft = Math.max(10, Math.min(rect.left, vw - hudWidth - 10));
1091
+ let top = rect.top - hudHeight - 8;
1092
+ if (top < 10) {
1093
+ const belowTop = rect.top + rect.height + 8;
1094
+ if (belowTop + hudHeight <= vh - 10) {
1095
+ top = belowTop;
1096
+ }
1097
+ else {
1098
+ // Large container spanning viewport: place near top-left of visible area, stepping below cursor if close
1099
+ const insideTop = Math.max(10, rect.top + 8);
1100
+ if (Math.abs(this.lastPointerY - insideTop) < hudHeight + 18 &&
1101
+ this.lastPointerX < clampedLeft + hudWidth + 24) {
1102
+ top = Math.min(vh - hudHeight - 10, this.lastPointerY + 22);
1103
+ }
1104
+ else {
1105
+ top = insideTop;
1106
+ }
1107
+ }
1108
+ }
1109
+ this.hudEl.style.left = `${Math.round(clampedLeft)}px`;
1110
+ this.hudEl.style.top = `${Math.round(Math.max(10, top))}px`;
1111
+ }
1112
+ updateOverlayPosition() {
1113
+ if (!this.overlayEl || !this.hudEl)
1114
+ return;
1115
+ const currentCandidate = this.getActiveCandidate();
1116
+ if (!currentCandidate) {
1117
+ this.overlayEl.style.display = "none";
1118
+ this.hudEl.style.display = "none";
1119
+ if (this.leafOutlineEl)
1120
+ this.leafOutlineEl.style.display = "none";
1121
+ return;
1122
+ }
1123
+ const rect = currentCandidate.element.getBoundingClientRect();
1124
+ if (rect.width <= 0 || rect.height <= 0) {
1125
+ this.overlayEl.style.display = "none";
1126
+ this.hudEl.style.display = "none";
1127
+ return;
1128
+ }
1129
+ this.overlayEl.style.display = "block";
1130
+ this.overlayEl.style.left = `${rect.left}px`;
1131
+ this.overlayEl.style.top = `${rect.top}px`;
1132
+ this.overlayEl.style.width = `${rect.width}px`;
1133
+ this.overlayEl.style.height = `${rect.height}px`;
1134
+ this.hudEl.style.display = "block";
1135
+ if (this.isSmartMode()) {
1136
+ this.renderSmartHudContent(currentCandidate);
1137
+ }
1138
+ else if (this.isScrubbingLayers && this.candidateStack.length > 1) {
1139
+ this.hudEl.style.borderRadius = "10px";
1140
+ this.renderScrubbingHudContent(currentCandidate, rect);
1141
+ }
1142
+ else {
1143
+ this.renderCompactHudContent(currentCandidate, rect);
1144
+ }
1145
+ this.positionHudElement(rect);
1146
+ // Show ghost leaf outline only in verbose inspector mode when inspecting an ancestor/overlap layer
1147
+ if (this.leafOutlineEl) {
1148
+ if (!this.isSmartMode() &&
1149
+ this.activeCandidateIndex > 0 &&
1150
+ this.leafElement &&
1151
+ this.leafElement !== currentCandidate.element) {
1152
+ const leafRect = this.leafElement.getBoundingClientRect();
1153
+ this.leafOutlineEl.style.display = "block";
1154
+ this.leafOutlineEl.style.left = `${leafRect.left}px`;
1155
+ this.leafOutlineEl.style.top = `${leafRect.top}px`;
1156
+ this.leafOutlineEl.style.width = `${leafRect.width}px`;
1157
+ this.leafOutlineEl.style.height = `${leafRect.height}px`;
1158
+ }
1159
+ else {
1160
+ this.leafOutlineEl.style.display = "none";
1161
+ }
1162
+ }
1163
+ }
1164
+ triggerHudBoundaryNudge(offsetY) {
1165
+ if (!this.hudEl)
1166
+ return;
1167
+ this.hudEl.style.transform = `translateY(${offsetY}px)`;
1168
+ if (this.nudgeTimer !== null) {
1169
+ clearTimeout(this.nudgeTimer);
1170
+ }
1171
+ this.nudgeTimer = setTimeout(() => {
1172
+ if (this.hudEl) {
1173
+ this.hudEl.style.transform = "translateY(0)";
1174
+ }
1175
+ this.nudgeTimer = null;
1176
+ }, 120);
1177
+ }
1178
+ flashSelectionPulse(kind) {
1179
+ if (!this.overlayEl)
1180
+ return;
1181
+ if (kind === "pinned") {
1182
+ this.overlayEl.style.borderColor = "#15803d";
1183
+ this.overlayEl.style.background = "rgba(21, 128, 61, 0.14)";
1184
+ }
1185
+ else {
1186
+ this.overlayEl.style.borderColor = "#c2410c";
1187
+ this.overlayEl.style.background = "rgba(194, 65, 12, 0.12)";
1188
+ }
1189
+ if (this.pulseTimer !== null) {
1190
+ clearTimeout(this.pulseTimer);
1191
+ }
1192
+ this.pulseTimer = setTimeout(() => {
1193
+ if (this.overlayEl) {
1194
+ this.overlayEl.style.borderColor = "#1d4ed8";
1195
+ this.overlayEl.style.background = "rgba(29, 78, 216, 0.10)";
1196
+ }
1197
+ this.pulseTimer = null;
1198
+ }, 280);
1199
+ }
1200
+ showCursorToast(selector, kind, x, y) {
1201
+ if (!this.toastEl || typeof window === "undefined")
1202
+ return;
1203
+ this.toastEl.innerHTML = "";
1204
+ const iconBadge = document.createElement("span");
1205
+ iconBadge.style.cssText = [
1206
+ "display: inline-flex",
1207
+ "align-items: center",
1208
+ "justify-content: center",
1209
+ "width: 16px",
1210
+ "height: 16px",
1211
+ "border-radius: 9999px",
1212
+ "background: " + (kind === "pinned" ? "#15803d" : "#4b5262"),
1213
+ "color: #faf9f6",
1214
+ "flex-shrink: 0"
1215
+ ].join(";");
1216
+ iconBadge.innerHTML = ICON_CHECK;
1217
+ this.toastEl.appendChild(iconBadge);
1218
+ const primaryText = document.createElement("span");
1219
+ primaryText.textContent = kind === "pinned" ? "Pinned " : "Removed ";
1220
+ this.toastEl.appendChild(primaryText);
1221
+ const monoSel = document.createElement("span");
1222
+ monoSel.textContent = selector;
1223
+ monoSel.style.cssText =
1224
+ "font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; font-weight: 600; color: #faf9f6; max-width: 160px; overflow: hidden; text-overflow: ellipsis;";
1225
+ this.toastEl.appendChild(monoSel);
1226
+ if (kind === "pinned") {
1227
+ const nextHint = document.createElement("span");
1228
+ nextHint.style.cssText =
1229
+ "display: inline-flex; align-items: center; gap: 4px; color: #cbd5e1; font-size: 10.5px; margin-left: 2px;";
1230
+ nextHint.appendChild(this.createSpanEl("· Click more or"));
1231
+ nextHint.appendChild(this.createKbdEl("Esc"));
1232
+ this.toastEl.appendChild(nextHint);
1233
+ }
1234
+ this.toastEl.style.display = "inline-flex";
1235
+ const vw = window.innerWidth || 1024;
1236
+ const vh = window.innerHeight || 768;
1237
+ const left = Math.max(10, Math.min(x + 14, vw - 310));
1238
+ const top = Math.max(10, Math.min(y + 16, vh - 44));
1239
+ this.toastEl.style.left = `${Math.round(left)}px`;
1240
+ this.toastEl.style.top = `${Math.round(top)}px`;
1241
+ if (this.toastTimer !== null) {
1242
+ clearTimeout(this.toastTimer);
1243
+ }
1244
+ this.toastTimer = setTimeout(() => {
1245
+ if (this.toastEl) {
1246
+ this.toastEl.style.display = "none";
1247
+ }
1248
+ this.toastTimer = null;
1249
+ }, 1450);
1250
+ }
1251
+ commitCandidateSelection(el, x, y) {
1252
+ const smartMode = this.isSmartMode();
1253
+ const existingPinned = this.findPinnedIndex(el);
1254
+ if (existingPinned && this.callbacks.onUnselect) {
1255
+ this.callbacks.onUnselect(existingPinned.entry.clientRef, el);
1256
+ this.lastAction = { kind: "unpinned", selector: existingPinned.entry.selector };
1257
+ this.isScrubbingLayers = false;
1258
+ this.refreshPinnedMarkers();
1259
+ this.flashSelectionPulse("unpinned");
1260
+ if (!smartMode) {
1261
+ this.showCursorToast(existingPinned.entry.selector, "unpinned", x, y);
1262
+ }
1263
+ this.updateOverlayPosition();
1264
+ this.notifyStateChange();
1265
+ return;
1266
+ }
1267
+ const excluded = this.callbacks.getExcludedSelectors();
1268
+ const extraMeta = {
1269
+ selection_mode: smartMode ? "smart" : "verbose"
1270
+ };
1271
+ if (smartMode && this.leafElement && this.leafElement !== el) {
1272
+ extraMeta.original_target_selector = computeSelector(this.leafElement);
1273
+ }
1274
+ const snapshot = captureElementSnapshot(el, excluded, undefined, extraMeta);
1275
+ if (!snapshot)
1276
+ return;
1277
+ this.callbacks.onSelect(snapshot, el);
1278
+ this.lastAction = { kind: "pinned", selector: snapshot.selector };
1279
+ this.isScrubbingLayers = false;
1280
+ this.refreshPinnedMarkers();
1281
+ // In Production ("smart") click-select mode, immediately return to the quick feedback card
1282
+ // instead of keeping the user inside a multi-pin inspector loop.
1283
+ if ((smartMode && !this.suppressCursorPin) || !this.callbacks.canSelectMore()) {
1284
+ this.stop();
1285
+ return;
1286
+ }
1287
+ this.flashSelectionPulse("pinned");
1288
+ if (!smartMode) {
1289
+ this.showCursorToast(snapshot.selector, "pinned", x, y);
1290
+ }
1291
+ this.updateOverlayPosition();
1292
+ this.notifyStateChange();
1293
+ }
1294
+ handleWheelCapture(event) {
1295
+ if (!this.active)
1296
+ return;
1297
+ if (this.isSmartMode())
1298
+ return;
1299
+ if (this.isInternalUiEvent(event))
1300
+ return;
1301
+ if (event.altKey || event.ctrlKey || event.metaKey)
1302
+ return;
1303
+ if (this.candidateStack.length <= 1) {
1304
+ return;
1305
+ }
1306
+ event.preventDefault();
1307
+ event.stopPropagation();
1308
+ const normalizedDelta = event.deltaMode === 1 ? event.deltaY * 32 : event.deltaY;
1309
+ if (normalizedDelta === 0)
1310
+ return;
1311
+ // Reset accumulator immediately when the user reverses scroll direction
1312
+ if (this.wheelAccumulator * normalizedDelta < 0) {
1313
+ this.wheelAccumulator = 0;
1314
+ }
1315
+ this.wheelAccumulator += normalizedDelta;
1316
+ if (Math.abs(this.wheelAccumulator) < WHEEL_STEP_THRESHOLD) {
1317
+ return;
1318
+ }
1319
+ const now = Date.now();
1320
+ if (now - this.lastWheelStepAt < WHEEL_STEP_COOLDOWN_MS) {
1321
+ this.wheelAccumulator = 0;
1322
+ return;
1323
+ }
1324
+ const step = this.wheelAccumulator > 0 ? 1 : -1;
1325
+ this.wheelAccumulator = 0;
1326
+ this.lastWheelStepAt = now;
1327
+ this.cycleDepth(step, true);
1328
+ }
1329
+ handlePointerSuppression(event) {
1330
+ if (!this.active)
1331
+ return;
1332
+ if (this.isInternalUiEvent(event)) {
1333
+ return;
1334
+ }
1335
+ // Intercept host page pointer events in capture phase so links/buttons do not fire
1336
+ event.preventDefault();
1337
+ event.stopPropagation();
1338
+ if (typeof event.stopImmediatePropagation === "function") {
1339
+ event.stopImmediatePropagation();
1340
+ }
1341
+ }
1342
+ handleBlurSuppression(event) {
1343
+ if (!this.active)
1344
+ return;
1345
+ if (this.isInternalUiEvent(event)) {
1346
+ return;
1347
+ }
1348
+ // Freeze DOM states: suppress blur / focusout on host elements while picker is active
1349
+ // so transient popovers, tooltips, flyout menus, and modals remain open
1350
+ event.stopPropagation();
1351
+ if (typeof event.stopImmediatePropagation === "function") {
1352
+ event.stopImmediatePropagation();
1353
+ }
1354
+ }
1355
+ handleClickCapture(event) {
1356
+ if (!this.active)
1357
+ return;
1358
+ if (this.isInternalUiEvent(event)) {
1359
+ return;
1360
+ }
1361
+ event.preventDefault();
1362
+ event.stopPropagation();
1363
+ if (typeof event.stopImmediatePropagation === "function") {
1364
+ event.stopImmediatePropagation();
1365
+ }
1366
+ this.lastPointerX = event.clientX;
1367
+ this.lastPointerY = event.clientY;
1368
+ this.updateCandidatesFromPoint(event.clientX, event.clientY, event);
1369
+ const activeCandidate = this.getActiveCandidate();
1370
+ if (!activeCandidate)
1371
+ return;
1372
+ this.commitCandidateSelection(activeCandidate.element, event.clientX, event.clientY);
1373
+ }
1374
+ handleKeyDown(event) {
1375
+ if (!this.active)
1376
+ return;
1377
+ if (event.key === "Escape") {
1378
+ event.preventDefault();
1379
+ event.stopPropagation();
1380
+ this.stop();
1381
+ return;
1382
+ }
1383
+ // Alt+Shift+P / Option+Shift+P toggle / exit Freeze & Pin mode
1384
+ if (event.altKey &&
1385
+ event.shiftKey &&
1386
+ (event.code === "KeyP" || event.key.toLowerCase() === "p")) {
1387
+ event.preventDefault();
1388
+ event.stopPropagation();
1389
+ this.stop();
1390
+ return;
1391
+ }
1392
+ // Space key freezes & pins the currently hovered element without triggering mouse clicks
1393
+ if (event.key === " " || event.key === "Spacebar" || event.code === "Space") {
1394
+ if (this.isInternalUiEvent(event))
1395
+ return;
1396
+ const candidate = this.getActiveCandidate();
1397
+ if (candidate) {
1398
+ event.preventDefault();
1399
+ event.stopPropagation();
1400
+ if (typeof event.stopImmediatePropagation === "function") {
1401
+ event.stopImmediatePropagation();
1402
+ }
1403
+ this.commitCandidateSelection(candidate.element, this.lastPointerX || 80, this.lastPointerY || 80);
1404
+ }
1405
+ return;
1406
+ }
1407
+ if (!this.isSmartMode() && (event.key === "ArrowUp" || event.key === "[")) {
1408
+ event.preventDefault();
1409
+ event.stopPropagation();
1410
+ this.cycleDepth(1, true);
1411
+ return;
1412
+ }
1413
+ if (!this.isSmartMode() && (event.key === "ArrowDown" || event.key === "]")) {
1414
+ event.preventDefault();
1415
+ event.stopPropagation();
1416
+ this.cycleDepth(-1, true);
1417
+ return;
1418
+ }
1419
+ if (event.key === "Enter") {
1420
+ const candidate = this.getActiveCandidate();
1421
+ if (candidate) {
1422
+ event.preventDefault();
1423
+ event.stopPropagation();
1424
+ this.commitCandidateSelection(candidate.element, this.lastPointerX || 80, this.lastPointerY || 80);
1425
+ }
1426
+ }
1427
+ }
1428
+ }