mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-24 08:00:31 +00:00
* fix(studio): clips inside nested compositions sit at their master time * fix(studio): nested clip starts read authored scene lengths as the runtime does * test(studio): drop a reference-start case the DOM fallback never reads * test(studio): a clip inside a referenced scene sits after its authored length * fix(studio): drag and trim a nested clip to where it is drawn * fix(studio): keep a root-time nested video's drag in master time * fix(studio): duplicate and keyframe edits on a nested clip use its own clock * fix(studio): nested clip drops stop at the host start and trims scale on its clock * fix(studio): a nested clip's head trim stops at its host's start * fix(studio): a dropped nested clip stops at its host's start before snapping
512 lines
19 KiB
TypeScript
512 lines
19 KiB
TypeScript
import { type TimelineElement, usePlayerStore } from "../player/store/playerStore";
|
|
import { toAuthoredStart } from "../player/store/timelineElement";
|
|
import {
|
|
applyPatchByTarget,
|
|
findTagByTarget,
|
|
readAttributeByTarget,
|
|
readTagSnippetByTarget,
|
|
type PatchOperation,
|
|
} from "../utils/sourcePatcher";
|
|
import {
|
|
formatTimelineAttributeNumber,
|
|
formatTimelineMediaOffset,
|
|
type TimelineStackingReorderIntent,
|
|
} from "../player/components/timelineEditing";
|
|
import { getElementZIndex } from "../player/lib/layerOrdering";
|
|
import {
|
|
furthestClipEndFromSource,
|
|
getTimelineElementIdentity,
|
|
playbackStartAttributeForElement,
|
|
readPlaybackStartAttributes,
|
|
} from "../player/lib/timelineElementHelpers";
|
|
import { resolveTimelinePlaybackRate } from "../player/components/timelineGroupEditing";
|
|
import {
|
|
saveProjectFilesWithHistory,
|
|
writeProjectFilesWithHistoryInQueue,
|
|
type RecordEditInput,
|
|
} from "../utils/studioFileHistory";
|
|
import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";
|
|
import type { TimelineZIndexReorderCommit } from "./useTimelineEditingTypes";
|
|
import { setCompositionDurationToContent } from "../utils/timelineAssetDrop";
|
|
import { readFileContent } from "./timelineTimingSync";
|
|
import {
|
|
findElementForSelection,
|
|
findElementForTimelineElement,
|
|
} from "../components/editor/domEditingElement";
|
|
export { deleteSelectedKeyframes } from "./deleteSelectedKeyframes";
|
|
export { readFileContent };
|
|
function isHTMLElement(element: Element | null): element is HTMLElement {
|
|
if (!element) return false;
|
|
// Use the element's OWN realm's HTMLElement: timeline clips live in the preview
|
|
// iframe, and cross-realm `element instanceof HTMLElement` (main window) is
|
|
// always false — which silently dropped every timeline z-index commit.
|
|
const Ctor = element.ownerDocument?.defaultView?.HTMLElement ?? globalThis.HTMLElement;
|
|
return element instanceof Ctor;
|
|
}
|
|
/**
|
|
* Resolve a timeline vertical move to a z-index stacking reorder and commit it
|
|
* through the shared layers-panel reorder path. Reads live sibling z-index from
|
|
* the preview DOM, remaps with the dup-preserving reorder math, and writes only
|
|
* z-index (never data-track-index). No-op when the move isn't a reorder, the
|
|
* dragged clip is audio (no visual layer to restack), or the live siblings can't
|
|
* be resolved. Extracted from StudioApp's timeline hook to keep it under the
|
|
* studio 600-LOC cap.
|
|
*/
|
|
// fallow-ignore-next-line complexity
|
|
export function applyTimelineStackingReorder(input: {
|
|
element: TimelineElement;
|
|
stackingReorder: TimelineStackingReorderIntent | null | undefined;
|
|
timelineElements: readonly TimelineElement[];
|
|
iframe: HTMLIFrameElement | null;
|
|
activeCompPath: string | null;
|
|
commit: TimelineZIndexReorderCommit | null | undefined;
|
|
coalesceKey?: string;
|
|
}): Promise<void> {
|
|
// Audio has no visual stacking; a vertical drag on it must never write z-index.
|
|
if (input.element.tag === "audio") return Promise.resolve();
|
|
const intent = input.stackingReorder ?? null;
|
|
if (intent == null || intent.zIndexChanges.length === 0) return Promise.resolve();
|
|
// Resolve each change's live element from the change's OWN locator (the intent
|
|
// is self-contained), falling back to the top-level element list. Sub-comp
|
|
// children aren't in `timelineElements`, so a list-only lookup would miss them.
|
|
const siblingByKey = new Map(
|
|
input.timelineElements.map((el) => [getTimelineElementIdentity(el), el]),
|
|
);
|
|
const doc = input.iframe?.contentDocument ?? null;
|
|
const commitEntries: Array<{
|
|
element: HTMLElement;
|
|
zIndex: number;
|
|
id?: string;
|
|
selector?: string;
|
|
selectorIndex?: number;
|
|
sourceFile: string;
|
|
key: string;
|
|
}> = [];
|
|
for (const change of intent.zIndexChanges) {
|
|
const sibling = siblingByKey.get(change.key);
|
|
const domId = change.domId ?? sibling?.domId;
|
|
const selector = change.selector ?? sibling?.selector;
|
|
const selectorIndex = change.selectorIndex ?? sibling?.selectorIndex;
|
|
const sourceFile =
|
|
change.sourceFile ?? sibling?.sourceFile ?? input.activeCompPath ?? "index.html";
|
|
const element = doc
|
|
? findElementForSelection(
|
|
doc,
|
|
{ id: domId, selector, selectorIndex, sourceFile },
|
|
input.activeCompPath,
|
|
)
|
|
: null;
|
|
if (!isHTMLElement(element)) return Promise.resolve();
|
|
if (getElementZIndex(element) === change.zIndex) continue;
|
|
commitEntries.push({
|
|
element,
|
|
zIndex: change.zIndex,
|
|
id: domId ?? sibling?.id ?? change.key,
|
|
selector,
|
|
selectorIndex,
|
|
sourceFile,
|
|
key: change.key,
|
|
});
|
|
}
|
|
if (commitEntries.length === 0) return Promise.resolve();
|
|
// The durability report is for gesture-level callers (z→lane mirror); this
|
|
// lane-drag z-sync path has no dependent follow-up write — swallow it.
|
|
// Promise.resolve-wrapped: a commit implementation may return void.
|
|
return Promise.resolve(input.commit?.(commitEntries, input.coalesceKey)).then(() => undefined);
|
|
}
|
|
export function extendRootDurationIfNeeded(newEnd: number): boolean {
|
|
const store = usePlayerStore.getState();
|
|
if (newEnd <= store.duration) return false;
|
|
store.setDuration(newEnd);
|
|
return true;
|
|
}
|
|
// ── Types ──
|
|
export type { RecordEditInput } from "../utils/studioFileHistory";
|
|
export function buildPatchTarget(element: {
|
|
domId?: string;
|
|
hfId?: string;
|
|
selector?: string;
|
|
selectorIndex?: number;
|
|
}) {
|
|
if (element.domId) {
|
|
return {
|
|
id: element.domId,
|
|
hfId: element.hfId,
|
|
selector: element.selector,
|
|
selectorIndex: element.selectorIndex,
|
|
};
|
|
}
|
|
if (element.hfId) {
|
|
return { hfId: element.hfId, selector: element.selector, selectorIndex: element.selectorIndex };
|
|
}
|
|
if (element.selector) {
|
|
return { selector: element.selector, selectorIndex: element.selectorIndex };
|
|
}
|
|
return null;
|
|
}
|
|
export type PatchTarget = NonNullable<ReturnType<typeof buildPatchTarget>>;
|
|
// The runtime re-reads data-start/data-duration from the DOM on each sync tick
|
|
// (packages/core/src/runtime/init.ts:1324-1368), so attribute mutations here are
|
|
// picked up automatically on the next frame without a rebind call.
|
|
export function findTimelineElementInIframe(
|
|
iframe: HTMLIFrameElement | null,
|
|
element: TimelineElement,
|
|
activeCompositionPath: string | null = null,
|
|
): Element | null {
|
|
try {
|
|
const doc = iframe?.contentDocument;
|
|
if (!doc) return null;
|
|
if (element.kind === "composition" && element.compositionSrc) {
|
|
return findElementForTimelineElement(doc, element, {
|
|
activeCompositionPath,
|
|
isMasterView: true,
|
|
});
|
|
}
|
|
return findElementForSelection(
|
|
doc,
|
|
{
|
|
hfId: element.hfId,
|
|
id: element.domId,
|
|
selector: element.selector,
|
|
selectorIndex: element.selectorIndex,
|
|
sourceFile: element.sourceFile || activeCompositionPath || "index.html",
|
|
},
|
|
activeCompositionPath,
|
|
);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
export function patchIframeDomTiming(
|
|
iframe: HTMLIFrameElement | null,
|
|
element: TimelineElement,
|
|
attrs: Array<[string, string]>,
|
|
activeCompositionPath: string | null = null,
|
|
): void {
|
|
try {
|
|
const el = findTimelineElementInIframe(iframe, element, activeCompositionPath);
|
|
if (!el) return;
|
|
for (const [name, value] of attrs) el.setAttribute(name, value);
|
|
} catch {
|
|
// Cross-origin or mid-navigation — file save is enqueued; iframe patch is best-effort.
|
|
}
|
|
}
|
|
|
|
/** Takes deleted clips out of the live preview at once: until the reload lands, anything that re-reads the
|
|
* preview (composition enrichment) would otherwise put them back on the timeline. */
|
|
export function removeIframeTimelineElements(
|
|
iframe: HTMLIFrameElement | null,
|
|
elements: TimelineElement[],
|
|
activeCompositionPath: string | null = null,
|
|
): void {
|
|
for (const element of elements)
|
|
findTimelineElementInIframe(iframe, element, activeCompositionPath)?.remove();
|
|
}
|
|
|
|
// fallow-ignore-next-line complexity
|
|
function resolveResizePlaybackStart(
|
|
original: string,
|
|
target: PatchTarget,
|
|
element: TimelineElement,
|
|
updates: Pick<TimelineElement, "start" | "playbackStart">,
|
|
): { attrName: string; value: number } | null {
|
|
if (updates.playbackStart != null) {
|
|
const attrName = playbackStartAttributeForElement(element).slice("data-".length);
|
|
return { attrName, value: updates.playbackStart };
|
|
}
|
|
const trimDelta = updates.start - element.start;
|
|
if (trimDelta === 0) return null;
|
|
const source = readPlaybackStartAttributes((name) =>
|
|
readAttributeByTarget(original, target, name),
|
|
);
|
|
if (source.playbackStart == null) return null;
|
|
const attrName = playbackStartAttributeForElement({ kind: element.kind, ...source }).slice(
|
|
"data-".length,
|
|
);
|
|
return {
|
|
attrName,
|
|
value: Math.max(
|
|
0,
|
|
source.playbackStart + trimDelta * resolveTimelinePlaybackRate(element.playbackRate),
|
|
),
|
|
};
|
|
}
|
|
|
|
export function buildTimelineMoveTimingPatch(
|
|
original: string,
|
|
target: PatchTarget,
|
|
start: number,
|
|
duration: number,
|
|
track?: number,
|
|
): string {
|
|
if (!Number.isFinite(start) || !Number.isFinite(duration)) {
|
|
console.warn(
|
|
`[Timeline] buildTimelineMoveTimingPatch: non-finite timing (start=${start}, duration=${duration}) — patch skipped`,
|
|
);
|
|
return original;
|
|
}
|
|
let patched = applyPatchByTarget(original, target, {
|
|
type: "attribute",
|
|
property: "start",
|
|
value: formatTimelineAttributeNumber(start),
|
|
});
|
|
if (track != null && Number.isFinite(track)) {
|
|
patched = applyPatchByTarget(patched, target, {
|
|
type: "attribute",
|
|
property: "track-index",
|
|
value: formatTimelineAttributeNumber(track),
|
|
});
|
|
}
|
|
// Content-driven duration: sync data-duration to the furthest clip end read
|
|
// from the PATCHED SOURCE (raw data-duration), so it grows if a clip moved
|
|
// past the end and shrinks if the furthest clip moved left. Measured from the
|
|
// source, NOT the store — store durations are runtime-truncated to the current
|
|
// comp length, which would ratchet the duration down every move.
|
|
return setCompositionDurationToContent(patched, furthestClipEndFromSource(patched));
|
|
}
|
|
|
|
export function buildTimelineResizeTimingPatch(
|
|
original: string,
|
|
target: PatchTarget,
|
|
element: TimelineElement,
|
|
updates: Pick<TimelineElement, "start" | "duration" | "playbackStart">,
|
|
): string {
|
|
const pbs = resolveResizePlaybackStart(original, target, element, updates);
|
|
let patched = applyPatchByTarget(original, target, {
|
|
type: "attribute",
|
|
property: "start",
|
|
value: formatTimelineAttributeNumber(toAuthoredStart(element, updates.start)),
|
|
});
|
|
patched = applyPatchByTarget(patched, target, {
|
|
type: "attribute",
|
|
property: "duration",
|
|
value: formatTimelineAttributeNumber(updates.duration),
|
|
});
|
|
if (pbs) {
|
|
patched = applyPatchByTarget(patched, target, {
|
|
type: "attribute",
|
|
property: pbs.attrName,
|
|
value: formatTimelineMediaOffset(pbs.value),
|
|
});
|
|
}
|
|
// Content-driven duration from the PATCHED SOURCE (raw data-duration) —
|
|
// grows/shrinks to the furthest clip end. Not from the store, whose
|
|
// durations are runtime-truncated.
|
|
return setCompositionDurationToContent(patched, furthestClipEndFromSource(patched));
|
|
}
|
|
|
|
export interface PersistTimelineEditInput {
|
|
projectId: string;
|
|
element: TimelineElement;
|
|
activeCompPath: string | null;
|
|
label: string;
|
|
buildPatches: (original: string, target: PatchTarget) => string;
|
|
writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise<void>;
|
|
recordEdit: (input: RecordEditInput) => Promise<void>;
|
|
pendingTimelineEditPathRef: React.MutableRefObject<Set<string>>;
|
|
coalesceKey?: string;
|
|
}
|
|
|
|
export async function persistTimelineEdit(input: PersistTimelineEditInput): Promise<void> {
|
|
const targetPath = input.element.sourceFile || input.activeCompPath || "index.html";
|
|
const patchTarget = buildPatchTarget(input.element);
|
|
if (!patchTarget) {
|
|
throw new Error(`Timeline element ${input.element.id} is missing a patchable target`);
|
|
}
|
|
|
|
input.pendingTimelineEditPathRef.current.add(targetPath);
|
|
await saveProjectFilesWithHistory({
|
|
projectId: input.projectId,
|
|
label: input.label,
|
|
coalesceKey: input.coalesceKey,
|
|
files: {
|
|
[targetPath]: (current) => {
|
|
const patched = input.buildPatches(current, patchTarget);
|
|
if (patched === current) {
|
|
throw new Error(`Unable to patch timeline element ${input.element.id} in ${targetPath}`);
|
|
}
|
|
return patched;
|
|
},
|
|
},
|
|
readFile: (path) => readFileContent(input.projectId, path),
|
|
writeFile: input.writeProjectFile,
|
|
recordEdit: input.recordEdit,
|
|
});
|
|
}
|
|
|
|
export interface PersistTimelineBatchChange {
|
|
element: TimelineElement;
|
|
buildPatches: (original: string, target: PatchTarget) => string;
|
|
}
|
|
|
|
/** One batch change per element, each applying the same patch operation. */
|
|
export function operationChanges(
|
|
elements: readonly TimelineElement[],
|
|
operation: PatchOperation,
|
|
): PersistTimelineBatchChange[] {
|
|
return elements.map((element) => ({
|
|
element,
|
|
buildPatches: (html, target) => applyPatchByTarget(html, target, operation),
|
|
}));
|
|
}
|
|
|
|
/** Patches each change into `source`, failing loudly on a target the file does not hold. */
|
|
export function patchTimelineChangesInSource(
|
|
source: string,
|
|
targetPath: string,
|
|
changes: readonly PersistTimelineBatchChange[],
|
|
): string {
|
|
let current = source;
|
|
for (const { element, buildPatches } of changes) {
|
|
const target = buildPatchTarget(element);
|
|
if (!target) throw new Error(`Timeline element ${element.id} is missing a patchable target`);
|
|
// Resolve first: a member already at its target values patches to the same string, a missing one must throw.
|
|
if (!findTagByTarget(current, target)) {
|
|
throw new Error(`Unable to patch timeline element ${element.id} in ${targetPath}`);
|
|
}
|
|
current = buildPatches(current, target);
|
|
}
|
|
return current;
|
|
}
|
|
|
|
export interface PersistTimelineBatchEditInput {
|
|
projectId: string;
|
|
activeCompPath: string | null;
|
|
label: string;
|
|
changes: PersistTimelineBatchChange[];
|
|
writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise<void>;
|
|
recordEdit: (input: RecordEditInput) => Promise<void>;
|
|
pendingTimelineEditPathRef: React.MutableRefObject<Set<string>>;
|
|
coalesceKey?: string;
|
|
/** Per-entry undo coalesce window override (ms) — see EditHistoryEntry.coalesceMs. */
|
|
coalesceMs?: number;
|
|
}
|
|
|
|
export async function persistTimelineBatchEdit(
|
|
input: PersistTimelineBatchEditInput,
|
|
): Promise<void> {
|
|
const changesByPath = new Map<string, PersistTimelineBatchChange[]>();
|
|
for (const change of input.changes) {
|
|
const targetPath = change.element.sourceFile || input.activeCompPath || "index.html";
|
|
changesByPath.set(targetPath, [...(changesByPath.get(targetPath) ?? []), change]);
|
|
}
|
|
const buildFile = (targetPath: string) => (original: string) => {
|
|
const next = patchTimelineChangesInSource(original, targetPath, changesByPath.get(targetPath)!);
|
|
if (next !== original) input.pendingTimelineEditPathRef.current.add(targetPath);
|
|
return next;
|
|
};
|
|
|
|
await saveProjectFilesWithHistory({
|
|
projectId: input.projectId,
|
|
label: input.label,
|
|
coalesceKey: input.coalesceKey,
|
|
coalesceMs: input.coalesceMs,
|
|
files: Object.fromEntries([...changesByPath.keys()].map((path) => [path, buildFile(path)])),
|
|
readFile: (path) => readFileContent(input.projectId, path),
|
|
writeFile: input.writeProjectFile,
|
|
recordEdit: input.recordEdit,
|
|
});
|
|
}
|
|
|
|
/** What the file holds for `attr` once queued writes to it land; undefined when unreadable. */
|
|
export async function readSavedAttribute(
|
|
projectId: string | null,
|
|
targetPath: string,
|
|
patchTarget: PatchTarget | null,
|
|
attr: string,
|
|
writeFile: (path: string, content: string, expectedContent?: string) => Promise<void>,
|
|
): Promise<string | null | undefined> {
|
|
if (!projectId || !patchTarget) return undefined;
|
|
const html = await serializeStudioFileMutations(writeFile, [targetPath], () =>
|
|
readFileContent(projectId, targetPath),
|
|
).catch(() => null);
|
|
return html === null ? undefined : readTargetAttribute(html, patchTarget, attr);
|
|
}
|
|
|
|
/** What `html` holds for `attr` on the target; undefined when the target is not in it. */
|
|
export function readTargetAttribute(
|
|
html: string,
|
|
patchTarget: PatchTarget,
|
|
attr: string,
|
|
): string | null | undefined {
|
|
if (readTagSnippetByTarget(html, patchTarget) === undefined) return undefined;
|
|
return readAttributeByTarget(html, patchTarget, attr) ?? null;
|
|
}
|
|
|
|
export { formatTimelineAttributeNumber, formatTimelineMediaOffset };
|
|
|
|
export { patchDocumentRootDuration } from "./timelineEditingGsap";
|
|
|
|
export interface PersistElementAttributeInput {
|
|
projectId: string;
|
|
targetPath: string;
|
|
patchTarget: PatchTarget;
|
|
attr: string;
|
|
value: string | null;
|
|
label: string;
|
|
writeProjectFile: (path: string, content: string) => Promise<void>;
|
|
recordEdit: (input: RecordEditInput) => Promise<void>;
|
|
pendingTimelineEditPathRef: { current: Set<string> };
|
|
/** Write the attribute directly on the live preview DOM node. */
|
|
patchLive: (value: string | null) => void;
|
|
/** What the file held for the attribute, read inside the queue before this write. */
|
|
onFileRead: (value: string | null) => void;
|
|
}
|
|
|
|
/**
|
|
* One attribute, persisted to source and optimistically patched onto the
|
|
* live preview, with a revert on save failure. The shared core behind
|
|
* `setAudioGroupAttribute` (a group id addressed by its own DOM id) and
|
|
* `useSetElementAttribute` (an arbitrary timeline clip) — same shape, only
|
|
* how the live node is found and where the patch target resolves to differs,
|
|
* which is exactly what `patchLive`/`patchTarget` parameterize.
|
|
*/
|
|
export async function persistElementAttribute({
|
|
projectId,
|
|
targetPath,
|
|
patchTarget,
|
|
attr,
|
|
value,
|
|
label,
|
|
writeProjectFile,
|
|
recordEdit,
|
|
pendingTimelineEditPathRef,
|
|
patchLive,
|
|
onFileRead,
|
|
}: PersistElementAttributeInput): Promise<string[]> {
|
|
// Joins the file's mutation queue before reading, so saves land in the order they start
|
|
// and each patches what the save before it wrote. Resolve the target before patching
|
|
// the live DOM, so an unresolvable target never leaves an unsaved preview.
|
|
return serializeStudioFileMutations(writeProjectFile, [targetPath], async () => {
|
|
const before = await readFileContent(projectId, targetPath);
|
|
if (readTagSnippetByTarget(before, patchTarget) === undefined) {
|
|
throw new Error(`Unable to patch element in ${targetPath}`);
|
|
}
|
|
// Unwind to the file's value: live writers already patched the DOM, so `readLive()`
|
|
// would equal `value` and a failed save would keep a never-saved preview.
|
|
const previousValue = readAttributeByTarget(before, patchTarget, attr) ?? null;
|
|
onFileRead(previousValue);
|
|
patchLive(value);
|
|
|
|
const operation: PatchOperation = { type: "attribute", property: attr, value };
|
|
const patched = applyPatchByTarget(before, patchTarget, operation);
|
|
|
|
pendingTimelineEditPathRef.current.add(targetPath);
|
|
try {
|
|
return await writeProjectFilesWithHistoryInQueue({
|
|
projectId,
|
|
label,
|
|
files: { [targetPath]: () => patched },
|
|
readFile: async () => before,
|
|
writeFile: writeProjectFile,
|
|
recordEdit,
|
|
});
|
|
} catch (error) {
|
|
// The optimistic live write already ran; unwind it on a save failure so
|
|
// the preview doesn't show a value that never reached disk.
|
|
patchLive(previousValue);
|
|
throw error;
|
|
}
|
|
});
|
|
}
|