autokast 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +165 -0
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +300 -0
  5. package/dist/index.d.ts +4 -0
  6. package/dist/index.js +4 -0
  7. package/dist/inspect.d.ts +26 -0
  8. package/dist/inspect.js +193 -0
  9. package/dist/record/animations.d.ts +7 -0
  10. package/dist/record/animations.js +34 -0
  11. package/dist/record/capture.d.ts +85 -0
  12. package/dist/record/capture.js +202 -0
  13. package/dist/record/cast.d.ts +142 -0
  14. package/dist/record/cast.js +680 -0
  15. package/dist/record/cursor-style.d.ts +33 -0
  16. package/dist/record/cursor-style.js +252 -0
  17. package/dist/record/defaults.d.ts +96 -0
  18. package/dist/record/defaults.js +125 -0
  19. package/dist/record/editor.d.ts +11 -0
  20. package/dist/record/editor.js +48 -0
  21. package/dist/record/frame-pump.d.ts +91 -0
  22. package/dist/record/frame-pump.js +306 -0
  23. package/dist/record/index.d.ts +11 -0
  24. package/dist/record/index.js +287 -0
  25. package/dist/record/motion.d.ts +48 -0
  26. package/dist/record/motion.js +112 -0
  27. package/dist/record/pace.d.ts +69 -0
  28. package/dist/record/pace.js +133 -0
  29. package/dist/record/random.d.ts +12 -0
  30. package/dist/record/random.js +24 -0
  31. package/dist/record/scroll.d.ts +71 -0
  32. package/dist/record/scroll.js +249 -0
  33. package/dist/record/timeline.d.ts +34 -0
  34. package/dist/record/timeline.js +44 -0
  35. package/dist/render/background.d.ts +12 -0
  36. package/dist/render/background.js +209 -0
  37. package/dist/render/browser.d.ts +46 -0
  38. package/dist/render/browser.js +223 -0
  39. package/dist/render/camera.d.ts +119 -0
  40. package/dist/render/camera.js +372 -0
  41. package/dist/render/captions.d.ts +114 -0
  42. package/dist/render/captions.js +254 -0
  43. package/dist/render/color.d.ts +23 -0
  44. package/dist/render/color.js +78 -0
  45. package/dist/render/compositor.d.ts +185 -0
  46. package/dist/render/compositor.js +656 -0
  47. package/dist/render/cursor.d.ts +85 -0
  48. package/dist/render/cursor.js +314 -0
  49. package/dist/render/cursors/shapes.d.ts +34 -0
  50. package/dist/render/cursors/shapes.js +268 -0
  51. package/dist/render/defaults.d.ts +91 -0
  52. package/dist/render/defaults.js +139 -0
  53. package/dist/render/easing.d.ts +13 -0
  54. package/dist/render/easing.js +40 -0
  55. package/dist/render/effects.d.ts +39 -0
  56. package/dist/render/effects.js +91 -0
  57. package/dist/render/encoder.d.ts +52 -0
  58. package/dist/render/encoder.js +226 -0
  59. package/dist/render/index.d.ts +19 -0
  60. package/dist/render/index.js +138 -0
  61. package/dist/render/keycaps.d.ts +67 -0
  62. package/dist/render/keycaps.js +270 -0
  63. package/dist/render/scene.d.ts +59 -0
  64. package/dist/render/scene.js +230 -0
  65. package/dist/render/source.d.ts +21 -0
  66. package/dist/render/source.js +57 -0
  67. package/dist/render/spring.d.ts +11 -0
  68. package/dist/render/spring.js +47 -0
  69. package/dist/render/stage.d.ts +59 -0
  70. package/dist/render/stage.js +91 -0
  71. package/dist/render/timeline.d.ts +70 -0
  72. package/dist/render/timeline.js +157 -0
  73. package/dist/session.d.ts +14 -0
  74. package/dist/session.js +33 -0
  75. package/dist/types.d.ts +708 -0
  76. package/dist/types.js +1 -0
  77. package/package.json +61 -3
  78. package/skills/autokast/SKILL.md +300 -0
  79. package/skills/autokast/reference/jupyterlab.md +100 -0
@@ -0,0 +1,112 @@
1
+ export function clamp(value, min, max) {
2
+ return Math.min(max, Math.max(min, value));
3
+ }
4
+ /**
5
+ * Duration of a cursor move in milliseconds (a Fitts-like log law). Zero below one pixel.
6
+ */
7
+ export function moveDuration(distance, timing) {
8
+ if (distance < 1) {
9
+ return 0;
10
+ }
11
+ const base = (210 + 120 * Math.log2(1 + distance / 60)) / timing.speed;
12
+ return clamp(base, timing.minDuration, timing.maxDuration);
13
+ }
14
+ /**
15
+ * Position profile of a move: minimum jerk, time-warped so the peak speed comes at about 39%
16
+ * of the move and the deceleration is longer than the acceleration, as in human reaching.
17
+ */
18
+ export function moveEase(progress) {
19
+ const w = clamp(progress, 0, 1) ** 0.8;
20
+ return w * w * w * (10 - 15 * w + 6 * w * w);
21
+ }
22
+ /**
23
+ * Position profile of a drag: minimum jerk, time-warped so the start is gentle (peak speed
24
+ * at about 56% of the move). A dragged element can then follow the cursor.
25
+ */
26
+ export function dragEase(progress) {
27
+ const w = clamp(progress, 0, 1) ** 1.2;
28
+ return w * w * w * (10 - 15 * w + 6 * w * w);
29
+ }
30
+ /**
31
+ * Smooth acceleration and deceleration, for scrolling.
32
+ */
33
+ export function easeInOutCubic(progress) {
34
+ const u = clamp(progress, 0, 1);
35
+ return u < 0.5 ? 4 * u * u * u : 1 - (-2 * u + 2) ** 3 / 2;
36
+ }
37
+ function bezier(p0, p1, p2, p3, s) {
38
+ const r = 1 - s;
39
+ const a = r * r * r;
40
+ const b = 3 * r * r * s;
41
+ const c = 3 * r * s * s;
42
+ const d = s * s * s;
43
+ return {
44
+ x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,
45
+ y: a * p0.y + b * p1.y + c * p2.y + d * p3.y,
46
+ };
47
+ }
48
+ function inside(point, bounds) {
49
+ return point.x >= 0 && point.y >= 0 && point.x <= bounds.width && point.y <= bounds.height;
50
+ }
51
+ /**
52
+ * A slightly curved path from `from` to `to`: a cubic Bezier whose control points are offset
53
+ * perpendicular to the straight line by up to `curvature` times the distance. The bend
54
+ * flips side when it would leave `bounds`. The path starts and ends exactly on the points.
55
+ */
56
+ export function cursorPath(from, to, curvature, random, bounds) {
57
+ const dx = to.x - from.x;
58
+ const dy = to.y - from.y;
59
+ const distance = Math.hypot(dx, dy);
60
+ const sign = random() < 0.5 ? -1 : 1;
61
+ const bend = curvature * distance * (0.2 + 0.4 * random());
62
+ const k1 = bend * (0.8 + 0.4 * random());
63
+ const k2 = bend * (0.8 + 0.4 * random());
64
+ const f1 = 0.25 + 0.1 * random();
65
+ const f2 = 0.65 + 0.1 * random();
66
+ if (distance < 1) {
67
+ return (s) => (s >= 1 ? { ...to } : { ...from });
68
+ }
69
+ const nx = -dy / distance;
70
+ const ny = dx / distance;
71
+ const controls = (side) => [
72
+ { x: from.x + dx * f1 + nx * k1 * side, y: from.y + dy * f1 + ny * k1 * side },
73
+ { x: from.x + dx * f2 + nx * k2 * side, y: from.y + dy * f2 + ny * k2 * side },
74
+ ];
75
+ let [c1, c2] = controls(sign);
76
+ if (bounds && !inside(bezier(from, c1, c2, to, 0.5), bounds)) {
77
+ [c1, c2] = controls(-sign);
78
+ }
79
+ return (s) => {
80
+ if (s <= 0) {
81
+ return { ...from };
82
+ }
83
+ if (s >= 1) {
84
+ return { ...to };
85
+ }
86
+ const p = bezier(from, c1, c2, to, s);
87
+ return bounds ? { x: clamp(p.x, 0, bounds.width), y: clamp(p.y, 0, bounds.height) } : p;
88
+ };
89
+ }
90
+ /**
91
+ * Delay before typing the next character: the mean delay with random variation,
92
+ * a bit longer after a space, punctuation or a new line.
93
+ */
94
+ export function typingDelay(previous, delay, variance, random) {
95
+ let ms = delay * (1 + clamp(variance, 0, 1) * (random() * 2 - 1));
96
+ if (previous === '\n') {
97
+ ms *= 2;
98
+ }
99
+ else if (/[.,;:!?]/.test(previous)) {
100
+ ms *= 1.6;
101
+ }
102
+ else if (previous === ' ') {
103
+ ms *= 1.25;
104
+ }
105
+ return Math.max(0, ms);
106
+ }
107
+ /**
108
+ * Default duration of a smooth scroll over `distance` pixels.
109
+ */
110
+ export function scrollDuration(distance) {
111
+ return clamp(300 + distance * 0.6, 400, 900);
112
+ }
@@ -0,0 +1,69 @@
1
+ import type { FramePump } from './frame-pump.ts';
2
+ import { type Clock } from './timeline.ts';
3
+ /**
4
+ * Sends one input to the page.
5
+ */
6
+ export type Send = () => Promise<unknown>;
7
+ /**
8
+ * Record time, waits and input for the Cast calls. Realtime uses the wall clock;
9
+ * frame-perfect uses page time, which the frame pump advances one frame at a time.
10
+ */
11
+ export interface Pace {
12
+ /**
13
+ * Multiplier for the default Cast timeout: Playwright waits in real time.
14
+ */
15
+ readonly timeoutScale: number;
16
+ /**
17
+ * Record time in milliseconds.
18
+ */
19
+ now(): number;
20
+ /**
21
+ * Waits for `ms` of record time.
22
+ */
23
+ sleep(ms: number): Promise<void>;
24
+ /**
25
+ * Sends one input. Resolves with the record time at which it reaches the page.
26
+ */
27
+ input(send: Send): Promise<number>;
28
+ /**
29
+ * Calls `step` with the progress (0 to 1) and the record time until the progress is 1:
30
+ * about every `tick` ms in realtime, once per frame in frame-perfect mode. `step` returns
31
+ * the input for that moment, with values that it computes when the input is sent.
32
+ */
33
+ animate(duration: number, tick: number, step: (progress: number, t: number) => Send | undefined): Promise<void>;
34
+ /**
35
+ * Turns frame capture on or off: off inside cut regions. Turning it on captures a frame
36
+ * and resolves with its record time, so that the end of a cut has its frame.
37
+ */
38
+ capture(on: boolean): Promise<number>;
39
+ }
40
+ /**
41
+ * Wall-clock pace: record time is real time since the capture started.
42
+ */
43
+ export declare class RealtimePace implements Pace {
44
+ readonly timeoutScale = 1;
45
+ private readonly clock;
46
+ private readonly refresh;
47
+ /**
48
+ * `refresh` captures the page now and gives the record time of that frame.
49
+ */
50
+ constructor(clock: Clock, refresh?: () => Promise<number | undefined>);
51
+ now(): number;
52
+ sleep(ms: number): Promise<void>;
53
+ input(send: Send): Promise<number>;
54
+ animate(duration: number, tick: number, step: (progress: number, t: number) => Send | undefined): Promise<void>;
55
+ capture(on: boolean): Promise<number>;
56
+ }
57
+ /**
58
+ * Page-time pace: record time is `frame * frameMs`, and input goes out once per frame.
59
+ */
60
+ export declare class FramePerfectPace implements Pace {
61
+ readonly timeoutScale = 5;
62
+ private readonly pump;
63
+ constructor(pump: FramePump);
64
+ now(): number;
65
+ sleep(ms: number): Promise<void>;
66
+ input(send: Send): Promise<number>;
67
+ animate(duration: number, tick: number, step: (progress: number, t: number) => Send | undefined): Promise<void>;
68
+ capture(on: boolean): Promise<number>;
69
+ }
@@ -0,0 +1,133 @@
1
+ import { sleep } from "./timeline.js";
2
+ /**
3
+ * Sends input one dispatch at a time and skips new ones while one is in flight.
4
+ * Chromium acknowledges mouse input only at its next frame, so waiting for each dispatch
5
+ * would tie the motion to the frame rate of the page.
6
+ */
7
+ class LatestDispatch {
8
+ /**
9
+ * The last dispatch that started.
10
+ */
11
+ last;
12
+ busy = false;
13
+ pending = Promise.resolve();
14
+ offer(send) {
15
+ if (this.busy) {
16
+ return;
17
+ }
18
+ this.busy = true;
19
+ this.last = send;
20
+ this.pending = send();
21
+ // After a failure `busy` stays true: no more dispatches, and settle() throws.
22
+ this.pending.then(() => {
23
+ this.busy = false;
24
+ }, () => { });
25
+ }
26
+ /**
27
+ * Waits for the dispatch in flight. Throws its error if it failed.
28
+ */
29
+ async settle() {
30
+ await this.pending;
31
+ }
32
+ }
33
+ /**
34
+ * Wall-clock pace: record time is real time since the capture started.
35
+ */
36
+ export class RealtimePace {
37
+ timeoutScale = 1;
38
+ clock;
39
+ refresh;
40
+ /**
41
+ * `refresh` captures the page now and gives the record time of that frame.
42
+ */
43
+ constructor(clock, refresh = async () => undefined) {
44
+ this.clock = clock;
45
+ this.refresh = refresh;
46
+ }
47
+ now() {
48
+ return this.clock.now();
49
+ }
50
+ sleep(ms) {
51
+ return sleep(ms);
52
+ }
53
+ async input(send) {
54
+ const t = this.now();
55
+ await send();
56
+ return t;
57
+ }
58
+ async animate(duration, tick, step) {
59
+ const dispatch = new LatestDispatch();
60
+ let last;
61
+ const start = this.now();
62
+ let next = start;
63
+ for (;;) {
64
+ const now = this.now();
65
+ const progress = duration > 0 ? Math.min(1, (now - start) / duration) : 1;
66
+ const send = step(progress, now);
67
+ if (send) {
68
+ last = send;
69
+ dispatch.offer(send);
70
+ }
71
+ if (progress >= 1) {
72
+ break;
73
+ }
74
+ next += tick;
75
+ const wait = next - this.now();
76
+ if (wait > 0) {
77
+ await sleep(wait);
78
+ }
79
+ else {
80
+ next = this.now();
81
+ }
82
+ }
83
+ await dispatch.settle();
84
+ // The final step may have been skipped while an earlier one was in flight.
85
+ if (last && dispatch.last !== last) {
86
+ await last();
87
+ }
88
+ }
89
+ async capture(on) {
90
+ const t = this.now();
91
+ if (on) {
92
+ // The forced capture keeps the time of its request: the call need not wait for it.
93
+ void this.refresh();
94
+ }
95
+ return t;
96
+ }
97
+ }
98
+ /**
99
+ * Page-time pace: record time is `frame * frameMs`, and input goes out once per frame.
100
+ */
101
+ export class FramePerfectPace {
102
+ timeoutScale = 5;
103
+ pump;
104
+ constructor(pump) {
105
+ this.pump = pump;
106
+ }
107
+ now() {
108
+ return this.pump.now();
109
+ }
110
+ sleep(ms) {
111
+ return this.pump.wait(ms);
112
+ }
113
+ input(send) {
114
+ return this.pump.input(send);
115
+ }
116
+ async animate(duration, tick, step) {
117
+ const start = this.now();
118
+ let progress = 0;
119
+ while (progress < 1) {
120
+ await this.pump.input((t) => {
121
+ progress = duration > 0 ? Math.min(1, (t - start) / duration) : 1;
122
+ return step(progress, t)?.();
123
+ });
124
+ }
125
+ }
126
+ async capture(on) {
127
+ this.pump.capture(on);
128
+ if (on) {
129
+ await this.pump.wait(this.pump.frameMs);
130
+ }
131
+ return this.now();
132
+ }
133
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A source of random floats in [0, 1).
3
+ */
4
+ export type Random = () => number;
5
+ /**
6
+ * Seeded pseudo-random generator (mulberry32), so that a seed gives the same recording.
7
+ */
8
+ export declare function createRandom(seed: number): Random;
9
+ /**
10
+ * 32-bit FNV-1a hash of a string.
11
+ */
12
+ export declare function hashString(text: string): number;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Seeded pseudo-random generator (mulberry32), so that a seed gives the same recording.
3
+ */
4
+ export function createRandom(seed) {
5
+ let state = seed >>> 0;
6
+ return () => {
7
+ state = (state + 0x6d2b79f5) >>> 0;
8
+ let t = state;
9
+ t = Math.imul(t ^ (t >>> 15), t | 1);
10
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
11
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12
+ };
13
+ }
14
+ /**
15
+ * 32-bit FNV-1a hash of a string.
16
+ */
17
+ export function hashString(text) {
18
+ let hash = 0x811c9dc5;
19
+ for (let i = 0; i < text.length; i++) {
20
+ hash ^= text.charCodeAt(i);
21
+ hash = Math.imul(hash, 0x01000193);
22
+ }
23
+ return hash >>> 0;
24
+ }
@@ -0,0 +1,71 @@
1
+ import type { Point, Rect } from '../types.ts';
2
+ export type Block = 'start' | 'center' | 'end' | 'nearest';
3
+ export interface ScrollPlanArgs {
4
+ block: Block;
5
+ /**
6
+ * Leaves a container as it is when the target is already fully visible in it.
7
+ */
8
+ ifNeeded: boolean;
9
+ cursor: Point;
10
+ /**
11
+ * Plans for this viewport rectangle and the page scroll only, instead of the element.
12
+ */
13
+ rect?: Rect;
14
+ }
15
+ /**
16
+ * The next wheel step that brings a target into view.
17
+ */
18
+ export interface ScrollPlan {
19
+ /**
20
+ * Wheel delta in CSS pixels. Zero when no container has to scroll.
21
+ */
22
+ dx: number;
23
+ dy: number;
24
+ /**
25
+ * Where the cursor must be so that the wheel scrolls the right container. Null when the
26
+ * wheel cannot reach that container.
27
+ */
28
+ point: Point | null;
29
+ /**
30
+ * Depth of the container to scroll: 0 is the outermost one.
31
+ */
32
+ level: number;
33
+ /**
34
+ * The target is visible in the viewport and in all its clipping ancestors.
35
+ */
36
+ inView: boolean;
37
+ /**
38
+ * The target is in a child frame, which the planner does not handle.
39
+ */
40
+ inFrame: boolean;
41
+ /**
42
+ * The bounding box of the target now, in viewport CSS pixels.
43
+ */
44
+ box: Rect;
45
+ /**
46
+ * A running animation or transition on the target or an ancestor can move it.
47
+ */
48
+ moving: boolean;
49
+ }
50
+ /**
51
+ * In-page: plans the next wheel step that brings `element` into view. It simulates the
52
+ * scroll of each scroll container, inner to outer, like `scrollIntoView`, then returns the
53
+ * step of the outermost container that must move, so that inner containers come into view
54
+ * first. Playwright serializes this function into the page: it must be self-contained.
55
+ */
56
+ export declare function planScroll(element: Element, args: ScrollPlanArgs): ScrollPlan;
57
+ /**
58
+ * In-page: whether a click at `point` reaches `element` now: the deepest element there is
59
+ * the element or inside it, and the element is not disabled. Also gives its current box.
60
+ * Self-contained, like `planScroll`.
61
+ */
62
+ export declare function hitCheck(element: Element, point: Point): {
63
+ box: Rect;
64
+ receives: boolean;
65
+ inFrame: boolean;
66
+ };
67
+ /**
68
+ * In-page: resolves when the scroll offsets of the element's ancestors stop changing,
69
+ * or after about 1.5 s.
70
+ */
71
+ export declare function scrollIdle(element: Element): Promise<void>;
@@ -0,0 +1,249 @@
1
+ /**
2
+ * In-page: plans the next wheel step that brings `element` into view. It simulates the
3
+ * scroll of each scroll container, inner to outer, like `scrollIntoView`, then returns the
4
+ * step of the outermost container that must move, so that inner containers come into view
5
+ * first. Playwright serializes this function into the page: it must be self-contained.
6
+ */
7
+ export function planScroll(element, args) {
8
+ const doc = element.ownerDocument;
9
+ const win = doc.defaultView;
10
+ const rect = element.getBoundingClientRect();
11
+ const current = { x: rect.left, y: rect.top, width: rect.width, height: rect.height };
12
+ const result = {
13
+ dx: 0,
14
+ dy: 0,
15
+ point: null,
16
+ level: 0,
17
+ inView: true,
18
+ inFrame: false,
19
+ box: current,
20
+ };
21
+ if (!win || win !== win.top) {
22
+ return { ...result, inFrame: true, moving: true };
23
+ }
24
+ const root = doc.scrollingElement ?? doc.documentElement;
25
+ const html = doc.documentElement;
26
+ // In quirks mode the scrolling element is <body>, and its client size is the viewport.
27
+ const viewport = { left: 0, top: 0, right: root.clientWidth, bottom: root.clientHeight };
28
+ const rootStyles = [html, doc.body].filter(Boolean).map((el) => getComputedStyle(el));
29
+ const parentOf = (node) => node.assignedSlot ?? node.parentElement ?? (node.getRootNode().host || null);
30
+ const portOf = (el) => {
31
+ if (el === root) {
32
+ return viewport;
33
+ }
34
+ const rect = el.getBoundingClientRect();
35
+ const left = rect.left + el.clientLeft;
36
+ const top = rect.top + el.clientTop;
37
+ return { left, top, right: left + el.clientWidth, bottom: top + el.clientHeight };
38
+ };
39
+ const intersect = (a, b) => ({
40
+ left: Math.max(a.left, b.left),
41
+ top: Math.max(a.top, b.top),
42
+ right: Math.min(a.right, b.right),
43
+ bottom: Math.min(a.bottom, b.bottom),
44
+ });
45
+ const range = (el, axis) => axis === 'y' ? el.scrollHeight - el.clientHeight : el.scrollWidth - el.clientWidth;
46
+ const scrollable = (el, axis) => {
47
+ if (range(el, axis) < 1) {
48
+ return false;
49
+ }
50
+ const key = axis === 'y' ? 'overflowY' : 'overflowX';
51
+ if (el === root) {
52
+ return !rootStyles.some((style) => /hidden|clip/.test(style[key]));
53
+ }
54
+ return /auto|scroll|overlay/.test(getComputedStyle(el)[key]);
55
+ };
56
+ const offset = (el, axis) => (axis === 'y' ? el.scrollTop : el.scrollLeft);
57
+ const room = (el, axis, sign) => {
58
+ if (!scrollable(el, axis)) {
59
+ return 0;
60
+ }
61
+ return sign > 0 ? range(el, axis) - offset(el, axis) : offset(el, axis);
62
+ };
63
+ const delta = (a0, a1, p0, p1, block, ifNeeded) => {
64
+ const size = a1 - a0;
65
+ const port = p1 - p0;
66
+ const margin = Math.max(0, Math.min(32, port * 0.1, (port - size) / 2));
67
+ const inside = a0 >= p0 - 0.5 && a1 <= p1 + 0.5;
68
+ if (inside && (ifNeeded || block === 'nearest')) {
69
+ return 0;
70
+ }
71
+ if (size > port || block === 'start') {
72
+ return a0 - (p0 + margin);
73
+ }
74
+ if (block === 'end') {
75
+ return a1 - (p1 - margin);
76
+ }
77
+ if (block === 'nearest') {
78
+ return a0 < p0 ? a0 - (p0 + margin) : a1 - (p1 - margin);
79
+ }
80
+ return (a0 + a1) / 2 - (p0 + p1) / 2;
81
+ };
82
+ const hit = (x, y) => {
83
+ let el = doc.elementFromPoint(x, y);
84
+ while (el?.shadowRoot) {
85
+ const inner = el.shadowRoot.elementFromPoint(x, y);
86
+ if (!inner || inner === el) {
87
+ break;
88
+ }
89
+ el = inner;
90
+ }
91
+ return el;
92
+ };
93
+ const clips = [];
94
+ const chain = [];
95
+ let fixed = false;
96
+ let node = args.rect ? null : parentOf(element);
97
+ const htmlVisible = getComputedStyle(html).overflowX === 'visible';
98
+ while (node && node !== root && node !== html) {
99
+ const style = getComputedStyle(node);
100
+ // Overflow on <body> moves to the viewport when <html> has none.
101
+ const clipping = (style.overflowX !== 'visible' || style.overflowY !== 'visible') &&
102
+ !(node === doc.body && htmlVisible);
103
+ if (clipping) {
104
+ clips.push(node);
105
+ if (scrollable(node, 'x') || scrollable(node, 'y')) {
106
+ chain.push(node);
107
+ }
108
+ }
109
+ if (style.position === 'fixed') {
110
+ fixed = true;
111
+ break;
112
+ }
113
+ node = parentOf(node);
114
+ }
115
+ if (!fixed) {
116
+ chain.push(root);
117
+ }
118
+ let moving = false;
119
+ for (let el = element; el && !moving; el = parentOf(el)) {
120
+ moving = el.getAnimations().some((a) => a.playState === 'running' || a.pending);
121
+ }
122
+ const target = args.rect
123
+ ? {
124
+ left: args.rect.x,
125
+ top: args.rect.y,
126
+ right: args.rect.x + args.rect.width,
127
+ bottom: args.rect.y + args.rect.height,
128
+ }
129
+ : (({ left, top, right, bottom }) => ({ left, top, right, bottom }))(element.getBoundingClientRect());
130
+ const region = clips.map(portOf).reduce(intersect, viewport);
131
+ const seen = intersect(target, region);
132
+ const fits = (a0, a1, s0, s1, r0, r1) => s1 - s0 >= Math.min(a1 - a0, r1 - r0) - 1;
133
+ const inView = fits(target.top, target.bottom, seen.top, seen.bottom, region.top, region.bottom) &&
134
+ fits(target.left, target.right, seen.left, seen.right, region.left, region.right);
135
+ let box = { ...target };
136
+ const steps = chain.map((el) => {
137
+ const port = portOf(el);
138
+ const clamp = (d, axis) => scrollable(el, axis)
139
+ ? Math.max(-offset(el, axis), Math.min(range(el, axis) - offset(el, axis), d))
140
+ : 0;
141
+ const dy = clamp(delta(box.top, box.bottom, port.top, port.bottom, args.block, args.ifNeeded), 'y');
142
+ const dx = clamp(delta(box.left, box.right, port.left, port.right, 'center', true), 'x');
143
+ box = {
144
+ left: box.left - dx,
145
+ right: box.right - dx,
146
+ top: box.top - dy,
147
+ bottom: box.bottom - dy,
148
+ };
149
+ return { el, dx, dy };
150
+ });
151
+ const index = steps.findLastIndex((step) => Math.abs(step.dx) >= 1 || Math.abs(step.dy) >= 1);
152
+ if (index < 0) {
153
+ return { ...result, inView, moving };
154
+ }
155
+ const step = steps[index];
156
+ const axis = Math.abs(step.dy) >= 1 ? 'y' : 'x';
157
+ const amount = axis === 'y' ? step.dy : step.dx;
158
+ const outer = step.el === root ? [] : clips.slice(clips.indexOf(step.el) + 1);
159
+ const area = [portOf(step.el), ...outer.map(portOf)].reduce(intersect, viewport);
160
+ const reaches = (p) => {
161
+ for (let el = hit(p.x, p.y); el; el = el === root ? null : parentOf(el)) {
162
+ if (room(el, axis, amount) >= 1) {
163
+ return el === step.el;
164
+ }
165
+ }
166
+ return false;
167
+ };
168
+ const candidates = [];
169
+ const grid = 7;
170
+ for (let i = 0; i < grid; i++) {
171
+ for (let j = 0; j < grid; j++) {
172
+ candidates.push({
173
+ x: area.left + ((area.right - area.left) * (i + 0.5)) / grid,
174
+ y: area.top + ((area.bottom - area.top) * (j + 0.5)) / grid,
175
+ });
176
+ }
177
+ }
178
+ const distance = (p) => Math.hypot(p.x - args.cursor.x, p.y - args.cursor.y);
179
+ candidates.sort((a, b) => distance(a) - distance(b));
180
+ const { x, y } = args.cursor;
181
+ if (x > area.left + 2 && x < area.right - 2 && y > area.top + 2 && y < area.bottom - 2) {
182
+ candidates.unshift(args.cursor);
183
+ }
184
+ const usable = area.right - area.left >= 4 && area.bottom - area.top >= 4;
185
+ return {
186
+ ...result,
187
+ dx: axis === 'x' ? amount : 0,
188
+ dy: axis === 'y' ? amount : 0,
189
+ point: usable ? (candidates.find(reaches) ?? null) : null,
190
+ level: chain.length - 1 - index,
191
+ inView,
192
+ moving,
193
+ };
194
+ }
195
+ /**
196
+ * In-page: whether a click at `point` reaches `element` now: the deepest element there is
197
+ * the element or inside it, and the element is not disabled. Also gives its current box.
198
+ * Self-contained, like `planScroll`.
199
+ */
200
+ export function hitCheck(element, point) {
201
+ const rect = element.getBoundingClientRect();
202
+ const box = { x: rect.left, y: rect.top, width: rect.width, height: rect.height };
203
+ const win = element.ownerDocument.defaultView;
204
+ if (!win || win !== win.top) {
205
+ return { box, receives: false, inFrame: true };
206
+ }
207
+ let hit = element.ownerDocument.elementFromPoint(point.x, point.y);
208
+ while (hit?.shadowRoot) {
209
+ const inner = hit.shadowRoot.elementFromPoint(point.x, point.y);
210
+ if (!inner || inner === hit) {
211
+ break;
212
+ }
213
+ hit = inner;
214
+ }
215
+ let inside = false;
216
+ for (let node = hit; node && !inside;) {
217
+ inside = node === element;
218
+ node = node.parentElement ?? (node.getRootNode().host || null);
219
+ }
220
+ const disabled = element.disabled === true ||
221
+ element.closest('fieldset[disabled]') !== null ||
222
+ element.getAttribute('aria-disabled') === 'true';
223
+ return { box, receives: inside && !disabled, inFrame: false };
224
+ }
225
+ /**
226
+ * In-page: resolves when the scroll offsets of the element's ancestors stop changing,
227
+ * or after about 1.5 s.
228
+ */
229
+ export async function scrollIdle(element) {
230
+ // Wheel scrolling applies each delta at once: no change over one frame means idle.
231
+ const parentOf = (node) => node.assignedSlot ?? node.parentElement ?? (node.getRootNode().host || null);
232
+ const ancestors = [];
233
+ for (let node = parentOf(element); node; node = parentOf(node)) {
234
+ ancestors.push(node);
235
+ }
236
+ const offsets = () => ancestors.map((el) => `${el.scrollLeft},${el.scrollTop}`).join(';');
237
+ let last = offsets();
238
+ for (let i = 0; i < 90; i++) {
239
+ await new Promise((resolve) => {
240
+ requestAnimationFrame(resolve);
241
+ setTimeout(resolve, 50);
242
+ });
243
+ const now = offsets();
244
+ if (now === last) {
245
+ return;
246
+ }
247
+ last = now;
248
+ }
249
+ }