Skip to content

Commit 8cbf438

Browse files
feat(studio): timeline inline expansion + __clipTree runtime primitive
When a child element inside a sub-composition is selected, the timeline replaces the parent scene clip with the deepest-level siblings. Deselect or selecting outside collapses back. Expanded clips are fully editable — move, resize, delete, and split — addressed by their real DOM id with timeline time rebased onto the sub-comp they live in. Runtime: - New window.__clipTree API: a read-only hierarchical ClipNode tree (id/parentId/children + backing element) so Studio can derive parent/child relationships for inline expansion. Studio: - useExpandedTimelineElements derives the expanded view from selectedElementId + clipParentMap (pure useMemo, no useEffect). Each child rebases onto its immediate sub-comp host (start + sourceFile), so multi-level nesting targets the right file. - NLELayout routes expanded-clip edits through the same handlers top-level clips use, in local coordinates — edits save to the sub-comp source and reflect via reloadPreview (no separate DOM-patch path). This is the canonical update; there is no reactive observer. - findMatchingTimelineElementId resolves sub-comp children with no top-level element to `sourceFile#id`. - Razor tool enabled by default; studio_razor_split analytics event fired on single and split-all. - O(n²) isElementGsapTargeted extracted to gsapTargetCache.ts with a cached Set+WeakSet O(1) lookup.
1 parent 0703029 commit 8cbf438

25 files changed

Lines changed: 843 additions & 126 deletions

.fallowrc.jsonc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"packages/studio/src/components/nle/TimelineEditorNotice.tsx",
4242
// Zoom hook extracted for downstream razor-blade PRs (#1330, #1331).
4343
"packages/studio/src/player/components/useTimelineZoom.ts",
44+
// Cached O(1) GSAP target lookup, replacing O(n²) inline checks.
45+
// Consumers migrate in a follow-up once useDomGeometryCommits adopts it.
46+
"packages/studio/src/hooks/gsapTargetCache.ts",
4447
// Preview helper consumed dynamically from the studio iframe bridge.
4548
"packages/studio/src/hooks/gsapRuntimePreview.ts",
4649
],
@@ -151,6 +154,19 @@
151154
"file": "packages/studio/src/player/components/timelineCallbacks.ts",
152155
"exports": ["*"],
153156
},
157+
// gsapTargetCache: cached O(1) GSAP target lookup, consumed by
158+
// useDomEditCommits and intended to replace the local copy in
159+
// useDomGeometryCommits once callers migrate.
160+
{
161+
"file": "packages/studio/src/hooks/gsapTargetCache.ts",
162+
"exports": ["isElementGsapTargeted"],
163+
},
164+
// Re-exports from useDomEditCommits: barrel-style re-exports
165+
// consumed by downstream studio code.
166+
{
167+
"file": "packages/studio/src/hooks/useDomEditCommits.ts",
168+
"exports": ["GSAP_CSS_FALLBACK_BLOCKED_MESSAGE", "PersistDomEditOperations"],
169+
},
154170
{
155171
"file": "packages/studio/src/utils/timelineElementSplit.ts",
156172
"exports": ["buildPatchTarget", "readFileContent"],

packages/core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"types": "./src/compiler/index.ts"
3636
},
3737
"./runtime": "./dist/hyperframe.runtime.iife.js",
38+
"./runtime/clipTree": {
39+
"import": "./src/runtime/clipTree.ts",
40+
"types": "./src/runtime/clipTree.ts"
41+
},
3842
"./runtime/lottie-readiness": {
3943
"import": "./src/lottieReadiness.ts",
4044
"types": "./src/lottieReadiness.ts"
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* window.__clipTree — hierarchical clip tree for Studio.
3+
*
4+
* Maps every timed element to a node so Studio can derive parent/child
5+
* relationships for inline timeline expansion. Read-only: timing edits go
6+
* through the host's normal save → reloadPreview path, not a DOM patch here.
7+
*
8+
* ponytail: intentionally minimal. Node carries only what consumers read
9+
* (id/parentId/children) plus the backing element. Add fields (label, kind,
10+
* absolute start) when a caller needs them.
11+
*/
12+
13+
import type { RuntimeTimelineLike } from "./types";
14+
15+
export interface ClipNode {
16+
readonly id: string;
17+
readonly element: Element;
18+
readonly parentId: string | null;
19+
readonly children: readonly ClipNode[];
20+
}
21+
22+
export interface ClipTree {
23+
readonly roots: readonly ClipNode[];
24+
}
25+
26+
// Mutable shape used only while building; the public ClipTree exposes it as
27+
// readonly so Studio consumers can't accidentally mutate the live tree.
28+
type MutableClipNode = {
29+
id: string;
30+
element: Element;
31+
parentId: string | null;
32+
children: MutableClipNode[];
33+
};
34+
35+
const DECORATIVE_TAGS = new Set(["SCRIPT", "STYLE", "LINK", "META", "TEMPLATE", "NOSCRIPT"]);
36+
37+
interface StartResolverLike {
38+
resolveStartForElement: (element: Element, fallback?: number) => number;
39+
}
40+
41+
function parseNum(value: string | null): number | null {
42+
if (value == null) return null;
43+
const n = Number(value);
44+
return Number.isFinite(n) ? n : null;
45+
}
46+
47+
function durationFromTimeline(
48+
el: Element,
49+
registry: Record<string, RuntimeTimelineLike | undefined>,
50+
): number | null {
51+
const compId = el.getAttribute("data-composition-id");
52+
if (!compId) return null;
53+
const d = Number(registry[compId]?.duration?.());
54+
return Number.isFinite(d) && d > 0 ? d : null;
55+
}
56+
57+
function durationFromMedia(el: Element): number | null {
58+
if (!(el instanceof HTMLMediaElement) || !Number.isFinite(el.duration)) return null;
59+
const mediaStart =
60+
parseNum(el.getAttribute("data-playback-start")) ??
61+
parseNum(el.getAttribute("data-media-start")) ??
62+
0;
63+
return el.duration > mediaStart ? el.duration - mediaStart : null;
64+
}
65+
66+
// Used only to filter out zero-duration (decorative) elements at build time.
67+
function resolveDuration(
68+
el: Element,
69+
timelineRegistry: Record<string, RuntimeTimelineLike | undefined>,
70+
rootDuration: number,
71+
absoluteStart: number,
72+
): number {
73+
const attr = parseNum(el.getAttribute("data-duration"));
74+
if (attr != null && attr > 0) return attr;
75+
return (
76+
durationFromTimeline(el, timelineRegistry) ??
77+
durationFromMedia(el) ??
78+
Math.max(0, rootDuration - absoluteStart)
79+
);
80+
}
81+
82+
function linkParentChild(elementToNode: Map<Element, MutableClipNode>): void {
83+
for (const [el, node] of elementToNode) {
84+
let cursor = el.parentElement;
85+
while (cursor) {
86+
const parentNode = elementToNode.get(cursor);
87+
if (parentNode) {
88+
node.parentId = parentNode.id;
89+
parentNode.children.push(node);
90+
break;
91+
}
92+
cursor = cursor.parentElement;
93+
}
94+
}
95+
}
96+
97+
export function createClipTree(params: {
98+
startResolver: StartResolverLike;
99+
timelineRegistry: Record<string, RuntimeTimelineLike | undefined>;
100+
rootDuration: number;
101+
}): ClipTree {
102+
const { startResolver, timelineRegistry, rootDuration } = params;
103+
const elementToNode = new Map<Element, MutableClipNode>();
104+
105+
const root = document.querySelector("[data-composition-id]");
106+
let ordinal = 0;
107+
108+
for (const el of document.querySelectorAll("[data-start]")) {
109+
if (el === root || DECORATIVE_TAGS.has(el.tagName)) continue;
110+
const absoluteStart = startResolver.resolveStartForElement(el, 0);
111+
if (resolveDuration(el, timelineRegistry, rootDuration, absoluteStart) <= 0) continue;
112+
const node: MutableClipNode = {
113+
id: (el as HTMLElement).id || `__clip-${ordinal++}`,
114+
element: el,
115+
parentId: null,
116+
children: [],
117+
};
118+
elementToNode.set(el, node);
119+
}
120+
121+
linkParentChild(elementToNode);
122+
123+
return {
124+
roots: Array.from(elementToNode.values()).filter((n) => n.parentId === null),
125+
};
126+
}

packages/core/src/runtime/init.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { createRuntimePlayer } from "./player";
2020
import { createRuntimeState } from "./state";
2121
import { collectRuntimeTimelinePayload } from "./timeline";
2222
import { createRuntimeStartTimeResolver } from "./startResolver";
23+
import { createClipTree } from "./clipTree";
2324
import { loadExternalCompositions, loadInlineTemplateCompositions } from "./compositionLoader";
2425
import { applyCaptionOverrides } from "./captionOverrides";
2526
import { TransportClock } from "./clock";
@@ -1560,6 +1561,18 @@ export function initSandboxRuntimeModular(): void {
15601561
});
15611562
};
15621563

1564+
// Signature the live __clipTree was built from; rebuild only when the set of
1565+
// timed elements changes (e.g. a sub-composition finishes loading), not every
1566+
// transport tick. A plain count misses same-count swaps (one sub-comp unloads
1567+
// as another loads), so the signature keys on id+tag in document order.
1568+
let clipTreeSignature = "";
1569+
const computeClipTreeSignature = (): string => {
1570+
let sig = "";
1571+
for (const el of document.querySelectorAll("[data-start]")) {
1572+
sig += `${el.id}:${el.tagName}|`;
1573+
}
1574+
return sig;
1575+
};
15631576
const postTimeline = () => {
15641577
sanitizeCompositionDurationAttributes();
15651578
applyCompositionSizing();
@@ -1580,6 +1593,23 @@ export function initSandboxRuntimeModular(): void {
15801593
canonicalFps: state.canonicalFps,
15811594
});
15821595
window.__clipManifest = payload;
1596+
1597+
const currentSignature = computeClipTreeSignature();
1598+
if (!window.__clipTree || clipTreeSignature !== currentSignature) {
1599+
const runtimeWindow = window as Window & {
1600+
__timelines?: Record<string, RuntimeTimelineLike | undefined>;
1601+
};
1602+
window.__clipTree = createClipTree({
1603+
startResolver: createRuntimeStartTimeResolver({
1604+
timelineRegistry: runtimeWindow.__timelines ?? {},
1605+
includeAuthoredTimingAttrs: true,
1606+
}),
1607+
timelineRegistry: runtimeWindow.__timelines ?? {},
1608+
rootDuration: payload.durationInFrames / state.canonicalFps,
1609+
});
1610+
clipTreeSignature = currentSignature;
1611+
}
1612+
15831613
postRuntimeMessage(payload);
15841614
scheduleRootStageLayoutDiagnostics();
15851615
};

packages/core/src/runtime/window.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RuntimeTimelineMessage, RuntimeTimelineLike } from "./types";
22
import type { HyperframePickerApi } from "../inline-scripts/pickerApi";
33
import type { PlayerAPI } from "../core.types";
4+
import type { ClipTree } from "./clipTree";
45

56
type ThreeClockLike = {
67
elapsedTime: number;
@@ -29,6 +30,7 @@ declare global {
2930
__timelines: Record<string, RuntimeTimelineLike>;
3031
__player?: PlayerAPI;
3132
__clipManifest?: RuntimeTimelineMessage;
33+
__clipTree?: ClipTree;
3234
__playerReady?: boolean;
3335
__renderReady?: boolean;
3436
__hfRuntimeTeardown?: (() => void) | null;

packages/studio/src/components/editor/PropertyPanel.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useEffect, useRef, useState } from "react";
1+
import { memo, useEffect, useMemo, useRef, useState } from "react";
22
import { Eye, Layers, Move, X } from "../../icons/SystemIcons";
33
import { useStudioShellContext } from "../../contexts/StudioContext";
44
import { readStudioBoxSize, readStudioPathOffset, readStudioRotation } from "./manualEdits";
@@ -111,6 +111,29 @@ export const PropertyPanel = memo(function PropertyPanel({
111111
const cacheElementKey = element?.id ?? element?.selector ?? "";
112112
const cacheEntry = usePlayerStore((s) => s.keyframeCache.get(cacheElementKey));
113113

114+
const iframeRef = previewIframeRef ?? { current: null };
115+
const gsapAnimIdForMemo = element
116+
? (gsapAnimations?.find((a: { keyframes?: unknown }) => a.keyframes)?.id ??
117+
gsapAnimations?.[0]?.id ??
118+
null)
119+
: null;
120+
const gsapRuntimeValues = useMemo(
121+
() =>
122+
element
123+
? readGsapRuntimeValuesForPanel(gsapAnimIdForMemo, gsapAnimations, element, iframeRef)
124+
: null,
125+
// eslint-disable-next-line react-hooks/exhaustive-deps -- iframeRef is stable; currentTime drives re-reads during playback
126+
[gsapAnimIdForMemo, gsapAnimations, element, currentTime],
127+
);
128+
const gsapBorderRadius = useMemo(
129+
() =>
130+
element
131+
? readGsapBorderRadiusForPanel(gsapRuntimeValues, gsapAnimations, element, iframeRef)
132+
: null,
133+
// eslint-disable-next-line react-hooks/exhaustive-deps
134+
[gsapRuntimeValues, gsapAnimations, element, currentTime],
135+
);
136+
114137
if (!element) {
115138
return (
116139
<div className="flex h-full flex-col bg-neutral-900">
@@ -194,21 +217,6 @@ export const PropertyPanel = memo(function PropertyPanel({
194217
return gsapAnimId ?? "";
195218
};
196219

197-
// Read ALL GSAP-interpolated values at the current seek time.
198-
const gsapRuntimeValues = readGsapRuntimeValuesForPanel(
199-
gsapAnimId,
200-
gsapAnimations,
201-
element,
202-
previewIframeRef ?? { current: null },
203-
);
204-
205-
const gsapBorderRadius = readGsapBorderRadiusForPanel(
206-
gsapRuntimeValues,
207-
gsapAnimations,
208-
element,
209-
previewIframeRef ?? { current: null },
210-
);
211-
212220
const displayX = gsapRuntimeValues?.x ?? manualOffset.x;
213221
const displayY = gsapRuntimeValues?.y ?? manualOffset.y;
214222
const displayW = gsapRuntimeValues?.width ?? resolvedWidth;

packages/studio/src/components/editor/manualEditingAvailability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const STUDIO_KEYFRAMES_ENABLED = resolveStudioBooleanEnvFlag(
7373
export const STUDIO_RAZOR_TOOL_ENABLED = resolveStudioBooleanEnvFlag(
7474
env,
7575
["VITE_STUDIO_ENABLE_RAZOR_TOOL", "VITE_STUDIO_RAZOR_TOOL_ENABLED"],
76-
false,
76+
true,
7777
);
7878

7979
// When disabled (the default), drag/resize/rotate commits always take the CSS

0 commit comments

Comments
 (0)