@hyperframes/studio 0.9.38 → 0.9.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{codec-Dz2VdhdD.js → codec-BPK_2CLL.js} +1 -1
- package/dist/assets/{hyperframes-player-BkaWdGLq.js → hyperframes-player-D7qTTDIp.js} +1 -1
- package/dist/assets/{index-BJTCbv8z.css → index-4VGEAZyf.css} +1 -1
- package/dist/assets/{index-DsNxqAwr.js → index-CdA00Ybn.js} +145 -145
- package/dist/assets/{index-DONaC7VF.js → index-QS-DuxAU.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-BGN2jKrN.js → pausedVideoDecoder-Byzz98Gb.js} +1 -1
- package/dist/{chunk-63DH4LRB.js → chunk-AV2PV7NE.js} +15 -68
- package/dist/chunk-FQ3VRKMC.js +148 -0
- package/dist/{chunk-YKBN653R.js → chunk-KF6YVXJP.js} +258 -1
- package/dist/{chunk-45KKBU63.js → chunk-VZ5MD32A.js} +1 -1
- package/dist/{domEditingLayers-7Q56QAU5.js → domEditingLayers-4KRREI5C.js} +2 -2
- package/dist/index.d.ts +3 -85
- package/dist/index.html +2 -2
- package/dist/index.js +321 -341
- package/dist/keys-CaWcxUST.d.ts +86 -0
- package/dist/keys.d.ts +1 -0
- package/dist/keys.js +11 -0
- package/dist/player.js +2 -2
- package/dist/styles.css +1 -1
- package/package.json +12 -8
- package/src/components/editor/DomEditGestureReadout.test.tsx +204 -20
- package/src/components/editor/DomEditGestureReadout.tsx +36 -18
- package/src/components/editor/InlineTextToolbar.tsx +2 -2
- package/src/components/editor/domEditGestureReadoutPlacement.ts +144 -0
- package/src/components/editor/domEditGestureReadoutState.ts +18 -8
- package/src/components/editor/domEditingDom.ts +23 -104
- package/src/components/editor/useDomEditCompositionRect.ts +13 -4
- package/src/components/editor/useDomEditOverlayGestures.ts +1 -1
- package/src/components/editor/usePreviewCompositionRect.test.tsx +39 -0
- package/src/hooks/elementPickerStyles.ts +53 -0
- package/src/hooks/useElementPicker.test.ts +83 -3
- package/src/hooks/useElementPicker.ts +27 -66
- package/src/keys.test.ts +53 -0
- package/src/keys.ts +11 -0
- package/src/player/components/Player.tsx +2 -0
- package/src/player/components/studioKeyDefaults.ts +68 -0
- package/src/player/components/studioShortcuts.ts +2 -67
- package/src/player/hooks/playbackAdapterResolution.ts +15 -0
- package/src/player/hooks/previewMessageRouter.manifest.test.ts +67 -0
- package/src/player/hooks/previewMessageRouter.ts +38 -8
- package/src/player/hooks/timelinePlayerTestHarness.ts +26 -1
- package/src/player/hooks/useShadowPreviewReload.ts +4 -1
- package/src/player/hooks/useTimelinePlayer.ts +5 -9
- package/src/player/lib/bridgePlayback.ts +156 -0
- package/src/player/lib/previewChannel.test.ts +260 -0
- package/src/player/lib/previewChannel.ts +115 -0
- package/src/player/lib/previewElements.test.ts +167 -0
- package/src/player/lib/previewElements.ts +117 -0
- package/src/player/sceneSwap.test.ts +12 -0
- package/src/player/sceneSwap.ts +2 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Every single-character bare key Studio answers, by action. */
|
|
2
|
+
export const DEFAULT_STUDIO_KEYS = {
|
|
3
|
+
fullscreen: "f",
|
|
4
|
+
split: "s",
|
|
5
|
+
record: "r",
|
|
6
|
+
addKeyframe: "k",
|
|
7
|
+
audioGain: "g",
|
|
8
|
+
zoomSelection: "z",
|
|
9
|
+
mute: "m",
|
|
10
|
+
pause: "k",
|
|
11
|
+
shuttleBack: "j",
|
|
12
|
+
shuttleForward: "l",
|
|
13
|
+
inPoint: "i",
|
|
14
|
+
outPoint: "o",
|
|
15
|
+
seekToIn: "a",
|
|
16
|
+
seekToOut: "e",
|
|
17
|
+
selectTool: "v",
|
|
18
|
+
razorTool: "b",
|
|
19
|
+
selectLeftward: "[",
|
|
20
|
+
selectRightward: "]",
|
|
21
|
+
exitRazor: "a",
|
|
22
|
+
snap: "s",
|
|
23
|
+
grid: "g",
|
|
24
|
+
timelineSnap: "n",
|
|
25
|
+
playPause: "Space",
|
|
26
|
+
frameBack: "ArrowLeft",
|
|
27
|
+
frameForward: "ArrowRight",
|
|
28
|
+
frameBackTen: "shift+ArrowLeft",
|
|
29
|
+
frameForwardTen: "shift+ArrowRight",
|
|
30
|
+
loop: "shift+l",
|
|
31
|
+
clearInPoint: "shift+i",
|
|
32
|
+
clearOutPoint: "shift+o",
|
|
33
|
+
undo: "mod+z",
|
|
34
|
+
redo: "mod+shift+z",
|
|
35
|
+
redoAlt: "ctrl+y",
|
|
36
|
+
tabCompositions: "mod+1",
|
|
37
|
+
tabAssets: "mod+2",
|
|
38
|
+
group: "mod+g",
|
|
39
|
+
ungroup: "mod+shift+g",
|
|
40
|
+
copy: "mod+c",
|
|
41
|
+
paste: "mod+v",
|
|
42
|
+
cut: "mod+x",
|
|
43
|
+
duplicate: "mod+d",
|
|
44
|
+
linkToggle: "mod+l",
|
|
45
|
+
detachAudio: "alt+shift+d",
|
|
46
|
+
deleteLinked: "alt+Backspace",
|
|
47
|
+
deleteLinkedAlt: "alt+Delete",
|
|
48
|
+
deleteSelected: "Delete",
|
|
49
|
+
deleteSelectedAlt: "Backspace",
|
|
50
|
+
resetKeyframes: "Backspace",
|
|
51
|
+
nudgeLeft: "ArrowLeft",
|
|
52
|
+
nudgeRight: "ArrowRight",
|
|
53
|
+
nudgeUp: "ArrowUp",
|
|
54
|
+
nudgeDown: "ArrowDown",
|
|
55
|
+
nudgeLeftTen: "shift+ArrowLeft",
|
|
56
|
+
nudgeRightTen: "shift+ArrowRight",
|
|
57
|
+
nudgeUpTen: "shift+ArrowUp",
|
|
58
|
+
nudgeDownTen: "shift+ArrowDown",
|
|
59
|
+
captionSelectAll: "mod+a",
|
|
60
|
+
pickupClip: "Space",
|
|
61
|
+
pickupUp: "ArrowUp",
|
|
62
|
+
pickupDown: "ArrowDown",
|
|
63
|
+
pickupDrop: "Enter",
|
|
64
|
+
cancel: "Escape",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export type StudioKeyAction = keyof typeof DEFAULT_STUDIO_KEYS;
|
|
68
|
+
export type StudioKeys = Readonly<Record<StudioKeyAction, string>>;
|
|
@@ -8,74 +8,9 @@ import {
|
|
|
8
8
|
parseCombo,
|
|
9
9
|
} from "./studioCombo";
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
export const DEFAULT_STUDIO_KEYS = {
|
|
13
|
-
fullscreen: "f",
|
|
14
|
-
split: "s",
|
|
15
|
-
record: "r",
|
|
16
|
-
addKeyframe: "k",
|
|
17
|
-
audioGain: "g",
|
|
18
|
-
zoomSelection: "z",
|
|
19
|
-
mute: "m",
|
|
20
|
-
pause: "k",
|
|
21
|
-
shuttleBack: "j",
|
|
22
|
-
shuttleForward: "l",
|
|
23
|
-
inPoint: "i",
|
|
24
|
-
outPoint: "o",
|
|
25
|
-
seekToIn: "a",
|
|
26
|
-
seekToOut: "e",
|
|
27
|
-
selectTool: "v",
|
|
28
|
-
razorTool: "b",
|
|
29
|
-
selectLeftward: "[",
|
|
30
|
-
selectRightward: "]",
|
|
31
|
-
exitRazor: "a",
|
|
32
|
-
snap: "s",
|
|
33
|
-
grid: "g",
|
|
34
|
-
timelineSnap: "n",
|
|
35
|
-
playPause: "Space",
|
|
36
|
-
frameBack: "ArrowLeft",
|
|
37
|
-
frameForward: "ArrowRight",
|
|
38
|
-
frameBackTen: "shift+ArrowLeft",
|
|
39
|
-
frameForwardTen: "shift+ArrowRight",
|
|
40
|
-
loop: "shift+l",
|
|
41
|
-
clearInPoint: "shift+i",
|
|
42
|
-
clearOutPoint: "shift+o",
|
|
43
|
-
undo: "mod+z",
|
|
44
|
-
redo: "mod+shift+z",
|
|
45
|
-
redoAlt: "ctrl+y",
|
|
46
|
-
tabCompositions: "mod+1",
|
|
47
|
-
tabAssets: "mod+2",
|
|
48
|
-
group: "mod+g",
|
|
49
|
-
ungroup: "mod+shift+g",
|
|
50
|
-
copy: "mod+c",
|
|
51
|
-
paste: "mod+v",
|
|
52
|
-
cut: "mod+x",
|
|
53
|
-
duplicate: "mod+d",
|
|
54
|
-
linkToggle: "mod+l",
|
|
55
|
-
detachAudio: "alt+shift+d",
|
|
56
|
-
deleteLinked: "alt+Backspace",
|
|
57
|
-
deleteLinkedAlt: "alt+Delete",
|
|
58
|
-
deleteSelected: "Delete",
|
|
59
|
-
deleteSelectedAlt: "Backspace",
|
|
60
|
-
resetKeyframes: "Backspace",
|
|
61
|
-
nudgeLeft: "ArrowLeft",
|
|
62
|
-
nudgeRight: "ArrowRight",
|
|
63
|
-
nudgeUp: "ArrowUp",
|
|
64
|
-
nudgeDown: "ArrowDown",
|
|
65
|
-
nudgeLeftTen: "shift+ArrowLeft",
|
|
66
|
-
nudgeRightTen: "shift+ArrowRight",
|
|
67
|
-
nudgeUpTen: "shift+ArrowUp",
|
|
68
|
-
nudgeDownTen: "shift+ArrowDown",
|
|
69
|
-
captionSelectAll: "mod+a",
|
|
70
|
-
pickupClip: "Space",
|
|
71
|
-
pickupUp: "ArrowUp",
|
|
72
|
-
pickupDown: "ArrowDown",
|
|
73
|
-
pickupDrop: "Enter",
|
|
74
|
-
cancel: "Escape",
|
|
75
|
-
};
|
|
11
|
+
import { DEFAULT_STUDIO_KEYS, type StudioKeyAction, type StudioKeys } from "./studioKeyDefaults";
|
|
76
12
|
|
|
77
|
-
export type StudioKeyAction
|
|
78
|
-
export type StudioKeys = Readonly<Record<StudioKeyAction, string>>;
|
|
13
|
+
export { DEFAULT_STUDIO_KEYS, type StudioKeyAction, type StudioKeys } from "./studioKeyDefaults";
|
|
79
14
|
|
|
80
15
|
const SHARED_BY_DESIGN: ReadonlyArray<readonly [StudioKeyAction, StudioKeyAction]> = [
|
|
81
16
|
["pause", "addKeyframe"],
|
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
type StaticSeekCacheEntry,
|
|
12
12
|
} from "../lib/playbackAdapter";
|
|
13
13
|
import { readTimelineDurationFromDocument } from "../lib/timelineDOM";
|
|
14
|
+
import { attachPausedVideoProvider } from "../lib/pausedVideoProvider";
|
|
15
|
+
import { previewFrameOpaque } from "../lib/previewChannel";
|
|
16
|
+
import { bridgePlaybackAdapter } from "../lib/bridgePlayback";
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* Timeline-based adapters (`__timeline`, then `__timelines`): `match` is the first that covers
|
|
@@ -96,3 +99,15 @@ export function resolvePlaybackAdapter(
|
|
|
96
99
|
|
|
97
100
|
return bestAdapter;
|
|
98
101
|
}
|
|
102
|
+
|
|
103
|
+
/** A frame Studio cannot script (opaque origin) is driven over the control bridge. */
|
|
104
|
+
export function resolveFramePlaybackAdapter(
|
|
105
|
+
iframe: HTMLIFrameElement,
|
|
106
|
+
refs: StaticSeekRefs,
|
|
107
|
+
): PlaybackAdapter | null {
|
|
108
|
+
if (previewFrameOpaque(iframe)) return bridgePlaybackAdapter(iframe);
|
|
109
|
+
const win = iframe.contentWindow as IframeWindow | null;
|
|
110
|
+
if (!win) return null;
|
|
111
|
+
attachPausedVideoProvider(iframe, win);
|
|
112
|
+
return resolvePlaybackAdapter(iframe, win, refs);
|
|
113
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { usePlayerStore } from "../store/playerStore";
|
|
5
|
+
import { createPreviewMessageHandler } from "./previewMessageRouter";
|
|
6
|
+
import { detachedFakeIframe, serveEditingRequests } from "./timelinePlayerTestHarness";
|
|
7
|
+
|
|
8
|
+
const closers: Array<() => void> = [];
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
closers.splice(0).forEach((close) => close());
|
|
11
|
+
usePlayerStore.getState().reset();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const PORT_MANIFEST = { clips: [{ id: "a" }], durationInFrames: 60, scenes: [] };
|
|
15
|
+
const GLOBAL_MANIFEST = { clips: [{ id: "x" }], durationInFrames: 30, scenes: [] };
|
|
16
|
+
|
|
17
|
+
/** A frame whose globals and editing port carry different manifests, so a test sees which was read. */
|
|
18
|
+
function routeStateTicks(opaque: boolean) {
|
|
19
|
+
const frame: Record<string, unknown> = { __clipManifest: GLOBAL_MANIFEST };
|
|
20
|
+
const iframe = detachedFakeIframe(frame);
|
|
21
|
+
if (opaque) {
|
|
22
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
23
|
+
Object.defineProperty(frame, "document", {
|
|
24
|
+
get: () => {
|
|
25
|
+
throw new DOMException("blocked", "SecurityError");
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const asked = vi.fn(() => ({ clipManifest: PORT_MANIFEST }));
|
|
30
|
+
closers.push(serveEditingRequests(frame, asked));
|
|
31
|
+
const processed = vi.fn();
|
|
32
|
+
const handle = createPreviewMessageHandler({
|
|
33
|
+
iframeRef: { current: iframe },
|
|
34
|
+
processTimelineMessageRef: { current: processed },
|
|
35
|
+
enrichMissingCompositionsRef: { current: vi.fn() },
|
|
36
|
+
lastTimelineMessageRef: { current: Date.now() },
|
|
37
|
+
getAdapter: () => null,
|
|
38
|
+
syncTimelineElements: vi.fn(),
|
|
39
|
+
});
|
|
40
|
+
const tick = () =>
|
|
41
|
+
handle(
|
|
42
|
+
new MessageEvent("message", {
|
|
43
|
+
source: frame as unknown as Window,
|
|
44
|
+
data: { source: "hf-preview", type: "state", isPlaying: false },
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
return { tick, asked, processed };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe("a state tick while the timeline is empty", () => {
|
|
51
|
+
it("reads a scriptable frame's manifest straight off it, at once, with no port", async () => {
|
|
52
|
+
const { tick, asked, processed } = routeStateTicks(false);
|
|
53
|
+
tick();
|
|
54
|
+
expect(processed).toHaveBeenCalledWith(GLOBAL_MANIFEST);
|
|
55
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
56
|
+
expect(asked).not.toHaveBeenCalled();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("asks an opaque frame's runtime over the editing port, one request at a time", async () => {
|
|
60
|
+
const { tick, asked, processed } = routeStateTicks(true);
|
|
61
|
+
tick();
|
|
62
|
+
tick();
|
|
63
|
+
await vi.waitFor(() => expect(processed).toHaveBeenCalledWith(PORT_MANIFEST));
|
|
64
|
+
expect(asked).toHaveBeenCalledOnce();
|
|
65
|
+
expect(processed).toHaveBeenCalledOnce();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
parsePreviewFacts,
|
|
24
24
|
} from "../store/previewQualitySlice";
|
|
25
25
|
import { parseTimelineFromDOM } from "../lib/timelineDOM";
|
|
26
|
+
import { previewClipManifest, previewFrameOpaque } from "../lib/previewChannel";
|
|
26
27
|
|
|
27
28
|
/** What `processTimelineMessage` accepts — the clip-manifest postMessage. */
|
|
28
29
|
export interface ClipManifestMessage {
|
|
@@ -83,6 +84,39 @@ function enrichDue(lastTimelineMessage: number, enriched: { at: number }): boole
|
|
|
83
84
|
return true;
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
/** Asks the runtime for its clip manifest, one request at a time, while the store has no elements. */
|
|
88
|
+
function recoverClipManifest(
|
|
89
|
+
deps: PreviewMessageRouterDeps,
|
|
90
|
+
iframe: HTMLIFrameElement,
|
|
91
|
+
recovery: { asking: boolean },
|
|
92
|
+
): void {
|
|
93
|
+
if (recovery.asking) return;
|
|
94
|
+
recovery.asking = true;
|
|
95
|
+
previewClipManifest(iframe)
|
|
96
|
+
.then((manifest) => {
|
|
97
|
+
const stillWanted =
|
|
98
|
+
usePlayerStore.getState().elements.length === 0 &&
|
|
99
|
+
deps.iframeRef.current === iframe &&
|
|
100
|
+
!hasTimelinePerformanceFixtureLease();
|
|
101
|
+
if (manifest && manifest.clips.length > 0 && stillWanted)
|
|
102
|
+
deps.processTimelineMessageRef.current(manifest);
|
|
103
|
+
})
|
|
104
|
+
.catch(() => {})
|
|
105
|
+
.finally(() => (recovery.asking = false));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function recoverEmptyTimeline(
|
|
109
|
+
deps: PreviewMessageRouterDeps,
|
|
110
|
+
iframe: HTMLIFrameElement | null,
|
|
111
|
+
recovery: { asking: boolean },
|
|
112
|
+
): void {
|
|
113
|
+
if (iframe && previewFrameOpaque(iframe)) return recoverClipManifest(deps, iframe, recovery);
|
|
114
|
+
const manifest = (iframe?.contentWindow as IframeWindow | null)?.__clipManifest;
|
|
115
|
+
if (manifest && manifest.clips.length > 0) {
|
|
116
|
+
deps.processTimelineMessageRef.current(manifest);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
86
120
|
/**
|
|
87
121
|
* A `state` tick doubles as a recovery hook: if the store still has no
|
|
88
122
|
* elements, read the manifest straight off the iframe, and if no `timeline`
|
|
@@ -92,15 +126,11 @@ function handleStateMessage(
|
|
|
92
126
|
deps: PreviewMessageRouterDeps,
|
|
93
127
|
iframe: HTMLIFrameElement | null,
|
|
94
128
|
playingHoldsClipListPosts: boolean,
|
|
95
|
-
enriched: { at: number },
|
|
129
|
+
enriched: { at: number; asking: boolean },
|
|
96
130
|
): void {
|
|
97
131
|
try {
|
|
98
|
-
if (usePlayerStore.getState().elements.length === 0)
|
|
99
|
-
|
|
100
|
-
if (manifest && manifest.clips.length > 0) {
|
|
101
|
-
deps.processTimelineMessageRef.current(manifest);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
132
|
+
if (usePlayerStore.getState().elements.length === 0)
|
|
133
|
+
recoverEmptyTimeline(deps, iframe, enriched);
|
|
104
134
|
if (!playingHoldsClipListPosts && enrichDue(deps.lastTimelineMessageRef.current, enriched)) {
|
|
105
135
|
deps.enrichMissingCompositionsRef.current();
|
|
106
136
|
}
|
|
@@ -156,7 +186,7 @@ function applyStoreReport(data: PreviewMessage): boolean {
|
|
|
156
186
|
export function createPreviewMessageHandler(
|
|
157
187
|
deps: PreviewMessageRouterDeps,
|
|
158
188
|
): (e: MessageEvent) => void {
|
|
159
|
-
const enriched = { at: 0 };
|
|
189
|
+
const enriched = { at: 0, asking: false };
|
|
160
190
|
return (e: MessageEvent) => {
|
|
161
191
|
const iframe = deps.iframeRef.current;
|
|
162
192
|
const data = acceptedPreviewMessage(e, iframe);
|
|
@@ -60,13 +60,18 @@ export function renderTimelinePlayerHarness(options?: UseTimelinePlayerOptions)
|
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
export function
|
|
63
|
+
export function detachedFakeIframe(iframeWindow: Record<string, unknown>): HTMLIFrameElement {
|
|
64
64
|
const iframe = document.createElement("iframe");
|
|
65
65
|
Object.defineProperty(iframe, "contentWindow", { value: iframeWindow, configurable: true });
|
|
66
66
|
Object.defineProperty(iframe, "contentDocument", {
|
|
67
67
|
value: document.implementation.createHTMLDocument("preview"),
|
|
68
68
|
configurable: true,
|
|
69
69
|
});
|
|
70
|
+
return iframe;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function makeFakeIframe(iframeWindow: Record<string, unknown>): HTMLIFrameElement {
|
|
74
|
+
const iframe = detachedFakeIframe(iframeWindow);
|
|
70
75
|
document.body.appendChild(iframe);
|
|
71
76
|
return iframe;
|
|
72
77
|
}
|
|
@@ -148,3 +153,23 @@ export async function paintShadow(getApi: () => TimelinePlayerApi, shadow: HTMLI
|
|
|
148
153
|
});
|
|
149
154
|
return slot.gen;
|
|
150
155
|
}
|
|
156
|
+
|
|
157
|
+
export function serveEditingRequests(
|
|
158
|
+
frame: Record<string, unknown>,
|
|
159
|
+
answer: (kind: string, params: unknown) => unknown,
|
|
160
|
+
): () => void {
|
|
161
|
+
const ports: MessagePort[] = [];
|
|
162
|
+
frame.postMessage = (message: { type?: string } | null) => {
|
|
163
|
+
if (message?.type !== "editing-connect") return;
|
|
164
|
+
const { port1, port2 } = new MessageChannel();
|
|
165
|
+
ports.push(port1);
|
|
166
|
+
port1.onmessage = (event: MessageEvent<{ id: number; kind: string; params: unknown }>) => {
|
|
167
|
+
const { id, kind, params } = event.data;
|
|
168
|
+
port1.postMessage({ v: 1, id, ok: true, result: answer(kind, params) });
|
|
169
|
+
};
|
|
170
|
+
const handoff = { source: "hf-preview", type: "editing-port", v: 1 };
|
|
171
|
+
const source = frame as unknown as Window;
|
|
172
|
+
window.dispatchEvent(new MessageEvent("message", { source, data: handoff, ports: [port2] }));
|
|
173
|
+
};
|
|
174
|
+
return () => ports.forEach((port) => port.close());
|
|
175
|
+
}
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
} from "./useTimelineSyncCallbacks";
|
|
21
21
|
import type { IframeWindow, PlaybackAdapter } from "../lib/playbackTypes";
|
|
22
22
|
import { setPreviewReloading } from "../previewReloading";
|
|
23
|
+
import { previewFrameOpaque, previewSeekPainted } from "../lib/previewChannel";
|
|
23
24
|
import { studioFileRevision } from "../../utils/studioFileVersion";
|
|
24
25
|
import { announcePreviewPromoted } from "../sceneSwap";
|
|
25
26
|
import { afterStudioPendingEdits, isStudioEditSaving } from "../../utils/studioPendingEdits";
|
|
@@ -34,7 +35,9 @@ export const SHADOW_READY_TIMEOUT_MS = 15_000;
|
|
|
34
35
|
export const SHADOW_READY_BUDGETS = 3;
|
|
35
36
|
|
|
36
37
|
function restoreSeekPainted(iframe: HTMLIFrameElement | null): Promise<void> | undefined {
|
|
37
|
-
|
|
38
|
+
if (!iframe) return undefined;
|
|
39
|
+
if (previewFrameOpaque(iframe)) return previewSeekPainted(iframe, SHADOW_READY_TIMEOUT_MS);
|
|
40
|
+
const win = iframe.contentWindow as IframeWindow | null;
|
|
38
41
|
return win?.__hfWaitForSeekCompletion?.().catch(() => {});
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -4,7 +4,7 @@ import { useMountEffect } from "../../hooks/useMountEffect";
|
|
|
4
4
|
import { usePlaybackKeyboard } from "./usePlaybackKeyboard";
|
|
5
5
|
import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks";
|
|
6
6
|
import { useShadowPreviewReload } from "./useShadowPreviewReload";
|
|
7
|
-
import {
|
|
7
|
+
import { resolveFramePlaybackAdapter } from "./playbackAdapterResolution";
|
|
8
8
|
import { subscribePreviewFrame, transportAdapter } from "../store/previewFrameStore";
|
|
9
9
|
import { clampToDuration } from "../lib/time";
|
|
10
10
|
import { useTimelinePlayerLoop } from "./useTimelinePlayerLoop";
|
|
@@ -28,7 +28,7 @@ export {
|
|
|
28
28
|
} from "../lib/playbackShortcuts";
|
|
29
29
|
|
|
30
30
|
import type { PlaybackAdapter, IframeWindow } from "../lib/playbackTypes";
|
|
31
|
-
import {
|
|
31
|
+
import { setFrameLoopStart } from "../lib/previewChannel";
|
|
32
32
|
import { releaseStaticSeekCache, type StaticSeekCacheEntry } from "../lib/playbackAdapter";
|
|
33
33
|
import { mergeTimelineElementsPreservingDowngrades } from "../lib/timelineDOM";
|
|
34
34
|
import { findTimelineElementInIframe } from "../../hooks/timelineEditingHelpers";
|
|
@@ -149,10 +149,8 @@ export function useTimelinePlayer({
|
|
|
149
149
|
try {
|
|
150
150
|
// undefined means "no override"; an explicit null override means "no adapter".
|
|
151
151
|
const iframe = overrideIframe !== undefined ? overrideIframe : iframeRef.current;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
attachPausedVideoProvider(iframe, win);
|
|
155
|
-
const adapter = resolvePlaybackAdapter(iframe, win, {
|
|
152
|
+
if (!iframe) return null;
|
|
153
|
+
const adapter = resolveFramePlaybackAdapter(iframe, {
|
|
156
154
|
cache: staticSeekAdapterRef,
|
|
157
155
|
warned: staticSeekWarnedRef,
|
|
158
156
|
});
|
|
@@ -165,9 +163,7 @@ export function useTimelinePlayer({
|
|
|
165
163
|
);
|
|
166
164
|
|
|
167
165
|
const setLoopStart = useCallback((seconds: number | null) => {
|
|
168
|
-
|
|
169
|
-
(iframeRef.current?.contentWindow as IframeWindow | null)?.__hf?.setLoopStart?.(seconds);
|
|
170
|
-
} catch {}
|
|
166
|
+
if (iframeRef.current) setFrameLoopStart(iframeRef.current, seconds);
|
|
171
167
|
}, []);
|
|
172
168
|
const { startRAFLoop, stopRAFLoop, stopReverseLoop } = useTimelinePlayerLoop({
|
|
173
169
|
rafRef,
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Playback of a frame Studio cannot script: control-bridge commands, with time and duration mirrored
|
|
2
|
+
// from the runtime's `state` and `timeline` posts. Player registers each frame before it loads,
|
|
3
|
+
// because a runtime posts its first `timeline` while it boots.
|
|
4
|
+
import type { PlaybackAdapter } from "./playbackTypes";
|
|
5
|
+
import { inspectStudioRuntimeMessage, postRuntimeControlMessage } from "./runtimeProtocol";
|
|
6
|
+
|
|
7
|
+
interface PlaybackMirror {
|
|
8
|
+
iframe: HTMLIFrameElement;
|
|
9
|
+
time: number;
|
|
10
|
+
duration: number;
|
|
11
|
+
playing: boolean;
|
|
12
|
+
/** A seek the runtime has not confirmed yet, and until when to wait for it. */
|
|
13
|
+
seekTo: number | null;
|
|
14
|
+
seekUntil: number;
|
|
15
|
+
/** The composition's declared size from the runtime's `stage-size` post. */
|
|
16
|
+
stageSize: { width: number; height: number } | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const SEEK_CONFIRM_MS = 500;
|
|
20
|
+
// A post without fps metadata is matched within half a frame of the lowest frame rate a film may use.
|
|
21
|
+
const LOWEST_FPS = 1;
|
|
22
|
+
|
|
23
|
+
/** Keyed by the frame's window, which a frame keeps across its loads. */
|
|
24
|
+
const mirrors = new WeakMap<object, PlaybackMirror>();
|
|
25
|
+
|
|
26
|
+
function mirrorOf(iframe: HTMLIFrameElement): PlaybackMirror | null {
|
|
27
|
+
const frame = iframe.contentWindow;
|
|
28
|
+
if (!frame) return null;
|
|
29
|
+
let mirror = mirrors.get(frame);
|
|
30
|
+
if (!mirror) {
|
|
31
|
+
mirror = {
|
|
32
|
+
iframe,
|
|
33
|
+
time: 0,
|
|
34
|
+
duration: 0,
|
|
35
|
+
playing: false,
|
|
36
|
+
seekTo: null,
|
|
37
|
+
seekUntil: 0,
|
|
38
|
+
stageSize: null,
|
|
39
|
+
};
|
|
40
|
+
mirrors.set(frame, mirror);
|
|
41
|
+
}
|
|
42
|
+
mirror.iframe = iframe;
|
|
43
|
+
return mirror;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Starts mirroring a preview frame Studio built; call it before the frame loads. */
|
|
47
|
+
export function watchPreviewFrame(iframe: HTMLIFrameElement): void {
|
|
48
|
+
mirrorOf(iframe);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The composition size a registered frame's runtime last posted. */
|
|
52
|
+
export function previewStageSize(
|
|
53
|
+
iframe: HTMLIFrameElement,
|
|
54
|
+
): { width: number; height: number } | null {
|
|
55
|
+
const frame = iframe.contentWindow;
|
|
56
|
+
return (frame && mirrors.get(frame)?.stageSize) ?? null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The origins a post from this frame can carry, given its sandbox and what it loads. */
|
|
60
|
+
export function frameOrigins(iframe: HTMLIFrameElement): string[] {
|
|
61
|
+
const sandbox = iframe.getAttribute("sandbox")?.toLowerCase().split(/\s+/);
|
|
62
|
+
if (sandbox && !sandbox.includes("allow-same-origin")) return ["null"];
|
|
63
|
+
const src = iframe.getAttribute("src");
|
|
64
|
+
if (iframe.hasAttribute("srcdoc") || !src) return [window.location.origin];
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(src, window.location.href);
|
|
67
|
+
if (url.protocol === "about:") return [window.location.origin];
|
|
68
|
+
return [url.protocol === "data:" ? "null" : url.origin];
|
|
69
|
+
} catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const finiteAtLeastZero = (value: unknown): value is number =>
|
|
75
|
+
typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
76
|
+
const finitePositive = (value: unknown): value is number => finiteAtLeastZero(value) && value > 0;
|
|
77
|
+
|
|
78
|
+
/** The runtime lands a seek on the frame at or before it, so only that frame confirms it. */
|
|
79
|
+
function confirmsSeek(currentTime: number, seekTo: number, fps: number | null): boolean {
|
|
80
|
+
if (fps === null) return Math.abs(currentTime - seekTo) <= 0.5 / LOWEST_FPS;
|
|
81
|
+
return Math.abs(currentTime - Math.floor(seekTo * fps + 1e-9) / fps) < 0.5 / fps;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The runtime's playback post and its frame rate, or null for anything else. */
|
|
85
|
+
function runtimePost(
|
|
86
|
+
data: unknown,
|
|
87
|
+
): { message: Record<string, unknown>; fps: number | null } | null {
|
|
88
|
+
if (typeof data !== "object" || data === null) return null;
|
|
89
|
+
const message = data as Record<string, unknown>;
|
|
90
|
+
if (message.source !== "hf-preview") return null;
|
|
91
|
+
const inspection = inspectStudioRuntimeMessage(message);
|
|
92
|
+
if (inspection.status === "unsupported") return null;
|
|
93
|
+
return { message, fps: inspection.status === "supported" ? inspection.fps : null };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function mirrorState(
|
|
97
|
+
mirror: PlaybackMirror,
|
|
98
|
+
currentTime: number,
|
|
99
|
+
playing: boolean,
|
|
100
|
+
fps: number | null,
|
|
101
|
+
) {
|
|
102
|
+
// A state posted before the runtime handled our seek would put the playhead back.
|
|
103
|
+
const { seekTo } = mirror;
|
|
104
|
+
const waiting = seekTo !== null && performance.now() < mirror.seekUntil;
|
|
105
|
+
if (waiting && !confirmsSeek(currentTime, seekTo, fps)) return;
|
|
106
|
+
mirror.seekTo = null;
|
|
107
|
+
mirror.time = currentTime;
|
|
108
|
+
mirror.playing = playing;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function mirrorPost(mirror: PlaybackMirror, message: Record<string, unknown>, fps: number | null) {
|
|
112
|
+
if (message.type === "timeline" && finiteAtLeastZero(message.durationSeconds)) {
|
|
113
|
+
mirror.duration = message.durationSeconds;
|
|
114
|
+
} else if (message.type === "state" && finiteAtLeastZero(message.currentTime)) {
|
|
115
|
+
mirrorState(mirror, message.currentTime, message.isPlaying === true, fps);
|
|
116
|
+
} else if (message.type === "stage-size") {
|
|
117
|
+
const { width, height } = message;
|
|
118
|
+
if (finitePositive(width) && finitePositive(height)) mirror.stageSize = { width, height };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function mirrorRuntimePlayback(event: MessageEvent): void {
|
|
123
|
+
// Only a frame Studio built, posting from an origin that frame can have.
|
|
124
|
+
const mirror = event.source ? mirrors.get(event.source) : undefined;
|
|
125
|
+
if (!mirror || !frameOrigins(mirror.iframe).includes(event.origin)) return;
|
|
126
|
+
const post = runtimePost(event.data);
|
|
127
|
+
if (post) mirrorPost(mirror, post.message, post.fps);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (typeof window !== "undefined") window.addEventListener("message", mirrorRuntimePlayback);
|
|
131
|
+
|
|
132
|
+
export function bridgePlaybackAdapter(iframe: HTMLIFrameElement): PlaybackAdapter | null {
|
|
133
|
+
const frame = iframe.contentWindow;
|
|
134
|
+
const mirror = mirrorOf(iframe);
|
|
135
|
+
if (!frame || !mirror) return null;
|
|
136
|
+
const send = (action: string, payload?: Record<string, unknown>) =>
|
|
137
|
+
postRuntimeControlMessage(frame, action, payload);
|
|
138
|
+
return {
|
|
139
|
+
play: () => {
|
|
140
|
+
send("play");
|
|
141
|
+
mirror.playing = true;
|
|
142
|
+
},
|
|
143
|
+
pause: () => {
|
|
144
|
+
send("pause");
|
|
145
|
+
mirror.playing = false;
|
|
146
|
+
},
|
|
147
|
+
// The bridge's seek takes no keepPlaying or settled option yet.
|
|
148
|
+
seek: (time) => {
|
|
149
|
+
send("seek", { timeSeconds: time });
|
|
150
|
+
Object.assign(mirror, { time, seekTo: time, seekUntil: performance.now() + SEEK_CONFIRM_MS });
|
|
151
|
+
},
|
|
152
|
+
getTime: () => mirror.time,
|
|
153
|
+
getDuration: () => mirror.duration,
|
|
154
|
+
isPlaying: () => mirror.playing,
|
|
155
|
+
};
|
|
156
|
+
}
|