@hyperframes/studio 0.9.40 → 0.9.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{codec-BPK_2CLL.js → codec-CKqLGYc6.js} +1 -1
- package/dist/assets/{hyperframes-player-D7qTTDIp.js → hyperframes-player-DoEYqOIa.js} +1 -1
- package/dist/assets/{index-CdA00Ybn.js → index-DLyQ91Ek.js} +166 -166
- package/dist/assets/{index-QS-DuxAU.js → index-s9iqoWnr.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-Byzz98Gb.js → pausedVideoDecoder-B-ReRg6i.js} +1 -1
- package/dist/{chunk-KF6YVXJP.js → chunk-4QQMX4BX.js} +6 -3
- package/dist/{chunk-VZ5MD32A.js → chunk-GVW7AU57.js} +190 -254
- package/dist/{chunk-AV2PV7NE.js → chunk-VHV2ZDKY.js} +4 -40
- package/dist/{domEditingLayers-4KRREI5C.js → domEditingLayers-SA5KLSSS.js} +4 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.html +1 -1
- package/dist/index.js +1501 -818
- package/dist/player.js +2 -2
- package/package.json +10 -10
- package/src/components/editor/DomEditOverlay.tsx +3 -15
- package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
- package/src/components/editor/LayersPanel.tsx +41 -10
- package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
- package/src/components/editor/domEditInlineText.ts +1 -16
- package/src/components/editor/domEditOverlayBasis.ts +7 -2
- package/src/components/editor/domEditOverlayCrop.test.ts +1 -1
- package/src/components/editor/domEditOverlayCrop.ts +17 -3
- package/src/components/editor/domEditOverlayGeometry.ts +64 -139
- package/src/components/editor/domEditSelectionRules.ts +138 -0
- package/src/components/editor/domEditing.test.ts +6 -28
- package/src/components/editor/domEditingDom.preview.test.ts +3 -3
- package/src/components/editor/domEditingDom.ts +3 -76
- package/src/components/editor/domEditingElement.ts +19 -240
- package/src/components/editor/domEditingGroups.ts +0 -31
- package/src/components/editor/domEditingLayers.ts +95 -100
- package/src/components/editor/domEditingRootLayer.ts +35 -30
- package/src/components/editor/domEditingTypes.ts +10 -0
- package/src/components/editor/gestureUndoRevert.ts +18 -1
- package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
- package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
- package/src/components/editor/layerKind.ts +2 -19
- package/src/components/editor/layersPanelSort.ts +2 -2
- package/src/components/editor/manualEdits.ts +1 -0
- package/src/components/editor/manualEditsSnapshot.ts +10 -0
- package/src/components/editor/manualOffsetDrag.test.ts +1 -1
- package/src/components/editor/manualOffsetDrag.ts +7 -1
- package/src/components/editor/marqueeCommit.ts +47 -4
- package/src/components/editor/plainTranslate.ts +35 -16
- package/src/components/editor/previewDrafts.test.ts +263 -0
- package/src/components/editor/previewDrafts.ts +73 -0
- package/src/components/editor/previewGestureStarts.ts +73 -10
- package/src/components/editor/previewLayers.test.ts +308 -0
- package/src/components/editor/previewLayers.ts +164 -0
- package/src/components/editor/previewOverlayRects.test.ts +316 -0
- package/src/components/editor/previewOverlayRects.ts +150 -0
- package/src/components/editor/previewPointerTargets.test.ts +327 -0
- package/src/components/editor/previewSelection.test.ts +481 -0
- package/src/components/editor/previewSelection.ts +516 -0
- package/src/components/editor/probeSourceElement.ts +19 -1
- package/src/components/editor/propertyPanelFill.tsx +5 -1
- package/src/components/editor/resizeDraft.ts +38 -0
- package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
- package/src/components/editor/useDomEditOverlayRects.ts +80 -1
- package/src/components/editor/useInlineTextEditing.tsx +1 -6
- package/src/components/editor/useLayerDrag.ts +1 -1
- package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
- package/src/hooks/domEditDeleteMembers.test.ts +7 -0
- package/src/hooks/domSelectionTestHarness.ts +1 -1
- package/src/hooks/domSelectionTimelineMirror.ts +3 -1
- package/src/hooks/timelineTimingSync.ts +4 -1
- package/src/hooks/useDomEditSession.test.tsx +23 -1
- package/src/hooks/useDomEditSession.ts +2 -1
- package/src/hooks/useDomSelection.ts +63 -47
- package/src/hooks/useEditorSave.test.tsx +22 -1
- package/src/hooks/useEditorSave.ts +3 -1
- package/src/hooks/useGsapSelectionHandlers.test.tsx +17 -0
- package/src/hooks/useGsapSelectionHandlers.ts +2 -1
- package/src/hooks/usePreviewInteraction.test.ts +10 -2
- package/src/hooks/usePreviewInteraction.ts +9 -5
- package/src/hooks/useProjectFileWriter.ts +7 -3
- package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
- package/src/hooks/useStudioSelectionPublisher.ts +9 -7
- package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
- package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
- package/src/player/lib/layerOrdering.ts +2 -11
- package/src/player/lib/previewChannel.ts +4 -3
- package/src/player/lib/previewElements.ts +143 -3
- package/src/styles/hex-baseline.json +1 -2
- package/src/test-setup.ts +2 -0
- package/src/utils/gsapSoftReload.ts +1 -1
- package/src/utils/studioFileHistory.ts +1 -1
- package/src/utils/studioHelpers.ts +27 -17
- package/src/utils/studioPreviewHelpers.test.ts +8 -8
- package/src/utils/studioPreviewHelpers.ts +26 -242
- package/src/utils/studioSaveDiagnostics.test.ts +34 -0
- package/src/utils/studioSaveDiagnostics.ts +35 -6
- package/src/utils/timelineCompositionInsert.ts +3 -1
- package/src/components/editor/domEditOverlayTransform.ts +0 -126
|
@@ -70,36 +70,55 @@ function evaluateLength(value: string, side: number): number {
|
|
|
70
70
|
|
|
71
71
|
const CONTENT_BOXES = new Set(["content-box", "fill-box"]);
|
|
72
72
|
|
|
73
|
+
type ComputedRead = (property: string) => string;
|
|
74
|
+
|
|
75
|
+
export const TRANSLATE_PX_PROPERTIES = [
|
|
76
|
+
"translate",
|
|
77
|
+
"width",
|
|
78
|
+
"height",
|
|
79
|
+
"box-sizing",
|
|
80
|
+
"transform-box",
|
|
81
|
+
...["left", "right", "top", "bottom"].flatMap((s) => [`padding-${s}`, `border-${s}-width`]),
|
|
82
|
+
];
|
|
83
|
+
|
|
73
84
|
/** The side % resolves against: the box `transform-box` names (fill-box is the content box on HTML). */
|
|
74
|
-
function referenceBox(
|
|
75
|
-
|
|
76
|
-
size: "width" | "height",
|
|
77
|
-
a: string,
|
|
78
|
-
b: string,
|
|
79
|
-
): number {
|
|
80
|
-
const n = (prop: string) => Number.parseFloat(cs.getPropertyValue(prop)) || 0;
|
|
85
|
+
function referenceBox(read: ComputedRead, size: "width" | "height", a: string, b: string): number {
|
|
86
|
+
const n = (prop: string) => Number.parseFloat(read(prop)) || 0;
|
|
81
87
|
const pad =
|
|
82
88
|
n(`padding-${a}`) + n(`padding-${b}`) + n(`border-${a}-width`) + n(`border-${b}-width`);
|
|
83
|
-
const content = n(size) - (
|
|
84
|
-
return CONTENT_BOXES.has(
|
|
89
|
+
const content = n(size) - (read("box-sizing") === "border-box" ? pad : 0);
|
|
90
|
+
return CONTENT_BOXES.has(read("transform-box")) ? content : content + pad;
|
|
85
91
|
}
|
|
86
92
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
export function readTranslatePx(el: HTMLElement): Point {
|
|
90
|
-
const cs = el.ownerDocument.defaultView?.getComputedStyle(el);
|
|
91
|
-
// Computed first: Chrome resolves em, vw and var() there and leaves only % to work out.
|
|
92
|
-
const value = cs?.translate || el.style.getPropertyValue("translate") || "none";
|
|
93
|
+
/** A computed translate in px; with no styles to size the box, a % is NaN rather than 0. */
|
|
94
|
+
export function translatePx(value: string, read?: ComputedRead): Point {
|
|
93
95
|
if (value === "none") return { x: 0, y: 0 };
|
|
94
96
|
const [x = "0px", y = "0px"] = splitTopLevelWhitespace(value);
|
|
95
97
|
const side = (size: "width" | "height", a: string, b: string) =>
|
|
96
|
-
|
|
98
|
+
read ? referenceBox(read, size, a, b) : Number.NaN;
|
|
97
99
|
return {
|
|
98
100
|
x: evaluateLength(x, side("width", "left", "right")),
|
|
99
101
|
y: evaluateLength(y, side("height", "top", "bottom")),
|
|
100
102
|
};
|
|
101
103
|
}
|
|
102
104
|
|
|
105
|
+
const frameTranslates = new WeakMap<Element, { inline: string; px: Point }>();
|
|
106
|
+
|
|
107
|
+
export function rememberFrameTranslate(el: HTMLElement, px: Point): void {
|
|
108
|
+
frameTranslates.set(el, { inline: el.style.getPropertyValue("translate"), px });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The element's CSS `translate` in px, as it renders now. */
|
|
112
|
+
// ponytail: a 3-value translate loses its z on the next move; keep z when a fixture needs it.
|
|
113
|
+
export function readTranslatePx(el: HTMLElement): Point {
|
|
114
|
+
const cs = el.ownerDocument.defaultView?.getComputedStyle(el);
|
|
115
|
+
const inline = el.style.getPropertyValue("translate");
|
|
116
|
+
const frame = cs ? undefined : frameTranslates.get(el);
|
|
117
|
+
if (frame?.inline === inline) return frame.px;
|
|
118
|
+
// Computed first: Chrome resolves em, vw and var() there and leaves only % to work out.
|
|
119
|
+
return translatePx(cs?.translate || inline || "none", cs && ((p) => cs.getPropertyValue(p)));
|
|
120
|
+
}
|
|
121
|
+
|
|
103
122
|
export function readTranslatePxLeavingPercent(el: HTMLElement): Point {
|
|
104
123
|
const value = el.ownerDocument.defaultView?.getComputedStyle(el).translate || "none";
|
|
105
124
|
const [x = "0px", y = "0px"] = value === "none" ? [] : splitTopLevelWhitespace(value);
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
// The frame's real runtime agent, serving the opaque frame from this document.
|
|
5
|
+
import { installEditingAgent } from "../../../../core/src/runtime/editingAgent";
|
|
6
|
+
import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
|
|
7
|
+
import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
|
|
8
|
+
import { resolvePreviewSelection, standInFor } from "./previewSelection";
|
|
9
|
+
import { manualOffsetMoveRevert } from "./gestureUndoRevert";
|
|
10
|
+
import { writePlainMove } from "./plainTranslate";
|
|
11
|
+
import {
|
|
12
|
+
beginStudioManualEditGesture,
|
|
13
|
+
captureStudioBoxSize,
|
|
14
|
+
restoreStudioBoxSize,
|
|
15
|
+
} from "./manualEdits";
|
|
16
|
+
import { STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR } from "./manualEditsTypes";
|
|
17
|
+
import {
|
|
18
|
+
applyManualOffsetDragCommit,
|
|
19
|
+
applyManualOffsetDragDraft,
|
|
20
|
+
createManualOffsetDragMember,
|
|
21
|
+
endManualOffsetDragMembers,
|
|
22
|
+
restoreManualOffsetDragMembers,
|
|
23
|
+
type ManualOffsetDragMember,
|
|
24
|
+
} from "./manualOffsetDrag";
|
|
25
|
+
import { endResizeWithoutMember, resolveResizeDraftRect } from "./resizeDraft";
|
|
26
|
+
import { applySoftReloadFinalization } from "../../utils/gsapSoftReload";
|
|
27
|
+
import type { DomEditSelection } from "./domEditing";
|
|
28
|
+
import type { GestureState } from "./domEditOverlayGestures";
|
|
29
|
+
import { previewRequest } from "../../player/lib/previewChannel";
|
|
30
|
+
|
|
31
|
+
vi.mock("../../player/lib/previewChannel", async (importOriginal) => {
|
|
32
|
+
const actual = await importOriginal<typeof import("../../player/lib/previewChannel")>();
|
|
33
|
+
return { ...actual, previewRequest: vi.fn(actual.previewRequest) };
|
|
34
|
+
});
|
|
35
|
+
const draftRequests = () =>
|
|
36
|
+
vi.mocked(previewRequest).mock.calls.filter(([, kind]) => kind === "draft");
|
|
37
|
+
|
|
38
|
+
const closers: Array<() => void> = [];
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
document.body.innerHTML = `<main data-composition-id="main"><div id="card" style="width: 200px; height: 100px"></div><div id="chip" style="translate: 30px 0px"></div><div id="centred" style="width: 200px; height: 100px; translate: -50% -50%"></div><div id="moved" data-hf-studio-original-inline-translate="" style="translate: 10px 20px"></div></main>`;
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
closers.splice(0).forEach((close) => close());
|
|
44
|
+
document.body.innerHTML = "";
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/** A frame whose window refuses every read, as a cross-origin one does; its runtime serves this document. */
|
|
48
|
+
function opaqueFrame(): HTMLIFrameElement {
|
|
49
|
+
const blocked = () => {
|
|
50
|
+
throw new DOMException("blocked", "SecurityError");
|
|
51
|
+
};
|
|
52
|
+
const target: Record<string, unknown> = {};
|
|
53
|
+
const frame = new Proxy(target, {
|
|
54
|
+
get: (_, key) => (key === "postMessage" ? target.postMessage : blocked()),
|
|
55
|
+
});
|
|
56
|
+
const iframe = detachedFakeIframe(frame);
|
|
57
|
+
Object.defineProperty(iframe, "contentDocument", { value: null });
|
|
58
|
+
const parent = {
|
|
59
|
+
postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
|
|
60
|
+
window.dispatchEvent(
|
|
61
|
+
new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
|
|
62
|
+
),
|
|
63
|
+
} as unknown as Window;
|
|
64
|
+
const agent = installEditingAgent(parent);
|
|
65
|
+
target.postMessage = (data: unknown) =>
|
|
66
|
+
window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
|
|
67
|
+
closers.push(() => agent.dispose());
|
|
68
|
+
return iframe;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function described(iframe: HTMLIFrameElement, selector: string) {
|
|
72
|
+
const handle = (await queryPreviewElements(iframe, selector))!.matches[0]!.handle;
|
|
73
|
+
return (await describePreviewElements(iframe, [handle]))![0]!;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function standIn(iframe: HTMLIFrameElement, selector: string): Promise<HTMLElement> {
|
|
77
|
+
return standInFor(iframe, await described(iframe, selector));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const settled = () => new Promise((resolve) => setTimeout(resolve, 20));
|
|
81
|
+
|
|
82
|
+
const RECT = { left: 10, top: 20, width: 100, height: 50, editScaleX: 0.5, editScaleY: 0.5 };
|
|
83
|
+
|
|
84
|
+
function dragMember(element: HTMLElement): ManualOffsetDragMember {
|
|
85
|
+
const selection = { element } as DomEditSelection;
|
|
86
|
+
const made = createManualOffsetDragMember({
|
|
87
|
+
key: "k",
|
|
88
|
+
selection,
|
|
89
|
+
element,
|
|
90
|
+
rect: RECT,
|
|
91
|
+
gesture: "drag",
|
|
92
|
+
});
|
|
93
|
+
if (!made.ok) throw new Error(made.reason);
|
|
94
|
+
return made.member;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const card = () => document.getElementById("card")!;
|
|
98
|
+
/** The test DOM keeps a calc() unsimplified, so each axis is read back as the sum of its px terms. */
|
|
99
|
+
const shown = (element: HTMLElement) =>
|
|
100
|
+
element.style
|
|
101
|
+
.getPropertyValue("translate")
|
|
102
|
+
.split(/ (?![^(]*\))/)
|
|
103
|
+
.map((axis) =>
|
|
104
|
+
[...axis.matchAll(/-?[\d.]+px/g)].reduce((sum, [px]) => sum + parseFloat(px), 0),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
describe("gesture drafts on an opaque frame", () => {
|
|
108
|
+
it("moves the frame's element with a drag, and puts it back when the drag is cancelled", async () => {
|
|
109
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
110
|
+
applyManualOffsetDragDraft(member, 10, 0);
|
|
111
|
+
applyManualOffsetDragDraft(member, 40, 20);
|
|
112
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
113
|
+
restoreManualOffsetDragMembers([member]);
|
|
114
|
+
await vi.waitFor(() =>
|
|
115
|
+
expect(card().getAttribute("style")).toBe("width: 200px; height: 100px;"),
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("holds a saved drag where it was dropped, and the next drag starts from there", async () => {
|
|
120
|
+
const element = await standIn(opaqueFrame(), "#card");
|
|
121
|
+
const first = dragMember(element);
|
|
122
|
+
applyManualOffsetDragCommit(first, 40, 20);
|
|
123
|
+
endManualOffsetDragMembers([first]);
|
|
124
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
125
|
+
const second = dragMember(element);
|
|
126
|
+
applyManualOffsetDragDraft(second, -10, 0);
|
|
127
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([60, 40]));
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("draws a drag that grabs the layer again before the last save settles from where that one left it", async () => {
|
|
131
|
+
const element = await standIn(opaqueFrame(), "#card");
|
|
132
|
+
const first = dragMember(element);
|
|
133
|
+
applyManualOffsetDragCommit(first, 40, 20);
|
|
134
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
135
|
+
const second = dragMember(element);
|
|
136
|
+
applyManualOffsetDragDraft(second, 10, 0);
|
|
137
|
+
endManualOffsetDragMembers([first]);
|
|
138
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([100, 40]));
|
|
139
|
+
applyManualOffsetDragCommit(second, 10, 0);
|
|
140
|
+
endManualOffsetDragMembers([second]);
|
|
141
|
+
await settled();
|
|
142
|
+
expect(shown(card())).toEqual([100, 40]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("puts a move back on the frame when an undo painted over it does not go through", async () => {
|
|
146
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
147
|
+
applyManualOffsetDragCommit(member, 40, 20);
|
|
148
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
149
|
+
const putBack = manualOffsetMoveRevert([member])();
|
|
150
|
+
await vi.waitFor(() =>
|
|
151
|
+
expect(card().getAttribute("style")).toBe("width: 200px; height: 100px;"),
|
|
152
|
+
);
|
|
153
|
+
putBack();
|
|
154
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 40]));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("moves a % translate from the px the frame renders it at, and keeps the author's value to reset to", async () => {
|
|
158
|
+
const iframe = opaqueFrame();
|
|
159
|
+
const options = { activeCompositionPath: "index.html", isMasterView: false };
|
|
160
|
+
const selection = await resolvePreviewSelection(
|
|
161
|
+
iframe,
|
|
162
|
+
await described(iframe, "#centred"),
|
|
163
|
+
options,
|
|
164
|
+
);
|
|
165
|
+
const member = dragMember(selection!.element);
|
|
166
|
+
expect(member.initialOffset).toEqual({ x: -100, y: -50 });
|
|
167
|
+
const offset = applyManualOffsetDragCommit(member, 40, 0);
|
|
168
|
+
expect(offset).toEqual({ x: -20, y: -50 });
|
|
169
|
+
expect(writePlainMove(selection!.element, offset).slice(0, 2)).toEqual([
|
|
170
|
+
{ type: "inline-style", property: "translate", value: "-20px -50px" },
|
|
171
|
+
{ type: "attribute", property: STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR, value: "-50% -50%" },
|
|
172
|
+
]);
|
|
173
|
+
const centred = document.getElementById("centred")!;
|
|
174
|
+
await vi.waitFor(() => expect(shown(centred)).toEqual([-20, -50]));
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("refuses to move a % translate it has no rendered px for, rather than drop it", async () => {
|
|
178
|
+
const element = await standIn(opaqueFrame(), "#centred");
|
|
179
|
+
const made = createManualOffsetDragMember({
|
|
180
|
+
key: "k",
|
|
181
|
+
selection: { element } as DomEditSelection,
|
|
182
|
+
element,
|
|
183
|
+
rect: RECT,
|
|
184
|
+
gesture: "drag",
|
|
185
|
+
});
|
|
186
|
+
expect(made.ok).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("keeps the translate the author had before Studio moved the layer as the one to reset to", async () => {
|
|
190
|
+
const element = await standIn(opaqueFrame(), "#moved");
|
|
191
|
+
applyManualOffsetDragCommit(dragMember(element), 40, 0);
|
|
192
|
+
expect(element.getAttribute(STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR)).toBe("");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("starts a drag on a freshly loaded frame from the translate its element has", async () => {
|
|
196
|
+
const member = dragMember(await standIn(opaqueFrame(), "#chip"));
|
|
197
|
+
expect(member.initialOffset).toEqual({ x: 30, y: 0 });
|
|
198
|
+
expect(applyManualOffsetDragCommit(member, 40, 0)).toEqual({ x: 110, y: 0 });
|
|
199
|
+
const chip = document.getElementById("chip")!;
|
|
200
|
+
await vi.waitFor(() => expect(shown(chip)).toEqual([110, 0]));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("sends one request at a time and draws only the latest of the moves that waited", async () => {
|
|
204
|
+
const member = dragMember(await standIn(opaqueFrame(), "#card"));
|
|
205
|
+
vi.mocked(previewRequest).mockClear();
|
|
206
|
+
for (let dx = 2; dx <= 40; dx += 2) applyManualOffsetDragDraft(member, dx, 0);
|
|
207
|
+
await vi.waitFor(() => expect(shown(card())).toEqual([80, 0]));
|
|
208
|
+
expect(draftRequests()).toHaveLength(2);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("grows a resized box about its centre without measuring the frame, and draws it there", async () => {
|
|
212
|
+
const iframe = opaqueFrame();
|
|
213
|
+
const element = await standIn(iframe, "#card");
|
|
214
|
+
const g = {
|
|
215
|
+
...{ originLeft: 10, originTop: 20, originWidth: 100, originHeight: 50 },
|
|
216
|
+
...{ actualWidth: 200, actualHeight: 100, editScaleX: 0.5, editScaleY: 0.5 },
|
|
217
|
+
actualRotation: 0,
|
|
218
|
+
pathOffsetMember: dragMember(element),
|
|
219
|
+
} as GestureState;
|
|
220
|
+
const size = { width: 300, height: 140 };
|
|
221
|
+
const rect = resolveResizeDraftRect(g, element, null, iframe, () => null, {
|
|
222
|
+
wanted: size,
|
|
223
|
+
written: size,
|
|
224
|
+
});
|
|
225
|
+
expect(rect).toMatchObject({ left: -15, top: 10, width: 150, height: 70, angle: 0 });
|
|
226
|
+
expect(g.lastResizeAnchor).toEqual({ dx: -25, dy: -10 });
|
|
227
|
+
await vi.waitFor(() => expect(card().style.width).toBe("300px"));
|
|
228
|
+
expect(card().style.height).toBe("140px");
|
|
229
|
+
expect(shown(card())).toEqual([-50, -20]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("keeps a saved resize with no offset member on the frame when a later restore runs", async () => {
|
|
233
|
+
const iframe = opaqueFrame();
|
|
234
|
+
const element = await standIn(iframe, "#card");
|
|
235
|
+
const before = captureStudioBoxSize(element);
|
|
236
|
+
const token = beginStudioManualEditGesture(element, "resize");
|
|
237
|
+
const g = {
|
|
238
|
+
...{ originLeft: 10, originTop: 20, originWidth: 100, originHeight: 50 },
|
|
239
|
+
...{ actualWidth: 200, actualHeight: 100, editScaleX: 0.5, editScaleY: 0.5 },
|
|
240
|
+
actualRotation: 0,
|
|
241
|
+
} as GestureState;
|
|
242
|
+
const size = { width: 300, height: 140 };
|
|
243
|
+
resolveResizeDraftRect(g, element, null, iframe, () => null, { wanted: size, written: size });
|
|
244
|
+
await vi.waitFor(() => expect(card().style.width).toBe("300px"));
|
|
245
|
+
endResizeWithoutMember(element, token);
|
|
246
|
+
restoreStudioBoxSize(element, before);
|
|
247
|
+
await settled();
|
|
248
|
+
expect(card().style.width).toBe("300px");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("leaves elements of a frame Studio scripts to the gesture alone", async () => {
|
|
252
|
+
vi.mocked(previewRequest).mockClear();
|
|
253
|
+
const member = dragMember(document.getElementById("chip")!);
|
|
254
|
+
applyManualOffsetDragDraft(member, 40, 20);
|
|
255
|
+
restoreManualOffsetDragMembers([member]);
|
|
256
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
257
|
+
expect(draftRequests()).toHaveLength(0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("reports a timeline rebind it cannot reach, so the caller syncs the preview another way", () => {
|
|
261
|
+
expect(applySoftReloadFinalization(opaqueFrame(), 1)).toBe(false);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A gesture on a preview frame Studio cannot script edits the selection's stand-in element. What the
|
|
3
|
+
* gesture draws on the stand-in is drawn on the frame's own element too, over the frame's channel,
|
|
4
|
+
* so the preview follows the pointer. One request is in flight per frame; later draws wait and merge.
|
|
5
|
+
*/
|
|
6
|
+
import { MAX_DESCRIBE_HANDLES, type EditingDraft } from "@hyperframes/core/runtime/editing-port";
|
|
7
|
+
import { previewRequest } from "../../player/lib/previewChannel";
|
|
8
|
+
|
|
9
|
+
type Drawn = {
|
|
10
|
+
translate: { x: number; y: number } | null;
|
|
11
|
+
size: { width: number; height: number } | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
interface Queue {
|
|
15
|
+
ops: EditingDraft[];
|
|
16
|
+
drawn: Map<string, Drawn>;
|
|
17
|
+
sending: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const standIns = new WeakMap<Element, { iframe: HTMLIFrameElement; handle: string }>();
|
|
21
|
+
const queues = new WeakMap<HTMLIFrameElement, Queue>();
|
|
22
|
+
|
|
23
|
+
export function registerPreviewStandIn(
|
|
24
|
+
standIn: Element,
|
|
25
|
+
iframe: HTMLIFrameElement,
|
|
26
|
+
handle: string,
|
|
27
|
+
): void {
|
|
28
|
+
standIns.set(standIn, { iframe, handle });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function queueFor(iframe: HTMLIFrameElement): Queue {
|
|
32
|
+
let queue = queues.get(iframe);
|
|
33
|
+
if (!queue) {
|
|
34
|
+
queue = { ops: [], drawn: new Map(), sending: false };
|
|
35
|
+
queues.set(iframe, queue);
|
|
36
|
+
}
|
|
37
|
+
return queue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function send(iframe: HTMLIFrameElement, queue: Queue): void {
|
|
41
|
+
if (queue.sending || !queue.ops.length) return;
|
|
42
|
+
const drafts = queue.ops.splice(0, MAX_DESCRIBE_HANDLES);
|
|
43
|
+
queue.sending = true;
|
|
44
|
+
void previewRequest(iframe, "draft", { drafts }).finally(() => {
|
|
45
|
+
queue.sending = false;
|
|
46
|
+
send(iframe, queue);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Draws `part` of the stand-in's draft on its frame element; any other element is left alone. */
|
|
51
|
+
export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
|
|
52
|
+
const target = standIns.get(element);
|
|
53
|
+
if (!target) return;
|
|
54
|
+
const queue = queueFor(target.iframe);
|
|
55
|
+
const drawn = { translate: null, size: null, ...queue.drawn.get(target.handle), ...part };
|
|
56
|
+
queue.drawn.set(target.handle, drawn);
|
|
57
|
+
const op = { handle: target.handle, ...drawn };
|
|
58
|
+
const last = queue.ops.map((queued) => queued.handle).lastIndexOf(target.handle);
|
|
59
|
+
const waiting = queue.ops[last];
|
|
60
|
+
if (waiting && !("end" in waiting)) queue.ops[last] = op;
|
|
61
|
+
else queue.ops.push(op);
|
|
62
|
+
send(target.iframe, queue);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Ends the stand-in's draft on its frame: "keep" once saved or regrabbed mid-save, else "restore". */
|
|
66
|
+
export function endPreviewDraft(element: Element, end: "keep" | "restore"): void {
|
|
67
|
+
const target = standIns.get(element);
|
|
68
|
+
if (!target) return;
|
|
69
|
+
const queue = queueFor(target.iframe);
|
|
70
|
+
if (!queue.drawn.delete(target.handle)) return;
|
|
71
|
+
queue.ops.push({ handle: target.handle, end });
|
|
72
|
+
send(target.iframe, queue);
|
|
73
|
+
}
|
|
@@ -21,6 +21,17 @@ import { giveUpOnPreviewChange, isPreviewChanging } from "../../player/previewRe
|
|
|
21
21
|
import { setStudioWaitingPressCancel } from "../../utils/studioPendingEdits";
|
|
22
22
|
import { playheadMoment } from "../../hooks/editMoment";
|
|
23
23
|
import type { EditMoment } from "./manualEditsTypes";
|
|
24
|
+
import { previewFrameOpaque } from "../../player/lib/previewChannel";
|
|
25
|
+
import { findPreviewElementForSelection } from "./previewSelection";
|
|
26
|
+
import { measurePreviewRects } from "./previewOverlayRects";
|
|
27
|
+
|
|
28
|
+
type Shown = { selection: DomEditSelection; rect: OverlayRect };
|
|
29
|
+
type ShownStart = (
|
|
30
|
+
e: React.PointerEvent<HTMLElement>,
|
|
31
|
+
at: EditMoment,
|
|
32
|
+
waited: boolean,
|
|
33
|
+
overChannel?: Shown[],
|
|
34
|
+
) => boolean;
|
|
24
35
|
|
|
25
36
|
const MAX_SETTLED_WAIT_FRAMES = 60;
|
|
26
37
|
export const PRESS_WAIT_MAX_MS = 10_000;
|
|
@@ -128,12 +139,33 @@ export function createPreviewGestureStarts(
|
|
|
128
139
|
};
|
|
129
140
|
};
|
|
130
141
|
|
|
142
|
+
/** An opaque frame's `selections` on the page it shows now, read over its channel; null when any is gone. */
|
|
143
|
+
const shownOverChannel = async (
|
|
144
|
+
iframe: HTMLIFrameElement,
|
|
145
|
+
selections: DomEditSelection[],
|
|
146
|
+
): Promise<Shown[] | null> => {
|
|
147
|
+
const overlay = opts.overlayRef.current;
|
|
148
|
+
const path = opts.activeCompositionPathRef.current;
|
|
149
|
+
const found = await Promise.all(
|
|
150
|
+
selections.map((selection) => findPreviewElementForSelection(iframe, selection, path)),
|
|
151
|
+
);
|
|
152
|
+
const handles = found.map((element) => element?.handle);
|
|
153
|
+
if (!overlay || !handles.length || !handles.every(Boolean)) return null;
|
|
154
|
+
const rects = await measurePreviewRects(iframe, overlay, handles as string[]);
|
|
155
|
+
const shown = selections.flatMap((selection, i) => {
|
|
156
|
+
const rect = rects?.[i];
|
|
157
|
+
return rect ? [{ selection: { ...selection, handle: handles[i] }, rect }] : [];
|
|
158
|
+
});
|
|
159
|
+
return shown.length === selections.length ? shown : null;
|
|
160
|
+
};
|
|
161
|
+
|
|
131
162
|
// A press on the page a reload is replacing would edit it by its old rules: it starts on the new page instead.
|
|
132
163
|
const startOnShownPreview = (
|
|
133
164
|
e: React.PointerEvent<HTMLElement>,
|
|
134
165
|
pressed: () => HTMLElement[],
|
|
135
|
-
start:
|
|
166
|
+
start: ShownStart,
|
|
136
167
|
draw: WaitingPressState["draw"] = null,
|
|
168
|
+
targets: () => DomEditSelection[] = () => [],
|
|
137
169
|
): boolean => {
|
|
138
170
|
const at = playheadMoment();
|
|
139
171
|
if (!isPreviewChanging() && !opts.waitingPressRef.current) return start(e, at, false);
|
|
@@ -170,8 +202,25 @@ export function createPreviewGestureStarts(
|
|
|
170
202
|
settledFrames = settled ? settledFrames + 1 : 0;
|
|
171
203
|
return elements.length;
|
|
172
204
|
};
|
|
205
|
+
// An opaque frame's pressed elements are found over its channel, once the preview settles.
|
|
206
|
+
const iframe = opts.iframeRef.current;
|
|
207
|
+
const channel = iframe && previewFrameOpaque(iframe) ? iframe : null;
|
|
208
|
+
let overChannel: Shown[] | null | undefined;
|
|
209
|
+
let asked = false;
|
|
210
|
+
const channelStep = (frame: HTMLIFrameElement): "wait" | "lost" | "run" => {
|
|
211
|
+
const overdue = performance.now() - since > PRESS_WAIT_MAX_MS;
|
|
212
|
+
if (overdue) giveUpOnPreviewChange();
|
|
213
|
+
if (isPreviewChanging() && !overdue) return "wait";
|
|
214
|
+
if (!asked) {
|
|
215
|
+
asked = true;
|
|
216
|
+
void shownOverChannel(frame, targets()).then((shown) => (overChannel = shown));
|
|
217
|
+
}
|
|
218
|
+
if (overChannel === undefined) return "wait";
|
|
219
|
+
return overChannel ? "run" : "lost";
|
|
220
|
+
};
|
|
173
221
|
const waitStep = (): "wait" | "lost" | "run" => {
|
|
174
222
|
if (press.after && !press.after.ended) return "wait";
|
|
223
|
+
if (channel) return channelStep(channel);
|
|
175
224
|
const found = countFrame();
|
|
176
225
|
// A change that never ends (a save with no reply) would hold every press: run on the page shown.
|
|
177
226
|
const overdue = performance.now() - since > PRESS_WAIT_MAX_MS;
|
|
@@ -188,8 +237,8 @@ export function createPreviewGestureStarts(
|
|
|
188
237
|
return;
|
|
189
238
|
}
|
|
190
239
|
endWaitingPress(press);
|
|
191
|
-
|
|
192
|
-
|
|
240
|
+
const pointerDown = down as unknown as React.PointerEvent<HTMLElement>;
|
|
241
|
+
if (step === "lost" || !start(pointerDown, at, true, overChannel ?? undefined)) return;
|
|
193
242
|
if (press.moved) moveActiveGesture(press.moved);
|
|
194
243
|
if (press.released) releaseActiveGesture(press.released);
|
|
195
244
|
};
|
|
@@ -208,6 +257,11 @@ export function createPreviewGestureStarts(
|
|
|
208
257
|
const iframe = opts.iframeRef.current;
|
|
209
258
|
return overlay && iframe && orientedGroupAwareOverlayRect(overlay, iframe, element);
|
|
210
259
|
};
|
|
260
|
+
const shownOnPage = (target: DomEditSelection | null): Shown | null => {
|
|
261
|
+
const selection = target && shownSelection(target);
|
|
262
|
+
const rect = selection && shownRect(selection.element);
|
|
263
|
+
return selection && rect ? { selection, rect } : null;
|
|
264
|
+
};
|
|
211
265
|
|
|
212
266
|
const startGroupDrag = (e: React.PointerEvent<HTMLElement>) => {
|
|
213
267
|
const items = opts.groupOverlayItemsRef.current;
|
|
@@ -218,8 +272,16 @@ export function createPreviewGestureStarts(
|
|
|
218
272
|
const selection = shownSelection(item.selection);
|
|
219
273
|
return selection ? [selection.element] : [];
|
|
220
274
|
}),
|
|
221
|
-
(pressed, at, waited) => {
|
|
275
|
+
(pressed, at, waited, overChannel) => {
|
|
222
276
|
if (!waited) return _startGroupDrag(pressed, opts, at);
|
|
277
|
+
if (overChannel) {
|
|
278
|
+
const measured = items.map((item, i) => ({ ...item, ...overChannel[i]! }));
|
|
279
|
+
return _startGroupDrag(
|
|
280
|
+
pressed,
|
|
281
|
+
{ ...opts, groupOverlayItemsRef: { current: measured } },
|
|
282
|
+
at,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
223
285
|
const overlay = opts.overlayRef.current;
|
|
224
286
|
const iframe = opts.iframeRef.current;
|
|
225
287
|
if (!overlay || !iframe) return false;
|
|
@@ -243,6 +305,7 @@ export function createPreviewGestureStarts(
|
|
|
243
305
|
opts.setGroupOverlayItems(
|
|
244
306
|
items.map((item) => ({ ...item, rect: shiftedOverlayRect(item.rect, dx, dy) })),
|
|
245
307
|
),
|
|
308
|
+
() => items.map((item) => item.selection),
|
|
246
309
|
);
|
|
247
310
|
};
|
|
248
311
|
const startGesture = (
|
|
@@ -270,15 +333,14 @@ export function createPreviewGestureStarts(
|
|
|
270
333
|
const shown = target && shownSelection(target);
|
|
271
334
|
return shown ? [shown.element] : [];
|
|
272
335
|
},
|
|
273
|
-
(pressed, at, waited) => {
|
|
336
|
+
(pressed, at, waited, overChannel) => {
|
|
274
337
|
if (!waited) return _startGesture(kind, pressed, opts, { ...options, at });
|
|
275
|
-
const
|
|
276
|
-
const rect = selection && shownRect(selection.element);
|
|
338
|
+
const shown = overChannel ? overChannel[0] : shownOnPage(target);
|
|
277
339
|
return (
|
|
278
|
-
!!
|
|
340
|
+
!!shown &&
|
|
279
341
|
_startGesture(kind, pressed, opts, {
|
|
280
|
-
selection,
|
|
281
|
-
rect,
|
|
342
|
+
selection: shown.selection,
|
|
343
|
+
rect: shown.rect,
|
|
282
344
|
resizeHandle: options?.resizeHandle,
|
|
283
345
|
resizeCorner: options?.resizeCorner,
|
|
284
346
|
at,
|
|
@@ -286,6 +348,7 @@ export function createPreviewGestureStarts(
|
|
|
286
348
|
);
|
|
287
349
|
},
|
|
288
350
|
draw,
|
|
351
|
+
() => (target ? [target] : []),
|
|
289
352
|
);
|
|
290
353
|
};
|
|
291
354
|
|