Skip to content

Commit 84e6531

Browse files
feat(studio): a host can make the preview read-only (#4200)
* feat(studio): a host can make the preview read-only Gates the shared geometry commit funnel (offset/size/rotation), so the inspector's manual X/Y/W/H/rotation fields refuse the same as canvas drag/resize/rotate. The flat inspector's fields render disabled with a host-supplied reason. Fixes a hooks-order crash on deselect found in review. * refactor(studio): derive the read-only preview flag from context, not a synced store EditorShell synced its readOnlyPreview prop into a zustand store via a raw useEffect behind an eslint-disable of no-restricted-syntax. Replace it with a PreviewReadOnlyProvider context computed fresh from the prop every render: Tier-1 consumers (genuine EditorShell descendants) read it via useContext; Tier-2 consumers (useAppHotkeys, useGestureCommit, the useDomEditSession/useDomEditCommits/useDomGeometryCommits chain), which run in App.tsx above EditorShell, take it as an explicit parameter threaded from StudioApp's own prop instead. Adds a host-mount test proving EditorShell gates its descendants from its own prop with no StudioApp/App.tsx involved. * fix(studio): keep preview hotkeys context explicit * fix(studio): close read-only interaction escapes * fix(studio): preserve timeline ownership in read-only preview * fix(studio): rollback read-only preview work * fix(studio): keep hotkey ownership on the active surface * fix(studio): format preview overlay and strengthen split test * fix(studio): clear preview gate findings * refactor(studio): share preview test fixtures
1 parent 9bd6da8 commit 84e6531

41 files changed

Lines changed: 1347 additions & 305 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/studio/src/App.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ import { useTimelineAddAtPlayhead } from "./hooks/useTimelineAddAtPlayhead";
6363
import { readStudioUrlStateFromWindow, resolveMasterCompositionPath } from "./utils/studioUrlState";
6464
import { useActiveComposition } from "./hooks/useActiveComposition";
6565
const getTimelineSelectionSet = () => usePlayerStore.getState().selectedElementIds;
66+
67+
export interface StudioAppProps {
68+
/** Clicks still select and report; the preview cannot move, edit or delete anything. */
69+
readOnlyPreview?: boolean;
70+
/** Short text shown on disabled hand-edit controls while `readOnlyPreview` is set. */
71+
readOnlyPreviewReason?: string;
72+
}
73+
6674
// fallow-ignore-next-line complexity
67-
export function StudioApp() {
75+
export function StudioApp({ readOnlyPreview = false, readOnlyPreviewReason }: StudioAppProps = {}) {
6876
const { projectId, resolving, waitingForServer } = useServerConnection();
6977
const initialUrlStateRef = useRef(readStudioUrlStateFromWindow());
7078
useStudioSessionStart(projectId, resolving, waitingForServer);
@@ -252,6 +260,7 @@ export function StudioApp() {
252260
activeCompPath,
253261
forceReloadSdkSession: sdkHandle.forceReload,
254262
onToggleRecording: () => handleToggleRecordingRef.current(),
263+
readOnlyPreview,
255264
});
256265
const domEditSession = useDomEditSession({
257266
projectId,
@@ -291,6 +300,7 @@ export function StudioApp() {
291300
publishSdkSession: sdkHandle.publish,
292301
forceReloadSdkSession: sdkHandle.forceReload,
293302
handleTimelineElementsDelete: timelineEditing.handleTimelineElementsDelete,
303+
readOnlyPreview,
294304
});
295305
domEditSelectionBridgeRef.current = domEditSession.domEditSelection;
296306
handleDomZIndexReorderCommitRef.current = domEditSession.handleDomZIndexReorderCommit;
@@ -346,6 +356,7 @@ export function StudioApp() {
346356
previewIframeRef,
347357
showToast,
348358
isGestureRecordingRef,
359+
readOnlyPreview,
349360
});
350361
handleToggleRecordingRef.current = handleToggleRecording;
351362
const canvasRectRef = useRef<DOMRect | null>(null);
@@ -465,6 +476,8 @@ export function StudioApp() {
465476
)}
466477
<ExternalFileConflictBanner coordinator={externalFileChanges} />
467478
<EditorShell
479+
readOnlyPreview={readOnlyPreview}
480+
readOnlyPreviewReason={readOnlyPreviewReason}
468481
panels={
469482
<>
470483
<StudioLeftPanels
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// @vitest-environment happy-dom
2+
3+
// A host that mounts EditorShell directly — no StudioApp, no App.tsx — must
4+
// still get its descendants gated purely from the readOnlyPreview prop.
5+
6+
import React, { act } from "react";
7+
import { createRoot } from "react-dom/client";
8+
import { afterEach, describe, expect, it, vi } from "vitest";
9+
import "./EditorShellTestMocks";
10+
import { EditorShell } from "./EditorShell";
11+
import { usePreviewBlockDrop } from "./nle/usePreviewBlockDrop";
12+
import { TIMELINE_BLOCK_MIME } from "../utils/timelineAssetDrop";
13+
14+
vi.mock("../hooks/useTimelineSelectionPreviewSync", () => ({
15+
useTimelineSelectionPreviewSync: vi.fn(),
16+
}));
17+
vi.mock("./nle/PreviewOverlays", () => ({ PreviewOverlays: () => null }));
18+
19+
// The real hook — this is the actual Tier-1 consumer a host relies on to gate
20+
// a drop onto the preview. Only PreviewPane's own DOM/player chrome is mocked.
21+
const stage = document.createElement("div");
22+
stage.getBoundingClientRect = () => ({ left: 0, top: 0, width: 100, height: 100 }) as DOMRect;
23+
const dropHandlers: { current: ReturnType<typeof usePreviewBlockDrop> | null } = { current: null };
24+
vi.mock("./nle/PreviewPane", () => ({
25+
PreviewPane: (props: { onPreviewBlockDrop?: (name: string, pos: unknown) => void }) => {
26+
dropHandlers.current = usePreviewBlockDrop({
27+
stageRef: { current: stage },
28+
compositionSize: { width: 1000, height: 1000 },
29+
onBlockDrop: props.onPreviewBlockDrop,
30+
});
31+
return null;
32+
},
33+
}));
34+
35+
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
36+
37+
let root: ReturnType<typeof createRoot> | null = null;
38+
39+
afterEach(() => {
40+
if (root) act(() => root!.unmount());
41+
root = null;
42+
document.body.innerHTML = "";
43+
});
44+
45+
function mountShellAlone(
46+
readOnlyPreview: boolean,
47+
onPreviewBlockDrop: (...args: unknown[]) => void,
48+
) {
49+
const host = document.createElement("div");
50+
document.body.append(host);
51+
root = createRoot(host);
52+
act(() =>
53+
root!.render(
54+
<EditorShell
55+
panels={null}
56+
timelineToolbar={null}
57+
renderClipContent={() => null}
58+
handleTimelineElementDelete={vi.fn()}
59+
handleTimelineAssetDrop={vi.fn()}
60+
handleTimelineFileDrop={vi.fn()}
61+
handleTimelineElementMove={vi.fn()}
62+
handleTimelineElementsMove={vi.fn()}
63+
handleTimelineElementResize={vi.fn()}
64+
handleTimelineGroupResize={vi.fn()}
65+
handleToggleTrackHidden={vi.fn()}
66+
setAudioGroupAttribute={{ setLive: vi.fn(), setQuiet: vi.fn() }}
67+
handleBlockedTimelineEdit={vi.fn()}
68+
handleTimelineElementSplit={vi.fn()}
69+
handleRazorSplit={vi.fn()}
70+
handleRazorSplitAll={vi.fn()}
71+
onCopyClip={vi.fn(() => false)}
72+
onPasteClip={vi.fn(async () => {})}
73+
onDuplicateClip={vi.fn(async () => false)}
74+
canPasteClip={vi.fn(() => false)}
75+
setCompIdToSrc={vi.fn()}
76+
setCompositionLoading={vi.fn()}
77+
shouldShowMotionPath={false}
78+
shouldShowSelectedDomBounds={false}
79+
handlePreviewBlockDrop={onPreviewBlockDrop}
80+
readOnlyPreview={readOnlyPreview}
81+
/>,
82+
),
83+
);
84+
return { host, root };
85+
}
86+
87+
function dropEvent() {
88+
return {
89+
clientX: 50,
90+
clientY: 50,
91+
preventDefault: vi.fn(),
92+
dataTransfer: {
93+
types: [TIMELINE_BLOCK_MIME],
94+
getData: () => JSON.stringify({ name: "lower-third" }),
95+
},
96+
};
97+
}
98+
99+
describe("a host mounting EditorShell alone (no StudioApp, no App.tsx)", () => {
100+
it("gates a Tier-1 descendant's drop handling purely from the prop", () => {
101+
const onBlockDrop = vi.fn();
102+
mountShellAlone(true, onBlockDrop);
103+
const event = dropEvent();
104+
act(() => dropHandlers.current!.handleDrop(event as never));
105+
expect(onBlockDrop).not.toHaveBeenCalled();
106+
expect(event.preventDefault).not.toHaveBeenCalled();
107+
});
108+
109+
it("control: with the flag off, the same descendant still places a drop", () => {
110+
const onBlockDrop = vi.fn();
111+
mountShellAlone(false, onBlockDrop);
112+
const event = dropEvent();
113+
act(() => dropHandlers.current!.handleDrop(event as never));
114+
expect(onBlockDrop).toHaveBeenCalledWith("lower-third", { left: 500, top: 500 });
115+
});
116+
});

‎packages/studio/src/components/EditorShell.selectionSync.test.tsx‎

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,27 @@
33
import React, { act } from "react";
44
import { createRoot } from "react-dom/client";
55
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import "./EditorShellTestMocks";
67
import { EditorShell } from "./EditorShell";
8+
import { usePreviewReadOnly } from "./editor/previewReadOnlyContext";
79

810
const hookMocks = vi.hoisted(() => ({
911
useTimelineSelectionPreviewSync: vi.fn(),
1012
}));
13+
const previewOverlaysProbe = vi.hoisted(() => ({ readOnly: null as boolean | null }));
1114

1215
vi.mock("../hooks/useTimelineSelectionPreviewSync", () => hookMocks);
13-
vi.mock("../contexts/StudioContext", () => ({
14-
useStudioPlaybackContext: () => ({
15-
captionEditMode: false,
16-
refreshKey: 0,
17-
refreshPreviewDocumentVersion: vi.fn(),
18-
timelineElements: [],
19-
}),
20-
useStudioShellContext: () => ({
21-
projectId: "project-1",
22-
activeCompPath: "index.html",
23-
setActiveCompPath: vi.fn(),
24-
handlePreviewIframeRef: vi.fn(),
25-
showToast: vi.fn(),
26-
}),
16+
vi.mock("./nle/PreviewPane", () => ({
17+
// Renders its previewOverlay prop for real, so the mocked PreviewOverlays
18+
// below (which reads the read-only context) actually mounts.
19+
PreviewPane: (props: { previewOverlay?: React.ReactNode }) => props.previewOverlay ?? null,
2720
}));
28-
vi.mock("../contexts/DomEditContext", () => ({
29-
useDomEditActionsContext: () => ({
30-
handleTimelineElementSelect: vi.fn(),
31-
buildDomSelectionForTimelineElement: vi.fn(),
32-
applyDomSelection: vi.fn(),
33-
applyMarqueeSelection: vi.fn(),
34-
}),
35-
useDomEditSelectionContext: () => ({
36-
domEditSelection: null,
37-
domEditGroupSelections: [],
38-
}),
39-
}));
40-
vi.mock("./nle/NLEContext", () => ({
41-
NLEProvider: ({ children }: { children: React.ReactNode }) => children,
42-
useNLEContext: () => ({
43-
compositionStack: [],
44-
updateCompositionStack: vi.fn(),
45-
}),
46-
}));
47-
vi.mock("./nle/useTimelineEditCallbacks", () => ({
48-
useTimelineEditCallbacks: () => ({}),
21+
vi.mock("./nle/PreviewOverlays", () => ({
22+
PreviewOverlays: () => {
23+
previewOverlaysProbe.readOnly = usePreviewReadOnly();
24+
return null;
25+
},
4926
}));
50-
vi.mock("./nle/PreviewPane", () => ({ PreviewPane: () => null }));
51-
vi.mock("./nle/PreviewOverlays", () => ({ PreviewOverlays: () => null }));
52-
vi.mock("./nle/TimelinePane", () => ({ TimelinePane: () => null }));
53-
vi.mock("../captions/components/CaptionTimeline", () => ({ CaptionTimeline: () => null }));
5427

5528
Object.assign(globalThis, {
5629
IS_REACT_ACT_ENVIRONMENT: true,
@@ -66,41 +39,44 @@ afterEach(() => {
6639
hookMocks.useTimelineSelectionPreviewSync.mockClear();
6740
});
6841

42+
const shell = (readOnlyPreview?: boolean) => (
43+
<EditorShell
44+
panels={null}
45+
timelineToolbar={null}
46+
renderClipContent={() => null}
47+
handleTimelineElementDelete={vi.fn()}
48+
handleTimelineAssetDrop={vi.fn()}
49+
handleTimelineFileDrop={vi.fn()}
50+
handleTimelineElementMove={vi.fn()}
51+
handleTimelineElementsMove={vi.fn()}
52+
handleTimelineElementResize={vi.fn()}
53+
handleTimelineGroupResize={vi.fn()}
54+
handleToggleTrackHidden={vi.fn()}
55+
setAudioGroupAttribute={{ setLive: vi.fn(), setQuiet: vi.fn() }}
56+
handleBlockedTimelineEdit={vi.fn()}
57+
handleTimelineElementSplit={vi.fn()}
58+
handleRazorSplit={vi.fn()}
59+
handleRazorSplitAll={vi.fn()}
60+
onCopyClip={vi.fn(() => false)}
61+
onPasteClip={vi.fn(async () => {})}
62+
onDuplicateClip={vi.fn(async () => false)}
63+
canPasteClip={vi.fn(() => false)}
64+
setCompIdToSrc={vi.fn()}
65+
setCompositionLoading={vi.fn()}
66+
shouldShowMotionPath={false}
67+
shouldShowSelectedDomBounds={false}
68+
readOnlyPreview={readOnlyPreview}
69+
/>
70+
);
71+
6972
describe("EditorShell timeline selection sync", () => {
7073
it("keeps the timeline store mirrored into the preview selection", () => {
7174
const host = document.createElement("div");
7275
document.body.append(host);
7376
const root = createRoot(host);
7477

7578
act(() => {
76-
root.render(
77-
<EditorShell
78-
panels={null}
79-
timelineToolbar={null}
80-
renderClipContent={() => null}
81-
handleTimelineElementDelete={vi.fn()}
82-
handleTimelineAssetDrop={vi.fn()}
83-
handleTimelineFileDrop={vi.fn()}
84-
handleTimelineElementMove={vi.fn()}
85-
handleTimelineElementsMove={vi.fn()}
86-
handleTimelineElementResize={vi.fn()}
87-
handleTimelineGroupResize={vi.fn()}
88-
handleToggleTrackHidden={vi.fn()}
89-
setAudioGroupAttribute={{ setLive: vi.fn(), setQuiet: vi.fn() }}
90-
handleBlockedTimelineEdit={vi.fn()}
91-
handleTimelineElementSplit={vi.fn()}
92-
handleRazorSplit={vi.fn()}
93-
handleRazorSplitAll={vi.fn()}
94-
onCopyClip={vi.fn(() => false)}
95-
onPasteClip={vi.fn(async () => {})}
96-
onDuplicateClip={vi.fn(async () => false)}
97-
canPasteClip={vi.fn(() => false)}
98-
setCompIdToSrc={vi.fn()}
99-
setCompositionLoading={vi.fn()}
100-
shouldShowMotionPath={false}
101-
shouldShowSelectedDomBounds={false}
102-
/>,
103-
);
79+
root.render(shell());
10480
});
10581

10682
expect(hookMocks.useTimelineSelectionPreviewSync).toHaveBeenCalledOnce();
@@ -115,4 +91,18 @@ describe("EditorShell timeline selection sync", () => {
11591

11692
act(() => root.unmount());
11793
});
94+
95+
it("threads the read-only prop into descendants via context, and follows it across renders", () => {
96+
const host = document.createElement("div");
97+
document.body.append(host);
98+
const root = createRoot(host);
99+
100+
act(() => root.render(shell(true)));
101+
expect(previewOverlaysProbe.readOnly).toBe(true);
102+
act(() => root.render(shell(false)));
103+
expect(previewOverlaysProbe.readOnly).toBe(false);
104+
act(() => root.render(shell(true)));
105+
expect(previewOverlaysProbe.readOnly).toBe(true);
106+
act(() => root.unmount());
107+
});
118108
});

0 commit comments

Comments
 (0)