|
| 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 | +} |
0 commit comments