Skip to content

Commit 4283542

Browse files
committed
fix(studio): an unmounted scene's rows and their state come from the markup it mounts with
1 parent 89db7a1 commit 4283542

6 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎packages/core/src/runtime/init.swapScenes.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,15 @@ describe("the preview's scene window", () => {
18471847
expect(clip && [clip.start, clip.duration]).toEqual([4.5, 1]);
18481848
});
18491849

1850+
it("hands Studio a parked scene's markup, and nothing for a mounted one", async () => {
1851+
bootWindowed([S0, S1, S2], trackingRoot().root);
1852+
await tick();
1853+
const markup = window.__hfParkedSceneMarkup!;
1854+
expect(markup(sceneHost("s2"))?.textContent).toBe("two");
1855+
expect(markup(sceneHost("s2"))?.isConnected).toBe(false);
1856+
expect(markup(sceneHost("s0"))).toBeNull();
1857+
});
1858+
18501859
it("gives an edit to a parked scene to its next mount, without mounting it", async () => {
18511860
bootWindowed([S0, S1, S2], trackingRoot().root);
18521861
await tick();

‎packages/core/src/runtime/init.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
hostOf as sceneHostOf,
77
isParked,
88
parkedClips,
9+
parkedSceneMarkup,
910
parkedTimelineScenes,
1011
park as parkScene,
1112
rememberScene,
@@ -4345,9 +4346,11 @@ export function initSandboxRuntimeModular(): void {
43454346
if (readSceneParts(document)) {
43464347
window.__hfSwapScenes = swapScenes;
43474348
window.__hfHoldScenes = holdScenes;
4349+
window.__hfParkedSceneMarkup = parkedSceneMarkup;
43484350
registerRuntimeCleanup(() => {
43494351
delete window.__hfSwapScenes;
43504352
delete window.__hfHoldScenes;
4353+
delete window.__hfParkedSceneMarkup;
43514354
});
43524355
}
43534356

‎packages/core/src/runtime/sceneWindow.ts‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,25 @@ export function rememberScene(name: string, host: Element, scripts: Element[]):
120120
/** The timeline's clips of each parked scene, built from its markup once and dropped when an edit changes it. */
121121
export const parkedClips = new Map<string, RuntimeTimelineClip[]>();
122122

123-
/** The parked scenes, each able to build its markup detached under a copy of the film root for the timeline. */
124-
export function parkedTimelineScenes(): ParkedTimelineScene[] {
125-
return Array.from(document.querySelectorAll(`[${PARKED}]`)).flatMap((placeholder) => {
126-
const name = placeholder.getAttribute(SCENE_PART_ATTR);
127-
const parts = name === null ? undefined : pristine.get(name);
128-
const film = placeholder.parentElement;
129-
if (name === null || !parts || !film) return [];
130-
const host = () => {
131-
const detachedFilm = film.cloneNode(false) as Element;
132-
const template = document.createElement("template");
133-
template.innerHTML = parts.html;
134-
detachedFilm.append(template.content);
135-
return detachedFilm.firstElementChild!;
136-
};
137-
return [{ name, host }];
138-
});
123+
const parkedName = (placeholder: Element): string | null => {
124+
const name = isParked(placeholder) ? placeholder.getAttribute(SCENE_PART_ATTR) : null;
125+
return name !== null && pristine.has(name) && placeholder.parentElement ? name : null;
126+
};
127+
128+
/** A parked scene's markup, built detached under a copy of the film root; null for any other element. */
129+
export function parkedSceneMarkup(placeholder: Element): Element | null {
130+
const name = parkedName(placeholder);
131+
if (name === null) return null;
132+
const detachedFilm = placeholder.parentElement!.cloneNode(false) as Element;
133+
const template = document.createElement("template");
134+
template.innerHTML = pristine.get(name)!.html;
135+
detachedFilm.append(template.content);
136+
return detachedFilm.firstElementChild;
139137
}
138+
139+
/** The parked scenes, each able to build its markup for the timeline. */
140+
export const parkedTimelineScenes = (): ParkedTimelineScene[] =>
141+
Array.from(document.querySelectorAll(`[${PARKED}]`)).flatMap((placeholder) => {
142+
const name = parkedName(placeholder);
143+
return name === null ? [] : [{ name, host: () => parkedSceneMarkup(placeholder)! }];
144+
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ declare global {
100100
/** Swap edited scenes from a rebuilt preview document; refuses before changing anything when it cannot. */
101101
__hfSwapScenes?: (html: string, signal?: AbortSignal) => Promise<void>;
102102
__hfHoldScenes?: (names: string[]) => Promise<void>;
103+
__hfParkedSceneMarkup?: (placeholder: Element) => Element | null;
103104
__HF_EXPORT_RENDER_SEEK_CONFIG?: {
104105
mode?: string;
105106
diagnostics?: boolean;

‎packages/studio/src/player/hooks/timelineSyncHydration.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ describe("collectSubCompositionHostState", () => {
107107
expect(rows.map((child) => child.id)).toEqual(["scene-2-title"]);
108108
});
109109

110+
it("reads a scene the preview unmounted from the markup it mounts with", () => {
111+
const markup = mountScene().getElementById("scene-2-slot")!.cloneNode(true) as Element;
112+
document.body.innerHTML = `<div id="scene-2-slot" data-composition-id="scene-2" data-hf-parked-scene></div>`;
113+
const view = window as Window & { __hfParkedSceneMarkup?: (el: Element) => Element | null };
114+
view.__hfParkedSceneMarkup = (el) => (el.hasAttribute("data-hf-parked-scene") ? markup : null);
115+
try {
116+
const rows = collectSubCompositionDomChildren(document, sceneClips, new Map());
117+
expect(rows.map((child) => child.id)).toEqual([
118+
"scene-2-video-region",
119+
"scene-2-title",
120+
"scene-2-caption",
121+
]);
122+
expect(collectSubCompositionHostState(document, sceneClips).get("scene-2-video")).toEqual({
123+
hidden: true,
124+
});
125+
} finally {
126+
delete view.__hfParkedSceneMarkup;
127+
}
128+
});
129+
110130
it("returns empty without a document, rather than throwing", () => {
111131
expect(collectSubCompositionHostState(null, sceneClips).size).toBe(0);
112132
});

‎packages/studio/src/player/hooks/timelineSyncHydration.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ function collectHostDomChildren(
112112
}
113113
}
114114

115+
type SceneMarkupWindow = Window & {
116+
__hfParkedSceneMarkup?: (placeholder: Element) => Element | null;
117+
};
118+
119+
/** A clip's host; for a scene the preview unmounted, the markup it mounts with. */
120+
function clipHostOf(iframeDoc: Document, clip: ClipManifestClip): Element | null {
121+
const host = findClipElementById(iframeDoc, clip);
122+
const view = iframeDoc.defaultView as SceneMarkupWindow | null;
123+
return (host && view?.__hfParkedSceneMarkup?.(host)) ?? host;
124+
}
125+
115126
/**
116127
* Every sub-composition's internal elements, across the whole manifest.
117128
*
@@ -129,7 +140,7 @@ export function collectSubCompositionDomChildren(
129140
if (!iframeDoc) return out;
130141
for (const clip of clips) {
131142
if (clip.kind !== "composition" || !clip.id) continue;
132-
const hostEl = findClipElementById(iframeDoc, clip);
143+
const hostEl = clipHostOf(iframeDoc, clip);
133144
if (!hostEl) continue;
134145
const innerRoot = hostEl.querySelector("[data-hf-inner-root]") ?? hostEl;
135146
collectHostDomChildren(clip.id, innerRoot, clip.id, parentMap, out);
@@ -173,7 +184,7 @@ export function collectSubCompositionHostState(
173184
if (!iframeDoc) return out;
174185
for (const clip of clips) {
175186
if (clip.kind !== "composition" || !clip.id) continue;
176-
const hostEl = findClipElementById(iframeDoc, clip);
187+
const hostEl = clipHostOf(iframeDoc, clip);
177188
if (!hostEl) continue;
178189
for (const el of Array.from(hostEl.querySelectorAll("[id]"))) {
179190
const state = readSubCompositionHostState(el);

0 commit comments

Comments
 (0)