Skip to content

Commit d1a5a05

Browse files
fix(studio): a click after an empty-canvas press selects on the first try (heygen-com#4759)
* fix(studio): a click after an empty-canvas press selects on the first try * fix(studio): a shift press outside the box no longer sets a click guard that never fires
1 parent 9ae1e3c commit d1a5a05

5 files changed

Lines changed: 122 additions & 40 deletions

File tree

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
161161
const groupGestureRef = useRef<GroupGestureState | null>(null);
162162
const blockedMoveRef = useRef<BlockedMoveState | null>(null);
163163
const suppressNextBoxClickRef = useRef(false);
164-
const suppressNextBoxMouseDownRef = useRef(false);
165-
const suppressNextOverlayMouseDownRef = useRef(false);
166164
const snapGuidesRef = useRef<SnapGuidesState | null>(null);
167165
const rafPausedRef = useRef(false);
168166

@@ -329,15 +327,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
329327

330328
const handleOverlayMouseDown = (event: React.MouseEvent<HTMLDivElement>) => {
331329
if (!allowCanvasMovement) return;
332-
if (suppressNextOverlayMouseDownRef.current) {
333-
logSelect("mousedown-suppressed", { shift: event.shiftKey });
334-
suppressNextOverlayMouseDownRef.current = false;
335-
suppressNextBoxMouseDownRef.current = false;
336-
suppressNextBoxClickRef.current = false;
337-
event.preventDefault();
338-
event.stopPropagation();
339-
return;
340-
}
341330
const target = event.target as HTMLElement | null;
342331
const onBox = Boolean(target?.closest('[data-dom-edit-selection-box="true"]'));
343332
logSelect("mousedown", { shift: event.shiftKey, onBox });
@@ -346,10 +335,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
346335
// extend beyond the composition rect into the gray zone, and users need
347336
// to select/deselect them by clicking there.
348337
onCanvasMouseDown(event, { hoverSelection: hoverSelectionRef.current });
349-
if (event.shiftKey) {
350-
suppressNextBoxMouseDownRef.current = true;
351-
suppressNextBoxClickRef.current = true;
352-
}
353338
};
354339

355340
// fallow-ignore-next-line complexity
@@ -373,8 +358,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
373358
if (!candidate) return;
374359
event.preventDefault();
375360
event.stopPropagation();
376-
suppressNextOverlayMouseDownRef.current = true;
377-
suppressNextBoxMouseDownRef.current = true;
378361
suppressNextBoxClickRef.current = true;
379362
onSelectionChangeRef.current(candidate, { additive: true });
380363
return;
@@ -419,7 +402,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
419402
// so those elements were always selectable once the band could begin.
420403
event.preventDefault();
421404
event.stopPropagation();
422-
suppressNextOverlayMouseDownRef.current = true;
423405
marquee.begin(event);
424406
return;
425407
}
@@ -437,13 +419,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
437419
onCanvasMouseDown(event, { hoverSelection: hoverSelectionRef.current });
438420
};
439421

440-
const suppressBoxMouseDown = (e: React.MouseEvent) => {
441-
if (!suppressNextBoxMouseDownRef.current) return;
442-
suppressNextBoxMouseDownRef.current = false;
443-
e.preventDefault();
444-
e.stopPropagation();
445-
};
446-
447422
// Right-click state + handler: select the element under the pointer (if
448423
// needed), then open the menu; closes when the selection moves off-target.
449424
const { contextMenu, closeContextMenu, handleContextMenu } = useCanvasContextMenuState({
@@ -509,7 +484,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
509484
allowBodyDrag={bodyDrag}
510485
groupCanMove={groupCanMove}
511486
gestures={gestures}
512-
onBoxMouseDown={suppressBoxMouseDown}
513487
onBoxClick={handleBoxClick}
514488
/>
515489
)}
@@ -528,7 +502,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
528502
groupSelectionCount={groupSelections.length}
529503
gestures={gestures}
530504
onStyleCommit={onStyleCommitRef.current}
531-
onBoxMouseDown={suppressBoxMouseDown}
532505
onBoxClick={handleBoxClick}
533506
/>
534507
)}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ describe("DomEditSelectionChrome crop composition", () => {
5656
groupSelectionCount={0}
5757
gestures={{ startGesture: vi.fn() } as never}
5858
onStyleCommit={vi.fn()}
59-
onBoxMouseDown={vi.fn()}
6059
onBoxClick={vi.fn()}
6160
/>,
6261
);
@@ -112,7 +111,6 @@ describe("DomEditSelectionChrome crop composition", () => {
112111
groupSelectionCount={0}
113112
gestures={{ startGesture: vi.fn() } as never}
114113
onStyleCommit={vi.fn()}
115-
onBoxMouseDown={vi.fn()}
116114
onBoxClick={vi.fn()}
117115
/>,
118116
);
@@ -173,7 +171,6 @@ describe("DomEditSelectionChrome while editing text", () => {
173171
groupSelectionCount={0}
174172
gestures={{ startGesture: vi.fn() } as never}
175173
onStyleCommit={vi.fn()}
176-
onBoxMouseDown={vi.fn()}
177174
onBoxClick={vi.fn()}
178175
inlineText={{ editing, startFromPress: vi.fn() }}
179176
/>,
@@ -258,7 +255,6 @@ describe("DomEditSelectionChrome with body drag off", () => {
258255
selectionKey="box"
259256
groupSelectionCount={0}
260257
gestures={gestures as never}
261-
onBoxMouseDown={vi.fn()}
262258
onBoxClick={vi.fn()}
263259
/>,
264260
);
@@ -313,7 +309,6 @@ describe("DomEditSelectionChrome with body drag off", () => {
313309
allowBodyDrag={false}
314310
groupCanMove
315311
gestures={gestures as never}
316-
onBoxMouseDown={vi.fn()}
317312
onBoxClick={vi.fn()}
318313
/>,
319314
);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ interface DomEditGroupChromeProps {
6262
allowBodyDrag: boolean;
6363
groupCanMove: boolean;
6464
gestures: GestureHandlers;
65-
onBoxMouseDown: (e: React.MouseEvent) => void;
6665
onBoxClick: (event: React.MouseEvent<HTMLDivElement>) => void;
6766
}
6867

@@ -75,7 +74,6 @@ export function DomEditGroupChrome({
7574
allowBodyDrag,
7675
groupCanMove,
7776
gestures,
78-
onBoxMouseDown,
7977
onBoxClick,
8078
}: DomEditGroupChromeProps) {
8179
const canManipulate = allowCanvasMovement && !usePreviewReadOnly();
@@ -108,7 +106,6 @@ export function DomEditGroupChrome({
108106
if (!canManipulate || !allowBodyDrag || (e.shiftKey && !groupCanMove)) return;
109107
gestures.startGroupDrag(e);
110108
}}
111-
onMouseDown={onBoxMouseDown}
112109
onClick={onBoxClick}
113110
/>
114111
</>
@@ -128,7 +125,6 @@ interface DomEditSelectionChromeProps {
128125
groupSelectionCount: number;
129126
gestures: GestureHandlers;
130127
onStyleCommit?: (property: string, value: string) => Promise<unknown> | void;
131-
onBoxMouseDown: (e: React.MouseEvent) => void;
132128
onBoxClick: (event: React.MouseEvent<HTMLDivElement>) => void;
133129
/** The canvas' text-editing session: what opens one, and whether one is open. */
134130
inlineText?: {
@@ -157,7 +153,6 @@ export function DomEditSelectionChrome({
157153
groupSelectionCount,
158154
gestures,
159155
onStyleCommit,
160-
onBoxMouseDown,
161156
onBoxClick,
162157
inlineText,
163158
}: DomEditSelectionChromeProps) {
@@ -225,7 +220,6 @@ export function DomEditSelectionChrome({
225220
}
226221
if (!e.shiftKey) gestures.startBlockedMove(e, selection);
227222
}}
228-
onMouseDown={onBoxMouseDown}
229223
onClick={onBoxClick}
230224
>
231225
{cropOutlineInsetPx && (
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// @vitest-environment happy-dom
2+
3+
import React, { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, expect, it, vi } from "vitest";
6+
import { makeSelection } from "../../hooks/domSelectionTestHarness";
7+
import type { DomEditSelection } from "./domEditing";
8+
import "./domEditOverlayTestMocks";
9+
import { DomEditOverlay } from "./DomEditOverlay";
10+
11+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
12+
HTMLElement.prototype.setPointerCapture ??= () => {};
13+
HTMLElement.prototype.releasePointerCapture ??= () => {};
14+
15+
const pointTarget = vi.hoisted(() => ({ current: null as HTMLElement | null }));
16+
const rect = vi.hoisted(() => ({ current: null as Record<string, number> | null }));
17+
vi.mock("../../utils/studioPreviewHelpers", async (importOriginal) => ({
18+
...(await importOriginal<typeof import("../../utils/studioPreviewHelpers")>()),
19+
getPreviewTargetFromPointer: () => pointTarget.current,
20+
}));
21+
vi.mock("./useDomEditOverlayRects", () => ({
22+
useDomEditOverlayRects: () => ({
23+
overlayRect: rect.current,
24+
overlayRectRef: rect,
25+
setOverlayRect: () => undefined,
26+
hoverRect: null,
27+
groupOverlayItems: [],
28+
groupOverlayItemsRef: { current: [] },
29+
setGroupOverlayItems: () => undefined,
30+
childRects: [],
31+
}),
32+
}));
33+
34+
let root: Root;
35+
const onCanvasMouseDown = vi.fn();
36+
const onSelectionChange = vi.fn();
37+
38+
function mount() {
39+
const host = document.createElement("div");
40+
document.body.append(host);
41+
root = createRoot(host);
42+
const element = document.createElement("h1");
43+
document.body.append(element);
44+
return { host, element, hover: makeSelection("Title", element) };
45+
}
46+
47+
function render(
48+
hoverSelection: DomEditSelection | null,
49+
selection: DomEditSelection | null = null,
50+
) {
51+
act(() =>
52+
root.render(
53+
<DomEditOverlay
54+
iframeRef={{ current: document.createElement("iframe") }}
55+
activeCompositionPath={null}
56+
selection={selection}
57+
hoverSelection={hoverSelection}
58+
onCanvasMouseDown={onCanvasMouseDown}
59+
onCanvasPointerMove={() => Promise.resolve(null)}
60+
onCanvasPointerLeave={() => undefined}
61+
onSelectionChange={onSelectionChange}
62+
onBlockedMove={() => undefined}
63+
onPathOffsetCommit={() => undefined}
64+
onGroupPathOffsetCommit={() => undefined}
65+
onBoxSizeCommit={() => undefined}
66+
onRotationCommit={() => undefined}
67+
onMarqueeSelect={() => undefined}
68+
/>,
69+
),
70+
);
71+
}
72+
73+
// Like Chrome: a default-prevented pointerdown sends no compatibility mousedown.
74+
function press(target: Element, shiftKey = false) {
75+
const init = { bubbles: true, cancelable: true, button: 0, pointerId: 1, shiftKey };
76+
const down = new PointerEvent("pointerdown", init);
77+
act(() => void target.dispatchEvent(down));
78+
if (!down.defaultPrevented)
79+
act(() => void target.dispatchEvent(new MouseEvent("mousedown", init)));
80+
act(() => void target.dispatchEvent(new PointerEvent("pointerup", init)));
81+
act(() => void target.dispatchEvent(new MouseEvent("click", init)));
82+
}
83+
84+
afterEach(() => {
85+
act(() => root.unmount());
86+
onCanvasMouseDown.mockClear();
87+
onSelectionChange.mockClear();
88+
pointTarget.current = null;
89+
rect.current = null;
90+
document.body.innerHTML = "";
91+
});
92+
93+
it.each([
94+
["a press on empty canvas", false],
95+
["a shift+click add", true],
96+
])("after %s, the next click on an element selects it", (_, shiftFirst) => {
97+
const { host, element, hover } = mount();
98+
const overlay = () => host.firstElementChild!;
99+
100+
pointTarget.current = shiftFirst ? element : null;
101+
render(shiftFirst ? hover : null);
102+
press(overlay(), shiftFirst);
103+
expect(onCanvasMouseDown).not.toHaveBeenCalled();
104+
105+
pointTarget.current = element;
106+
render(hover);
107+
press(overlay());
108+
expect(onCanvasMouseDown).toHaveBeenCalledTimes(1);
109+
});
110+
111+
it("a shift+click on a selected box that cannot move toggles it once", () => {
112+
const { host, element, hover } = mount();
113+
hover.capabilities.canApplyManualOffset = false;
114+
rect.current = { left: 20, top: 30, width: 100, height: 40, editScaleX: 1, editScaleY: 1 };
115+
pointTarget.current = element;
116+
render(hover, hover);
117+
118+
press(host.querySelector('[data-dom-edit-selection-box="true"]')!, true);
119+
120+
expect(onSelectionChange).toHaveBeenCalledTimes(1);
121+
expect(onCanvasMouseDown).not.toHaveBeenCalled();
122+
});

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ describe("a shift press on a selected box starts the drag", () => {
193193
selectionKey="box"
194194
groupSelectionCount={0}
195195
gestures={spies as never}
196-
onBoxMouseDown={vi.fn()}
197196
onBoxClick={vi.fn()}
198197
/>
199198
));
@@ -207,7 +206,6 @@ describe("a shift press on a selected box starts the drag", () => {
207206
allowBodyDrag
208207
groupCanMove={groupCanMove}
209208
gestures={spies as never}
210-
onBoxMouseDown={vi.fn()}
211209
onBoxClick={vi.fn()}
212210
/>
213211
));

0 commit comments

Comments
 (0)