@hyperframes/studio 0.9.65 → 0.9.67

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.
Files changed (66) hide show
  1. package/dist/assets/hyperframes-player-DHYu0XuV.js +573 -0
  2. package/dist/assets/{index-tvLrzMBv.js → index-BBKHfua5.js} +180 -180
  3. package/dist/assets/{index-CAjz3b83.js → index-DqND4rHq.js} +1 -1
  4. package/dist/assets/{pausedVideoDecoder-xC5IGkfB.js → pausedVideoDecoder-B1FNXBMe.js} +1 -1
  5. package/dist/{chunk-VDDEXNVX.js → chunk-LR3VT3EF.js} +20 -0
  6. package/dist/index.html +1 -1
  7. package/dist/index.js +396 -160
  8. package/dist/player.js +1 -1
  9. package/package.json +8 -8
  10. package/src/captions/components/CaptionOverlay.sealed.test.ts +2 -25
  11. package/src/components/editor/LayersPanel.opaque.test.ts +4 -29
  12. package/src/components/editor/domEditOverlayCrop.ts +78 -15
  13. package/src/components/editor/domEditOverlayCropFrameLift.test.ts +64 -0
  14. package/src/components/editor/gsapLivePreview.sealed.test.ts +35 -0
  15. package/src/components/editor/gsapLivePreview.ts +6 -2
  16. package/src/components/editor/gsapMoveSealedPreview.test.ts +3 -23
  17. package/src/components/editor/previewDrafts.test.ts +13 -39
  18. package/src/components/editor/previewDrafts.ts +2 -0
  19. package/src/components/editor/previewGestureStartRead.test.ts +59 -21
  20. package/src/components/editor/previewGestureStartRead.ts +9 -0
  21. package/src/components/editor/previewLayers.test.ts +3 -20
  22. package/src/components/editor/previewOverlayRects.test.ts +3 -20
  23. package/src/components/editor/previewPointerTargets.test.ts +3 -20
  24. package/src/components/editor/previewSelection.test.ts +51 -35
  25. package/src/components/editor/previewSelection.ts +17 -0
  26. package/src/components/editor/useColorGradingController.sealed.test.ts +5 -30
  27. package/src/components/editor/useMotionPathData.sealed.test.ts +2 -29
  28. package/src/components/nle/NLEContext.drillDown.test.ts +85 -0
  29. package/src/components/nle/NLEContext.tsx +21 -23
  30. package/src/components/nle/NLEPreview.test.ts +18 -3
  31. package/src/components/nle/NLEPreview.tsx +10 -1
  32. package/src/components/nle/audioSealedPreview.test.ts +5 -32
  33. package/src/hooks/appHotkeysDispatch.ts +38 -1
  34. package/src/hooks/domEditCommits.sealed.test.ts +191 -0
  35. package/src/hooks/domStyleCommit.ts +18 -7
  36. package/src/hooks/gsapMutationClient.test.ts +32 -0
  37. package/src/hooks/gsapMutationClient.ts +6 -3
  38. package/src/hooks/gsapResizeIntercept.ts +7 -2
  39. package/src/hooks/timelineTimingSync.test.ts +1 -1
  40. package/src/hooks/useAppHotkeys.previewForwarding.test.tsx +134 -0
  41. package/src/hooks/useAppHotkeys.ts +36 -1
  42. package/src/hooks/useDomEditAttributeCommits.ts +43 -19
  43. package/src/hooks/useEnableKeyframes.sealed.test.ts +2 -29
  44. package/src/hooks/useFreezeFrame.test.ts +3 -2
  45. package/src/hooks/useFreezeFrame.ts +2 -2
  46. package/src/hooks/useGroupCommits.ts +7 -2
  47. package/src/hooks/useSlideshowTabState.test.ts +47 -3
  48. package/src/hooks/useSlideshowTabState.ts +32 -16
  49. package/src/player/hooks/timelinePlayerTestHarness.ts +2 -1
  50. package/src/player/hooks/usePlaybackKeyboard.ts +1 -1
  51. package/src/player/hooks/usePreviewFirstFrameTelemetry.sealed.test.ts +43 -0
  52. package/src/player/hooks/usePreviewFirstFrameTelemetry.ts +11 -7
  53. package/src/player/hooks/useTimelineSyncCallbacks.sealed.test.ts +39 -31
  54. package/src/player/hooks/useTimelineSyncCallbacks.ts +21 -11
  55. package/src/player/lib/previewChannel.test.ts +10 -0
  56. package/src/player/lib/previewChannel.ts +21 -0
  57. package/src/player/lib/previewElements.test.ts +34 -1
  58. package/src/player/lib/previewElements.ts +99 -16
  59. package/src/player/lib/sealedPreviewFrame.test-helpers.ts +38 -0
  60. package/src/player/lib/timelineIframeHelpers.ts +1 -1
  61. package/src/utils/blockInstaller.test.ts +82 -48
  62. package/src/utils/blockInstaller.ts +6 -14
  63. package/src/utils/previewCompositionSize.test.ts +61 -0
  64. package/src/utils/previewCompositionSize.ts +21 -1
  65. package/src/utils/studioSaveDiagnostics.ts +2 -0
  66. package/dist/assets/hyperframes-player-Dscc6yHD.js +0 -573
@@ -0,0 +1,43 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { act, createElement } from "react";
4
+ import { createRoot } from "react-dom/client";
5
+ import { afterEach, expect, it, vi } from "vitest";
6
+ import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers";
7
+ import { usePreviewFirstFrameTelemetry } from "./usePreviewFirstFrameTelemetry";
8
+
9
+ const firstFrames = vi.hoisted(() => [] as Array<Record<string, unknown>>);
10
+ vi.mock("../../telemetry/events", () => ({
11
+ trackPreviewFirstFrame: (props: Record<string, unknown>) => firstFrames.push(props),
12
+ }));
13
+
14
+ (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
15
+
16
+ afterEach(() => {
17
+ document.body.innerHTML = "";
18
+ firstFrames.length = 0;
19
+ });
20
+
21
+ // fails without the fix: a sealed frame's document read as null, so the event said 0 clips and 0 seconds.
22
+ it("reports a sealed preview's clips and length on its first frame", async () => {
23
+ document.body.innerHTML = `<div data-composition-id="main" data-duration="6">
24
+ <div id="title" class="clip" data-start="0" data-duration="2"></div>
25
+ <video id="shot" class="clip" data-start="2" data-duration="4" src="shot.mp4"></video>
26
+ </div>`;
27
+ const slot = { gen: 1, role: "live" as const };
28
+ let report: ReturnType<typeof usePreviewFirstFrameTelemetry> | undefined;
29
+ function Harness() {
30
+ report = usePreviewFirstFrameTelemetry([slot]);
31
+ return null;
32
+ }
33
+ act(() => createRoot(document.createElement("div")).render(createElement(Harness)));
34
+
35
+ report!(slot, { iframe: sealedPreviewFrame().iframe, startedAt: performance.now(), loadId: 1 });
36
+
37
+ await vi.waitFor(() => expect(firstFrames).toHaveLength(1));
38
+ expect(firstFrames[0]).toMatchObject({
39
+ composition_seconds: 6,
40
+ clip_count: 2,
41
+ media_clip_count: 1,
42
+ });
43
+ });
@@ -4,6 +4,7 @@ import { readTimelineDurationFromDocument } from "../lib/timelineElementHelpers"
4
4
  import type { PreviewIframeSlot } from "./useTimelineSyncCallbacks";
5
5
  import { trackPreviewFirstFrame } from "../../telemetry/events";
6
6
  import { getBrowserSystemMeta } from "../../telemetry/system";
7
+ import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
7
8
 
8
9
  export function readPreviewComplexity(doc: Document | null | undefined): {
9
10
  clip_count: number;
@@ -38,13 +39,16 @@ export function usePreviewFirstFrameTelemetry(previewSlots: PreviewIframeSlot[])
38
39
  const reportKey = `${slot.role}:${slot.gen}:${loadId}`;
39
40
  if (reportedPaintsRef.current.has(reportKey)) return;
40
41
  reportedPaintsRef.current.add(reportKey);
41
- const complexity = readPreviewComplexity(iframe.contentDocument);
42
- trackPreviewFirstFrame({
43
- duration_ms: Math.max(0, Math.round(performance.now() - startedAt)),
44
- composition_seconds: readTimelineDurationFromDocument(iframe.contentDocument),
45
- ...complexity,
46
- studio_version: getBrowserSystemMeta().studio_version,
47
- });
42
+ const durationMs = Math.max(0, Math.round(performance.now() - startedAt));
43
+ const report = (doc: Document | null) =>
44
+ trackPreviewFirstFrame({
45
+ duration_ms: durationMs,
46
+ composition_seconds: readTimelineDurationFromDocument(doc),
47
+ ...readPreviewComplexity(doc),
48
+ studio_version: getBrowserSystemMeta().studio_version,
49
+ });
50
+ if (previewFrameOpaque(iframe)) void previewDocumentCopy(iframe).then(report);
51
+ else report(iframe.contentDocument);
48
52
  },
49
53
  [],
50
54
  );
@@ -2,44 +2,17 @@
2
2
 
3
3
  import { act, createElement } from "react";
4
4
  import { createRoot } from "react-dom/client";
5
- import { afterEach, expect, it, vi } from "vitest";
6
- // The frame's real runtime agent, serving the opaque frame from this document as Studio's marked preview page.
7
- import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
8
- import type { TimelineElement } from "../store/playerStore";
9
- import { detachedFakeIframe } from "./timelinePlayerTestHarness";
5
+ import { afterEach, expect, it, onTestFinished, vi } from "vitest";
6
+ import { usePlayerStore, type TimelineElement } from "../store/playerStore";
10
7
  import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks";
8
+ import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers";
11
9
 
12
10
  (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
13
11
 
14
- const closers: Array<() => void> = [];
15
12
  afterEach(() => {
16
- closers.splice(0).forEach((close) => close());
17
13
  document.body.innerHTML = "";
18
14
  });
19
15
 
20
- /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
21
- function opaqueFrame() {
22
- const frame: Record<string, unknown> = {};
23
- const iframe = detachedFakeIframe(frame);
24
- Object.defineProperty(iframe, "contentDocument", { value: null });
25
- Object.defineProperty(frame, "document", {
26
- get: () => {
27
- throw new DOMException("blocked", "SecurityError");
28
- },
29
- });
30
- const parent = {
31
- postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
32
- window.dispatchEvent(
33
- new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
34
- ),
35
- } as unknown as Window;
36
- const agent = installMarkedEditingAgent(parent);
37
- frame.postMessage = (data: unknown) =>
38
- window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
39
- closers.push(() => agent.dispose());
40
- return iframe;
41
- }
42
-
43
16
  function processWith(iframe: HTMLIFrameElement) {
44
17
  const sync = vi.fn<(els: TimelineElement[], duration?: number) => void>();
45
18
  let callbacks: ReturnType<typeof useTimelineSyncCallbacks> | undefined;
@@ -84,7 +57,7 @@ it("binds a sealed preview's timeline row to its element, as an open preview doe
84
57
  document.body.innerHTML = `<div data-composition-id="main" data-composition-file="index.html">
85
58
  <div id="box" class="clip" data-start="0" data-duration="2" data-audio-group="voice"></div>
86
59
  </div>`;
87
- const { sync, process } = processWith(opaqueFrame());
60
+ const { sync, process } = processWith(sealedPreviewFrame().iframe);
88
61
 
89
62
  process({ clips: [boxClip], durationInFrames: 60 });
90
63
 
@@ -96,3 +69,38 @@ it("binds a sealed preview's timeline row to its element, as an open preview doe
96
69
  audioGroup: "voice",
97
70
  });
98
71
  });
72
+
73
+ // fails without the fix: reading the frame's `__clipTree` threw, so a sealed scene's children and parents stayed empty.
74
+ it("fills a sealed scene's children and the runtime's clip parents", async () => {
75
+ document.body.innerHTML = `<div data-composition-id="main" data-composition-file="index.html">
76
+ <div id="scene" data-composition-id="scene" data-composition-file="scene.html" data-start="0" data-duration="2">
77
+ <h1 id="title">Title</h1>
78
+ </div>
79
+ </div>`;
80
+ (window as unknown as { __clipTree: unknown }).__clipTree = {
81
+ roots: [
82
+ { id: "scene", parentId: null, children: [{ id: "cue", parentId: "scene", children: [] }] },
83
+ ],
84
+ };
85
+ onTestFinished(() => delete (window as unknown as { __clipTree?: unknown }).__clipTree);
86
+ usePlayerStore.getState().setClipParentMap(new Map());
87
+ usePlayerStore.getState().setDomClipChildren([]);
88
+ const { process } = processWith(sealedPreviewFrame().iframe);
89
+
90
+ process({
91
+ clips: [
92
+ {
93
+ ...boxClip,
94
+ id: "scene",
95
+ label: "scene",
96
+ kind: "composition",
97
+ tagName: "div",
98
+ compositionId: "scene",
99
+ },
100
+ ],
101
+ durationInFrames: 60,
102
+ });
103
+
104
+ await vi.waitFor(() => expect(usePlayerStore.getState().clipParentMap.get("cue")).toBe("scene"));
105
+ expect(usePlayerStore.getState().domClipChildren.map((child) => child.id)).toContain("title");
106
+ });
@@ -15,7 +15,7 @@ import type { PlaybackAdapter, IframeWindow } from "../lib/playbackTypes";
15
15
  import { readTimelineDurationFromDocument } from "../lib/timelineDOM";
16
16
  import { buildMissingCompositionElements } from "../lib/timelineIframeHelpers";
17
17
  import { acceptedRuntimeMessageFps } from "../lib/runtimeProtocol";
18
- import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
18
+ import { previewClipParents, previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
19
19
  import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences";
20
20
  import {
21
21
  buildTimelineElementsFromClips,
@@ -153,6 +153,15 @@ export function resolveTimelineTotalDuration(input: {
153
153
  );
154
154
  }
155
155
 
156
+ /** A same-origin frame's clip tree, read in place; a frame that refuses the read has none. */
157
+ function openClipParents(iframe: HTMLIFrameElement | null): Map<string, string> {
158
+ try {
159
+ return clipTreeParentMap(iframe?.contentWindow ?? null);
160
+ } catch {
161
+ return new Map();
162
+ }
163
+ }
164
+
156
165
  export function useTimelineSyncCallbacks({
157
166
  iframeRef,
158
167
  probeIntervalRef,
@@ -173,14 +182,13 @@ export function useTimelineSyncCallbacks({
173
182
  }: UseTimelineSyncCallbacksParams) {
174
183
  // Convert a runtime timeline message (from iframe postMessage) into TimelineElements
175
184
  const applyTimelineMessage = useCallback(
176
- (data: RuntimeTimelineMessage, iframeDoc: Document | null) => {
185
+ (data: RuntimeTimelineMessage, iframeDoc: Document | null, parentMap: Map<string, string>) => {
177
186
  // Show root-level clips: no parentCompositionId, OR parent is a "phantom wrapper"
178
187
  const clipCompositionIds = new Set(data.clips.map((c) => c.compositionId).filter(Boolean));
179
188
  const filtered = data.clips.filter(
180
189
  (clip) => !clip.parentCompositionId || !clipCompositionIds.has(clip.parentCompositionId),
181
190
  );
182
191
  try {
183
- const parentMap = clipTreeParentMap(iframeRef.current?.contentWindow ?? null);
184
192
  const domClipChildren = collectSubCompositionDomChildren(iframeDoc, data.clips, parentMap);
185
193
  usePlayerStore.getState().setClipParentMap(parentMap);
186
194
  usePlayerStore.getState().setDomClipChildren(domClipChildren);
@@ -188,7 +196,7 @@ export function useTimelineSyncCallbacks({
188
196
  .getState()
189
197
  .setSubCompositionHostState(collectSubCompositionHostState(iframeDoc, data.clips));
190
198
  } catch {
191
- // cross-origin or __clipTree not available — maps stay empty
199
+ // A document the walk cannot read leaves the maps empty.
192
200
  }
193
201
 
194
202
  const els = buildTimelineElementsFromClips(filtered, iframeDoc);
@@ -211,7 +219,7 @@ export function useTimelineSyncCallbacks({
211
219
  syncTimelineElements,
212
220
  );
213
221
  },
214
- [iframeRef, syncTimelineElements],
222
+ [syncTimelineElements],
215
223
  );
216
224
 
217
225
  const timelineReads = useRef(0);
@@ -222,14 +230,16 @@ export function useTimelineSyncCallbacks({
222
230
  const iframe = iframeRef.current;
223
231
  const read = ++timelineReads.current;
224
232
  if (!iframe || !previewFrameOpaque(iframe)) {
225
- applyTimelineMessage(data, safeContentDocument(iframe));
233
+ applyTimelineMessage(data, safeContentDocument(iframe), openClipParents(iframe));
226
234
  return;
227
235
  }
228
- // A sealed frame's rows bind to a copy of its document from the editing port; the newest read wins.
229
- void previewDocumentCopy(iframe).then((doc) => {
230
- if (read === timelineReads.current && iframeRef.current === iframe)
231
- applyTimelineMessage(data, doc);
232
- });
236
+ // A sealed frame's rows bind to its document copy and clip tree from the editing port; the newest read wins.
237
+ void Promise.all([previewDocumentCopy(iframe), previewClipParents(iframe)]).then(
238
+ ([doc, parents]) => {
239
+ if (read === timelineReads.current && iframeRef.current === iframe)
240
+ applyTimelineMessage(data, doc, parents);
241
+ },
242
+ );
233
243
  },
234
244
  [iframeRef, applyTimelineMessage],
235
245
  );
@@ -120,6 +120,16 @@ describe("previewEditingClient", () => {
120
120
  expect(Date.now() - started).toBeLessThan(AGENT_PROBE_MS + 500);
121
121
  });
122
122
 
123
+ it("answers a frame whose runtime never came at once, rather than after another probe", async () => {
124
+ const { iframe } = opaqueFrame();
125
+ const started = Date.now();
126
+ await previewSeekPainted(iframe, 15_000);
127
+ expect(Date.now() - started).toBeGreaterThanOrEqual(AGENT_PROBE_MS - 50);
128
+ const again = Date.now();
129
+ await previewSeekPainted(iframe, 15_000);
130
+ expect(Date.now() - again).toBeLessThan(100);
131
+ });
132
+
123
133
  it("returns the runtime's manifest only when its reply has the manifest's shape", async () => {
124
134
  const manifest = { clips: [], durationInFrames: 30, scenes: [] };
125
135
  let reply: unknown = { clipManifest: manifest };
@@ -54,7 +54,15 @@ export function setFrameLoopStart(iframe: HTMLIFrameElement, seconds: number | n
54
54
 
55
55
  export const AGENT_PROBE_MS = 1_000;
56
56
 
57
+ const agents = new WeakMap<EditingPortClient, boolean>();
58
+
57
59
  async function agentConnected(client: EditingPortClient): Promise<boolean> {
60
+ const known = agents.get(client);
61
+ if (known !== undefined) return known;
62
+ client.ready.then(
63
+ () => agents.set(client, true),
64
+ () => agents.set(client, false),
65
+ );
58
66
  let timer: ReturnType<typeof setTimeout> | undefined;
59
67
  const probe = new Promise<boolean>(
60
68
  (resolve) => (timer = setTimeout(resolve, AGENT_PROBE_MS, false)),
@@ -67,6 +75,7 @@ async function agentConnected(client: EditingPortClient): Promise<boolean> {
67
75
  probe,
68
76
  ]);
69
77
  clearTimeout(timer);
78
+ if (!agents.has(client)) agents.set(client, connected);
70
79
  return connected;
71
80
  }
72
81
 
@@ -97,6 +106,18 @@ export async function previewClipManifest(iframe: HTMLIFrameElement): Promise<Cl
97
106
  return manifest as ClipManifest;
98
107
  }
99
108
 
109
+ export async function previewClipParents(iframe: HTMLIFrameElement): Promise<Map<string, string>> {
110
+ const snapshot = await previewRequest(iframe, "runtime-snapshot", {});
111
+ const pairs: unknown = snapshot?.clipParents;
112
+ const parents = new Map<string, string>();
113
+ if (!Array.isArray(pairs)) return parents;
114
+ for (const pair of pairs) {
115
+ if (Array.isArray(pair) && typeof pair[0] === "string" && typeof pair[1] === "string")
116
+ parents.set(pair[0], pair[1]);
117
+ }
118
+ return parents;
119
+ }
120
+
100
121
  export async function previewConsoleErrors(
101
122
  iframe: HTMLIFrameElement,
102
123
  from: number,
@@ -1,7 +1,12 @@
1
1
  // @vitest-environment happy-dom
2
2
 
3
3
  import { afterEach, describe, expect, it, vi } from "vitest";
4
- import type { EditingElementDescription } from "@hyperframes/core/runtime/editing-port";
4
+ import {
5
+ MAX_ELEMENT_ENTRIES,
6
+ MAX_ELEMENT_TEXT,
7
+ MAX_ELEMENT_VALUE,
8
+ type EditingElementDescription,
9
+ } from "@hyperframes/core/runtime/editing-port";
5
10
  import {
6
11
  describePreviewElements,
7
12
  previewElementSelectorIndex,
@@ -93,6 +98,34 @@ describe("readPreviewElement", () => {
93
98
  expect(readPreviewElement(bad)).toBeNull();
94
99
  }
95
100
  });
101
+
102
+ it("refuses a description longer anywhere than the runtime ever sends one", () => {
103
+ const value = "x".repeat(MAX_ELEMENT_VALUE);
104
+ const atLimit = described(1, { selector: value, id: value, attributes: [["src", value]] });
105
+ expect(readPreviewElement(atLimit)).toEqual(atLimit);
106
+ const over = `${value}x`;
107
+ const text = "y".repeat(MAX_ELEMENT_TEXT + 1);
108
+ const many = Array.from({ length: MAX_ELEMENT_ENTRIES + 1 }, (_, i) => [`data-${i}`, ""]);
109
+ for (const bad of [
110
+ described(1, { selector: over }),
111
+ described(1, { id: over }),
112
+ described(1, { tag: over }),
113
+ described(1, { label: over }),
114
+ described(1, { attributes: [["src", over]] }),
115
+ described(1, { attributes: [[`data-${over}`, "1"]] }),
116
+ described(1, { attributes: many as never }),
117
+ described(1, { inlineStyles: { left: over } }),
118
+ described(1, { inlineStyles: { [`--${over}`]: "1" } }),
119
+ described(1, { source: { ...NO_SOURCE, file: over } }),
120
+ described(1, { source: { ...NO_SOURCE, compositionId: over } }),
121
+ described(1, { source: { ...NO_SOURCE, originalCompositionId: over } }),
122
+ described(1, { matchesBefore: [{ source: { ...NO_SOURCE, file: over }, count: 1 }] }),
123
+ described(1, { text }),
124
+ described(1, { children: [{ text }] }),
125
+ ]) {
126
+ expect(readPreviewElement(bad)).toBeNull();
127
+ }
128
+ });
96
129
  });
97
130
 
98
131
  describe("element requests over an opaque frame's port", () => {
@@ -5,10 +5,13 @@
5
5
  import {
6
6
  EDITING_ELEMENT_HANDLE,
7
7
  MAX_DESCRIBE_HANDLES,
8
+ MAX_DRAFT_PX,
8
9
  MAX_ELEMENT_ENTRIES,
10
+ MAX_ELEMENT_TEXT,
9
11
  MAX_ELEMENT_VALUE,
10
12
  MAX_GEOMETRY_CHILDREN,
11
13
  MAX_SNAP_TARGETS,
14
+ MAX_SOURCE_GROUPS,
12
15
  type EditingElementBox,
13
16
  type EditingElementDescription,
14
17
  type EditingElementGeometry,
@@ -32,29 +35,38 @@ export const isFields = (value: unknown): value is Fields =>
32
35
  export const isHandle = (value: unknown) =>
33
36
  typeof value === "string" && EDITING_ELEMENT_HANDLE.test(value);
34
37
  const isText = (value: unknown) => typeof value === "string";
35
- const isTextOrNull = (value: unknown) => value === null || typeof value === "string";
38
+ const isValue = (value: unknown) => isText(value) && (value as string).length <= MAX_ELEMENT_VALUE;
39
+ const isLongText = (value: unknown) =>
40
+ isText(value) && (value as string).length <= MAX_ELEMENT_TEXT;
41
+ const isValueOrNull = (value: unknown) => value === null || isValue(value);
42
+ const isEntries = (value: unknown): value is unknown[] =>
43
+ Array.isArray(value) && value.length <= MAX_ELEMENT_ENTRIES;
36
44
  const isCount = (value: unknown) => Number.isSafeInteger(value) && (value as number) >= 0;
37
45
 
38
46
  const isSource = (value: unknown): value is EditingElementSource =>
39
47
  isFields(value) &&
40
- isTextOrNull(value.file) &&
41
- isTextOrNull(value.compositionId) &&
42
- isTextOrNull(value.originalCompositionId);
48
+ isValueOrNull(value.file) &&
49
+ isValueOrNull(value.compositionId) &&
50
+ isValueOrNull(value.originalCompositionId);
43
51
 
44
- const isTextRecord = (value: unknown) => isFields(value) && Object.values(value).every(isText);
52
+ const isStyles = (value: unknown) =>
53
+ isFields(value) &&
54
+ isEntries(Object.keys(value)) &&
55
+ Object.entries(value).every(([name, style]) => isValue(name) && isValue(style));
45
56
 
46
57
  function hasPlainFields(value: Fields): boolean {
47
58
  const { matchesBefore, attributes, children } = value;
48
59
  const groupsOk =
49
60
  matchesBefore === null ||
50
61
  (Array.isArray(matchesBefore) &&
62
+ matchesBefore.length <= MAX_SOURCE_GROUPS &&
51
63
  matchesBefore.every((g) => isFields(g) && isSource(g.source) && isCount(g.count)));
52
64
  const attributesOk =
53
- Array.isArray(attributes) &&
54
- attributes.every((a) => Array.isArray(a) && a.length === 2 && a.every(isText));
65
+ isEntries(attributes) &&
66
+ attributes.every((a) => Array.isArray(a) && a.length === 2 && a.every(isValue));
55
67
  const childrenOk =
56
- Array.isArray(children) &&
57
- children.every((c) => isFields(c) && (isHandle(c.handle) || isText(c.text)));
68
+ isEntries(children) &&
69
+ children.every((c) => isFields(c) && (isHandle(c.handle) || isLongText(c.text)));
58
70
  return groupsOk && attributesOk && childrenOk && typeof value.truncated === "boolean";
59
71
  }
60
72
 
@@ -62,7 +74,7 @@ const isRect = (value: unknown) =>
62
74
  isFields(value) && [value.x, value.y, value.width, value.height].every(Number.isFinite);
63
75
 
64
76
  function readHit(value: unknown): PreviewElementHit | null {
65
- if (!isFields(value) || !isHandle(value.handle) || !isText(value.tag)) return null;
77
+ if (!isFields(value) || !isHandle(value.handle) || !isValue(value.tag)) return null;
66
78
  return isRect(value.rect) ? (value as unknown as PreviewElementHit) : null;
67
79
  }
68
80
 
@@ -70,10 +82,10 @@ function readHit(value: unknown): PreviewElementHit | null {
70
82
  export function readPreviewElement(value: unknown): PreviewElement | null {
71
83
  if (!readHit(value) || !isFields(value)) return null;
72
84
  if (value.parent !== null && !isHandle(value.parent)) return null;
73
- if (value.label !== undefined && !isText(value.label)) return null;
74
- if (!isTextOrNull(value.id) || !isTextOrNull(value.selector)) return null;
85
+ if (value.label !== undefined && !isValue(value.label)) return null;
86
+ if (!isValueOrNull(value.id) || !isValueOrNull(value.selector)) return null;
75
87
  if (!isSource(value.source) || typeof value.locked !== "boolean") return null;
76
- if (!isTextRecord(value.inlineStyles) || !isText(value.text)) return null;
88
+ if (!isStyles(value.inlineStyles) || !isLongText(value.text)) return null;
77
89
  return hasPlainFields(value) ? (value as unknown as PreviewElement) : null;
78
90
  }
79
91
 
@@ -182,8 +194,7 @@ const isSourceBox = (value: unknown) =>
182
194
  isSizeOrNull(value.declaredWidth) &&
183
195
  isSizeOrNull(value.declaredHeight));
184
196
 
185
- const isClipPath = (value: unknown) =>
186
- value === null || (isText(value) && (value as string).length <= MAX_ELEMENT_VALUE);
197
+ const isClipPath = isValueOrNull;
187
198
 
188
199
  const isBox = (value: unknown): value is EditingElementBox =>
189
200
  isFields(value) &&
@@ -233,11 +244,17 @@ const isFlag = (value: unknown) => typeof value === "boolean";
233
244
  const isFiniteFields = (value: unknown, keys: string[]) =>
234
245
  isFields(value) && keys.every((key) => Number.isFinite(value[key]));
235
246
 
247
+ const isLayout = (value: unknown) =>
248
+ isFields(value) &&
249
+ [value.width, value.height].every(
250
+ (side) => Number.isFinite(side) && (side as number) >= 0 && (side as number) <= MAX_DRAFT_PX,
251
+ );
252
+
236
253
  const readGestureStart = (value: unknown): EditingGestureStart | null => {
237
254
  if (!readGeometry(value) || !isFields(value)) return null;
238
255
  const { layout, move, gsap } = value;
239
256
  const valid = [
240
- isFiniteFields(layout, ["width", "height"]),
257
+ isLayout(layout),
241
258
  move === null || isFiniteFields(move, ["a", "b", "c", "d"]),
242
259
  gsap === null || (isFields(gsap) && isFiniteFields(gsap, ["x", "y"]) && isFlag(gsap.moving)),
243
260
  ];
@@ -293,3 +310,69 @@ export async function previewLayers(
293
310
  if (typeof missingGroup !== "boolean") return null;
294
311
  return layers.every(isLayer) ? { layers, truncated, missingGroup } : null;
295
312
  }
313
+
314
+ type EditEntry = [name: string, value: string | null];
315
+ type StyleEntry = [name: string, value: string | null, priority?: "important"];
316
+ const isEditEntries = (value: unknown, priority = false): value is StyleEntry[] =>
317
+ Array.isArray(value) &&
318
+ value.every(
319
+ (entry) =>
320
+ Array.isArray(entry) &&
321
+ isText(entry[0]) &&
322
+ (entry[1] === null || isText(entry[1])) &&
323
+ (entry.length === 2 || (priority && entry.length === 3 && entry[2] === "important")),
324
+ );
325
+
326
+ export interface PreviewElementEdit {
327
+ drawn: Promise<boolean>;
328
+ restore(names?: readonly string[]): void;
329
+ }
330
+
331
+ /** Draws inline `styles` and `attributes` on the frame's element, null removing one. */
332
+ const latestWriters = new WeakMap<HTMLIFrameElement, Map<string, symbol>>();
333
+
334
+ export function editPreviewElement(
335
+ iframe: HTMLIFrameElement,
336
+ handle: string,
337
+ edit: { styles?: EditEntry[]; attributes?: EditEntry[] },
338
+ ): PreviewElementEdit {
339
+ const send = (styles: StyleEntry[], attributes: EditEntry[]) =>
340
+ previewRequest(iframe, "edit-element", { handle, styles, attributes });
341
+ const writers = latestWriters.get(iframe) ?? new Map<string, symbol>();
342
+ latestWriters.set(iframe, writers);
343
+ const self = Symbol("edit");
344
+ const key = (part: string, name: string) => `${handle} ${part} ${name}`;
345
+ for (const [name] of edit.styles ?? []) writers.set(key("style", name), self);
346
+ for (const [name] of edit.attributes ?? []) writers.set(key("attr", name), self);
347
+ const reply = send(edit.styles ?? [], edit.attributes ?? []);
348
+ return {
349
+ drawn: reply.then((before) => before?.applied === true),
350
+ restore(names) {
351
+ // Checked when the old values go back: a later edit of a name owns it by then.
352
+ const owns = (part: string, name: string) =>
353
+ (!names || names.includes(name)) && writers.get(key(part, name)) === self;
354
+ void reply.then((before) => {
355
+ if (!before?.applied || !isEditEntries(before.styles, true)) return;
356
+ if (!isEditEntries(before.attributes)) return;
357
+ const styles = before.styles.filter(([name]) => owns("style", name));
358
+ const attributes = before.attributes.filter(([name]) => owns("attr", name));
359
+ if (styles.length || attributes.length) void send(styles, attributes);
360
+ });
361
+ },
362
+ };
363
+ }
364
+
365
+ const SEALED_DRAW_WAIT_MS = 300;
366
+
367
+ /** Saves a live edit at once; a sealed one once the frame says it drew it, else after the wait. */
368
+ export function afterSealedDraw<T>(edit: PreviewElementEdit | null, save: (drawn: boolean) => T) {
369
+ if (!edit) return save(true);
370
+ let timer: ReturnType<typeof setTimeout> | undefined;
371
+ const late = new Promise<boolean>((resolve) => {
372
+ timer = setTimeout(resolve, SEALED_DRAW_WAIT_MS, false);
373
+ });
374
+ return Promise.race([edit.drawn, late]).then((drawn) => {
375
+ clearTimeout(timer);
376
+ return save(drawn);
377
+ });
378
+ }
@@ -0,0 +1,38 @@
1
+ // A preview frame Studio cannot script, served by the frame's real runtime agent over this document.
2
+ import { onTestFinished } from "vitest";
3
+ import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
4
+ import { detachedFakeIframe } from "../hooks/timelinePlayerTestHarness";
5
+
6
+ /** A browser's postMessage: delivered on a later task, to every listener present then. */
7
+ function postLater(init: MessageEventInit): void {
8
+ setTimeout(() => window.dispatchEvent(new MessageEvent("message", init)));
9
+ }
10
+
11
+ export interface SealedPreviewFrame {
12
+ iframe: HTMLIFrameElement;
13
+ /** The frame's window as Studio sees it: only `postMessage` answers, and it reaches the runtime. */
14
+ frame: Record<string, unknown>;
15
+ }
16
+
17
+ export function sealedPreviewFrame(
18
+ options: { wrapHandedOverPort?: (port: MessagePort) => MessagePort } = {},
19
+ ): SealedPreviewFrame {
20
+ const target: Record<string, unknown> = {};
21
+ const frame = new Proxy(target, {
22
+ get: (_, key) => {
23
+ if (key === "postMessage") return target.postMessage;
24
+ throw new DOMException("blocked", "SecurityError");
25
+ },
26
+ });
27
+ const iframe = detachedFakeIframe(frame);
28
+ Object.defineProperty(iframe, "contentDocument", { value: null });
29
+ const wrap = options.wrapHandedOverPort ?? ((port: MessagePort) => port);
30
+ const parent = {
31
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
32
+ postLater({ source: frame as unknown as Window, data, ports: ports.map(wrap) }),
33
+ } as unknown as Window;
34
+ const agent = installMarkedEditingAgent(parent);
35
+ frame.postMessage = (data: unknown) => postLater({ source: parent, data });
36
+ onTestFinished(() => agent.dispose());
37
+ return { iframe, frame };
38
+ }
@@ -101,7 +101,7 @@ function isPreviewPlayerHost(value: unknown): value is PreviewPlayerHost {
101
101
  return value instanceof HTMLElement;
102
102
  }
103
103
 
104
- function resolvePreviewPlayerHost(iframe: HTMLIFrameElement): PreviewPlayerHost | null {
104
+ export function resolvePreviewPlayerHost(iframe: HTMLIFrameElement): PreviewPlayerHost | null {
105
105
  const root = iframe.getRootNode();
106
106
  if (
107
107
  typeof ShadowRoot !== "undefined" &&