@hyperframes/studio 0.9.31 → 0.9.33
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-DXM4vA6G.js → codec-qz1-OXLj.js} +1 -1
- package/dist/assets/{hyperframes-player-D_016Ubp.js → hyperframes-player-2eG1zFX5.js} +1 -1
- package/dist/assets/{index-dsfjBidg.css → index-BJTCbv8z.css} +1 -1
- package/dist/assets/{index-Ob53qo6K.js → index-DDmhnfrS.js} +189 -189
- package/dist/assets/{index-Bw4stQMm.js → index-DUveJ_YP.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-vbdgaMqL.js → pausedVideoDecoder-CoHTKUjM.js} +1 -1
- package/dist/{chunk-J3VJE463.js → chunk-52AJB4PN.js} +4 -1
- package/dist/{chunk-LRTUWOR4.js → chunk-YKBN653R.js} +1 -1
- package/dist/index.html +2 -2
- package/dist/index.js +2054 -1392
- package/dist/player.js +2 -2
- package/dist/styles.css +1 -1
- package/dist/ui.js +1 -1
- package/package.json +8 -8
- package/src/App.tsx +1 -0
- package/src/components/StudioHeader.tsx +4 -0
- package/src/components/share/QuickShare.test.tsx +622 -0
- package/src/components/share/QuickShare.tsx +104 -0
- package/src/components/share/QuickShareControls.tsx +265 -0
- package/src/components/share/ShareAudience.tsx +120 -0
- package/src/components/share/localShareClient.ts +171 -0
- package/src/components/share/localShareTelemetry.test.ts +22 -0
- package/src/components/share/localShareTelemetry.ts +13 -0
- package/src/components/share/useQuickShare.ts +215 -0
- package/src/components/sidebar/CompositionsTab.hoverPreview.test.tsx +100 -0
- package/src/components/sidebar/CompositionsTab.test.ts +2 -24
- package/src/components/sidebar/CompositionsTab.tsx +35 -100
- package/src/components/ui/Input.tsx +4 -1
- package/src/components/ui/valueControls.test.tsx +85 -0
- package/src/webmcp/polyfill.transport.test.ts +59 -0
- package/src/webmcp/polyfill.ts +15 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { LocalShareInteraction } from "@hyperframes/studio-server";
|
|
2
|
+
import { browserTelemetryAllowed } from "../../telemetry/policy";
|
|
3
|
+
import { studioApiFetch } from "../../utils/studioApiFetch";
|
|
4
|
+
|
|
5
|
+
export function shareInteraction(token: string, event: LocalShareInteraction): void {
|
|
6
|
+
if (!browserTelemetryAllowed()) return;
|
|
7
|
+
void studioApiFetch("/api/share/events", {
|
|
8
|
+
method: "POST",
|
|
9
|
+
signal: AbortSignal.timeout(5000),
|
|
10
|
+
headers: { "Content-Type": "application/json", "X-Hyperframes-Share-CSRF": token },
|
|
11
|
+
body: JSON.stringify(event),
|
|
12
|
+
}).catch(() => {});
|
|
13
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
LocalShareAction,
|
|
4
|
+
LocalShareAudience,
|
|
5
|
+
LocalShareJob,
|
|
6
|
+
LocalShareSnapshot,
|
|
7
|
+
} from "@hyperframes/studio-server";
|
|
8
|
+
import { useMountEffect } from "../../hooks/useMountEffect";
|
|
9
|
+
import { LocalShareError, shareAction, shareJob, shareSnapshot } from "./localShareClient";
|
|
10
|
+
import { shareInteraction } from "./localShareTelemetry";
|
|
11
|
+
const STEP_LABELS: Record<string, string> = {
|
|
12
|
+
preparing: "Preparing your video…",
|
|
13
|
+
signing_in: "Finish signing in in your browser…",
|
|
14
|
+
uploading: "Uploading your video…",
|
|
15
|
+
processing: "Processing your video…",
|
|
16
|
+
checking: "Checking your video…",
|
|
17
|
+
};
|
|
18
|
+
const failure = (error: unknown) =>
|
|
19
|
+
error instanceof LocalShareError
|
|
20
|
+
? error.message
|
|
21
|
+
: "Sharing couldn’t finish. Try again to check its status.";
|
|
22
|
+
|
|
23
|
+
export function useQuickShare(token: string, beforeShare?: () => Promise<void>) {
|
|
24
|
+
const [snapshot, setSnapshot] = useState<LocalShareSnapshot | null>(null);
|
|
25
|
+
const [audience, setAudience] = useState<LocalShareAudience | "">("");
|
|
26
|
+
const [spaceId, setSpaceId] = useState("");
|
|
27
|
+
const [busy, setBusyState] = useState(false);
|
|
28
|
+
const working = useRef(false);
|
|
29
|
+
const reconnectOnReturn = useRef(false);
|
|
30
|
+
const [error, setError] = useState<string | null>(null);
|
|
31
|
+
const [errorCode, setErrorCode] = useState<string | null>(null);
|
|
32
|
+
const { copied, setCopied, acknowledgeCopy } = useCopyAcknowledgement();
|
|
33
|
+
const [notice, setNotice] = useState("");
|
|
34
|
+
const opened = useRef(false);
|
|
35
|
+
const lifetime = useRef<AbortController | null>(null);
|
|
36
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
37
|
+
const [progress, setProgress] = useState("");
|
|
38
|
+
|
|
39
|
+
function setWorking(value: boolean) {
|
|
40
|
+
working.current = value;
|
|
41
|
+
setBusyState(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function poll(job: LocalShareJob, signal: AbortSignal) {
|
|
45
|
+
if (signal.aborted) return;
|
|
46
|
+
if (job.state === "running") {
|
|
47
|
+
setWorking(true);
|
|
48
|
+
setProgress(STEP_LABELS[job.step] ?? "Preparing share…");
|
|
49
|
+
timer.current = setTimeout(() => {
|
|
50
|
+
void shareJob(token, job.id, signal)
|
|
51
|
+
.then((next) => poll(next, signal))
|
|
52
|
+
.catch((cause) => {
|
|
53
|
+
reportFailure(cause, signal);
|
|
54
|
+
});
|
|
55
|
+
}, 1000);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
setWorking(false);
|
|
59
|
+
setProgress("");
|
|
60
|
+
const result = job.result;
|
|
61
|
+
if (result) setSnapshot((prior) => (prior ? { ...prior, share: result, job } : prior));
|
|
62
|
+
await refresh(signal, job.state === "failed");
|
|
63
|
+
if (!signal.aborted) setNotice(completionNotice(job));
|
|
64
|
+
}
|
|
65
|
+
async function refresh(signal: AbortSignal, preserveSelection = false) {
|
|
66
|
+
setError(null);
|
|
67
|
+
setErrorCode(null);
|
|
68
|
+
setWorking(true);
|
|
69
|
+
if (timer.current) clearTimeout(timer.current);
|
|
70
|
+
try {
|
|
71
|
+
const next = await shareSnapshot(token, signal);
|
|
72
|
+
if (signal.aborted) return;
|
|
73
|
+
recordOpen(next);
|
|
74
|
+
reconnectOnReturn.current = false;
|
|
75
|
+
setSnapshot(next);
|
|
76
|
+
if (!preserveSelection) resetSelection(next);
|
|
77
|
+
if (next.job?.error) {
|
|
78
|
+
setError(next.job.error.message);
|
|
79
|
+
setErrorCode(next.job.error.code);
|
|
80
|
+
}
|
|
81
|
+
if (next.job?.state === "running") await poll(next.job, signal);
|
|
82
|
+
else setWorking(false);
|
|
83
|
+
return true;
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
reportFailure(cause, signal);
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
useMountEffect(() => {
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
lifetime.current = controller;
|
|
92
|
+
const resume = () => {
|
|
93
|
+
if (document.visibilityState !== "hidden" && reconnectOnReturn.current && !working.current)
|
|
94
|
+
void refresh(controller.signal);
|
|
95
|
+
};
|
|
96
|
+
window.addEventListener("focus", resume);
|
|
97
|
+
document.addEventListener("visibilitychange", resume);
|
|
98
|
+
void refresh(controller.signal);
|
|
99
|
+
return () => {
|
|
100
|
+
window.removeEventListener("focus", resume);
|
|
101
|
+
document.removeEventListener("visibilitychange", resume);
|
|
102
|
+
controller.abort();
|
|
103
|
+
if (timer.current) clearTimeout(timer.current);
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
async function run(action: LocalShareAction) {
|
|
107
|
+
const signal = lifetime.current?.signal;
|
|
108
|
+
if (!signal || signal.aborted || working.current) return;
|
|
109
|
+
setWorking(true);
|
|
110
|
+
setError(null);
|
|
111
|
+
setErrorCode(null);
|
|
112
|
+
setCopied(false);
|
|
113
|
+
setNotice("");
|
|
114
|
+
setProgress("Preparing share…");
|
|
115
|
+
try {
|
|
116
|
+
if (["share", "update"].includes(action.action)) await beforeShare?.();
|
|
117
|
+
if (!signal.aborted) await poll(await shareAction(token, action, signal), signal);
|
|
118
|
+
} catch (cause) {
|
|
119
|
+
await recoverActionFailure(cause, signal);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function recoverActionFailure(cause: unknown, signal: AbortSignal) {
|
|
123
|
+
if (
|
|
124
|
+
cause instanceof LocalShareError &&
|
|
125
|
+
[
|
|
126
|
+
"login_changed",
|
|
127
|
+
"login_expired",
|
|
128
|
+
"unauthenticated",
|
|
129
|
+
"refresh_failed",
|
|
130
|
+
"not_configured",
|
|
131
|
+
].includes(cause.code)
|
|
132
|
+
) {
|
|
133
|
+
reportFailure(cause, signal);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
setProgress("");
|
|
137
|
+
const recovered = await refresh(signal, true);
|
|
138
|
+
if (recovered && !signal.aborted) {
|
|
139
|
+
setError(failure(cause));
|
|
140
|
+
setErrorCode(cause instanceof LocalShareError ? cause.code : null);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function recordOpen(value: LocalShareSnapshot | null) {
|
|
144
|
+
if (opened.current) return;
|
|
145
|
+
opened.current = true;
|
|
146
|
+
const share = value?.share;
|
|
147
|
+
const link = share?.url ? (share.changed ? "changed" : "live") : "none";
|
|
148
|
+
shareInteraction(token, { event: "share_opened", link });
|
|
149
|
+
}
|
|
150
|
+
function resetSelection(next: LocalShareSnapshot) {
|
|
151
|
+
setAudience(next.share.audience ?? "");
|
|
152
|
+
setSpaceId(next.share.spaceId ?? next.spaces.find((space) => space.bound)?.id ?? "");
|
|
153
|
+
}
|
|
154
|
+
function reportFailure(cause: unknown, signal: AbortSignal) {
|
|
155
|
+
if (signal.aborted) return;
|
|
156
|
+
recordOpen(null);
|
|
157
|
+
reconnectOnReturn.current = true;
|
|
158
|
+
setSnapshot(null);
|
|
159
|
+
setCopied(false);
|
|
160
|
+
setError(failure(cause));
|
|
161
|
+
setErrorCode(cause instanceof LocalShareError ? cause.code : null);
|
|
162
|
+
if (cause instanceof LocalShareError && cause.job?.state === "running") {
|
|
163
|
+
void poll(cause.job, signal);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
setWorking(false);
|
|
167
|
+
setProgress("");
|
|
168
|
+
}
|
|
169
|
+
async function copy(url: string) {
|
|
170
|
+
try {
|
|
171
|
+
await navigator.clipboard.writeText(url);
|
|
172
|
+
if (lifetime.current?.signal.aborted) return;
|
|
173
|
+
acknowledgeCopy();
|
|
174
|
+
shareInteraction(token, { event: "share_link_copied", via: "copy" });
|
|
175
|
+
} catch {
|
|
176
|
+
setError("Couldn’t copy. Open the shared page and copy its address.");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
snapshot,
|
|
181
|
+
audience,
|
|
182
|
+
setAudience,
|
|
183
|
+
spaceId,
|
|
184
|
+
busy,
|
|
185
|
+
error,
|
|
186
|
+
errorCode,
|
|
187
|
+
copied,
|
|
188
|
+
notice,
|
|
189
|
+
progress,
|
|
190
|
+
run,
|
|
191
|
+
copy,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
export type QuickShareModel = ReturnType<typeof useQuickShare> & {
|
|
195
|
+
includeSource: boolean;
|
|
196
|
+
setIncludeSource: (value: boolean) => void;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function completionNotice(job: LocalShareJob) {
|
|
200
|
+
if (job.state !== "done") return "";
|
|
201
|
+
if (job.result?.upToDate) return "Already up to date.";
|
|
202
|
+
return job.result?.updated ? "Shared version updated." : "";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function useCopyAcknowledgement() {
|
|
206
|
+
const [copied, setCopied] = useState(false);
|
|
207
|
+
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
208
|
+
useMountEffect(() => () => clearTimeout(timer.current));
|
|
209
|
+
function acknowledgeCopy() {
|
|
210
|
+
clearTimeout(timer.current);
|
|
211
|
+
setCopied(true);
|
|
212
|
+
timer.current = setTimeout(() => setCopied(false), 2000);
|
|
213
|
+
}
|
|
214
|
+
return { copied, setCopied, acknowledgeCopy };
|
|
215
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { act } from "react";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { mountCompositionsTab } from "./compositionsTabTestUtils";
|
|
6
|
+
|
|
7
|
+
type PostedControl = { source: string; type: string; action: string; muted?: boolean };
|
|
8
|
+
|
|
9
|
+
function hoverPreview() {
|
|
10
|
+
const host = mountCompositionsTab();
|
|
11
|
+
const card = host.querySelector<HTMLElement>('[draggable="true"]');
|
|
12
|
+
if (!card) throw new Error("composition card did not render");
|
|
13
|
+
act(() => {
|
|
14
|
+
card.dispatchEvent(new Event("pointerover", { bubbles: true }));
|
|
15
|
+
vi.advanceTimersByTime(300);
|
|
16
|
+
});
|
|
17
|
+
const iframe = host.querySelector("iframe");
|
|
18
|
+
if (!iframe) throw new Error("hover preview did not mount");
|
|
19
|
+
// Stand-in for the frame's window. The document getter throws, as reading into
|
|
20
|
+
// an opaque-origin frame would, so any read the card made would fail the test.
|
|
21
|
+
const frameWindow = { postMessage: vi.fn() };
|
|
22
|
+
Object.defineProperty(iframe, "contentWindow", { configurable: true, get: () => frameWindow });
|
|
23
|
+
Object.defineProperty(iframe, "contentDocument", {
|
|
24
|
+
configurable: true,
|
|
25
|
+
get: () => {
|
|
26
|
+
throw new Error("the hover preview must not read the frame document");
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return { iframe, frameWindow };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function fromFrame(source: unknown, data: Record<string, unknown>) {
|
|
33
|
+
act(() => {
|
|
34
|
+
window.dispatchEvent(
|
|
35
|
+
new MessageEvent("message", {
|
|
36
|
+
data: { source: "hf-preview", ...data },
|
|
37
|
+
source: source as MessageEventSource,
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const posted = (frameWindow: { postMessage: ReturnType<typeof vi.fn> }) =>
|
|
44
|
+
frameWindow.postMessage.mock.calls.map(([message]) => message as PostedControl);
|
|
45
|
+
|
|
46
|
+
let consoleError: ReturnType<typeof vi.spyOn>;
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
vi.useFakeTimers();
|
|
50
|
+
consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
consoleError.mockRestore();
|
|
55
|
+
vi.useRealTimers();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("composition card hover preview", () => {
|
|
59
|
+
it("frames the composition in an opaque origin", () => {
|
|
60
|
+
const { iframe } = hoverPreview();
|
|
61
|
+
expect(iframe.getAttribute("sandbox")).toBe("allow-scripts");
|
|
62
|
+
expect(iframe.sandbox.contains("allow-scripts")).toBe(true);
|
|
63
|
+
expect(iframe.sandbox.contains("allow-same-origin")).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("plays muted over the runtime bridge once the runtime is listening", () => {
|
|
67
|
+
const { frameWindow } = hoverPreview();
|
|
68
|
+
expect(frameWindow.postMessage).not.toHaveBeenCalled();
|
|
69
|
+
|
|
70
|
+
fromFrame(frameWindow, { type: "ready" });
|
|
71
|
+
|
|
72
|
+
expect(posted(frameWindow).map(({ source, type, action }) => [source, type, action])).toEqual([
|
|
73
|
+
["hf-parent", "control", "set-muted"],
|
|
74
|
+
["hf-parent", "control", "play"],
|
|
75
|
+
]);
|
|
76
|
+
expect(posted(frameWindow)[0]?.muted).toBe(true);
|
|
77
|
+
for (const [, targetOrigin] of frameWindow.postMessage.mock.calls) {
|
|
78
|
+
expect(targetOrigin).toBe("*");
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("sizes the stage from the runtime's timeline message", () => {
|
|
83
|
+
const { iframe, frameWindow } = hoverPreview();
|
|
84
|
+
expect([iframe.style.width, iframe.style.height]).toEqual(["1920px", "1080px"]);
|
|
85
|
+
|
|
86
|
+
fromFrame(frameWindow, { type: "timeline", compositionWidth: 1080, compositionHeight: 1920 });
|
|
87
|
+
|
|
88
|
+
expect([iframe.style.width, iframe.style.height]).toEqual(["1080px", "1920px"]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("ignores runtime messages that come from another window", () => {
|
|
92
|
+
const { iframe, frameWindow } = hoverPreview();
|
|
93
|
+
|
|
94
|
+
fromFrame({ postMessage: vi.fn() }, { type: "ready" });
|
|
95
|
+
fromFrame(window, { type: "timeline", compositionWidth: 640, compositionHeight: 640 });
|
|
96
|
+
|
|
97
|
+
expect(frameWindow.postMessage).not.toHaveBeenCalled();
|
|
98
|
+
expect([iframe.style.width, iframe.style.height]).toEqual(["1920px", "1080px"]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { describe, expect, it
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { resolveThumbnailSeekTime } from "../../player/components/CompositionThumbnail";
|
|
3
|
-
import { resolveCompositionPreviewScale
|
|
3
|
+
import { resolveCompositionPreviewScale } from "./CompositionsTab";
|
|
4
4
|
|
|
5
5
|
describe("resolveCompositionPreviewScale", () => {
|
|
6
6
|
it("scales a 16:9 stage to fit the composition card", () => {
|
|
@@ -52,25 +52,3 @@ describe("resolveThumbnailSeekTime", () => {
|
|
|
52
52
|
expect(resolveThumbnailSeekTime(Number.NaN)).toBe(3);
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
-
|
|
56
|
-
describe("syncIframePlayback", () => {
|
|
57
|
-
it("mutes a composition-card preview before playing it", () => {
|
|
58
|
-
const calls: string[] = [];
|
|
59
|
-
const postMessage = vi.fn(() => calls.push("mute"));
|
|
60
|
-
const player = {
|
|
61
|
-
play: vi.fn(() => calls.push("play")),
|
|
62
|
-
};
|
|
63
|
-
const iframe = {
|
|
64
|
-
contentWindow: { __player: player, postMessage },
|
|
65
|
-
getRootNode: () => ({}),
|
|
66
|
-
} as unknown as HTMLIFrameElement;
|
|
67
|
-
|
|
68
|
-
expect(syncIframePlayback(iframe, true)).toBe(true);
|
|
69
|
-
expect(postMessage).toHaveBeenCalledWith(
|
|
70
|
-
expect.objectContaining({ action: "set-muted", muted: true }),
|
|
71
|
-
"*",
|
|
72
|
-
);
|
|
73
|
-
expect(calls).toEqual(["mute", "play"]);
|
|
74
|
-
expect(player.play).toHaveBeenCalledOnce();
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildProjectApiPath } from "../../utils/projectRouting";
|
|
2
|
-
import { memo,
|
|
2
|
+
import { memo, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { useThumbnailLease } from "../../hooks/useThumbnailLease";
|
|
4
4
|
import {
|
|
5
5
|
thumbnailScheduler,
|
|
@@ -9,11 +9,10 @@ import {
|
|
|
9
9
|
import {
|
|
10
10
|
buildCompositionThumbnailUrl,
|
|
11
11
|
compositionThumbnailRequest,
|
|
12
|
-
resolveThumbnailSeekTime,
|
|
13
12
|
THUMBNAIL_SEEK_TIME_SECONDS,
|
|
14
13
|
} from "../../player/components/CompositionThumbnail";
|
|
15
14
|
import { setPreviewMediaMuted } from "../../player/lib/timelineIframeHelpers";
|
|
16
|
-
import { postFrameDisplayScale } from "../../player/lib/runtimeProtocol";
|
|
15
|
+
import { postFrameDisplayScale, postRuntimeControlMessage } from "../../player/lib/runtimeProtocol";
|
|
17
16
|
import { usePlayerStore } from "../../player/store/playerStore";
|
|
18
17
|
import { thumbnailRevisionOf } from "../../player/store/thumbnailSlice";
|
|
19
18
|
import { encodePreviewPath } from "../../player/components/thumbnailUtils";
|
|
@@ -36,16 +35,8 @@ interface CompositionsTabProps {
|
|
|
36
35
|
const DEFAULT_PREVIEW_STAGE = { width: 1920, height: 1080 };
|
|
37
36
|
const CARD_W = 80;
|
|
38
37
|
const CARD_H = 45;
|
|
39
|
-
const THUMBNAIL_PLAYBACK_SYNC_ATTEMPTS = 10;
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
__player?: {
|
|
43
|
-
play?: () => void;
|
|
44
|
-
pause?: () => void;
|
|
45
|
-
seek?: (time: number) => void;
|
|
46
|
-
getDuration?: () => number;
|
|
47
|
-
};
|
|
48
|
-
};
|
|
39
|
+
const OPAQUE_ORIGIN_SCRIPTS_ONLY_SANDBOX = "allow-scripts";
|
|
49
40
|
|
|
50
41
|
export function resolveCompositionPreviewScale(input: {
|
|
51
42
|
cardWidth: number;
|
|
@@ -84,53 +75,26 @@ export function compositionCardThumbnailUrl(
|
|
|
84
75
|
});
|
|
85
76
|
}
|
|
86
77
|
|
|
87
|
-
function
|
|
88
|
-
|
|
89
|
-
const parsed = Number.parseFloat(value);
|
|
90
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
78
|
+
function positiveDimension(value: unknown, fallback: number): number {
|
|
79
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : fallback;
|
|
91
80
|
}
|
|
92
81
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
try {
|
|
106
|
-
const doc = iframe?.contentDocument;
|
|
107
|
-
const root = doc?.querySelector("[data-composition-id]") ?? doc?.documentElement ?? null;
|
|
108
|
-
return (
|
|
109
|
-
parsePositiveNumber(root?.getAttribute("data-composition-duration") ?? null) ??
|
|
110
|
-
parsePositiveNumber(root?.getAttribute("data-duration") ?? null)
|
|
111
|
-
);
|
|
112
|
-
} catch {
|
|
113
|
-
return null;
|
|
114
|
-
}
|
|
82
|
+
function stageSizeFromRuntimeTimelineMessage(
|
|
83
|
+
data: unknown,
|
|
84
|
+
): { width: number; height: number } | null {
|
|
85
|
+
if (typeof data !== "object" || data === null) return null;
|
|
86
|
+
const message = data as Record<string, unknown>;
|
|
87
|
+
if (message.source !== "hf-preview" || message.type !== "timeline") return null;
|
|
88
|
+
return {
|
|
89
|
+
width: positiveDimension(message.compositionWidth, DEFAULT_PREVIEW_STAGE.width),
|
|
90
|
+
height: positiveDimension(message.compositionHeight, DEFAULT_PREVIEW_STAGE.height),
|
|
91
|
+
};
|
|
115
92
|
}
|
|
116
93
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (shouldPlay) {
|
|
123
|
-
setPreviewMediaMuted(iframe, true);
|
|
124
|
-
player.play?.();
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
player.pause?.();
|
|
129
|
-
player.seek?.(resolveThumbnailSeekTime(resolveIframeDuration(iframe)));
|
|
130
|
-
return true;
|
|
131
|
-
} catch {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
94
|
+
function muteThenPlayOverRuntimeBridge(iframe: HTMLIFrameElement | null): void {
|
|
95
|
+
if (!iframe) return;
|
|
96
|
+
setPreviewMediaMuted(iframe, true);
|
|
97
|
+
postRuntimeControlMessage(iframe.contentWindow, "play");
|
|
134
98
|
}
|
|
135
99
|
|
|
136
100
|
const imageUrlOf = (snapshot: ThumbnailSnapshot) =>
|
|
@@ -195,24 +159,8 @@ function CompCard({
|
|
|
195
159
|
const [livePreviewLoaded, setLivePreviewLoaded] = useState(false);
|
|
196
160
|
const iframeRef = useRef<HTMLIFrameElement | null>(null);
|
|
197
161
|
const hoverTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
198
|
-
const syncTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
199
162
|
const draggedRef = useRef(false);
|
|
200
163
|
|
|
201
|
-
const requestIframePlaybackSync = useCallback((shouldPlay: boolean) => {
|
|
202
|
-
if (syncTimer.current) {
|
|
203
|
-
clearTimeout(syncTimer.current);
|
|
204
|
-
syncTimer.current = null;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const sync = (remainingAttempts: number) => {
|
|
208
|
-
if (syncIframePlayback(iframeRef.current, shouldPlay) || remainingAttempts <= 0) return;
|
|
209
|
-
|
|
210
|
-
syncTimer.current = setTimeout(() => sync(remainingAttempts - 1), 100);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
sync(THUMBNAIL_PLAYBACK_SYNC_ATTEMPTS);
|
|
214
|
-
}, []);
|
|
215
|
-
|
|
216
164
|
const handleEnter = () => {
|
|
217
165
|
hoverTimer.current = setTimeout(() => setHovered(true), 300);
|
|
218
166
|
};
|
|
@@ -221,10 +169,6 @@ function CompCard({
|
|
|
221
169
|
clearTimeout(hoverTimer.current);
|
|
222
170
|
hoverTimer.current = null;
|
|
223
171
|
}
|
|
224
|
-
if (syncTimer.current) {
|
|
225
|
-
clearTimeout(syncTimer.current);
|
|
226
|
-
syncTimer.current = null;
|
|
227
|
-
}
|
|
228
172
|
setHovered(false);
|
|
229
173
|
setLivePreviewLoaded(false);
|
|
230
174
|
};
|
|
@@ -255,30 +199,35 @@ function CompCard({
|
|
|
255
199
|
const thumbnailOffsetX = (CARD_W - stageSize.width * previewScale) / 2;
|
|
256
200
|
const thumbnailOffsetY = (CARD_H - stageSize.height * previewScale) / 2;
|
|
257
201
|
|
|
258
|
-
useEffect(() => {
|
|
259
|
-
if (hovered) requestIframePlaybackSync(true);
|
|
260
|
-
}, [hovered, requestIframePlaybackSync]);
|
|
261
|
-
|
|
262
202
|
useEffect(() => {
|
|
263
203
|
if (livePreviewLoaded) postFrameDisplayScale(iframeRef.current);
|
|
264
204
|
}, [livePreviewLoaded, previewScale]);
|
|
265
205
|
|
|
266
206
|
useEffect(() => {
|
|
267
207
|
if (!hovered) return;
|
|
268
|
-
const
|
|
208
|
+
const onRuntimeMessage = (event: MessageEvent) => {
|
|
269
209
|
const frame = iframeRef.current;
|
|
270
|
-
if (frame
|
|
210
|
+
if (!frame || event.source !== frame.contentWindow) return;
|
|
211
|
+
const runtimeControlListenerInstalled =
|
|
212
|
+
event.data?.source === "hf-preview" && event.data.type === "ready";
|
|
213
|
+
if (runtimeControlListenerInstalled) {
|
|
271
214
|
postFrameDisplayScale(frame);
|
|
215
|
+
muteThenPlayOverRuntimeBridge(frame);
|
|
216
|
+
return;
|
|
272
217
|
}
|
|
218
|
+
const stage = stageSizeFromRuntimeTimelineMessage(event.data);
|
|
219
|
+
if (!stage) return;
|
|
220
|
+
setStageSize((current) =>
|
|
221
|
+
current.width === stage.width && current.height === stage.height ? current : stage,
|
|
222
|
+
);
|
|
273
223
|
};
|
|
274
|
-
window.addEventListener("message",
|
|
275
|
-
return () => window.removeEventListener("message",
|
|
224
|
+
window.addEventListener("message", onRuntimeMessage);
|
|
225
|
+
return () => window.removeEventListener("message", onRuntimeMessage);
|
|
276
226
|
}, [hovered]);
|
|
277
227
|
|
|
278
228
|
useEffect(() => {
|
|
279
229
|
return () => {
|
|
280
230
|
if (hoverTimer.current) clearTimeout(hoverTimer.current);
|
|
281
|
-
if (syncTimer.current) clearTimeout(syncTimer.current);
|
|
282
231
|
};
|
|
283
232
|
}, []);
|
|
284
233
|
|
|
@@ -342,7 +291,7 @@ function CompCard({
|
|
|
342
291
|
<iframe
|
|
343
292
|
ref={iframeRef}
|
|
344
293
|
src={previewUrl}
|
|
345
|
-
sandbox=
|
|
294
|
+
sandbox={OPAQUE_ORIGIN_SCRIPTS_ONLY_SANDBOX}
|
|
346
295
|
className="absolute border-none pointer-events-none"
|
|
347
296
|
style={{
|
|
348
297
|
transformOrigin: "0 0",
|
|
@@ -352,21 +301,7 @@ function CompCard({
|
|
|
352
301
|
top: thumbnailOffsetY,
|
|
353
302
|
transform: `scale(${previewScale})`,
|
|
354
303
|
}}
|
|
355
|
-
onLoad={(
|
|
356
|
-
try {
|
|
357
|
-
const iframe = e.currentTarget;
|
|
358
|
-
const root = iframe.contentDocument?.querySelector("[data-composition-id]");
|
|
359
|
-
const width =
|
|
360
|
-
Number(root?.getAttribute("data-width")) || DEFAULT_PREVIEW_STAGE.width;
|
|
361
|
-
const height =
|
|
362
|
-
Number(root?.getAttribute("data-height")) || DEFAULT_PREVIEW_STAGE.height;
|
|
363
|
-
setStageSize({ width, height });
|
|
364
|
-
setLivePreviewLoaded(true);
|
|
365
|
-
requestIframePlaybackSync(true);
|
|
366
|
-
} catch {
|
|
367
|
-
setStageSize(DEFAULT_PREVIEW_STAGE);
|
|
368
|
-
}
|
|
369
|
-
}}
|
|
304
|
+
onLoad={() => setLivePreviewLoaded(true)}
|
|
370
305
|
title={`${name} preview`}
|
|
371
306
|
tabIndex={-1}
|
|
372
307
|
/>
|
|
@@ -92,8 +92,11 @@ export function Input({
|
|
|
92
92
|
dirtyRef.current = true;
|
|
93
93
|
setDraft(next);
|
|
94
94
|
}}
|
|
95
|
-
onBlur={() =>
|
|
95
|
+
onBlur={() => {
|
|
96
|
+
if (dirtyRef.current) commit(draft);
|
|
97
|
+
}}
|
|
96
98
|
onKeyDown={(event) => {
|
|
99
|
+
if (event.nativeEvent.isComposing) return;
|
|
97
100
|
if (event.key === "Enter") {
|
|
98
101
|
event.preventDefault();
|
|
99
102
|
commit(draft);
|
|
@@ -362,6 +362,91 @@ describe("NumberField", () => {
|
|
|
362
362
|
});
|
|
363
363
|
|
|
364
364
|
describe("Input", () => {
|
|
365
|
+
it.each(["Enter", "Escape"])("leaves %s to the active IME composition", (key) => {
|
|
366
|
+
const commits: string[] = [];
|
|
367
|
+
const trackCommit = vi.fn(track("text"));
|
|
368
|
+
const host = render(
|
|
369
|
+
<Input
|
|
370
|
+
value="hello"
|
|
371
|
+
aria-label="Name"
|
|
372
|
+
onCommit={(value) => commits.push(value)}
|
|
373
|
+
onTrack={trackCommit}
|
|
374
|
+
/>,
|
|
375
|
+
);
|
|
376
|
+
const input = host.querySelector("input");
|
|
377
|
+
if (!input) throw new Error("Input did not render");
|
|
378
|
+
|
|
379
|
+
act(() => input.focus());
|
|
380
|
+
act(
|
|
381
|
+
() => void input.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true })),
|
|
382
|
+
);
|
|
383
|
+
type(input, "中");
|
|
384
|
+
const composingKey = new KeyboardEvent("keydown", {
|
|
385
|
+
key,
|
|
386
|
+
bubbles: true,
|
|
387
|
+
cancelable: true,
|
|
388
|
+
isComposing: true,
|
|
389
|
+
});
|
|
390
|
+
act(() => void input.dispatchEvent(composingKey));
|
|
391
|
+
|
|
392
|
+
expect(composingKey.defaultPrevented).toBe(false);
|
|
393
|
+
expect(document.activeElement).toBe(input);
|
|
394
|
+
expect(input.value).toBe("中");
|
|
395
|
+
expect(commits).toEqual([]);
|
|
396
|
+
expect(trackCommit).not.toHaveBeenCalled();
|
|
397
|
+
expect(designInputCalls()).toHaveLength(0);
|
|
398
|
+
|
|
399
|
+
act(() => void input.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })));
|
|
400
|
+
type(input, "中文");
|
|
401
|
+
fire(input, "keydown", { key: "Enter" });
|
|
402
|
+
expect(commits).toEqual(["中文"]);
|
|
403
|
+
expect(trackCommit).toHaveBeenCalledTimes(1);
|
|
404
|
+
expect(designInputCalls()).toHaveLength(1);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it.each(["Enter", "Escape"])("does not commit the draft again on blur after %s", (key) => {
|
|
408
|
+
const commits: string[] = [];
|
|
409
|
+
const trackCommit = vi.fn(track("text"));
|
|
410
|
+
const host = render(
|
|
411
|
+
<Input
|
|
412
|
+
value="hello"
|
|
413
|
+
aria-label="Name"
|
|
414
|
+
onCommit={(value) => commits.push(value)}
|
|
415
|
+
onTrack={trackCommit}
|
|
416
|
+
/>,
|
|
417
|
+
);
|
|
418
|
+
const input = host.querySelector("input");
|
|
419
|
+
if (!input) throw new Error("Input did not render");
|
|
420
|
+
|
|
421
|
+
act(() => input.focus());
|
|
422
|
+
type(input, "world");
|
|
423
|
+
fire(input, "keydown", { key });
|
|
424
|
+
act(() => input.blur());
|
|
425
|
+
|
|
426
|
+
expect(commits).toEqual(key === "Enter" ? ["world"] : []);
|
|
427
|
+
expect(input.value).toBe(key === "Enter" ? "world" : "hello");
|
|
428
|
+
expect(trackCommit).toHaveBeenCalledTimes(key === "Enter" ? 1 : 0);
|
|
429
|
+
expect(designInputCalls()).toHaveLength(key === "Enter" ? 1 : 0);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("commits a changed draft on blur without Enter", () => {
|
|
433
|
+
const commits: string[] = [];
|
|
434
|
+
const trackCommit = vi.fn(track("text"));
|
|
435
|
+
const host = render(
|
|
436
|
+
<Input value="hello" onCommit={(value) => commits.push(value)} onTrack={trackCommit} />,
|
|
437
|
+
);
|
|
438
|
+
const input = host.querySelector("input");
|
|
439
|
+
if (!input) throw new Error("Input did not render");
|
|
440
|
+
|
|
441
|
+
act(() => input.focus());
|
|
442
|
+
type(input, "world");
|
|
443
|
+
act(() => input.blur());
|
|
444
|
+
|
|
445
|
+
expect(commits).toEqual(["world"]);
|
|
446
|
+
expect(trackCommit).toHaveBeenCalledTimes(1);
|
|
447
|
+
expect(designInputCalls()).toHaveLength(1);
|
|
448
|
+
});
|
|
449
|
+
|
|
365
450
|
it("commits a typed value on Enter, once, and not per keystroke", () => {
|
|
366
451
|
const commits: string[] = [];
|
|
367
452
|
const host = render(
|