Skip to content

Commit c752b89

Browse files
fix(runtime): the editor keeps waking during a drag on a preview it did not build (#3850)
The manual-edit gesture watch tested mutation targets with `instanceof Element`. The composition body is adopted into the preview frame, so its nodes answer to another realm's Element and the check is false for every one of them: the watch never sees a gesture, and the paused transport it gates does not wake while the user drags. Routes the check through the runtime's structural predicate, which is what the preview-guard lint added alongside it now requires. Main is red on that lint for this line, so this also unbreaks it. Test adopts an element from a second realm and asserts the marker is seen and cleared; it fails with the identity check restored.
1 parent d4fba55 commit c752b89

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎packages/core/src/runtime/manualEditGestureWatch.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { JSDOM } from "jsdom";
23
import { STUDIO_MANUAL_EDIT_GESTURE_ATTR } from "../editing/draftMarkers";
34
import { createManualEditGestureWatch } from "./manualEditGestureWatch";
45

@@ -34,6 +35,28 @@ describe("manual-edit gesture watch", () => {
3435
}
3536
});
3637

38+
it("sees a gesture on an element adopted from another realm", async () => {
39+
// The composition body is built in the editor window and moved into the
40+
// preview document, so its nodes carry another realm's prototypes and
41+
// `target instanceof Element` answers false for every one of them. A
42+
// gesture on such an element must still be seen, or a parked transport
43+
// never wakes while the user drags.
44+
const foreign = new JSDOM("<!doctype html><html><body></body></html>");
45+
const el = foreign.window.document.createElement("div");
46+
document.body.appendChild(document.adoptNode(el as unknown as Node));
47+
const watch = createManualEditGestureWatch(document, () => {});
48+
try {
49+
expect(el instanceof Element).toBe(false);
50+
(el as unknown as Element).setAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR, "token");
51+
expect(watch.isActive()).toBe(true);
52+
await flush();
53+
(el as unknown as Element).removeAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR);
54+
expect(watch.isActive()).toBe(false);
55+
} finally {
56+
watch.disconnect();
57+
}
58+
});
59+
3760
it("stops reporting a gesture whose element was torn out mid-drag", async () => {
3861
document.body.innerHTML = `<div id="a"></div>`;
3962
const watch = createManualEditGestureWatch(document, () => {});

‎packages/core/src/runtime/manualEditGestureWatch.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { STUDIO_MANUAL_EDIT_GESTURE_ATTR } from "../editing/draftMarkers";
2+
import { isElementNode } from "./domRealm";
23

34
export interface ManualEditGestureWatch {
45
/** True while any element in the document carries the gesture marker. */
@@ -53,7 +54,10 @@ export function createManualEditGestureWatch(
5354
const ingest = (records: MutationRecord[]): void => {
5455
for (const record of records) {
5556
const target = record.target;
56-
if (!(target instanceof Element)) continue;
57+
// Structural, not `instanceof`: the composition body is adopted into the
58+
// preview frame, so its nodes answer to another realm's Element and an
59+
// identity check silently skips every gesture. See domRealm.ts.
60+
if (!isElementNode(target)) continue;
5761
if (target.hasAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR)) marked.add(target);
5862
else marked.delete(target);
5963
}

0 commit comments

Comments
 (0)