Skip to content

Commit 2251e5f

Browse files
feat(studio): export DOM editing for a host outside EditorShell (#4640)
* feat(studio): export DOM editing for a host outside EditorShell * fix(studio): host hears only single-box clicks, and a handle release no longer eats the next one * refactor(studio): cut the sibling z-order comment to one line
1 parent 93ab289 commit 2251e5f

15 files changed

Lines changed: 755 additions & 241 deletions

‎packages/studio/src/components/editor/CanvasContextMenu.test.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { act } from "react";
33
import { createRoot, type Root } from "react-dom/client";
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55
import { installReactActEnvironment, makeSelection } from "../../hooks/domSelectionTestHarness";
6-
import { resolveZIndexEntries } from "../nle/PreviewOverlays";
6+
import { resolveZIndexEntries } from "./ConnectedDomEditOverlay";
77
import { useElementLifecycleOps } from "../../hooks/useElementLifecycleOps";
88
import { makeLifecycleOpsParams } from "../../hooks/elementLifecycleOpsTestUtils";
99
import type { DomEditPatchBatch } from "../../hooks/domEditCommitTypes";
@@ -138,7 +138,7 @@ describe("CanvasContextMenu — handler gating", () => {
138138
});
139139
});
140140

141-
// ── Menu z-action → commit path (wired the way PreviewOverlays wires the app) ──
141+
// ── Menu z-action → commit path (wired the way ConnectedDomEditOverlay wires the app) ──
142142

143143
function pressMenuItem(label: string) {
144144
const button = zOrderButtons().find((b) => b.textContent === label);
@@ -200,7 +200,7 @@ describe("CanvasContextMenu — z-action commit path", () => {
200200
const captured: CapturedBatchCall[] = [];
201201
const { commit, cleanup } = renderCommitHook(captured);
202202

203-
// Wire onApplyZIndex the way the app does (PreviewOverlays → the commit
203+
// Wire onApplyZIndex the way the app does (ConnectedDomEditOverlay → the commit
204204
// hook), asserting the menu has NOT touched the DOM when it fires — the
205205
// hook must capture true pre-change styles for its rollback.
206206
const stylesAtApply: Array<{ zIndex: string; position: string }> = [];

‎packages/studio/src/components/editor/CanvasContextMenu.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* flow, and captures the TRUE prior styles for its failure rollback.
1717
*
1818
* The prop MUST be wired at the call site to route through the full persist
19-
* path. PreviewOverlays.tsx builds the per-patch PatchTargets (the selected
19+
* path. ConnectedDomEditOverlay.tsx builds the per-patch PatchTargets (the selected
2020
* element carries its full selection identity; sibling elements are iframe DOM
2121
* nodes, so their id / selector are derived from the node and they share the
2222
* selection's sourceFile) and forwards them to handleDomZIndexReorderCommit.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// @vitest-environment happy-dom
2+
3+
import React, { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import { getPreviewState, iframeRef } from "../nle/PreviewOverlaysTestMocks";
7+
import { PreviewOverlays } from "../nle/PreviewOverlays";
8+
import { ConnectedDomEditOverlay } from "./ConnectedDomEditOverlay";
9+
10+
// Overlay prop -> session handler, as PreviewOverlays wired DomEditOverlay itself.
11+
const WIRING = {
12+
onCanvasMouseDown: "handlePreviewCanvasMouseDown",
13+
onCanvasPointerMove: "handlePreviewCanvasPointerMove",
14+
onCanvasPointerLeave: "handlePreviewCanvasPointerLeave",
15+
onSelectionChange: "applyDomSelection",
16+
onBlockedMove: "handleBlockedDomMove",
17+
onManualDragStart: "handleDomManualDragStart",
18+
onPathOffsetCommit: "handleDomPathOffsetCommit",
19+
onGroupPathOffsetCommit: "handleDomGroupPathOffsetCommit",
20+
onBoxSizeCommit: "handleDomBoxSizeCommit",
21+
onRotationCommit: "handleDomRotationCommit",
22+
onStyleCommit: "handleDomStyleCommit",
23+
onDeleteSelection: "handleDomEditElementDelete",
24+
onMarqueeSelect: "applyMarqueeSelection",
25+
} as const;
26+
27+
const ctx = vi.hoisted(() => ({
28+
overlayProps: null as Record<string, unknown> | null,
29+
actions: {} as Record<string, unknown>,
30+
selection: {
31+
domEditHoverSelection: { label: "hover" },
32+
domEditSelection: { label: "selected" },
33+
domEditGroupSelections: [{ label: "member" }],
34+
},
35+
}));
36+
37+
vi.mock("../../contexts/DomEditContext", () => ({
38+
useDomEditSelectionContext: () => ctx.selection,
39+
useDomEditActionsContext: () => ctx.actions,
40+
}));
41+
vi.mock("./DomEditOverlay", () => ({
42+
DomEditOverlay: (props: Record<string, unknown>) => {
43+
ctx.overlayProps = props;
44+
return null;
45+
},
46+
}));
47+
vi.mock("./TopologyLens", () => ({ TopologyLens: () => null }));
48+
vi.mock("./MotionPathOverlay", () => ({ MotionPathOverlay: () => null }));
49+
vi.mock("./SnapToolbar", () => ({ SnapToolbar: () => null }));
50+
vi.mock("./GridOverlay", () => ({ GridOverlay: () => null }));
51+
52+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
53+
54+
let root: Root;
55+
56+
function mount(node: React.ReactNode): Record<string, unknown> {
57+
ctx.actions = {
58+
previewIframeRef: iframeRef,
59+
handleDomZIndexReorderCommit: vi.fn(() => Promise.resolve(undefined)),
60+
};
61+
for (const name of Object.values(WIRING)) ctx.actions[name] = vi.fn();
62+
const host = document.createElement("div");
63+
document.body.append(host);
64+
root = createRoot(host);
65+
act(() => root.render(node));
66+
return ctx.overlayProps!;
67+
}
68+
69+
afterEach(() => {
70+
act(() => root.unmount());
71+
ctx.overlayProps = null;
72+
getPreviewState().isPlaying = false;
73+
document.body.replaceChildren();
74+
});
75+
76+
describe("ConnectedDomEditOverlay", () => {
77+
it("is what PreviewOverlays renders, with every handler from the session", () => {
78+
const props = mount(
79+
<PreviewOverlays
80+
shouldShowMotionPath={false}
81+
shouldShowSelectedDomBounds={true}
82+
recordingState={"idle" as never}
83+
onToggleRecording={vi.fn()}
84+
/>,
85+
);
86+
// The overlay never read these, so the connected wrapper does not pass them.
87+
expect(props).not.toHaveProperty("recordingState");
88+
expect(props).not.toHaveProperty("onToggleRecording");
89+
for (const [prop, handler] of Object.entries(WIRING)) {
90+
expect(props[prop], prop).toBe(ctx.actions[handler]);
91+
}
92+
expect(props).toMatchObject({
93+
iframeRef,
94+
activeCompositionPath: "index.html",
95+
hoverSelection: ctx.selection.domEditHoverSelection,
96+
selection: ctx.selection.domEditSelection,
97+
groupSelections: ctx.selection.domEditGroupSelections,
98+
allowCanvasMovement: true,
99+
canvasInput: undefined,
100+
onTextEditingChange: undefined,
101+
});
102+
});
103+
104+
it("shows no hover box while playback runs", () => {
105+
getPreviewState().isPlaying = true;
106+
const props = mount(
107+
<PreviewOverlays shouldShowMotionPath={false} shouldShowSelectedDomBounds={true} />,
108+
);
109+
expect(props.hoverSelection).toBeNull();
110+
expect(props.selection).toBe(ctx.selection.domEditSelection);
111+
});
112+
113+
it("hides the selection and stops movement while a gesture records", () => {
114+
const props = mount(
115+
<PreviewOverlays
116+
shouldShowMotionPath={false}
117+
shouldShowSelectedDomBounds={false}
118+
isGestureRecording={true}
119+
/>,
120+
);
121+
expect(props).toMatchObject({
122+
selection: null,
123+
groupSelections: [],
124+
allowCanvasMovement: false,
125+
});
126+
});
127+
128+
it("persists a z-order change through the session's reorder commit", () => {
129+
const props = mount(
130+
<PreviewOverlays shouldShowMotionPath={false} shouldShowSelectedDomBounds={true} />,
131+
);
132+
const element = document.createElement("div");
133+
element.id = "card";
134+
const sel = { element, id: "card", selector: "#card", sourceFile: "index.html" };
135+
const apply = props.onApplyZIndex as (...args: unknown[]) => void;
136+
act(() => apply(sel, [{ element, zIndex: 4 }], "bring-forward", null));
137+
expect(ctx.actions.handleDomZIndexReorderCommit).toHaveBeenCalledWith(
138+
[expect.objectContaining({ element, zIndex: 4, id: "card", sourceFile: "index.html" })],
139+
expect.stringContaining("z-reorder:bring-forward"),
140+
"bring-forward",
141+
);
142+
});
143+
144+
it("passes a host's canvas input, text-editing listener and hover gate to the overlay", () => {
145+
const onTextEditingChange = vi.fn();
146+
const props = mount(
147+
<ConnectedDomEditOverlay
148+
activeCompositionPath={null}
149+
showHoverSelection={false}
150+
shouldShowSelectedDomBounds={true}
151+
canvasInput="host"
152+
onTextEditingChange={onTextEditingChange}
153+
/>,
154+
);
155+
expect(props).toMatchObject({
156+
canvasInput: "host",
157+
onTextEditingChange,
158+
hoverSelection: null,
159+
selection: ctx.selection.domEditSelection,
160+
iframeRef,
161+
});
162+
});
163+
});

0 commit comments

Comments
 (0)