context_drop 0.1.2 → 0.1.4

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