@hyperframes/studio 0.6.100 → 0.6.101

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 (38) hide show
  1. package/dist/assets/index-BITwbxi-.css +1 -0
  2. package/dist/assets/{index-gpSohHUn.js → index-CQ3n6Y9q.js} +1 -1
  3. package/dist/assets/{index-BkT9VKwz.js → index-CTiqZ7XQ.js} +165 -165
  4. package/dist/assets/{index-CKWBqyRd.js → index-DvttAtOD.js} +1 -1
  5. package/dist/index.html +2 -2
  6. package/package.json +5 -5
  7. package/src/App.tsx +2 -1
  8. package/src/components/editor/PropertyPanel.tsx +24 -16
  9. package/src/components/editor/manualEditingAvailability.ts +5 -3
  10. package/src/components/nle/NLELayout.tsx +89 -1
  11. package/src/hooks/gsapScriptCommitHelpers.ts +8 -5
  12. package/src/hooks/gsapScriptCommitTypes.ts +3 -0
  13. package/src/hooks/gsapTargetCache.ts +65 -0
  14. package/src/hooks/useAppHotkeys.ts +10 -0
  15. package/src/hooks/useDomEditCommits.ts +6 -5
  16. package/src/hooks/useDomEditSession.ts +6 -1
  17. package/src/hooks/useDomGeometryCommits.ts +1 -36
  18. package/src/hooks/useElementLifecycleOps.ts +5 -0
  19. package/src/hooks/useGsapAnimationOps.ts +26 -1
  20. package/src/hooks/useGsapScriptCommits.ts +5 -2
  21. package/src/hooks/useRazorSplit.ts +3 -0
  22. package/src/hooks/useSdkSession.ts +15 -12
  23. package/src/hooks/useTimelineEditing.ts +23 -3
  24. package/src/player/components/Timeline.tsx +31 -18
  25. package/src/player/components/TimelineClip.tsx +3 -3
  26. package/src/player/components/useResolvedTimelineEditCallbacks.ts +30 -0
  27. package/src/player/hooks/useExpandedTimelineElements.test.ts +91 -0
  28. package/src/player/hooks/useExpandedTimelineElements.ts +153 -0
  29. package/src/player/hooks/useTimelineSyncCallbacks.ts +22 -0
  30. package/src/player/store/playerStore.ts +22 -8
  31. package/src/telemetry/events.test.ts +16 -1
  32. package/src/telemetry/events.ts +15 -0
  33. package/src/utils/blockCategories.ts +2 -2
  34. package/src/utils/sdkShadow.test.ts +102 -2
  35. package/src/utils/sdkShadow.ts +200 -2
  36. package/src/utils/studioHelpers.test.ts +25 -1
  37. package/src/utils/studioHelpers.ts +54 -28
  38. package/dist/assets/index-B62bDCQv.css +0 -1
@@ -43,7 +43,10 @@ async function mutateGsapScript(
43
43
 
44
44
  // oxfmt-ignore
45
45
  // fallow-ignore-next-line complexity
46
- export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIframeRef, editHistory, domEditSaveTimestampRef, reloadPreview, onCacheInvalidate, onFileContentChanged, showToast }: GsapScriptCommitsParams) {
46
+ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIframeRef, editHistory, domEditSaveTimestampRef, reloadPreview, onCacheInvalidate, onFileContentChanged, showToast, sdkSession }: GsapScriptCommitsParams) {
47
+ // Pre-existing complexity (server mutate + history + reload branches); this PR
48
+ // adds only a guarded shadow-fidelity dispatch.
49
+ // fallow-ignore-next-line complexity
47
50
  const commitMutation = useCallback(async (selection: DomEditSelection, mutation: Record<string, unknown>, options: CommitMutationOptions) => {
48
51
  const pid = projectIdRef.current;
49
52
  if (!pid) return;
@@ -81,7 +84,7 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
81
84
  const trackGsapSaveFailure = useGsapSaveFailureTelemetry(activeCompPath);
82
85
  const commitMutationSafely = useSafeGsapCommitMutation(commitMutation, trackGsapSaveFailure, showToast);
83
86
  const propertyOps = useGsapPropertyDebounce(commitMutationSafely);
84
- const animationOps = useGsapAnimationOps({ projectIdRef, activeCompPath, commitMutation, commitMutationSafely, showToast });
87
+ const animationOps = useGsapAnimationOps({ projectIdRef, activeCompPath, commitMutation, commitMutationSafely, showToast, sdkSession });
85
88
  const keyframeOps = useGsapKeyframeOps({ activeCompPath, commitMutation, commitMutationSafely, trackGsapSaveFailure });
86
89
  const arcPathOps = useGsapArcPathOps(commitMutationSafely);
87
90
  return { commitMutation, ...propertyOps, ...animationOps, ...keyframeOps, ...arcPathOps };
@@ -3,6 +3,7 @@ import type { TimelineElement } from "../player";
3
3
  import { usePlayerStore } from "../player";
4
4
  import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
5
5
  import { getTimelineElementLabel, collectHtmlIds } from "../utils/studioHelpers";
6
+ import { trackStudioRazorSplit } from "../telemetry/events";
6
7
  import {
7
8
  canSplitElement,
8
9
  buildPatchTarget,
@@ -196,6 +197,7 @@ export function useRazorSplit({
196
197
  });
197
198
 
198
199
  reloadPreview();
200
+ trackStudioRazorSplit({ mode: "single", count: 1 });
199
201
  showToast(`Split ${getTimelineElementLabel(element)} at ${splitTime.toFixed(2)}s`, "info");
200
202
  if (skippedSelectors?.length) {
201
203
  showToast(
@@ -277,6 +279,7 @@ export function useRazorSplit({
277
279
  });
278
280
 
279
281
  reloadPreview();
282
+ trackStudioRazorSplit({ mode: "all", count: splitCount });
280
283
  showToast(`Split ${splitCount} clips at ${splitTime.toFixed(2)}s`, "info");
281
284
  } catch (error) {
282
285
  const message = error instanceof Error ? error.message : "Failed to split clips";
@@ -20,12 +20,15 @@ export function shouldReloadSdkSession(payload: unknown, activeCompPath: string
20
20
  * (projectId, activeCompPath) change, disposes the old one on cleanup, and
21
21
  * re-opens it when the active composition file changes on disk (code editor,
22
22
  * agent, or server-side patch) so the in-memory linkedom document never goes
23
- * stale. The persist queue writes back to `activeCompPath` (not the
24
- * "composition.html" default).
23
+ * stale.
25
24
  *
26
- * The session is idle until Step 3c routes dispatch ops through it; re-opening
27
- * is therefore purely additive no SDK self-write exists yet, so there is no
28
- * persist echo. Step 3c must add self-write suppression once dispatch writes.
25
+ * Opened WITHOUT a persist queue: this session is shadow-telemetry +
26
+ * selection-sync only it reads from the server but must NEVER write back.
27
+ * Shadow dispatch ops mutate the in-memory model and are discarded on the next
28
+ * reload-on-change (the studio's own authoritative write triggers it). Routing
29
+ * authoritative writes through this session (cutover, Step 3c+) must re-add
30
+ * persist TOGETHER WITH self-write suppression — without it, the SDK's
31
+ * serialize() output races and clobbers the studio's authoritative write.
29
32
  */
30
33
  export function useSdkSession(
31
34
  projectId: string | null,
@@ -37,6 +40,9 @@ export function useSdkSession(
37
40
  // ── Re-open on external change to the active composition ──
38
41
  useEffect(() => {
39
42
  if (!activeCompPath) return;
43
+ // Pre-existing clone of the file-change reload handler (usePreviewPersistence);
44
+ // surfaced by this PR's adjacent edits, not introduced by it.
45
+ // fallow-ignore-next-line code-duplication
40
46
  const handler = (payload?: unknown) => {
41
47
  if (shouldReloadSdkSession(payload, activeCompPath)) {
42
48
  setReloadToken((t) => t + 1);
@@ -69,13 +75,10 @@ export function useSdkSession(
69
75
  .read(activeCompPath)
70
76
  .then(async (content) => {
71
77
  if (cancelled || typeof content !== "string") return;
72
- comp = await openComposition(content, {
73
- persist: adapter,
74
- persistPath: activeCompPath,
75
- });
76
- comp.on("persist:error", (e) => {
77
- console.warn("[sdk] persist:error", e.error);
78
- });
78
+ // No persist shadow/selection only; see the hook docstring. The SDK
79
+ // must not write back to the server while it shadows the authoritative
80
+ // studio path.
81
+ comp = await openComposition(content);
79
82
  // Cleanup may have fired while openComposition was awaited; dispose immediately.
80
83
  if (cancelled) {
81
84
  comp.dispose();
@@ -1,4 +1,11 @@
1
+ // Pre-existing-complex timeline hook (DOM patch + GSAP position shift/scale +
2
+ // playback-start resolution); this PR adds guarded shadow-timing dispatches in
3
+ // the move/resize .then() chains, which nudges several callbacks over the CC
4
+ // threshold. The added branches are telemetry-only.
5
+ // fallow-ignore-file complexity
1
6
  import { useCallback, useRef } from "react";
7
+ import type { Composition } from "@hyperframes/sdk";
8
+ import { runShadowTiming } from "../utils/sdkShadow";
2
9
  import type { TimelineElement } from "../player";
3
10
  import { usePlayerStore } from "../player";
4
11
  import { useRazorSplit } from "./useRazorSplit";
@@ -33,7 +40,7 @@ import type { PersistTimelineEditInput } from "./timelineEditingHelpers";
33
40
 
34
41
  // ── Types ──
35
42
 
36
- export interface RecordEditInput {
43
+ interface RecordEditInput {
37
44
  label: string;
38
45
  kind: EditHistoryKind;
39
46
  coalesceKey?: string;
@@ -53,6 +60,8 @@ interface UseTimelineEditingOptions {
53
60
  pendingTimelineEditPathRef: React.MutableRefObject<Set<string>>;
54
61
  uploadProjectFiles: (files: Iterable<File>, dir?: string) => Promise<string[]>;
55
62
  isRecordingRef?: React.RefObject<boolean>;
63
+ /** Stage 7 Step 3b: SDK session for shadow timing dispatch (server stays authoritative). */
64
+ sdkSession?: Composition | null;
56
65
  }
57
66
 
58
67
  // ── Hook ──
@@ -70,6 +79,7 @@ export function useTimelineEditing({
70
79
  pendingTimelineEditPathRef,
71
80
  uploadProjectFiles,
72
81
  isRecordingRef,
82
+ sdkSession,
73
83
  }: UseTimelineEditingOptions) {
74
84
  const projectIdRef = useRef(projectId);
75
85
  projectIdRef.current = projectId;
@@ -138,6 +148,11 @@ export function useTimelineEditing({
138
148
  value: String(updates.track),
139
149
  });
140
150
  }).then(() => {
151
+ if (sdkSession)
152
+ runShadowTiming(sdkSession, element.hfId, {
153
+ start: updates.start,
154
+ trackIndex: updates.track,
155
+ });
141
156
  const pid = projectIdRef.current;
142
157
  if (delta !== 0 && element.domId && pid) {
143
158
  return shiftGsapPositions(pid, filePath, element.domId, delta)
@@ -146,7 +161,7 @@ export function useTimelineEditing({
146
161
  }
147
162
  });
148
163
  },
149
- [previewIframeRef, enqueueEdit, activeCompPath, reloadPreview],
164
+ [previewIframeRef, enqueueEdit, activeCompPath, reloadPreview, sdkSession],
150
165
  );
151
166
 
152
167
  const handleTimelineElementResize = useCallback(
@@ -190,6 +205,11 @@ export function useTimelineEditing({
190
205
  }
191
206
  return patched;
192
207
  }).then(() => {
208
+ if (sdkSession)
209
+ runShadowTiming(sdkSession, element.hfId, {
210
+ start: updates.start,
211
+ duration: updates.duration,
212
+ });
193
213
  const pid = projectIdRef.current;
194
214
  if (timingChanged && element.domId && pid) {
195
215
  return scaleGsapPositions(
@@ -207,7 +227,7 @@ export function useTimelineEditing({
207
227
  return reloadPreview();
208
228
  });
209
229
  },
210
- [previewIframeRef, enqueueEdit, activeCompPath, reloadPreview],
230
+ [previewIframeRef, enqueueEdit, activeCompPath, reloadPreview, sdkSession],
211
231
  );
212
232
 
213
233
  const handleTimelineElementDelete = useCallback(
@@ -3,6 +3,7 @@ import { useMusicBeatAnalysis } from "../../hooks/useMusicBeatAnalysis";
3
3
  import { isMusicTrack } from "../../utils/timelineInspector";
4
4
  import { remapBeatAnalysisToComposition } from "../../utils/beatEditActions";
5
5
  import { usePlayerStore, type TimelineElement } from "../store/playerStore";
6
+ import { useExpandedTimelineElements } from "../hooks/useExpandedTimelineElements";
6
7
  import { useMountEffect } from "../../hooks/useMountEffect";
7
8
  import { EditPopover } from "./EditModal";
8
9
  import { defaultTimelineTheme, type TimelineTheme } from "./timelineTheme";
@@ -28,7 +29,10 @@ import {
28
29
  shouldShowTimelineShortcutHint,
29
30
  } from "./timelineLayout";
30
31
  import type { TimelineDropCallbacks } from "./timelineCallbacks";
31
- import { useTimelineEditContext } from "../../contexts/TimelineEditContext";
32
+ import {
33
+ useResolvedTimelineEditCallbacks,
34
+ type TimelineEditOverrides,
35
+ } from "./useResolvedTimelineEditCallbacks";
32
36
 
33
37
  // Re-export pure utilities so existing imports from "./Timeline" still resolve.
34
38
  export {
@@ -45,7 +49,7 @@ export {
45
49
  getDefaultDroppedTrack,
46
50
  } from "./timelineLayout";
47
51
 
48
- interface TimelineProps extends TimelineDropCallbacks {
52
+ interface TimelineProps extends TimelineDropCallbacks, TimelineEditOverrides {
49
53
  onSeek?: (time: number) => void;
50
54
  onDrillDown?: (element: TimelineElement) => void;
51
55
  renderClipContent?: (
@@ -67,6 +71,10 @@ export const Timeline = memo(function Timeline({
67
71
  onAssetDrop,
68
72
  onBlockDrop,
69
73
  onDeleteElement: _onDeleteElement,
74
+ onMoveElement: onMoveElementOverride,
75
+ onResizeElement: onResizeElementOverride,
76
+ onBlockedEditAttempt: onBlockedEditAttemptOverride,
77
+ onSplitElement: onSplitElementOverride,
70
78
  onSelectElement,
71
79
  theme: themeOverrides,
72
80
  }: TimelineProps = {}) {
@@ -80,14 +88,18 @@ export const Timeline = memo(function Timeline({
80
88
  onDeleteAllKeyframes,
81
89
  onChangeKeyframeEase,
82
90
  onMoveKeyframe,
83
- } = useTimelineEditContext();
91
+ } = useResolvedTimelineEditCallbacks({
92
+ onMoveElement: onMoveElementOverride,
93
+ onResizeElement: onResizeElementOverride,
94
+ onBlockedEditAttempt: onBlockedEditAttemptOverride,
95
+ onSplitElement: onSplitElementOverride,
96
+ });
84
97
  const theme = useMemo(() => ({ ...defaultTimelineTheme, ...themeOverrides }), [themeOverrides]);
85
98
  useMusicBeatAnalysis();
86
- const elements = usePlayerStore((s) => s.elements);
99
+ const rawElements = usePlayerStore((s) => s.elements);
100
+ const expandedElements = useExpandedTimelineElements();
87
101
  const beatAnalysis = usePlayerStore((s) => s.beatAnalysis);
88
102
  const musicElement = usePlayerStore((s) => s.elements.find(isMusicTrack) ?? null);
89
-
90
- // Merge user edits + remap beats from audio-file → composition coordinates.
91
103
  const beatEdits = usePlayerStore((s) => s.beatEdits);
92
104
  const adjustedBeatAnalysis = useMemo(
93
105
  () => remapBeatAnalysisToComposition(beatAnalysis, musicElement, beatEdits),
@@ -176,21 +188,21 @@ export const Timeline = memo(function Timeline({
176
188
 
177
189
  const effectiveDuration = useMemo(() => {
178
190
  const safeDur = Number.isFinite(duration) ? duration : 0;
179
- if (elements.length === 0) return safeDur;
180
- const maxEnd = Math.max(...elements.map((el) => el.start + el.duration));
191
+ if (rawElements.length === 0) return safeDur;
192
+ const maxEnd = Math.max(...rawElements.map((el) => el.start + el.duration));
181
193
  const result = Math.max(safeDur, maxEnd);
182
194
  return Number.isFinite(result) ? result : safeDur;
183
- }, [elements, duration]);
195
+ }, [rawElements, duration]);
184
196
 
185
197
  const tracks = useMemo(() => {
186
- const map = new Map<number, typeof elements>();
187
- for (const el of elements) {
198
+ const map = new Map<number, typeof expandedElements>();
199
+ for (const el of expandedElements) {
188
200
  const list = map.get(el.track) ?? [];
189
201
  list.push(el);
190
202
  map.set(el.track, list);
191
203
  }
192
204
  return Array.from(map.entries()).sort(([a], [b]) => a - b);
193
- }, [elements]);
205
+ }, [expandedElements]);
194
206
 
195
207
  const trackStyles = useMemo(() => {
196
208
  const map = new Map<number, TrackVisualStyle>();
@@ -247,8 +259,9 @@ export const Timeline = memo(function Timeline({
247
259
  const toggleSelectedKeyframe = usePlayerStore((s) => s.toggleSelectedKeyframe);
248
260
 
249
261
  const selectedElement = useMemo(
250
- () => elements.find((element) => (element.key ?? element.id) === selectedElementId) ?? null,
251
- [elements, selectedElementId],
262
+ () =>
263
+ expandedElements.find((element) => (element.key ?? element.id) === selectedElementId) ?? null,
264
+ [expandedElements, selectedElementId],
252
265
  );
253
266
  const selectedElementRef = useRef<TimelineElement | null>(selectedElement);
254
267
  selectedElementRef.current = selectedElement;
@@ -283,7 +296,7 @@ export const Timeline = memo(function Timeline({
283
296
  effectiveDuration,
284
297
  pps,
285
298
  timelineReady,
286
- elementsLength: elements.length,
299
+ elementsLength: expandedElements.length,
287
300
  setZoomMode,
288
301
  setManualZoomPercent,
289
302
  onSeek,
@@ -332,7 +345,7 @@ export const Timeline = memo(function Timeline({
332
345
 
333
346
  useEffect(() => {
334
347
  syncShortcutHintVisibility();
335
- }, [syncShortcutHintVisibility, timelineReady, elements.length, totalH]);
348
+ }, [syncShortcutHintVisibility, timelineReady, expandedElements.length, totalH]);
336
349
 
337
350
  const getPreviewElement = useCallback(
338
351
  (element: TimelineElement): TimelineElement => {
@@ -362,7 +375,7 @@ export const Timeline = memo(function Timeline({
362
375
  onBlockDrop,
363
376
  });
364
377
 
365
- if (!timelineReady || elements.length === 0) {
378
+ if (!timelineReady || expandedElements.length === 0) {
366
379
  return (
367
380
  <TimelineEmptyState
368
381
  isDragOver={isDragOver}
@@ -482,7 +495,7 @@ export const Timeline = memo(function Timeline({
482
495
  }
483
496
  }}
484
497
  onContextMenuKeyframe={(e, elId, pct) => {
485
- const el = elements.find((x) => (x.key ?? x.id) === elId);
498
+ const el = expandedElements.find((x) => (x.key ?? x.id) === elId);
486
499
  if (el) {
487
500
  setSelectedElementId(elId);
488
501
  onSelectElement?.(el);
@@ -102,7 +102,7 @@ export const TimelineClip = memo(function TimelineClip({
102
102
  onDoubleClick={onDoubleClick}
103
103
  onContextMenu={onContextMenu}
104
104
  >
105
- {/* Left accent stripe */}
105
+ {/* Left accent stripe — wider + brighter for expanded sub-comp children */}
106
106
  <div
107
107
  aria-hidden="true"
108
108
  style={{
@@ -110,9 +110,9 @@ export const TimelineClip = memo(function TimelineClip({
110
110
  left: 0,
111
111
  top: 0,
112
112
  bottom: 0,
113
- width: 3,
113
+ width: el.expandedParentStart !== undefined ? 4 : 3,
114
114
  background: trackStyle.accent,
115
- opacity: isSelected ? 0.7 : 0.3,
115
+ opacity: el.expandedParentStart !== undefined ? 0.8 : isSelected ? 0.7 : 0.3,
116
116
  borderRadius: `${theme.clipRadius} 0 0 ${theme.clipRadius}`,
117
117
  zIndex: 2,
118
118
  pointerEvents: "none",
@@ -0,0 +1,30 @@
1
+ import { useMemo } from "react";
2
+ import { useTimelineEditContext } from "../../contexts/TimelineEditContext";
3
+ import type { TimelineEditCallbacks } from "./timelineCallbacks";
4
+
5
+ // Props a parent (e.g. NLELayout) may pass to <Timeline> to intercept edits —
6
+ // the rest of the callback bag still comes from TimelineEditContext.
7
+ export type TimelineEditOverrides = Pick<
8
+ TimelineEditCallbacks,
9
+ "onMoveElement" | "onResizeElement" | "onBlockedEditAttempt" | "onSplitElement"
10
+ >;
11
+
12
+ // Merge any prop overrides over the context callbacks. Used so NLELayout can
13
+ // wrap move/resize/split (to rebase expanded sub-comp clips) while every other
14
+ // callback falls through to the context unchanged.
15
+ export function useResolvedTimelineEditCallbacks(
16
+ overrides: TimelineEditOverrides,
17
+ ): TimelineEditCallbacks {
18
+ const ctx = useTimelineEditContext();
19
+ const { onMoveElement, onResizeElement, onBlockedEditAttempt, onSplitElement } = overrides;
20
+ return useMemo(
21
+ () => ({
22
+ ...ctx,
23
+ onMoveElement: onMoveElement ?? ctx.onMoveElement,
24
+ onResizeElement: onResizeElement ?? ctx.onResizeElement,
25
+ onBlockedEditAttempt: onBlockedEditAttempt ?? ctx.onBlockedEditAttempt,
26
+ onSplitElement: onSplitElement ?? ctx.onSplitElement,
27
+ }),
28
+ [ctx, onMoveElement, onResizeElement, onBlockedEditAttempt, onSplitElement],
29
+ );
30
+ }
@@ -0,0 +1,91 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { buildExpandedElements } from "./useExpandedTimelineElements";
3
+ import type { TimelineElement } from "../store/playerStore";
4
+ import type { ClipManifestClip } from "../lib/playbackTypes";
5
+
6
+ const clip = (over: Partial<ClipManifestClip>): ClipManifestClip => ({
7
+ id: "x",
8
+ label: "x",
9
+ start: 0,
10
+ duration: 1,
11
+ track: 0,
12
+ kind: "element",
13
+ tagName: "div",
14
+ compositionId: null,
15
+ parentCompositionId: null,
16
+ compositionSrc: null,
17
+ assetUrl: null,
18
+ ...over,
19
+ });
20
+
21
+ const el = (over: Partial<TimelineElement>): TimelineElement =>
22
+ ({ id: "x", start: 0, duration: 1, track: 0, tag: "div", ...over }) as TimelineElement;
23
+
24
+ describe("buildExpandedElements", () => {
25
+ it("rebases a 1-level child onto its sub-comp host (start + sourceFile)", () => {
26
+ // host s3 at absolute 16 → stats-panel.html; children live in that file.
27
+ const elements = [el({ id: "s3", start: 16, duration: 7, compositionSrc: "stats.html" })];
28
+ const manifest = [
29
+ clip({ id: "s3", start: 16, duration: 7, compositionSrc: "stats.html" }),
30
+ clip({ id: "stat-1", start: 16.5, duration: 5 }),
31
+ clip({ id: "stat-2", start: 16.9, duration: 5 }),
32
+ ];
33
+ const parentMap = new Map([
34
+ ["stat-1", "s3"],
35
+ ["stat-2", "s3"],
36
+ ]);
37
+
38
+ const out = buildExpandedElements(elements, manifest, parentMap, "s3", "s3");
39
+ const child = out.find((e) => e.domId === "stat-1")!;
40
+ expect(child.expandedParentStart).toBe(16);
41
+ expect(child.sourceFile).toBe("stats.html");
42
+ });
43
+
44
+ it("rebases a 2-level child onto its NESTED host, not the top-level scene", () => {
45
+ // top host A@10 (a.html) embeds host B@12 (b.html); child C lives in b.html.
46
+ // Edits must rebase onto B (12 / b.html), not A (10 / a.html).
47
+ const elements = [el({ id: "A", start: 10, duration: 8, compositionSrc: "a.html" })];
48
+ const manifest = [
49
+ clip({ id: "A", start: 10, duration: 8, compositionSrc: "a.html" }),
50
+ clip({ id: "B", start: 12, duration: 4, compositionSrc: "b.html" }),
51
+ clip({ id: "C", start: 13, duration: 2 }),
52
+ clip({ id: "C2", start: 14, duration: 1 }),
53
+ ];
54
+ const parentMap = new Map([
55
+ ["B", "A"],
56
+ ["C", "B"],
57
+ ["C2", "B"],
58
+ ]);
59
+
60
+ // Expanding C's siblings: topLevel A, immediate parent B.
61
+ const out = buildExpandedElements(elements, manifest, parentMap, "A", "B");
62
+ const child = out.find((e) => e.domId === "C")!;
63
+ expect(child.expandedParentStart).toBe(12); // B's start, not A's 10
64
+ expect(child.sourceFile).toBe("b.html"); // B's file, not a.html
65
+ });
66
+
67
+ it("rebases a 3-level child onto its deepest host, not intermediate or top", () => {
68
+ // A@10 (a.html) → B@12 (b.html) → C@13 (c.html); leaf D lives in c.html.
69
+ // Edits must rebase onto C (13 / c.html), not B (12 / b.html) or A (10 / a.html).
70
+ const elements = [el({ id: "A", start: 10, duration: 8, compositionSrc: "a.html" })];
71
+ const manifest = [
72
+ clip({ id: "A", start: 10, duration: 8, compositionSrc: "a.html" }),
73
+ clip({ id: "B", start: 12, duration: 5, compositionSrc: "b.html" }),
74
+ clip({ id: "C", start: 13, duration: 3, compositionSrc: "c.html" }),
75
+ clip({ id: "D", start: 13.5, duration: 1 }),
76
+ clip({ id: "D2", start: 14, duration: 1 }),
77
+ ];
78
+ const parentMap = new Map([
79
+ ["B", "A"],
80
+ ["C", "B"],
81
+ ["D", "C"],
82
+ ["D2", "C"],
83
+ ]);
84
+
85
+ // Expanding D's siblings: topLevel A, immediate parent C.
86
+ const out = buildExpandedElements(elements, manifest, parentMap, "A", "C");
87
+ const child = out.find((e) => e.domId === "D")!;
88
+ expect(child.expandedParentStart).toBe(13); // C's start, not B's 12 or A's 10
89
+ expect(child.sourceFile).toBe("c.html"); // C's file, not b.html or a.html
90
+ });
91
+ });
@@ -0,0 +1,153 @@
1
+ import { useMemo } from "react";
2
+ import { usePlayerStore, type TimelineElement } from "../store/playerStore";
3
+ import type { ClipManifestClip } from "../lib/playbackTypes";
4
+ import { createTimelineElementFromManifestClip } from "../lib/timelineDOM";
5
+
6
+ function findTopLevelAncestor(id: string, parentMap: Map<string, string>): string | null {
7
+ let current = parentMap.get(id);
8
+ if (!current) return null;
9
+ const visited = new Set<string>();
10
+ visited.add(id);
11
+ while (parentMap.has(current)) {
12
+ if (visited.has(current)) return current;
13
+ visited.add(current);
14
+ current = parentMap.get(current)!;
15
+ }
16
+ return current;
17
+ }
18
+
19
+ function extractDomId(key: string): string {
20
+ const hashIdx = key.lastIndexOf("#");
21
+ return hashIdx >= 0 ? key.slice(hashIdx + 1) : key;
22
+ }
23
+
24
+ function resolveRawId(
25
+ selectedId: string | null,
26
+ manifest: ClipManifestClip[],
27
+ parentMap: Map<string, string>,
28
+ ): string | null {
29
+ if (!selectedId) return null;
30
+ const rawId = extractDomId(selectedId);
31
+ if (parentMap.has(rawId)) return rawId;
32
+ if (parentMap.has(selectedId)) return selectedId;
33
+ const clip = manifest.find((c) => c.label === selectedId || c.label === rawId);
34
+ if (clip?.id && parentMap.has(clip.id)) return clip.id;
35
+ return null;
36
+ }
37
+
38
+ function filterToTopLevel(
39
+ elements: TimelineElement[],
40
+ parentMap: Map<string, string>,
41
+ ): TimelineElement[] {
42
+ if (parentMap.size === 0) return elements;
43
+ return elements.filter((el) => !parentMap.has(el.domId ?? el.id));
44
+ }
45
+
46
+ function clampChildToParent(
47
+ child: ClipManifestClip,
48
+ parentStart: number,
49
+ parentEnd: number,
50
+ ): { start: number; duration: number } | null {
51
+ const childEnd = child.start + child.duration;
52
+ if (child.start >= parentEnd || childEnd <= parentStart) return null;
53
+ const clampedStart = Math.max(child.start, parentStart);
54
+ const clampedDuration = Math.min(childEnd, parentEnd) - clampedStart;
55
+ return clampedDuration > 0 ? { start: clampedStart, duration: clampedDuration } : null;
56
+ }
57
+
58
+ interface DisplayBounds {
59
+ start: number;
60
+ end: number;
61
+ track: number;
62
+ }
63
+
64
+ // `display` bounds come from the top-level scene clip (where the expanded row is
65
+ // drawn). `editBasis` comes from the child's immediate sub-comp host: its absolute
66
+ // start anchors local-time edits and its compositionSrc is the file edits write to.
67
+ // They differ only for sub-comp-inside-sub-comp nesting.
68
+ function buildChildElements(
69
+ siblings: ClipManifestClip[],
70
+ display: DisplayBounds,
71
+ editBasis: { start: number; sourceFile: string | undefined },
72
+ ): TimelineElement[] {
73
+ const result: TimelineElement[] = [];
74
+ for (const child of siblings) {
75
+ const clamped = clampChildToParent(child, display.start, display.end);
76
+ if (!clamped) continue;
77
+ const base = createTimelineElementFromManifestClip({
78
+ clip: child,
79
+ fallbackIndex: result.length,
80
+ });
81
+ result.push({
82
+ ...base,
83
+ start: clamped.start,
84
+ duration: clamped.duration,
85
+ track: display.track + result.length,
86
+ expandedParentStart: editBasis.start,
87
+ domId: child.id ?? undefined,
88
+ selector: child.id ? `#${child.id}` : undefined,
89
+ sourceFile: editBasis.sourceFile,
90
+ timingSource: "authored" as const,
91
+ });
92
+ }
93
+ return result;
94
+ }
95
+
96
+ // Exported for tests.
97
+ export function buildExpandedElements(
98
+ elements: TimelineElement[],
99
+ manifest: ClipManifestClip[],
100
+ parentMap: Map<string, string>,
101
+ topLevelId: string,
102
+ siblingParentId: string,
103
+ ): TimelineElement[] {
104
+ const topLevelElement = elements.find((el) => el.id === topLevelId || el.domId === topLevelId);
105
+ if (!topLevelElement) return filterToTopLevel(elements, parentMap);
106
+
107
+ const siblings = manifest.filter((c) => c.id != null && parentMap.get(c.id) === siblingParentId);
108
+ if (siblings.length === 0) return filterToTopLevel(elements, parentMap);
109
+
110
+ // The sub-comp host the children actually live in: top-level host for 1-level
111
+ // nesting, a nested host for deeper nesting. Its start/file anchor edits.
112
+ const parentHost = manifest.find((c) => c.id === siblingParentId);
113
+ const editBasis = {
114
+ start: parentHost?.start ?? topLevelElement.start,
115
+ sourceFile: parentHost?.compositionSrc ?? topLevelElement.compositionSrc ?? undefined,
116
+ };
117
+
118
+ const parentKey = topLevelElement.key ?? topLevelElement.id;
119
+ const expanded = buildChildElements(
120
+ siblings,
121
+ {
122
+ start: topLevelElement.start,
123
+ end: topLevelElement.start + topLevelElement.duration,
124
+ track: topLevelElement.track,
125
+ },
126
+ editBasis,
127
+ );
128
+ if (expanded.length === 0) return filterToTopLevel(elements, parentMap);
129
+
130
+ return elements
131
+ .filter((el) => (el.key ?? el.id) === parentKey || !parentMap.has(el.domId ?? el.id))
132
+ .flatMap((el) => ((el.key ?? el.id) === parentKey ? expanded : [el]));
133
+ }
134
+
135
+ export function useExpandedTimelineElements(): TimelineElement[] {
136
+ const elements = usePlayerStore((s) => s.elements);
137
+ const clipManifest = usePlayerStore((s) => s.clipManifest);
138
+ const clipParentMap = usePlayerStore((s) => s.clipParentMap);
139
+ const selectedElementId = usePlayerStore((s) => s.selectedElementId);
140
+
141
+ return useMemo(() => {
142
+ if (!clipManifest || clipManifest.length === 0 || clipParentMap.size === 0) {
143
+ return elements;
144
+ }
145
+
146
+ const rawId = resolveRawId(selectedElementId, clipManifest, clipParentMap);
147
+ if (!rawId) return filterToTopLevel(elements, clipParentMap);
148
+
149
+ const immediateParent = clipParentMap.get(rawId)!;
150
+ const topLevel = findTopLevelAncestor(rawId, clipParentMap) ?? immediateParent;
151
+ return buildExpandedElements(elements, clipManifest, clipParentMap, topLevel, immediateParent);
152
+ }, [elements, clipManifest, clipParentMap, selectedElementId]);
153
+ }
@@ -66,6 +66,8 @@ export function useTimelineSyncCallbacks({
66
66
  return;
67
67
  }
68
68
 
69
+ usePlayerStore.getState().setClipManifest(data.clips);
70
+
69
71
  // Show root-level clips: no parentCompositionId, OR parent is a "phantom wrapper"
70
72
  const clipCompositionIds = new Set(data.clips.map((c) => c.compositionId).filter(Boolean));
71
73
  const filtered = data.clips.filter(
@@ -77,6 +79,26 @@ export function useTimelineSyncCallbacks({
77
79
  } catch {
78
80
  iframeDoc = null;
79
81
  }
82
+
83
+ try {
84
+ const iframeWin = iframeRef.current?.contentWindow as
85
+ | (Window & { __clipTree?: import("@hyperframes/core/runtime/clipTree").ClipTree })
86
+ | null;
87
+ const clipTree = iframeWin?.__clipTree;
88
+ if (clipTree) {
89
+ const parentMap = new Map<string, string>();
90
+ const walk = (nodes: typeof clipTree.roots) => {
91
+ for (const node of nodes) {
92
+ if (node.id && node.parentId) parentMap.set(node.id, node.parentId);
93
+ if (node.children.length > 0) walk(node.children);
94
+ }
95
+ };
96
+ walk(clipTree.roots);
97
+ usePlayerStore.getState().setClipParentMap(parentMap);
98
+ }
99
+ } catch {
100
+ // cross-origin or __clipTree not available — parentMap stays empty
101
+ }
80
102
  const usedHostEls = new Set<Element>();
81
103
  const els: TimelineElement[] = filtered.map((clip, index) => {
82
104
  const hostEl = iframeDoc