mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(studio): keep a sealed drag's waiting outline until the frame draws its replay (#8588)
* fix(studio): keep a sealed drag's waiting outline until the frame draws its replay * fix(studio): wait on the frame a reload swapped in before showing a sealed replay * fix(studio): only the newest sealed replay's draw clears the waiting outline * fix(studio): give the sealed waiting outline one owner per overlay mount * refactor(studio): the waiting outline decides what a replay's end means
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import type { RefObject } from "react";
|
||||
import {
|
||||
PRESS_WAITING_ATTR,
|
||||
type UseDomEditOverlayGesturesOptions,
|
||||
} from "./domEditOverlayGestures";
|
||||
import { previewDraftsSent } from "./previewDrafts";
|
||||
|
||||
type OutlineRefs = Pick<
|
||||
UseDomEditOverlayGesturesOptions,
|
||||
"boxRef" | "rafPausedRef" | "iframeRef" | "waitingPressRef"
|
||||
>;
|
||||
type ReplayDrawWait = { frame: HTMLIFrameElement };
|
||||
|
||||
function createPressOutline({ boxRef, rafPausedRef, iframeRef, waitingPressRef }: OutlineRefs) {
|
||||
let drawWait: ReplayDrawWait | null = null;
|
||||
const idle = () => {
|
||||
drawWait = null;
|
||||
boxRef.current?.removeAttribute(PRESS_WAITING_ATTR);
|
||||
rafPausedRef.current = false;
|
||||
};
|
||||
const confirm = (wait: ReplayDrawWait): void =>
|
||||
void previewDraftsSent(wait.frame).then(() => {
|
||||
if (drawWait !== wait) return;
|
||||
const shown = iframeRef.current;
|
||||
if (shown && shown !== wait.frame) {
|
||||
wait.frame = shown;
|
||||
return confirm(wait);
|
||||
}
|
||||
idle();
|
||||
});
|
||||
return {
|
||||
idle,
|
||||
waitOnSave() {
|
||||
drawWait = null;
|
||||
boxRef.current?.setAttribute(PRESS_WAITING_ATTR, "true");
|
||||
rafPausedRef.current = true;
|
||||
},
|
||||
/** A replay ended: `drewIn` is the sealed frame its draws went to, null if none. */
|
||||
replayEnded(drewIn: HTMLIFrameElement | null) {
|
||||
if (waitingPressRef.current) return;
|
||||
if (!drewIn) return idle();
|
||||
drawWait = { frame: drewIn };
|
||||
confirm(drawWait);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const outlines = new WeakMap<RefObject<unknown>, ReturnType<typeof createPressOutline>>();
|
||||
|
||||
/** The waiting outline's one owner per overlay mount: idle, waiting on a save, or waiting on a replay's draw. */
|
||||
export function pressOutlineOf(opts: OutlineRefs) {
|
||||
let outline = outlines.get(opts.waitingPressRef);
|
||||
if (!outline) outlines.set(opts.waitingPressRef, (outline = createPressOutline(opts)));
|
||||
return outline;
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import { findElementForSelection, type DomEditSelection } from "./domEditing";
|
||||
import { computeOverlayRootScale } from "./domEditOverlayBasis";
|
||||
import { resolveCenterResizeScale } from "./domEditResizeLocal";
|
||||
import {
|
||||
PRESS_WAITING_ATTR,
|
||||
type WaitingPressState,
|
||||
type GestureKind,
|
||||
type ResizeHandle,
|
||||
@@ -22,6 +21,7 @@ import { setStudioWaitingPressCancel } from "../../utils/studioPendingEdits";
|
||||
import { playheadMoment } from "../../hooks/editMoment";
|
||||
import type { EditMoment } from "./manualEditsTypes";
|
||||
import { previewFrameOpaque } from "../../player/lib/previewChannel";
|
||||
import { pressOutlineOf } from "./pressOutline";
|
||||
import {
|
||||
askShownGestureStarts,
|
||||
UNANSWERED,
|
||||
@@ -52,10 +52,8 @@ export function createPreviewGestureStarts(
|
||||
setStudioWaitingPressCancel(press && (() => dropPress(press)));
|
||||
};
|
||||
|
||||
const clearWaitingMark = () => {
|
||||
opts.boxRef.current?.removeAttribute(PRESS_WAITING_ATTR);
|
||||
opts.rafPausedRef.current = false;
|
||||
};
|
||||
const outline = pressOutlineOf(opts);
|
||||
const clearWaitingMark = outline.idle;
|
||||
|
||||
/** Points the press queued right behind `press` at `next` instead. */
|
||||
const unlink = (press: WaitingPressState, next: WaitingPressState | null) => {
|
||||
@@ -63,14 +61,17 @@ export function createPreviewGestureStarts(
|
||||
if (p.after === press) p.after = next;
|
||||
};
|
||||
|
||||
const endWaitingPress = (press: WaitingPressState | null = opts.waitingPressRef.current) => {
|
||||
const endWaitingPress = (
|
||||
press: WaitingPressState | null = opts.waitingPressRef.current,
|
||||
keepMark = false,
|
||||
) => {
|
||||
for (let p = press; p && !p.ended; p = p.after) {
|
||||
p.ended = true;
|
||||
cancelAnimationFrame(p.frame);
|
||||
}
|
||||
if (press === opts.waitingPressRef.current) {
|
||||
opts.waitingPressRef.current = null;
|
||||
clearWaitingMark();
|
||||
if (!keepMark) clearWaitingMark();
|
||||
}
|
||||
// A press queued behind this one keeps waiting, on nothing that has ended.
|
||||
else if (press) unlink(press, null);
|
||||
@@ -174,8 +175,7 @@ export function createPreviewGestureStarts(
|
||||
ended: false,
|
||||
};
|
||||
opts.waitingPressRef.current = press;
|
||||
opts.boxRef.current?.setAttribute(PRESS_WAITING_ATTR, "true");
|
||||
opts.rafPausedRef.current = true;
|
||||
outline.waitOnSave();
|
||||
let shownFrames = 0;
|
||||
let settledFrames = 0;
|
||||
const since = performance.now();
|
||||
@@ -236,12 +236,16 @@ export function createPreviewGestureStarts(
|
||||
press.frame = requestAnimationFrame(poll);
|
||||
return;
|
||||
}
|
||||
endWaitingPress(press);
|
||||
endWaitingPress(press, !!channel);
|
||||
const replayed = replay(startsFrom(step));
|
||||
outline.replayEnded(replayed && channel ? opts.iframeRef.current : null);
|
||||
};
|
||||
const replay = (read: ReturnType<typeof startsFrom>): boolean => {
|
||||
const pointerDown = down as unknown as React.PointerEvent<HTMLElement>;
|
||||
const read = startsFrom(step);
|
||||
if (read === false || !start(pointerDown, at, true, read)) return;
|
||||
if (read === false || !start(pointerDown, at, true, read)) return false;
|
||||
if (press.moved) moveActiveGesture(press.moved);
|
||||
if (press.released) releaseActiveGesture(press.released);
|
||||
return true;
|
||||
};
|
||||
press.frame = requestAnimationFrame(poll);
|
||||
return true;
|
||||
|
||||
@@ -9,7 +9,10 @@ import { queryPreviewElements } from "../../player/lib/previewElements";
|
||||
import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
|
||||
import type { DomEditSelection } from "./domEditing";
|
||||
import { orientedGroupAwareOverlayRect, type OverlayRect } from "./domEditOverlayGeometry";
|
||||
import type { UseDomEditOverlayGesturesOptions } from "./domEditOverlayGestures";
|
||||
import {
|
||||
PRESS_WAITING_ATTR,
|
||||
type UseDomEditOverlayGesturesOptions,
|
||||
} from "./domEditOverlayGestures";
|
||||
import { createPreviewGestureStarts } from "./previewGestureStarts";
|
||||
import { standInHandle } from "./previewSelection";
|
||||
import { useDomEditOverlayRects } from "./useDomEditOverlayRects";
|
||||
@@ -24,6 +27,13 @@ vi.mock("./overlayFrameLoop", () => ({
|
||||
}));
|
||||
|
||||
const press = vi.hoisted(() => ({ changing: true, start: vi.fn((..._args: unknown[]) => true) }));
|
||||
const replay = vi.hoisted(() => ({
|
||||
sent: (_iframe: HTMLIFrameElement) => Promise.resolve(),
|
||||
}));
|
||||
vi.mock("./previewDrafts", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("./previewDrafts")>()),
|
||||
previewDraftsSent: (iframe: HTMLIFrameElement) => replay.sent(iframe),
|
||||
}));
|
||||
vi.mock("./domEditOverlayStartGesture", () => ({
|
||||
startGesture: press.start,
|
||||
startGroupDrag: vi.fn(() => true),
|
||||
@@ -366,16 +376,29 @@ function silentFrame() {
|
||||
}
|
||||
|
||||
describe("a press held across a preview change on an opaque frame", () => {
|
||||
afterEach(() => {
|
||||
replay.sent = () => Promise.resolve();
|
||||
});
|
||||
|
||||
/** Holds each frame's answer to the replay's draws until the test lets it go. */
|
||||
function holdDraws() {
|
||||
const held = new Map<HTMLIFrameElement, () => void>();
|
||||
replay.sent = (iframe) => new Promise<void>((resolve) => held.set(iframe, resolve));
|
||||
return held;
|
||||
}
|
||||
|
||||
async function pressDuringChange(
|
||||
selection: DomEditSelection,
|
||||
{
|
||||
iframe = opaqueFrame(),
|
||||
overlayRect = null,
|
||||
whileWaiting = () => {},
|
||||
box = null,
|
||||
}: {
|
||||
iframe?: HTMLIFrameElement;
|
||||
overlayRect?: OverlayRect | null;
|
||||
whileWaiting?: (opts: UseDomEditOverlayGesturesOptions) => void;
|
||||
box?: HTMLDivElement | null;
|
||||
} = {},
|
||||
) {
|
||||
press.changing = true;
|
||||
@@ -383,7 +406,7 @@ describe("a press held across a preview change on an opaque frame", () => {
|
||||
const overlay = sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement;
|
||||
const opts = {
|
||||
waitingPressRef: { current: null },
|
||||
boxRef: { current: null },
|
||||
boxRef: { current: box },
|
||||
rafPausedRef: { current: false },
|
||||
iframeRef: { current: iframe },
|
||||
overlayRef: { current: overlay },
|
||||
@@ -392,7 +415,7 @@ describe("a press held across a preview change on an opaque frame", () => {
|
||||
overlayRectRef: { current: overlayRect },
|
||||
setOverlayRect: vi.fn((rect: OverlayRect | null) => (opts.overlayRectRef.current = rect)),
|
||||
} as unknown as UseDomEditOverlayGesturesOptions;
|
||||
const { startGesture } = createPreviewGestureStarts(opts, vi.fn(), vi.fn());
|
||||
const starts = createPreviewGestureStarts(opts, vi.fn(), vi.fn());
|
||||
const down = {
|
||||
pointerId: 1,
|
||||
clientX: 5,
|
||||
@@ -401,14 +424,151 @@ describe("a press held across a preview change on an opaque frame", () => {
|
||||
preventDefault: vi.fn(),
|
||||
stopPropagation: vi.fn(),
|
||||
};
|
||||
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
starts.startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
expect(press.start).not.toHaveBeenCalled();
|
||||
whileWaiting(opts);
|
||||
press.changing = false;
|
||||
return { opts, overlay };
|
||||
return { opts, overlay, down, ...starts };
|
||||
}
|
||||
|
||||
// fails without the fix: the outline went at once while the frame still showed the layer where the last save put
|
||||
// it, so for one frame the layer showed at the drag's start (312 px on the edit bench's seqheldtwice case).
|
||||
it("keeps the waiting outline until the frame has drawn the replayed drag", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const iframe = opaqueFrame();
|
||||
const { opts } = await pressDuringChange(stale, { box, iframe });
|
||||
await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce());
|
||||
expect([box.hasAttribute(PRESS_WAITING_ATTR), opts.rafPausedRef.current]).toEqual([true, true]);
|
||||
held.get(iframe)!();
|
||||
await vi.waitFor(() => expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(false));
|
||||
expect(opts.rafPausedRef.current).toBe(false);
|
||||
});
|
||||
|
||||
// fails without the fix: the outline waited on the frame pressed, which a reload had replaced, so it went while
|
||||
// the replacement frame's draw of the replay was still on its way.
|
||||
it("keeps the waiting outline until the frame the change swapped in has drawn the replay", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const swappedIn = opaqueFrame();
|
||||
await pressDuringChange(stale, {
|
||||
box,
|
||||
whileWaiting: (o) => (o.iframeRef.current = swappedIn),
|
||||
});
|
||||
await vi.waitFor(() => expect(held.has(swappedIn)).toBe(true));
|
||||
expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(true);
|
||||
held.get(swappedIn)!();
|
||||
await vi.waitFor(() => expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(false));
|
||||
});
|
||||
|
||||
// fails without the fix: the older replay's frame answering cleared the outline the newer replay still held.
|
||||
it("keeps a newer replay's outline when an older replay's frame answers first", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const [first, second] = [opaqueFrame(), opaqueFrame()];
|
||||
const { opts, startGesture, down } = await pressDuringChange(stale, { box, iframe: first });
|
||||
await vi.waitFor(() => expect(held.has(first)).toBe(true));
|
||||
opts.iframeRef.current = second;
|
||||
press.changing = true;
|
||||
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
press.changing = false;
|
||||
await vi.waitFor(() => expect(held.has(second)).toBe(true));
|
||||
held.get(first)!();
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(true);
|
||||
held.get(second)!();
|
||||
await vi.waitFor(() => expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(false));
|
||||
});
|
||||
|
||||
it("keeps a press's outline that comes while the replay before it waits on its frame", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const iframe = opaqueFrame();
|
||||
const { opts, startGesture, down } = await pressDuringChange(stale, { box, iframe });
|
||||
await vi.waitFor(() => expect(held.has(iframe)).toBe(true));
|
||||
press.changing = true;
|
||||
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
held.get(iframe)!();
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
expect([box.hasAttribute(PRESS_WAITING_ATTR), opts.waitingPressRef.current]).toEqual([
|
||||
true,
|
||||
expect.anything(),
|
||||
]);
|
||||
press.changing = false;
|
||||
});
|
||||
|
||||
// fails without the fix: the overlay re-renders its handlers, and the older replay's answer reached the new
|
||||
// handlers' copy of the outline, so it cleared the outline of a press made through them.
|
||||
it("keeps a press's outline made after a re-render when the replay before it answers", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const iframe = opaqueFrame();
|
||||
const { opts, down } = await pressDuringChange(stale, { box, iframe });
|
||||
await vi.waitFor(() => expect(held.has(iframe)).toBe(true));
|
||||
const rerendered = createPreviewGestureStarts(opts, vi.fn(), vi.fn());
|
||||
press.changing = true;
|
||||
rerendered.startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
held.get(iframe)!();
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(true);
|
||||
press.changing = false;
|
||||
rerendered.endWaitingPress();
|
||||
});
|
||||
|
||||
// fails without the fix: the frame the replay drew in answered after a reload had swapped it out, and the outline
|
||||
// went before the swapped-in frame showed the replay.
|
||||
it("waits on the frame swapped in after the replay, not the one it drew in", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const [drewIn, swappedIn] = [opaqueFrame(), opaqueFrame()];
|
||||
const { opts } = await pressDuringChange(stale, { box, iframe: drewIn });
|
||||
await vi.waitFor(() => expect(held.has(drewIn)).toBe(true));
|
||||
opts.iframeRef.current = swappedIn;
|
||||
held.get(drewIn)!();
|
||||
await vi.waitFor(() => expect(held.has(swappedIn)).toBe(true));
|
||||
expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(true);
|
||||
held.get(swappedIn)!();
|
||||
await vi.waitFor(() => expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(false));
|
||||
});
|
||||
|
||||
it("drops the outline at once when the overlay leaves, and a late answer changes nothing", async () => {
|
||||
const held = holdDraws();
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
const box = document.createElement("div");
|
||||
const iframe = opaqueFrame();
|
||||
const { opts, startGesture, down, endWaitingPress } = await pressDuringChange(stale, {
|
||||
box,
|
||||
iframe,
|
||||
});
|
||||
await vi.waitFor(() => expect(held.has(iframe)).toBe(true));
|
||||
endWaitingPress();
|
||||
expect([box.hasAttribute(PRESS_WAITING_ATTR), opts.rafPausedRef.current]).toEqual([
|
||||
false,
|
||||
false,
|
||||
]);
|
||||
press.changing = true;
|
||||
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
|
||||
held.get(iframe)!();
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
expect(box.hasAttribute(PRESS_WAITING_ATTR)).toBe(true);
|
||||
press.changing = false;
|
||||
});
|
||||
|
||||
it("re-finds the pressed element on the page shown and starts on its box, read over the channel", async () => {
|
||||
const turned = document.getElementById("turned")!;
|
||||
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
|
||||
|
||||
Reference in New Issue
Block a user