Files
openmotion/packages/edit-core/split.ts
open-design-crew[bot] 47a7e650b9 perf(studio,api): optimize timeline thumbnails, sidebar covers, SnapDOM capture, and filmstrip caching (#1128)
* feat(studio): stabilize sidebar loading, add per-item error reload, and cache timeline snapshots

* fix(timeline): inherit duration for untimed root clips, distinguish save failure banners, and block direct canvas splits

* fix(timeline): block splitting canvas-wrapper clips across preview, UI, and edit-core

* fix(timeline): preserve split sub-composition source and seed right-half fallback filmstrips

* feat(studio): add zero-slice sprite thumbnails, 0ms sidebar hover scrub, and native drilled-in media routing

* fix(studio): pin Root composition card cover to master timeline's earliest scene across sub-composition switches

* fix(studio): refresh sidebar covers on sub-composition edits and track trimmed root playback window

* fix(studio): evenly space left dock icon navigation tabs and refine active pill proportion

* fix(studio): match sidebar cover to first visible opening frame and restore live hover playback

* perf(studio): tighten visual filmstrip deps, SnapDOM capture, and S3 write-back gate (Phase 3)

* refactor(studio): modularize composition capture cache, revision registry, and lazy editor dep reads (Phase 4)

* fix(studio,api,edit-core): resolve P0/P1 findings from independent review

* refactor(studio): deduplicate CompositionsTab near-viewport hook via shared useNearViewport

* fix(studio): keep bare relative media paths root-relative unless sibling exists in fileTree

* fix(studio): skip hidden child clips when selecting Root composition cover

---------

Co-authored-by: 罗高 <luogao@users.noreply.github.com>
2026-11-26 17:13:32 +08:00

124 lines
4.1 KiB
TypeScript

import { findTargetElement, nextUniqueId, parseSourceDocument, relinkSplitHalvesInHtml, splitElementInHtml, type SourceMutationTarget } from '@hyperframes/studio-server/source-mutation';
import { isTimelineIgnoredElement } from './content-duration.ts';
import { applyGsapMutationsInMemory } from './gsap-mutations.ts';
export const CANVAS_SPLIT_UNSUPPORTED_MESSAGE =
'Canvas layers cannot be split directly. Trim the clip or ask your Agent to edit the scene.';
/**
* Procedural `<canvas>` elements (and non-sub-composition wrappers around them)
* cannot be split by DOM cloning because the drawing script remains bound to a
* single canvas, leaving the cloned right half empty/black.
*/
export function isUnsplittableCanvasElement(el: Element): boolean {
if (el.hasAttribute('data-composition-src') || el.hasAttribute('data-composition-file')) {
return false;
}
if (el.localName === 'canvas') return !isTimelineIgnoredElement(el);
for (const canvas of Array.from(el.querySelectorAll('canvas'))) {
if (isTimelineIgnoredElement(canvas)) continue;
let isolated = false;
for (let p = canvas.parentElement; p && p !== el; p = p.parentElement) {
if (
p.localName === 'template' ||
p.hasAttribute('data-composition-src') ||
p.hasAttribute('data-composition-file')
) {
isolated = true;
break;
}
}
if (!isolated) return true;
}
return false;
}
export function assertSplittableElementInHtml(html: string, target: SourceMutationTarget): void {
const { document } = parseSourceDocument(html);
const el = findTargetElement(document, target);
if (el && isUnsplittableCanvasElement(el)) {
throw new Error(CANVAS_SPLIT_UNSUPPORTED_MESSAGE);
}
}
export interface AtomicCutTarget {
target: SourceMutationTarget;
originalId?: string;
splitTime: number;
elementStart: number;
elementDuration: number;
playbackStart?: number;
playbackRate?: number;
track?: number;
}
/**
* Apply all cut targets for a single file atomically in memory (DOM split + GSAP split + relink).
*/
export function applyAtomicCutsToHtml(
before: string,
targets: readonly AtomicCutTarget[],
takenIds?: ReadonlySet<string>,
): { after: string; splitCount: number; skippedSelectors: string[]; rightHalfIds: string[] } {
let html = before;
let splitCount = 0;
const skippedSelectors = new Set<string>();
const rightHalfIds: string[] = [];
const usedIds = new Set<string>(takenIds ?? []);
for (const cut of targets) {
const { document } = parseSourceDocument(html);
const el = findTargetElement(document, cut.target);
if (!el) continue;
if (isUnsplittableCanvasElement(el)) {
throw new Error(CANVAS_SPLIT_UNSUPPORTED_MESSAGE);
}
const originalId = cut.originalId || el.getAttribute('id') || undefined;
const baseNewId = originalId ? `${originalId}-split` : 'clip-split';
const newId = nextUniqueId(document, baseNewId, usedIds);
usedIds.add(newId);
const splitRes = splitElementInHtml(html, cut.target, cut.splitTime, newId, {
start: cut.elementStart,
duration: cut.elementDuration,
...(cut.playbackStart !== undefined ? { playbackStart: cut.playbackStart } : {}),
...(cut.playbackRate !== undefined ? { playbackRate: cut.playbackRate } : {}),
...(cut.track !== undefined ? { track: cut.track } : {}),
});
if (!splitRes.matched || !splitRes.newId) continue;
html = splitRes.html;
splitCount += 1;
rightHalfIds.push(splitRes.newId);
if (originalId) {
const gsapRes = applyGsapMutationsInMemory(html, [
{
type: 'split-animations',
originalId,
newId: splitRes.newId,
splitTime: cut.splitTime,
elementStart: cut.elementStart,
elementDuration: cut.elementDuration,
},
]);
if (gsapRes.ok) {
html = gsapRes.after;
for (const s of gsapRes.skippedSelectors) skippedSelectors.add(s);
}
}
}
if (rightHalfIds.length > 0) {
html = relinkSplitHalvesInHtml(html, rightHalfIds);
}
return {
after: html,
splitCount,
skippedSelectors: [...skippedSelectors],
rightHalfIds,
};
}