Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion packages/studio/src/components/editor/previewDrafts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type ManualOffsetDragMember,
} from "./manualOffsetDrag";
import { endResizeWithoutMember, resolveResizeDraftRect } from "./resizeDraft";
import { armPreviewDraftEnd, drawPreviewDraft, takePreviewDraftEnds } from "./previewDrafts";
import { applySoftReloadFinalization } from "../../utils/gsapSoftReload";
import type { DomEditSelection } from "./domEditing";
import type { GestureState } from "./domEditOverlayGestures";
Expand Down Expand Up @@ -177,11 +178,60 @@ describe("gesture drafts on an opaque frame", () => {
it("sends one request at a time and draws only the latest of the moves that waited", async () => {
const member = dragMember(await standIn(sealedPreviewFrame().iframe, "#card"));
vi.mocked(previewRequest).mockClear();
for (let dx = 2; dx <= 40; dx += 2) applyManualOffsetDragDraft(member, dx, 0);
applyManualOffsetDragDraft(member, 2, 0);
await Promise.resolve();
for (let dx = 4; dx <= 40; dx += 2) applyManualOffsetDragDraft(member, dx, 0);
await vi.waitFor(() => expect(shown(card())).toEqual([80, 0]));
expect(draftRequests()).toHaveLength(2);
});

// fails without the fix: the step's offset left alone with the old size, and the frame painted that half state.
it("sends a resize step's offset and size in one message, so the frame never shows one without the other", async () => {
const iframe = sealedPreviewFrame().iframe;
const element = await standIn(iframe, "#card");
const g = {
...{ originLeft: 10, originTop: 20, originWidth: 100, originHeight: 50 },
...{ actualWidth: 200, actualHeight: 100, editScaleX: 0.5, editScaleY: 0.5 },
actualRotation: 0,
pathOffsetMember: dragMember(element),
} as GestureState;
vi.mocked(previewRequest).mockClear();
const size = { width: 300, height: 140 };
resolveResizeDraftRect(g, element, null, iframe, () => null, { wanted: size, written: size });
await vi.waitFor(() => expect(card().style.width).toBe("300px"));
const sent = draftRequests().map(([, , params]) => (params as { drafts: object[] }).drafts);
expect(sent).toEqual([[expect.objectContaining({ translate: { x: -50, y: -20 }, size })]]);
});

it("keeps what each draw in one task set: a later field wins, and a size drawn earlier stays", async () => {
const element = await standIn(sealedPreviewFrame().iframe, "#card");
vi.mocked(previewRequest).mockClear();
drawPreviewDraft(element, { size: null });
drawPreviewDraft(element, { size: { width: 260, height: 90 } });
drawPreviewDraft(element, { translate: { x: 4, y: 0 } });
drawPreviewDraft(element, { translate: { x: 7, y: 3 } });
await vi.waitFor(() => expect(card().style.width).toBe("260px"));
const sent = draftRequests().map(([, , params]) => (params as { drafts: object[] }).drafts);
expect(sent).toEqual([
[expect.objectContaining({ translate: { x: 7, y: 3 }, size: { width: 260, height: 90 } })],
]);
});

it("sends a draw waiting in this task before a save that carries its end", async () => {
const iframe = sealedPreviewFrame().iframe;
const element = await standIn(iframe, "#card");
const member = dragMember(element);
applyManualOffsetDragDraft(member, 10, 0);
await settled();
armPreviewDraftEnd(element, "#card");
vi.mocked(previewRequest).mockClear();
applyManualOffsetDragDraft(member, 12, 0);
const ends = takePreviewDraftEnds(iframe, ["#card"]);
// without the flush the draw still waits, so the end can't ride the save and a late draw could undo it
expect(draftRequests()).toHaveLength(1);
expect(ends).toEqual([expect.objectContaining({ end: "keep-size" })]);
});

it("grows a resized box about its centre without measuring the frame, and draws it there", async () => {
const iframe = sealedPreviewFrame().iframe;
const element = await standIn(iframe, "#card");
Expand Down
17 changes: 14 additions & 3 deletions packages/studio/src/components/editor/previewDrafts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A gesture on a preview frame Studio cannot script edits the selection's stand-in element. What the
* gesture draws on the stand-in is drawn on the frame's own element too, over the frame's channel,
* so the preview follows the pointer. One request is in flight per frame; later draws wait and merge.
* so the preview follows the pointer. One request is in flight per frame; draws in one task, or waiting, merge.
*/
import {
MAX_DESCRIBE_HANDLES,
Expand All @@ -24,6 +24,7 @@ interface Queue {
ops: EditingDraft[];
drawn: Map<string, Entry>;
sending: boolean;
sendsAtTaskEnd: boolean;
}

const standIns = new WeakMap<Element, { iframe: HTMLIFrameElement; handle: string }>();
Expand All @@ -49,7 +50,7 @@ export function previewDraftLive(standIn: Element): boolean {
function queueFor(iframe: HTMLIFrameElement): Queue {
let queue = queues.get(iframe);
if (!queue) {
queue = { ops: [], drawn: new Map(), sending: false };
queue = { ops: [], drawn: new Map(), sending: false, sendsAtTaskEnd: false };
queues.set(iframe, queue);
}
return queue;
Expand All @@ -75,6 +76,15 @@ function send(iframe: HTMLIFrameElement, queue: Queue): void {
});
}

function sendAtTaskEnd(iframe: HTMLIFrameElement, queue: Queue): void {
if (queue.sendsAtTaskEnd) return;
queue.sendsAtTaskEnd = true;
queueMicrotask(() => {
queue.sendsAtTaskEnd = false;
send(iframe, queue);
});
}

/** Draws `part` of the stand-in's draft on its frame element; any other element is left alone. */
export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
const target = standIns.get(element);
Expand All @@ -95,7 +105,7 @@ export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
const waiting = queue.ops[last];
if (waiting && !("end" in waiting)) queue.ops[last] = op;
else queue.ops.push(op);
send(target.iframe, queue);
sendAtTaskEnd(target.iframe, queue);
}

/** A gesture's commit started: the save that patches `selector` may carry this draft's end. */
Expand All @@ -111,6 +121,7 @@ export function takePreviewDraftEnds(
selectors: readonly string[],
): EditingDraftEnd[] {
const queue = queueFor(iframe);
send(iframe, queue);
const ends: EditingDraftEnd[] = [];
for (const [handle, entry] of queue.drawn) {
const waiting = queue.ops.some((op) => op.handle === handle);
Expand Down
Loading