@hyperframes/studio 0.9.31 → 0.9.32
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-Bz1jsLs8.js} +1 -1
- package/dist/assets/{hyperframes-player-D_016Ubp.js → hyperframes-player-cdH5Sz_t.js} +1 -1
- package/dist/assets/{index-Ob53qo6K.js → index-D1l7izdf.js} +176 -176
- package/dist/assets/{index-Bw4stQMm.js → index-DFRbcd5J.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-vbdgaMqL.js → pausedVideoDecoder-CgV8YB6S.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 +1 -1
- package/dist/index.js +165 -200
- package/dist/player.js +2 -2
- package/dist/ui.js +1 -1
- package/package.json +8 -8
- 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
package/dist/player.js
CHANGED
package/dist/ui.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperframes/studio",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.32",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -60,12 +60,12 @@
|
|
|
60
60
|
"dockview-react": "8.3.1",
|
|
61
61
|
"gsap": "^3.13.0",
|
|
62
62
|
"mediabunny": "^1.65.2",
|
|
63
|
-
"@hyperframes/
|
|
64
|
-
"@hyperframes/
|
|
65
|
-
"@hyperframes/
|
|
66
|
-
"@hyperframes/
|
|
67
|
-
"@hyperframes/
|
|
68
|
-
"@hyperframes/
|
|
63
|
+
"@hyperframes/parsers": "0.9.32",
|
|
64
|
+
"@hyperframes/shader-transitions": "0.9.32",
|
|
65
|
+
"@hyperframes/core": "0.9.32",
|
|
66
|
+
"@hyperframes/sdk": "0.9.32",
|
|
67
|
+
"@hyperframes/studio-server": "0.9.32",
|
|
68
|
+
"@hyperframes/player": "0.9.32"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"vite": "^6.4.2",
|
|
83
83
|
"vitest": "^4.1.11",
|
|
84
84
|
"zustand": "^5.0.0",
|
|
85
|
-
"@hyperframes/producer": "0.9.
|
|
85
|
+
"@hyperframes/producer": "0.9.32"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
88
|
"react": "19",
|
|
@@ -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(
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
/**
|
|
3
|
+
* Loads the REAL `@mcp-b/global` through Studio's loader, inside a page that is
|
|
4
|
+
* framed by another site, and checks that neither of the package's message
|
|
5
|
+
* transports answers anyone but Studio's own page.
|
|
6
|
+
*/
|
|
7
|
+
import { afterAll, expect, it, vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
vi.mock("../telemetry/client", () => ({ trackEvent: vi.fn() }));
|
|
10
|
+
|
|
11
|
+
const framer = { postMessage: vi.fn() };
|
|
12
|
+
const ownParent = Object.getOwnPropertyDescriptor(window, "parent");
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
if (ownParent) Object.defineProperty(window, "parent", ownParent);
|
|
15
|
+
else Reflect.deleteProperty(window, "parent");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
|
|
19
|
+
|
|
20
|
+
it("answers no MCP client but Studio's own page when another site frames Studio", async () => {
|
|
21
|
+
Object.defineProperty(window, "parent", { value: framer, configurable: true });
|
|
22
|
+
const ownMessages = vi.spyOn(window, "postMessage");
|
|
23
|
+
const { loadModelContextPolyfill } = await import("./polyfill");
|
|
24
|
+
|
|
25
|
+
expect(await loadModelContextPolyfill()).not.toBeNull();
|
|
26
|
+
await settle();
|
|
27
|
+
expect(framer.postMessage).not.toHaveBeenCalled();
|
|
28
|
+
|
|
29
|
+
ownMessages.mockClear();
|
|
30
|
+
window.dispatchEvent(
|
|
31
|
+
new MessageEvent("message", {
|
|
32
|
+
data: {
|
|
33
|
+
channel: "mcp-default",
|
|
34
|
+
type: "mcp",
|
|
35
|
+
direction: "client-to-server",
|
|
36
|
+
payload: "mcp-check-ready",
|
|
37
|
+
},
|
|
38
|
+
origin: "https://other.example",
|
|
39
|
+
source: window,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
await settle();
|
|
43
|
+
expect(ownMessages).not.toHaveBeenCalled();
|
|
44
|
+
|
|
45
|
+
window.dispatchEvent(
|
|
46
|
+
new MessageEvent("message", {
|
|
47
|
+
data: {
|
|
48
|
+
channel: "mcp-default",
|
|
49
|
+
type: "mcp",
|
|
50
|
+
direction: "client-to-server",
|
|
51
|
+
payload: "mcp-check-ready",
|
|
52
|
+
},
|
|
53
|
+
origin: window.location.origin,
|
|
54
|
+
source: window,
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
await settle();
|
|
58
|
+
expect(ownMessages).toHaveBeenCalled();
|
|
59
|
+
});
|
package/src/webmcp/polyfill.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* and so it lands in its own chunk rather than the entry bundle.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
+
import type { WebModelContextInitOptions } from "@mcp-b/global";
|
|
15
16
|
import { makeStudioDebugLogger } from "../utils/studioDebug";
|
|
16
17
|
import { trackEvent } from "../telemetry/client";
|
|
17
18
|
import { getModelContext, type ModelContext } from "./types";
|
|
@@ -24,8 +25,22 @@ const log = makeStudioDebugLogger("webmcp");
|
|
|
24
25
|
*/
|
|
25
26
|
let pending: Promise<ModelContext | null> | null = null;
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Left to its defaults the package answers MCP messages from any origin, and from
|
|
30
|
+
* whatever page frames Studio. Only Studio's own page may be a client.
|
|
31
|
+
*/
|
|
32
|
+
function ownPageOnly(): WebModelContextInitOptions {
|
|
33
|
+
return {
|
|
34
|
+
transport: {
|
|
35
|
+
tabServer: { allowedOrigins: [window.location.origin] },
|
|
36
|
+
iframeServer: false,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
27
41
|
async function importPolyfill(): Promise<ModelContext | null> {
|
|
28
42
|
try {
|
|
43
|
+
window.__webModelContextOptions = ownPageOnly();
|
|
29
44
|
await import("@mcp-b/global");
|
|
30
45
|
const modelContext = getModelContext();
|
|
31
46
|
if (!modelContext) {
|