@hyperframes/studio 0.9.41 → 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.
Files changed (54) hide show
  1. package/dist/assets/{codec-B_mpbc4w.js → codec-CKqLGYc6.js} +1 -1
  2. package/dist/assets/{hyperframes-player-Dvi3I_mv.js → hyperframes-player-DoEYqOIa.js} +1 -1
  3. package/dist/assets/{index-B5ExwsOp.js → index-DLyQ91Ek.js} +160 -160
  4. package/dist/assets/{index-BrFm9Y5k.js → index-s9iqoWnr.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-MHQ57yZ6.js → pausedVideoDecoder-B-ReRg6i.js} +1 -1
  6. package/dist/index.d.ts +9 -1
  7. package/dist/index.html +1 -1
  8. package/dist/index.js +1057 -721
  9. package/package.json +10 -10
  10. package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
  11. package/src/components/editor/LayersPanel.tsx +41 -10
  12. package/src/components/editor/domEditInlineText.ts +1 -16
  13. package/src/components/editor/domEditingTypes.ts +7 -1
  14. package/src/components/editor/gestureUndoRevert.ts +18 -1
  15. package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
  16. package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
  17. package/src/components/editor/layerKind.ts +2 -19
  18. package/src/components/editor/layersPanelSort.ts +2 -2
  19. package/src/components/editor/manualEdits.ts +1 -0
  20. package/src/components/editor/manualEditsSnapshot.ts +10 -0
  21. package/src/components/editor/manualOffsetDrag.test.ts +1 -1
  22. package/src/components/editor/manualOffsetDrag.ts +7 -1
  23. package/src/components/editor/marqueeCommit.ts +47 -4
  24. package/src/components/editor/plainTranslate.ts +35 -16
  25. package/src/components/editor/previewDrafts.test.ts +263 -0
  26. package/src/components/editor/previewDrafts.ts +73 -0
  27. package/src/components/editor/previewLayers.test.ts +308 -0
  28. package/src/components/editor/previewLayers.ts +164 -0
  29. package/src/components/editor/previewOverlayRects.ts +27 -0
  30. package/src/components/editor/previewPointerTargets.test.ts +68 -1
  31. package/src/components/editor/previewSelection.ts +55 -11
  32. package/src/components/editor/resizeDraft.ts +38 -0
  33. package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
  34. package/src/components/editor/useLayerDrag.ts +1 -1
  35. package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
  36. package/src/hooks/domEditDeleteMembers.test.ts +7 -0
  37. package/src/hooks/domSelectionTimelineMirror.ts +3 -1
  38. package/src/hooks/timelineTimingSync.ts +4 -1
  39. package/src/hooks/useDomSelection.ts +2 -0
  40. package/src/hooks/useEditorSave.test.tsx +22 -1
  41. package/src/hooks/useEditorSave.ts +3 -1
  42. package/src/hooks/usePreviewInteraction.ts +4 -1
  43. package/src/hooks/useProjectFileWriter.ts +7 -3
  44. package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
  45. package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
  46. package/src/player/lib/layerOrdering.ts +2 -11
  47. package/src/player/lib/previewElements.ts +31 -0
  48. package/src/test-setup.ts +2 -0
  49. package/src/utils/gsapSoftReload.ts +1 -1
  50. package/src/utils/studioFileHistory.ts +1 -1
  51. package/src/utils/studioHelpers.ts +27 -4
  52. package/src/utils/studioSaveDiagnostics.test.ts +34 -0
  53. package/src/utils/studioSaveDiagnostics.ts +35 -6
  54. package/src/utils/timelineCompositionInsert.ts +3 -1
@@ -40,8 +40,8 @@ export function sortLayersByZIndex(layers: DomEditLayerItem[]): DomEditLayerItem
40
40
  }
41
41
 
42
42
  chunks.sort((a, b) => {
43
- const zA = getElementZIndex(a.root.element);
44
- const zB = getElementZIndex(b.root.element);
43
+ const zA = a.root.preview?.zIndex ?? getElementZIndex(a.root.element);
44
+ const zB = b.root.preview?.zIndex ?? getElementZIndex(b.root.element);
45
45
  if (zA !== zB) return zB - zA;
46
46
  return b.domIndex - a.domIndex;
47
47
  });
@@ -41,6 +41,7 @@ export {
41
41
  restoreStudioBoxSize,
42
42
  restoreStudioRotation,
43
43
  restoreStudioPathOffset,
44
+ restoreStudioPathOffsetStyles,
44
45
  clearStudioPathOffset,
45
46
  clearStudioRotation,
46
47
  clearStudioBoxSize,
@@ -36,6 +36,7 @@ import {
36
36
  STUDIO_ORIGINAL_ROTATION_TRANSFORM_ORIGIN_ATTR,
37
37
  STUDIO_ROTATION_DRAFT_ATTR,
38
38
  } from "./manualEditsTypes";
39
+ import { endPreviewDraft } from "./previewDrafts";
39
40
  import type {
40
41
  StudioBoxSizeSnapshot,
41
42
  StudioRotationSnapshot,
@@ -117,6 +118,7 @@ function restoreStyleProperty(element: HTMLElement, property: string, value: str
117
118
  }
118
119
 
119
120
  export function restoreStudioBoxSize(element: HTMLElement, previous: StudioBoxSizeSnapshot): void {
121
+ endPreviewDraft(element, "restore");
120
122
  forgetStudioBoxSizeDraftBase(element);
121
123
  restoreStyleProperty(element, "width", previous.width);
122
124
  restoreStyleProperty(element, "height", previous.height);
@@ -176,6 +178,14 @@ export function restoreStudioRotation(
176
178
  export function restoreStudioPathOffset(
177
179
  element: HTMLElement,
178
180
  previous: StudioPathOffsetSnapshot,
181
+ ): void {
182
+ endPreviewDraft(element, "restore");
183
+ restoreStudioPathOffsetStyles(element, previous);
184
+ }
185
+
186
+ export function restoreStudioPathOffsetStyles(
187
+ element: HTMLElement,
188
+ previous: StudioPathOffsetSnapshot,
179
189
  ): void {
180
190
  if (previous.translate) element.style.setProperty("translate", previous.translate);
181
191
  else element.style.removeProperty("translate");
@@ -128,7 +128,7 @@ describe("measureManualOffsetDragScreenToOffsetMatrix", () => {
128
128
  });
129
129
 
130
130
  it("measures movement in parent viewport pixels when the element is inside a scaled iframe", () => {
131
- const window = new Window();
131
+ const window = new Window({ settings: { enableIframePageLoading: true } });
132
132
  const iframe = window.document.createElement("iframe");
133
133
  window.document.body.append(iframe);
134
134
  const iframeWindow = iframe.contentWindow;
@@ -10,12 +10,14 @@ import {
10
10
  isStudioManualEditGestureCurrent,
11
11
  readAppliedStudioPathOffset,
12
12
  restoreStudioPathOffset,
13
+ restoreStudioPathOffsetStyles,
13
14
  type StudioPathOffsetSnapshot,
14
15
  } from "./manualEdits";
15
16
  import type { EditMoment } from "./manualEditsTypes";
16
17
  import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition";
17
18
  import { editsPlainCss } from "../../hooks/gsapRuntimeKeyframes";
18
19
  import { readTranslatePx, UNREADABLE_TRANSLATE, writeTranslatePx } from "./plainTranslate";
20
+ import { drawPreviewDraft, endPreviewDraft } from "./previewDrafts";
19
21
 
20
22
  interface OffsetDragGsap {
21
23
  set: (el: Element, vars: Record<string, number | string>) => void;
@@ -289,7 +291,7 @@ export function measureManualOffsetDragScreenToOffsetMatrix(
289
291
 
290
292
  return { ok: true, matrix: screenToOffset };
291
293
  } finally {
292
- restoreStudioPathOffset(element, snapshot);
294
+ restoreStudioPathOffsetStyles(element, snapshot);
293
295
  }
294
296
  }
295
297
 
@@ -367,6 +369,7 @@ export function createManualOffsetDragMember(input: {
367
369
  scaleY: input.rect.editScaleY,
368
370
  plainTranslate,
369
371
  });
372
+ endPreviewDraft(input.element, "keep");
370
373
  if (!measured.ok) {
371
374
  // GSAP transforms can defeat the probe: approximate with the preview scale. The commit
372
375
  // reads GSAP's real position, so only the drag preview is imprecise.
@@ -426,6 +429,8 @@ function drawMemberOffset(
426
429
  offset: { x: number; y: number },
427
430
  commit: boolean,
428
431
  ): void {
432
+ const { x, y } = member.initialOffset;
433
+ drawPreviewDraft(member.element, { translate: { x: offset.x - x, y: offset.y - y } });
429
434
  if (member.plainTranslate) writeTranslatePx(member.element, offset);
430
435
  else if (applyOffsetDragDraftViaGsap(member.element, offset, member.baseGsap)) return;
431
436
  else if (commit) applyStudioPathOffset(member.element, offset);
@@ -537,6 +542,7 @@ export function endManualOffsetDragMembers(members: ManualOffsetDragMember[]): v
537
542
  for (const member of members) {
538
543
  if (!isStudioManualEditGestureCurrent(member.element, member.gestureToken)) continue;
539
544
  endStudioManualEditGesture(member.element, member.gestureToken);
545
+ endPreviewDraft(member.element, "keep");
540
546
  member.element.removeAttribute("data-hf-drag-initial-offset-x");
541
547
  member.element.removeAttribute("data-hf-drag-initial-offset-y");
542
548
  member.element.removeAttribute("data-hf-drag-gsap-base-x");
@@ -16,6 +16,12 @@ import {
16
16
  } from "../../utils/marqueeGeometry";
17
17
  import { toVisibleOverlayRect } from "./domEditOverlayGeometry";
18
18
  import { movesGesture } from "./domEditOverlayGestures";
19
+ import { previewFrameOpaque } from "../../player/lib/previewChannel";
20
+ import {
21
+ collectPreviewMarqueeCandidates,
22
+ resolvePreviewMarqueeHits,
23
+ type PreviewMarqueeHit,
24
+ } from "./previewLayers";
19
25
 
20
26
  interface MarqueeState {
21
27
  startX: number;
@@ -42,6 +48,7 @@ const MARQUEE_THRESHOLD_PX = 4;
42
48
  interface MarqueeHit {
43
49
  element: HTMLElement;
44
50
  rect: Rect;
51
+ description?: PreviewMarqueeHit["description"];
45
52
  }
46
53
 
47
54
  /**
@@ -118,6 +125,25 @@ async function resolveDomEditSelections(
118
125
  return hits;
119
126
  }
120
127
 
128
+ /** The band's candidates: an opaque frame's are measured over its channel, once per band. */
129
+ function bandCandidates(
130
+ iframe: HTMLIFrameElement,
131
+ overlay: HTMLDivElement,
132
+ acp: string,
133
+ measured: { cached: MarqueeHit[] | null; measuring: Promise<MarqueeHit[]> | null },
134
+ ): MarqueeHit[] | Promise<MarqueeHit[]> {
135
+ if (!previewFrameOpaque(iframe))
136
+ return currentMarqueeCandidates(measured.cached, iframe, overlay, acp);
137
+ return measured.measuring ?? collectPreviewMarqueeCandidates(iframe, overlay);
138
+ }
139
+
140
+ /** Studio's edit selections for the hits; an opaque frame's are read from their descriptions. */
141
+ function resolveMarqueeHits(iframe: HTMLIFrameElement, hits: MarqueeHit[], acp: string) {
142
+ const elements = hits.map((hit) => hit.element);
143
+ if (!previewFrameOpaque(iframe)) return resolveDomEditSelections(elements, acp);
144
+ return resolvePreviewMarqueeHits(iframe, hits as PreviewMarqueeHit[], acp);
145
+ }
146
+
121
147
  export interface MarqueeGesturesDeps<T = DomEditSelection> {
122
148
  iframeRef: React.RefObject<HTMLIFrameElement | null>;
123
149
  overlayRef: React.RefObject<HTMLDivElement | null>;
@@ -176,6 +202,8 @@ export function useMarqueeGestures<T>(deps: MarqueeGesturesDeps<T>): MarqueeGest
176
202
  const [candidateRects, setCandidateRects] = useState<Rect[]>([]);
177
203
  // Measured once when the drag passes the threshold and reused until it ends.
178
204
  const candidatesRef = useRef<MarqueeHit[] | null>(null);
205
+ const opaqueCandidatesRef = useRef<Promise<MarqueeHit[]> | null>(null);
206
+ const bandRef = useRef<{ rect: Rect; mode: MarqueeSelectionMode } | null>(null);
179
207
 
180
208
  const commitMarquee = useCallback(
181
209
  async (rect: Rect, mode: MarqueeSelectionMode) => {
@@ -183,12 +211,12 @@ export function useMarqueeGestures<T>(deps: MarqueeGesturesDeps<T>): MarqueeGest
183
211
  const overlay = deps.overlayRef.current;
184
212
  if (!iframe || !overlay || !deps.onMarqueeSelectRef.current) return;
185
213
  const acp = deps.activeCompositionPathRef.current ?? "index.html";
186
- const candidates = currentMarqueeCandidates(candidatesRef.current, iframe, overlay, acp);
187
- const elements = hitsWithin(rect, candidates).map((hit) => hit.element);
214
+ const measured = { cached: candidatesRef.current, measuring: opaqueCandidatesRef.current };
215
+ const hits = hitsWithin(rect, await bandCandidates(iframe, overlay, acp, measured));
188
216
  const resolveHits = deps.resolveHits;
189
217
  const picks = resolveHits
190
- ? await resolveHits(elements)
191
- : ((await resolveDomEditSelections(elements, acp)) as T[]);
218
+ ? await resolveHits(hits.map((hit) => hit.element))
219
+ : ((await resolveMarqueeHits(iframe, hits, acp)) as T[]);
192
220
  const applySelection = deps.onMarqueeSelectRef.current;
193
221
  if (!applySelection) return;
194
222
  const result = applySelection(picks, mode);
@@ -208,6 +236,8 @@ export function useMarqueeGestures<T>(deps: MarqueeGesturesDeps<T>): MarqueeGest
208
236
  setMarqueeRect(null);
209
237
  setCandidateRects([]);
210
238
  candidatesRef.current = null;
239
+ opaqueCandidatesRef.current = null;
240
+ bandRef.current = null;
211
241
  }, []);
212
242
 
213
243
  const begin = useCallback(
@@ -261,6 +291,18 @@ export function useMarqueeGestures<T>(deps: MarqueeGesturesDeps<T>): MarqueeGest
261
291
  const overlay = deps.overlayRef.current;
262
292
  if (!iframe || !overlay) return;
263
293
  const acp = deps.activeCompositionPathRef.current ?? "index.html";
294
+ bandRef.current = { rect, mode };
295
+ if (!candidatesRef.current && previewFrameOpaque(iframe)) {
296
+ if (opaqueCandidatesRef.current) return;
297
+ const measuring = collectPreviewMarqueeCandidates(iframe, overlay);
298
+ opaqueCandidatesRef.current = measuring;
299
+ void measuring.then((hits) => {
300
+ if (opaqueCandidatesRef.current !== measuring || !bandRef.current) return;
301
+ candidatesRef.current = hits;
302
+ showCandidates(bandRef.current.rect, bandRef.current.mode);
303
+ });
304
+ return;
305
+ }
264
306
  candidatesRef.current ??= collectMarqueeCandidates(iframe, overlay, acp);
265
307
  let hits = hitsWithin(rect, candidatesRef.current);
266
308
  if (mode === "subtract") hits = hits.filter((h) => isSelectedElement(h.element));
@@ -284,6 +326,7 @@ export function useMarqueeGestures<T>(deps: MarqueeGesturesDeps<T>): MarqueeGest
284
326
  if (Math.hypot(dx, dy) < MARQUEE_THRESHOLD_PX) return;
285
327
  m.pastThreshold = true;
286
328
  candidatesRef.current = null;
329
+ opaqueCandidatesRef.current = null;
287
330
  }
288
331
  const rect: Rect = {
289
332
  left: Math.min(m.startX, m.currentX),
@@ -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
- cs: CSSStyleDeclaration,
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) - (cs.boxSizing === "border-box" ? pad : 0);
84
- return CONTENT_BOXES.has(cs.getPropertyValue("transform-box")) ? content : content + pad;
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
- /** The element's CSS `translate` in px, as it renders now. */
88
- // ponytail: a 3-value translate loses its z on the next move; keep z when a fixture needs it.
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
- cs ? referenceBox(cs, size, a, b) : 0;
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
+ }