@hyperframes/studio 0.7.45 → 0.7.47

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 (110) hide show
  1. package/dist/assets/index-Dq7FEg0K.css +1 -0
  2. package/dist/assets/{index-CZkdm5YL.js → index-DsckwbdW.js} +1 -1
  3. package/dist/assets/index-VsAbY3rl.js +423 -0
  4. package/dist/assets/{index-CbQKp0Ek.js → index-uvB30_yI.js} +1 -1
  5. package/dist/index.d.ts +96 -29
  6. package/dist/index.html +2 -2
  7. package/dist/index.js +8443 -4305
  8. package/dist/index.js.map +1 -1
  9. package/package.json +7 -7
  10. package/src/App.tsx +26 -32
  11. package/src/components/DesignPanelPromoteProvider.tsx +45 -0
  12. package/src/components/PanelTabButton.tsx +31 -0
  13. package/src/components/StudioPreviewArea.tsx +75 -3
  14. package/src/components/StudioRightPanel.tsx +111 -113
  15. package/src/components/editor/LayersPanel.tsx +2 -22
  16. package/src/components/editor/PromotableControl.tsx +105 -0
  17. package/src/components/editor/propertyPanelSections.tsx +42 -21
  18. package/src/components/panels/VariablesBindElement.tsx +266 -0
  19. package/src/components/panels/VariablesDeclarationForm.test.ts +135 -0
  20. package/src/components/panels/VariablesDeclarationForm.tsx +321 -0
  21. package/src/components/panels/VariablesOtherCompositions.tsx +159 -0
  22. package/src/components/panels/VariablesPanel.tsx +554 -0
  23. package/src/components/panels/VariablesRowAction.tsx +25 -0
  24. package/src/components/panels/VariablesValueControls.tsx +215 -0
  25. package/src/components/renders/useRenderQueue.ts +12 -0
  26. package/src/contexts/TimelineEditContext.tsx +7 -2
  27. package/src/contexts/VariablePromoteContext.tsx +138 -0
  28. package/src/contexts/variablePromoteHelpers.test.ts +124 -0
  29. package/src/contexts/variablePromoteHelpers.ts +53 -0
  30. package/src/contexts/variablePromoteIntegration.test.ts +80 -0
  31. package/src/hooks/previewVariablesStore.ts +34 -0
  32. package/src/hooks/timelineEditingHelpers.test.ts +104 -0
  33. package/src/hooks/timelineEditingHelpers.ts +386 -10
  34. package/src/hooks/useDomEditWiring.ts +9 -7
  35. package/src/hooks/useDomSelection.test.ts +36 -2
  36. package/src/hooks/useDomSelection.ts +56 -75
  37. package/src/hooks/useElementLifecycleOps.ts +57 -17
  38. package/src/hooks/usePreviewDocumentVersion.ts +27 -0
  39. package/src/hooks/useProjectCompositionVariables.ts +131 -0
  40. package/src/hooks/useStudioContextValue.ts +6 -1
  41. package/src/hooks/useStudioSdkSessions.ts +37 -0
  42. package/src/hooks/useTimelineEditing.test.tsx +963 -0
  43. package/src/hooks/useTimelineEditing.ts +122 -100
  44. package/src/hooks/useTimelineEditingTypes.ts +15 -0
  45. package/src/hooks/useTimelineGroupEditing.ts +370 -0
  46. package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +129 -0
  47. package/src/hooks/useTimelineSelectionPreviewSync.ts +130 -0
  48. package/src/hooks/useVariablesPersist.ts +61 -0
  49. package/src/player/components/BeatStrip.tsx +19 -3
  50. package/src/player/components/Player.tsx +7 -1
  51. package/src/player/components/Timeline.test.ts +38 -0
  52. package/src/player/components/Timeline.tsx +102 -80
  53. package/src/player/components/TimelineCanvas.tsx +340 -296
  54. package/src/player/components/TimelineClip.test.tsx +11 -0
  55. package/src/player/components/TimelineClipDiamonds.tsx +40 -9
  56. package/src/player/components/TimelineDragGhost.tsx +59 -0
  57. package/src/player/components/TimelineDropInsertionLine.tsx +32 -0
  58. package/src/player/components/TimelineLayerGroupHeader.tsx +97 -0
  59. package/src/player/components/TimelineLayerGutter.tsx +61 -0
  60. package/src/player/components/TimelineRuler.tsx +4 -2
  61. package/src/player/components/TimelineSelectionOverlays.tsx +55 -0
  62. package/src/player/components/timelineCallbacks.ts +20 -2
  63. package/src/player/components/timelineClipDragPreview.ts +52 -0
  64. package/src/player/components/timelineDragDrop.ts +19 -3
  65. package/src/player/components/timelineDropIndicator.test.ts +48 -0
  66. package/src/player/components/timelineDropIndicator.ts +33 -0
  67. package/src/player/components/timelineEditing.test.ts +251 -1
  68. package/src/player/components/timelineEditing.ts +178 -19
  69. package/src/player/components/timelineGroupEditing.ts +159 -0
  70. package/src/player/components/timelineLayerDrag.test.ts +200 -0
  71. package/src/player/components/timelineLayerDrag.ts +383 -0
  72. package/src/player/components/timelineLayout.test.ts +34 -0
  73. package/src/player/components/timelineLayout.ts +47 -0
  74. package/src/player/components/timelineMarqueeSelection.test.ts +87 -0
  75. package/src/player/components/timelineSnapTargets.test.ts +144 -0
  76. package/src/player/components/timelineSnapTargets.ts +164 -0
  77. package/src/player/components/timelineStacking.ts +62 -0
  78. package/src/player/components/timelineTrackOrder.test.ts +144 -0
  79. package/src/player/components/timelineTrackOrder.ts +173 -0
  80. package/src/player/components/useTimelineActiveClips.test.ts +20 -0
  81. package/src/player/components/useTimelineActiveClips.ts +15 -4
  82. package/src/player/components/useTimelineClipDrag.test.tsx +506 -0
  83. package/src/player/components/useTimelineClipDrag.ts +172 -126
  84. package/src/player/components/useTimelineClipGroupDrag.ts +417 -0
  85. package/src/player/components/useTimelineKeyframeHandlers.ts +73 -0
  86. package/src/player/components/useTimelineMarqueeSelection.test.tsx +231 -0
  87. package/src/player/components/useTimelineMarqueeSelection.ts +276 -0
  88. package/src/player/hooks/useExpandedTimelineElements.ts +3 -0
  89. package/src/player/hooks/useTimelinePlayer.test.ts +4 -1
  90. package/src/player/hooks/useTimelinePlayer.ts +7 -0
  91. package/src/player/lib/layerOrdering.test.ts +77 -0
  92. package/src/player/lib/layerOrdering.ts +98 -0
  93. package/src/player/lib/playbackTypes.ts +3 -0
  94. package/src/player/lib/timelineDOM.test.ts +103 -0
  95. package/src/player/lib/timelineDOM.ts +80 -0
  96. package/src/player/lib/timelineElementHelpers.ts +1 -1
  97. package/src/player/store/playerStore.test.ts +92 -0
  98. package/src/player/store/playerStore.ts +69 -9
  99. package/src/utils/blockInstaller.ts +2 -14
  100. package/src/utils/editHistory.test.ts +61 -0
  101. package/src/utils/editHistory.ts +7 -1
  102. package/src/utils/rootDuration.test.ts +34 -0
  103. package/src/utils/rootDuration.ts +17 -0
  104. package/src/utils/sdkCutover.ts +45 -0
  105. package/src/utils/studioHelpers.test.ts +31 -0
  106. package/src/utils/studioHelpers.ts +29 -1
  107. package/src/utils/studioUrlState.test.ts +29 -0
  108. package/src/utils/studioUrlState.ts +16 -1
  109. package/dist/assets/index-CZUbpYhQ.js +0 -416
  110. package/dist/assets/index-DoVLXke0.css +0 -1
@@ -0,0 +1,231 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import React, { act } from "react";
4
+ import { createRoot } from "react-dom/client";
5
+ import { afterEach, describe, expect, it } from "vitest";
6
+ import type { TimelineElement } from "../store/playerStore";
7
+ import { usePlayerStore } from "../store/playerStore";
8
+ import { GUTTER, RULER_H, TRACK_H } from "./timelineLayout";
9
+ import type { StackingTimelineLayer } from "./timelineTrackOrder";
10
+ import { useTimelineMarqueeSelection } from "./useTimelineMarqueeSelection";
11
+
12
+ (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
13
+
14
+ function element(id: string, start: number, duration: number, track: number): TimelineElement {
15
+ return { id, tag: "div", start, duration, track };
16
+ }
17
+
18
+ function layer(id: string, elements: TimelineElement[]): StackingTimelineLayer {
19
+ return {
20
+ id,
21
+ kind: "visual",
22
+ contextKey: "",
23
+ zIndex: 0,
24
+ placementTrack: elements[0]?.track ?? 0,
25
+ elements,
26
+ };
27
+ }
28
+
29
+ function pointerEvent(type: string, init: MouseEventInit & { pointerId?: number }) {
30
+ const event = new MouseEvent(type, { bubbles: true, cancelable: true, ...init });
31
+ Object.defineProperty(event, "pointerId", { value: init.pointerId ?? 1 });
32
+ return event;
33
+ }
34
+
35
+ function dispatchPointer(
36
+ target: HTMLElement,
37
+ type: "pointerdown" | "pointermove" | "pointerup",
38
+ point: { x: number; y: number },
39
+ ) {
40
+ target.dispatchEvent(pointerEvent(type, { button: 0, clientX: point.x, clientY: point.y }));
41
+ }
42
+
43
+ function dragMarquee(
44
+ harness: ReturnType<typeof renderMarqueeHarness>,
45
+ start: { x: number; y: number },
46
+ end: { x: number; y: number },
47
+ downTarget: HTMLElement = harness.scroll,
48
+ ) {
49
+ dispatchPointer(downTarget, "pointerdown", start);
50
+ dispatchPointer(harness.scroll, "pointermove", end);
51
+ dispatchPointer(harness.scroll, "pointerup", end);
52
+ }
53
+
54
+ function renderMarqueeHarness(layers: StackingTimelineLayer[]) {
55
+ const host = document.createElement("div");
56
+ document.body.append(host);
57
+ const layerOrder = layers.map((item) => item.id);
58
+ const setShowPopover = () => {};
59
+ const setRangeSelection = () => {};
60
+ const seekedX: number[] = [];
61
+
62
+ function Harness() {
63
+ const scrollRef = React.useRef<HTMLDivElement | null>(null);
64
+ const hook = useTimelineMarqueeSelection({
65
+ scrollRef,
66
+ ppsRef: { current: 100 },
67
+ trackOrderRef: { current: layerOrder },
68
+ timelineLayersRef: { current: layers },
69
+ setShowPopover,
70
+ setRangeSelectionRef: { current: setRangeSelection },
71
+ seekFromX: (clientX: number) => seekedX.push(clientX),
72
+ });
73
+ return (
74
+ <div
75
+ ref={scrollRef}
76
+ data-scroll="true"
77
+ onPointerDown={(event) => {
78
+ hook.handlePointerDown(event);
79
+ }}
80
+ onPointerMove={(event) => {
81
+ hook.handlePointerMove(event);
82
+ }}
83
+ onPointerUp={(event) => {
84
+ hook.handlePointerUp(event);
85
+ }}
86
+ >
87
+ <button data-clip="true">clip</button>
88
+ {hook.marqueeRect && <span data-marquee="true" />}
89
+ </div>
90
+ );
91
+ }
92
+
93
+ const root = createRoot(host);
94
+ act(() => {
95
+ root.render(<Harness />);
96
+ });
97
+ const scroll = host.querySelector<HTMLElement>("[data-scroll]");
98
+ if (!scroll) throw new Error("Expected scroll host");
99
+ Object.defineProperty(scroll, "clientWidth", { configurable: true, value: 160 });
100
+ Object.defineProperty(scroll, "clientHeight", { configurable: true, value: 160 });
101
+ Object.defineProperty(scroll, "scrollWidth", { configurable: true, value: 600 });
102
+ Object.defineProperty(scroll, "scrollHeight", { configurable: true, value: 260 });
103
+ scroll.getBoundingClientRect = () =>
104
+ ({
105
+ left: 0,
106
+ top: 0,
107
+ right: 160,
108
+ bottom: 160,
109
+ width: 160,
110
+ height: 160,
111
+ x: 0,
112
+ y: 0,
113
+ toJSON: () => ({}),
114
+ }) as DOMRect;
115
+
116
+ return {
117
+ host,
118
+ scroll,
119
+ root,
120
+ seekedX,
121
+ clip: host.querySelector<HTMLElement>("[data-clip]")!,
122
+ unmount() {
123
+ act(() => root.unmount());
124
+ },
125
+ };
126
+ }
127
+
128
+ afterEach(() => {
129
+ document.body.innerHTML = "";
130
+ usePlayerStore.getState().reset();
131
+ });
132
+
133
+ describe("useTimelineMarqueeSelection", () => {
134
+ it("selects clips intersecting an empty-lane drag rectangle", () => {
135
+ const layers = [
136
+ layer("lane-0", [element("first", 0.5, 0.5, 0)]),
137
+ layer("lane-1", [element("second", 2, 0.5, 1)]),
138
+ layer("lane-2", [element("third", 0.5, 0.5, 2)]),
139
+ ];
140
+ const harness = renderMarqueeHarness(layers);
141
+ const start = { x: GUTTER + 10, y: RULER_H + 4 };
142
+ const end = { x: GUTTER + 260, y: RULER_H + TRACK_H * 2 - 2 };
143
+
144
+ act(() => {
145
+ dispatchPointer(harness.scroll, "pointerdown", start);
146
+ dispatchPointer(harness.scroll, "pointermove", end);
147
+ });
148
+ expect(harness.host.querySelector("[data-marquee]")).not.toBeNull();
149
+
150
+ act(() => {
151
+ dispatchPointer(harness.scroll, "pointerup", end);
152
+ });
153
+
154
+ expect([...usePlayerStore.getState().selectedElementIds]).toEqual(["first", "second"]);
155
+ harness.unmount();
156
+ });
157
+
158
+ it("treats a sub-threshold empty-lane drag as a clear click that also seeks", () => {
159
+ usePlayerStore.getState().setSelection(["selected"]);
160
+ const harness = renderMarqueeHarness([layer("lane-0", [element("selected", 0, 1, 0)])]);
161
+
162
+ act(() => {
163
+ dragMarquee(harness, { x: GUTTER + 20, y: RULER_H + 4 }, { x: GUTTER + 21, y: RULER_H + 5 });
164
+ });
165
+
166
+ expect(usePlayerStore.getState().selectedElementIds.size).toBe(0);
167
+ expect(harness.host.querySelector("[data-marquee]")).toBeNull();
168
+ // A sub-threshold press still scrubs the playhead to the click, like a plain lane click.
169
+ expect(harness.seekedX).toEqual([GUTTER + 20]);
170
+ harness.unmount();
171
+ });
172
+
173
+ it("does not start from clips or the ruler", () => {
174
+ usePlayerStore.getState().setSelection(["kept"]);
175
+ const harness = renderMarqueeHarness([layer("lane-0", [element("kept", 0, 1, 0)])]);
176
+
177
+ act(() => {
178
+ dragMarquee(
179
+ harness,
180
+ { x: GUTTER + 20, y: RULER_H + 4 },
181
+ { x: GUTTER + 80, y: RULER_H + 40 },
182
+ harness.clip,
183
+ );
184
+ dragMarquee(harness, { x: GUTTER + 20, y: RULER_H - 2 }, { x: GUTTER + 80, y: RULER_H + 40 });
185
+ });
186
+
187
+ expect([...usePlayerStore.getState().selectedElementIds]).toEqual(["kept"]);
188
+ expect(harness.host.querySelector("[data-marquee]")).toBeNull();
189
+ harness.unmount();
190
+ });
191
+
192
+ it("clears selection when the released marquee hits no clips", () => {
193
+ usePlayerStore.getState().setSelection(["selected"]);
194
+ const harness = renderMarqueeHarness([layer("lane-0", [element("selected", 4, 1, 0)])]);
195
+
196
+ act(() => {
197
+ dragMarquee(harness, { x: GUTTER + 10, y: RULER_H + 4 }, { x: GUTTER + 80, y: RULER_H + 40 });
198
+ });
199
+
200
+ expect(usePlayerStore.getState().selectedElementIds.size).toBe(0);
201
+ harness.unmount();
202
+ });
203
+
204
+ it("uses autoscroll position when resolving the released marquee", () => {
205
+ const originalRaf = window.requestAnimationFrame;
206
+ const originalCancel = window.cancelAnimationFrame;
207
+ const callbacks: FrameRequestCallback[] = [];
208
+ window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
209
+ callbacks.push(callback);
210
+ return callbacks.length;
211
+ }) as typeof window.requestAnimationFrame;
212
+ window.cancelAnimationFrame = (() => {}) as typeof window.cancelAnimationFrame;
213
+ const harness = renderMarqueeHarness([layer("lane-0", [element("reachable", 1.28, 0.2, 0)])]);
214
+
215
+ try {
216
+ act(() => {
217
+ dispatchPointer(harness.scroll, "pointerdown", { x: GUTTER + 10, y: RULER_H + 4 });
218
+ dispatchPointer(harness.scroll, "pointermove", { x: 155, y: RULER_H + 40 });
219
+ callbacks.shift()?.(0);
220
+ dispatchPointer(harness.scroll, "pointerup", { x: 155, y: RULER_H + 40 });
221
+ });
222
+
223
+ expect(harness.scroll.scrollLeft).toBeGreaterThan(0);
224
+ expect([...usePlayerStore.getState().selectedElementIds]).toEqual(["reachable"]);
225
+ harness.unmount();
226
+ } finally {
227
+ window.requestAnimationFrame = originalRaf;
228
+ window.cancelAnimationFrame = originalCancel;
229
+ }
230
+ });
231
+ });
@@ -0,0 +1,276 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useRef,
5
+ useState,
6
+ type PointerEvent as ReactPointerEvent,
7
+ type RefObject,
8
+ } from "react";
9
+ import { usePlayerStore } from "../store/playerStore";
10
+ import {
11
+ resolveTimelineAutoScroll,
12
+ selectTimelineElementsInMarquee,
13
+ type TimelineMarqueeSelectionRect,
14
+ } from "./timelineEditing";
15
+ import { GUTTER, RULER_H, TRACK_H } from "./timelineLayout";
16
+ import { TIMELINE_LAYER_GROUP_HEADER_H } from "./TimelineLayerGroupHeader";
17
+ import type { StackingTimelineLayer, TimelineLayerId } from "./timelineTrackOrder";
18
+
19
+ const MARQUEE_THRESHOLD_PX = 4;
20
+
21
+ export interface TimelineMarqueeOverlayRect {
22
+ left: number;
23
+ top: number;
24
+ width: number;
25
+ height: number;
26
+ }
27
+
28
+ interface ActiveMarqueeGesture {
29
+ pointerId: number;
30
+ anchorClientX: number;
31
+ anchorClientY: number;
32
+ anchorX: number;
33
+ anchorY: number;
34
+ lastClientX: number;
35
+ lastClientY: number;
36
+ started: boolean;
37
+ }
38
+
39
+ interface UseTimelineMarqueeSelectionInput {
40
+ scrollRef: RefObject<HTMLDivElement | null>;
41
+ ppsRef: RefObject<number>;
42
+ trackOrderRef: RefObject<TimelineLayerId[]>;
43
+ timelineLayersRef: RefObject<StackingTimelineLayer[]>;
44
+ disabled?: boolean;
45
+ setShowPopover: (show: boolean) => void;
46
+ setRangeSelectionRef: RefObject<((sel: null) => void) | null>;
47
+ /** Canonical playhead seek, used to keep empty-lane clicks scrubbing the playhead. */
48
+ seekFromX: (clientX: number) => void;
49
+ }
50
+
51
+ function getCanvasPoint(scroll: HTMLDivElement, clientX: number, clientY: number) {
52
+ const rect = scroll.getBoundingClientRect();
53
+ return {
54
+ x: clientX - rect.left + scroll.scrollLeft,
55
+ y: clientY - rect.top + scroll.scrollTop,
56
+ };
57
+ }
58
+
59
+ function getMarqueeStartPoint(
60
+ event: ReactPointerEvent<HTMLDivElement>,
61
+ scroll: HTMLDivElement | null,
62
+ disabled: boolean,
63
+ ) {
64
+ if (disabled || event.button !== 0 || event.shiftKey || !scroll) return null;
65
+ const target = event.target as HTMLElement;
66
+ if (target.closest("[data-clip]")) return null;
67
+ const point = getCanvasPoint(scroll, event.clientX, event.clientY);
68
+ if (point.x < GUTTER || point.y < RULER_H) return null;
69
+ return point;
70
+ }
71
+
72
+ function capturePointerIfAvailable(event: ReactPointerEvent<HTMLDivElement>) {
73
+ const currentTarget = event.currentTarget as HTMLElement;
74
+ if (typeof currentTarget.setPointerCapture === "function") {
75
+ currentTarget.setPointerCapture(event.pointerId);
76
+ }
77
+ }
78
+
79
+ function buildSelectionRect(
80
+ active: ActiveMarqueeGesture,
81
+ scroll: HTMLDivElement,
82
+ pps: number,
83
+ ): { overlay: TimelineMarqueeOverlayRect; selection: TimelineMarqueeSelectionRect } {
84
+ const current = getCanvasPoint(scroll, active.lastClientX, active.lastClientY);
85
+ const left = Math.min(active.anchorX, current.x);
86
+ const right = Math.max(active.anchorX, current.x);
87
+ const top = Math.min(active.anchorY, current.y);
88
+ const bottom = Math.max(active.anchorY, current.y);
89
+ const overlayLeft = Math.max(GUTTER, left);
90
+ const overlayTop = Math.max(RULER_H, top);
91
+ const overlayRight = Math.max(overlayLeft, right);
92
+ const overlayBottom = Math.max(overlayTop, bottom);
93
+
94
+ // Hit-test must use the SAME pixels-per-second the overlay is drawn at, or the
95
+ // selected time span diverges from the visible box at low zoom (pps < 1). Guard
96
+ // only against a non-finite/zero pps (would yield NaN/Infinity), never floor it.
97
+ const safePps = Number.isFinite(pps) && pps > 0 ? pps : 0;
98
+ const timeFromX = (x: number) => (safePps > 0 ? Math.max(0, (x - GUTTER) / safePps) : 0);
99
+
100
+ return {
101
+ overlay: {
102
+ left: overlayLeft,
103
+ top: overlayTop,
104
+ width: overlayRight - overlayLeft,
105
+ height: overlayBottom - overlayTop,
106
+ },
107
+ selection: {
108
+ startTime: timeFromX(left),
109
+ endTime: timeFromX(right),
110
+ top,
111
+ bottom,
112
+ },
113
+ };
114
+ }
115
+
116
+ export function useTimelineMarqueeSelection({
117
+ scrollRef,
118
+ ppsRef,
119
+ trackOrderRef,
120
+ timelineLayersRef,
121
+ disabled = false,
122
+ setShowPopover,
123
+ setRangeSelectionRef,
124
+ seekFromX,
125
+ }: UseTimelineMarqueeSelectionInput) {
126
+ const activeRef = useRef<ActiveMarqueeGesture | null>(null);
127
+ const pointerRef = useRef<{ clientX: number; clientY: number } | null>(null);
128
+ const scrollRafRef = useRef(0);
129
+ const [marqueeRect, setMarqueeRect] = useState<TimelineMarqueeOverlayRect | null>(null);
130
+
131
+ const stopAutoScroll = useCallback(() => {
132
+ pointerRef.current = null;
133
+ if (scrollRafRef.current) {
134
+ cancelAnimationFrame(scrollRafRef.current);
135
+ scrollRafRef.current = 0;
136
+ }
137
+ }, []);
138
+
139
+ const updateMarqueeRect = useCallback(() => {
140
+ const active = activeRef.current;
141
+ const scroll = scrollRef.current;
142
+ if (!active || !scroll) return null;
143
+ const rects = buildSelectionRect(active, scroll, ppsRef.current);
144
+ setMarqueeRect(rects.overlay);
145
+ return rects;
146
+ }, [ppsRef, scrollRef]);
147
+
148
+ const stepAutoScroll = useCallback(() => {
149
+ scrollRafRef.current = 0;
150
+ const pointer = pointerRef.current;
151
+ const scroll = scrollRef.current;
152
+ if (!pointer || !scroll || !activeRef.current) return;
153
+
154
+ const delta = resolveTimelineAutoScroll(
155
+ scroll.getBoundingClientRect(),
156
+ pointer.clientX,
157
+ pointer.clientY,
158
+ );
159
+ if (delta.x === 0 && delta.y === 0) return;
160
+
161
+ const maxScrollLeft = Math.max(0, scroll.scrollWidth - scroll.clientWidth);
162
+ const maxScrollTop = Math.max(0, scroll.scrollHeight - scroll.clientHeight);
163
+ const nextScrollLeft = Math.max(0, Math.min(maxScrollLeft, scroll.scrollLeft + delta.x));
164
+ const nextScrollTop = Math.max(0, Math.min(maxScrollTop, scroll.scrollTop + delta.y));
165
+ if (nextScrollLeft === scroll.scrollLeft && nextScrollTop === scroll.scrollTop) return;
166
+
167
+ scroll.scrollLeft = nextScrollLeft;
168
+ scroll.scrollTop = nextScrollTop;
169
+ updateMarqueeRect();
170
+ scrollRafRef.current = requestAnimationFrame(stepAutoScroll);
171
+ }, [scrollRef, updateMarqueeRect]);
172
+
173
+ const syncAutoScroll = useCallback(
174
+ (clientX: number, clientY: number) => {
175
+ pointerRef.current = { clientX, clientY };
176
+ const scroll = scrollRef.current;
177
+ if (!scroll) return;
178
+ const delta = resolveTimelineAutoScroll(scroll.getBoundingClientRect(), clientX, clientY);
179
+ if (delta.x === 0 && delta.y === 0) {
180
+ if (scrollRafRef.current) {
181
+ cancelAnimationFrame(scrollRafRef.current);
182
+ scrollRafRef.current = 0;
183
+ }
184
+ return;
185
+ }
186
+ if (!scrollRafRef.current) {
187
+ scrollRafRef.current = requestAnimationFrame(stepAutoScroll);
188
+ }
189
+ },
190
+ [scrollRef, stepAutoScroll],
191
+ );
192
+
193
+ const handlePointerDown = useCallback(
194
+ (event: ReactPointerEvent<HTMLDivElement>) => {
195
+ const point = getMarqueeStartPoint(event, scrollRef.current, disabled);
196
+ if (!point) return false;
197
+ capturePointerIfAvailable(event);
198
+ activeRef.current = {
199
+ pointerId: event.pointerId,
200
+ anchorClientX: event.clientX,
201
+ anchorClientY: event.clientY,
202
+ anchorX: point.x,
203
+ anchorY: point.y,
204
+ lastClientX: event.clientX,
205
+ lastClientY: event.clientY,
206
+ started: false,
207
+ };
208
+ setShowPopover(false);
209
+ setRangeSelectionRef.current?.(null);
210
+ return true;
211
+ },
212
+ [disabled, scrollRef, setRangeSelectionRef, setShowPopover],
213
+ );
214
+
215
+ const handlePointerMove = useCallback(
216
+ (event: ReactPointerEvent<HTMLDivElement>) => {
217
+ const active = activeRef.current;
218
+ if (!active || active.pointerId !== event.pointerId) return false;
219
+ active.lastClientX = event.clientX;
220
+ active.lastClientY = event.clientY;
221
+ const distance = Math.hypot(
222
+ event.clientX - active.anchorClientX,
223
+ event.clientY - active.anchorClientY,
224
+ );
225
+ if (!active.started && distance < MARQUEE_THRESHOLD_PX) return true;
226
+ active.started = true;
227
+ updateMarqueeRect();
228
+ syncAutoScroll(event.clientX, event.clientY);
229
+ return true;
230
+ },
231
+ [syncAutoScroll, updateMarqueeRect],
232
+ );
233
+
234
+ const handlePointerUp = useCallback(
235
+ (event?: ReactPointerEvent<HTMLDivElement>) => {
236
+ const active = activeRef.current;
237
+ if (!active || (event && active.pointerId !== event.pointerId)) return false;
238
+ activeRef.current = null;
239
+ stopAutoScroll();
240
+ setMarqueeRect(null);
241
+
242
+ if (!active.started) {
243
+ // A press that never crossed the marquee threshold is a plain empty-lane
244
+ // click: clear the selection AND scrub the playhead, matching the seek that
245
+ // the range/playhead handler would have run had the marquee not claimed it.
246
+ usePlayerStore.getState().clearSelection();
247
+ seekFromX(active.anchorClientX);
248
+ return true;
249
+ }
250
+
251
+ const scroll = scrollRef.current;
252
+ if (!scroll) return true;
253
+ const rects = buildSelectionRect(active, scroll, ppsRef.current);
254
+ const selectedIds = selectTimelineElementsInMarquee({
255
+ rect: rects.selection,
256
+ layers: timelineLayersRef.current,
257
+ layerOrder: trackOrderRef.current,
258
+ rulerHeight: RULER_H,
259
+ trackHeight: TRACK_H,
260
+ groupHeaderHeight: TIMELINE_LAYER_GROUP_HEADER_H,
261
+ });
262
+ usePlayerStore.getState().setSelection(selectedIds);
263
+ return true;
264
+ },
265
+ [ppsRef, scrollRef, seekFromX, stopAutoScroll, timelineLayersRef, trackOrderRef],
266
+ );
267
+
268
+ useEffect(() => stopAutoScroll, [stopAutoScroll]);
269
+
270
+ return {
271
+ marqueeRect,
272
+ handlePointerDown,
273
+ handlePointerMove,
274
+ handlePointerUp,
275
+ };
276
+ }
@@ -192,9 +192,12 @@ function domSiblingClips(
192
192
  start: host.start,
193
193
  duration: host.duration,
194
194
  track: host.track,
195
+ zIndex: 0,
196
+ stackingContextId: host.stackingContextId ?? host.domId ?? host.id ?? null,
195
197
  kind: "element",
196
198
  tagName: null,
197
199
  compositionId: null,
200
+ compositionAncestors: host.compositionAncestors ?? [],
198
201
  parentCompositionId: host.id ?? null,
199
202
  compositionSrc: host.compositionSrc ?? null,
200
203
  assetUrl: null,
@@ -243,7 +243,10 @@ describe("buildStandaloneRootTimelineElement", () => {
243
243
  iframeSrc: "http://127.0.0.1:4173/api/projects/demo/preview/comp/scenes/hero.html?_t=123",
244
244
  selector: '[data-composition-id="hero"]',
245
245
  }),
246
- ).toEqual({
246
+ // toMatchObject (not toEqual): asserts the selector/source metadata this
247
+ // test is about, without re-pinning the full element shape (stacking
248
+ // metadata like hasExplicitZIndex is covered in timelineDOM.test.ts).
249
+ ).toMatchObject({
247
250
  id: "hero",
248
251
  label: "hero",
249
252
  key: 'scenes/hero.html:[data-composition-id="hero"]:0',
@@ -44,6 +44,7 @@ import {
44
44
  import { scrubMusicAtSeek, stopScrubPreviewAudio } from "../lib/playbackScrub";
45
45
  import { applyCachedSourceDurations, probeMissingSourceDurations } from "../lib/mediaProbe";
46
46
  import { shouldResumeForwardPlaybackAfterSeek, shouldStopAfterSeek } from "../lib/playbackSeek";
47
+ import { applyPreviewVariablesToUrl } from "../../hooks/previewVariablesStore";
47
48
 
48
49
  /**
49
50
  * Whether the derived elements differ from the current ones in any field that
@@ -127,6 +128,8 @@ export function useTimelinePlayer() {
127
128
  [setElements, setTimelineReady, setDuration],
128
129
  );
129
130
 
131
+ // Pre-existing dispatcher complexity — surfaced by this PR's line shifts, not new logic.
132
+ // fallow-ignore-next-line complexity
130
133
  const getAdapter = useCallback((): PlaybackAdapter | null => {
131
134
  try {
132
135
  const iframe = iframeRef.current;
@@ -209,6 +212,7 @@ export function useTimelinePlayer() {
209
212
  }, []);
210
213
 
211
214
  const startRAFLoop = useCallback(() => {
215
+ // fallow-ignore-next-line complexity
212
216
  const tick = () => {
213
217
  const adapter = getAdapter();
214
218
  if (adapter) {
@@ -475,6 +479,7 @@ export function useTimelinePlayer() {
475
479
  const src = iframe.src;
476
480
  const url = new URL(src, window.location.origin);
477
481
  url.searchParams.set("_t", String(Date.now()));
482
+ applyPreviewVariablesToUrl(url);
478
483
  iframe.src = url.toString();
479
484
  }, [saveSeekPosition]);
480
485
  const getAdapterRef = useRef(getAdapter);
@@ -484,6 +489,8 @@ export function useTimelinePlayer() {
484
489
  const handleWindowKeyDown = (e: KeyboardEvent) => playbackKeyDownRef.current(e);
485
490
  const handleWindowKeyUp = (e: KeyboardEvent) => playbackKeyUpRef.current(e);
486
491
 
492
+ // Pre-existing message-router complexity — surfaced by line shifts, not new logic.
493
+ // fallow-ignore-next-line complexity
487
494
  const handleMessage = (e: MessageEvent) => {
488
495
  const data = e.data;
489
496
  const ourIframe = iframeRef.current;
@@ -0,0 +1,77 @@
1
+ // @vitest-environment jsdom
2
+
3
+ import { describe, expect, it } from "vitest";
4
+ import { computeReorderZValues, getElementZIndex, resolveContextOrder } from "./layerOrdering";
5
+
6
+ function makeElement(zIndex?: string): HTMLElement {
7
+ const element = document.createElement("div");
8
+ if (zIndex != null) element.style.zIndex = zIndex;
9
+ document.body.appendChild(element);
10
+ return element;
11
+ }
12
+
13
+ describe("getElementZIndex", () => {
14
+ it("returns inline z-index when present", () => {
15
+ expect(getElementZIndex(makeElement("7"))).toBe(7);
16
+ });
17
+
18
+ it("falls back to computed style when inline z-index is not usable", () => {
19
+ const element = makeElement();
20
+ element.className = "computed-z";
21
+ const style = document.createElement("style");
22
+ style.textContent = ".computed-z { position: relative; z-index: 12; }";
23
+ document.head.appendChild(style);
24
+
25
+ expect(getElementZIndex(element)).toBe(12);
26
+ });
27
+
28
+ it("returns zero for auto or missing z-index", () => {
29
+ expect(getElementZIndex(makeElement())).toBe(0);
30
+ expect(getElementZIndex(makeElement("auto"))).toBe(0);
31
+ });
32
+ });
33
+
34
+ describe("computeReorderZValues", () => {
35
+ it("preserves distinct existing z-index values and remaps them onto the new order", () => {
36
+ expect(computeReorderZValues([1, 8, 3], 0, 2)).toEqual([8, 3, 1]);
37
+ });
38
+
39
+ it("renumbers an all-tied group to descending contiguous z-index values", () => {
40
+ expect(computeReorderZValues([0, 0, 0], 2, 0)).toEqual([3, 2, 1]);
41
+ });
42
+
43
+ it("renumbers the whole group when any existing z-index values are tied", () => {
44
+ expect(computeReorderZValues([5, 5, 1], 2, 0)).toEqual([3, 2, 1]);
45
+ });
46
+ });
47
+
48
+ describe("resolveContextOrder", () => {
49
+ it("sorts a flat sibling group by z-index descending then original order", () => {
50
+ const ordered = resolveContextOrder([
51
+ { id: "a", zIndex: 2, parentCompositionId: null, compositionAncestors: ["root"] },
52
+ { id: "b", zIndex: 5, parentCompositionId: null, compositionAncestors: ["root"] },
53
+ { id: "c", zIndex: 5, parentCompositionId: null, compositionAncestors: ["root"] },
54
+ ]);
55
+
56
+ expect(ordered.map((item) => item.id)).toEqual(["b", "c", "a"]);
57
+ });
58
+
59
+ it("keeps distinct stacking contexts from interleaving", () => {
60
+ const ordered = resolveContextOrder([
61
+ { id: "root-low", zIndex: 1, parentCompositionId: null, compositionAncestors: ["root"] },
62
+ {
63
+ id: "nested-high",
64
+ zIndex: 100,
65
+ parentCompositionId: "scene",
66
+ compositionAncestors: ["root", "scene"],
67
+ },
68
+ { id: "root-top", zIndex: 2, parentCompositionId: null, compositionAncestors: ["root"] },
69
+ ]);
70
+
71
+ expect(ordered.map((item) => item.id)).toEqual(["root-top", "root-low", "nested-high"]);
72
+ });
73
+
74
+ it("returns an empty list for empty input", () => {
75
+ expect(resolveContextOrder([])).toEqual([]);
76
+ });
77
+ });