@hyperframes/studio 0.6.59 → 0.6.61

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.
@@ -0,0 +1,138 @@
1
+ import { useCallback, useMemo, useRef, useState, type DragEvent } from "react";
2
+ import {
3
+ STUDIO_INSPECTOR_PANELS_ENABLED,
4
+ STUDIO_MOTION_PANEL_ENABLED,
5
+ } from "../components/editor/manualEditingAvailability";
6
+ import { readStudioMotionFromElement } from "../components/editor/studioMotion";
7
+ import type { StudioContextValue } from "../contexts/StudioContext";
8
+ import type { DomEditSelection } from "../components/editor/domEditing";
9
+
10
+ interface StudioContextInput {
11
+ projectId: string;
12
+ activeCompPath: string | null;
13
+ setActiveCompPath: (path: string | null) => void;
14
+ showToast: (message: string, tone?: "error" | "info") => void;
15
+ previewIframeRef: React.MutableRefObject<HTMLIFrameElement | null>;
16
+ captionEditMode: boolean;
17
+ compositionLoading: boolean;
18
+ refreshKey: number;
19
+ setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
20
+ currentTime: number;
21
+ timelineElements: StudioContextValue["timelineElements"];
22
+ isPlaying: boolean;
23
+ editHistory: { canUndo: boolean; canRedo: boolean; undoLabel: string; redoLabel: string };
24
+ handleUndo: StudioContextValue["handleUndo"];
25
+ handleRedo: StudioContextValue["handleRedo"];
26
+ renderQueue: {
27
+ jobs: unknown[];
28
+ isRendering: boolean;
29
+ deleteRender: (id: string) => void;
30
+ clearCompleted: () => void;
31
+ startRender: (options: unknown) => Promise<void>;
32
+ };
33
+ compositionDimensions: { width: number; height: number } | null;
34
+ waitForPendingDomEditSaves: () => Promise<void>;
35
+ handlePreviewIframeRef: (iframe: HTMLIFrameElement | null) => void;
36
+ refreshPreviewDocumentVersion: () => void;
37
+ timelineVisible: boolean;
38
+ toggleTimelineVisibility: () => void;
39
+ }
40
+
41
+ // fallow-ignore-next-line complexity
42
+ export function buildStudioContextValue(input: StudioContextInput): StudioContextValue {
43
+ return {
44
+ projectId: input.projectId,
45
+ activeCompPath: input.activeCompPath,
46
+ setActiveCompPath: input.setActiveCompPath,
47
+ showToast: input.showToast,
48
+ previewIframeRef: input.previewIframeRef,
49
+ captionEditMode: input.captionEditMode,
50
+ compositionLoading: input.compositionLoading,
51
+ refreshKey: input.refreshKey,
52
+ setRefreshKey: input.setRefreshKey,
53
+ currentTime: input.currentTime,
54
+ timelineElements: input.timelineElements,
55
+ isPlaying: input.isPlaying,
56
+ editHistory: input.editHistory,
57
+ handleUndo: input.handleUndo,
58
+ handleRedo: input.handleRedo,
59
+ renderQueue: input.renderQueue,
60
+ compositionDimensions: input.compositionDimensions,
61
+ waitForPendingDomEditSaves: input.waitForPendingDomEditSaves,
62
+ handlePreviewIframeRef: input.handlePreviewIframeRef,
63
+ refreshPreviewDocumentVersion: input.refreshPreviewDocumentVersion,
64
+ timelineVisible: input.timelineVisible,
65
+ toggleTimelineVisibility: input.toggleTimelineVisibility,
66
+ };
67
+ }
68
+
69
+ export interface InspectorState {
70
+ selectedStudioMotion: ReturnType<typeof readStudioMotionFromElement> | null;
71
+ layersPanelActive: boolean;
72
+ designPanelActive: boolean;
73
+ motionPanelActive: boolean;
74
+ inspectorPanelActive: boolean;
75
+ inspectorButtonActive: boolean;
76
+ shouldShowSelectedDomBounds: boolean;
77
+ }
78
+
79
+ export function useInspectorState(
80
+ rightPanelTab: string,
81
+ rightCollapsed: boolean,
82
+ isPlaying: boolean,
83
+ domEditSelection: DomEditSelection | null,
84
+ ): InspectorState {
85
+ // fallow-ignore-next-line complexity
86
+ return useMemo(() => {
87
+ const selectedStudioMotion =
88
+ STUDIO_INSPECTOR_PANELS_ENABLED && domEditSelection
89
+ ? readStudioMotionFromElement(domEditSelection.element)
90
+ : null;
91
+ const layersPanelActive = STUDIO_INSPECTOR_PANELS_ENABLED && rightPanelTab === "layers";
92
+ const designPanelActive = STUDIO_INSPECTOR_PANELS_ENABLED && rightPanelTab === "design";
93
+ const motionPanelActive =
94
+ STUDIO_INSPECTOR_PANELS_ENABLED && STUDIO_MOTION_PANEL_ENABLED && rightPanelTab === "motion";
95
+ const inspectorPanelActive = layersPanelActive || designPanelActive || motionPanelActive;
96
+ return {
97
+ selectedStudioMotion,
98
+ layersPanelActive,
99
+ designPanelActive,
100
+ motionPanelActive,
101
+ inspectorPanelActive,
102
+ inspectorButtonActive:
103
+ STUDIO_INSPECTOR_PANELS_ENABLED && !rightCollapsed && inspectorPanelActive,
104
+ shouldShowSelectedDomBounds: inspectorPanelActive && !rightCollapsed && !isPlaying,
105
+ };
106
+ }, [rightPanelTab, rightCollapsed, isPlaying, domEditSelection]);
107
+ }
108
+
109
+ // fallow-ignore-next-line complexity
110
+ export function useDragOverlay(onImportFiles: (files: FileList) => void) {
111
+ const [active, setActive] = useState(false);
112
+ const counterRef = useRef(0);
113
+ const onDragOver = useCallback((e: DragEvent) => {
114
+ if (!e.dataTransfer.types.includes("Files")) return;
115
+ e.preventDefault();
116
+ }, []);
117
+ const onDragEnter = useCallback((e: DragEvent) => {
118
+ if (!e.dataTransfer.types.includes("Files")) return;
119
+ e.preventDefault();
120
+ counterRef.current++;
121
+ setActive(true);
122
+ }, []);
123
+ const onDragLeave = useCallback(() => {
124
+ counterRef.current--;
125
+ if (counterRef.current === 0) setActive(false);
126
+ }, []);
127
+ const onDrop = useCallback(
128
+ (e: DragEvent) => {
129
+ counterRef.current = 0;
130
+ setActive(false);
131
+ if (e.defaultPrevented) return;
132
+ e.preventDefault();
133
+ if (e.dataTransfer.files.length) onImportFiles(e.dataTransfer.files);
134
+ },
135
+ [onImportFiles],
136
+ );
137
+ return { active, onDragOver, onDragEnter, onDragLeave, onDrop };
138
+ }
@@ -0,0 +1,104 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { describe, it, expect, vi } from "vitest";
4
+ import { applySoftReload } from "./gsapSoftReload";
5
+
6
+ const SCRIPT_TEXT = `
7
+ window.__timelines = window.__timelines || {};
8
+ const tl = gsap.timeline({ paused: true });
9
+ tl.to("#box", { opacity: 0.8 });
10
+ window.__timelines["root"] = tl;
11
+ `;
12
+
13
+ function buildMockIframe(overrides: Record<string, unknown> = {}) {
14
+ const scriptEl = document.createElement("script");
15
+ scriptEl.textContent =
16
+ 'const tl = gsap.timeline({ paused: true }); tl.to("#box", { opacity: 0.5 });';
17
+ const container = document.createElement("div");
18
+ container.appendChild(scriptEl);
19
+
20
+ const mockTimeline = { kill: vi.fn(), pause: vi.fn() };
21
+ const contentWindow = {
22
+ gsap: { timeline: vi.fn() },
23
+ __hfForceTimelineRebind: vi.fn(),
24
+ __timelines: { root: mockTimeline } as Record<string, typeof mockTimeline>,
25
+ __player: { getTime: () => 2.0, seek: vi.fn() },
26
+ __hfStudioManualEditsApply: vi.fn(),
27
+ __hfSuppressSceneMutations: undefined as undefined | (<T>(fn: () => T) => T),
28
+ ...overrides,
29
+ };
30
+
31
+ const contentDocument = {
32
+ querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [scriptEl] : []),
33
+ createElement: (tag: string) => document.createElement(tag),
34
+ body: container,
35
+ };
36
+
37
+ return {
38
+ iframe: { contentWindow, contentDocument } as unknown as HTMLIFrameElement,
39
+ contentWindow,
40
+ mockTimeline,
41
+ };
42
+ }
43
+
44
+ describe("applySoftReload", () => {
45
+ it("returns false when iframe is null", () => {
46
+ expect(applySoftReload(null, SCRIPT_TEXT)).toBe(false);
47
+ });
48
+
49
+ it("returns false when scriptText is empty", () => {
50
+ const { iframe } = buildMockIframe();
51
+ expect(applySoftReload(iframe, "")).toBe(false);
52
+ });
53
+
54
+ it("returns false when gsap is not on iframe window", () => {
55
+ const { iframe } = buildMockIframe({ gsap: undefined });
56
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
57
+ });
58
+
59
+ it("returns false when __hfForceTimelineRebind is missing", () => {
60
+ const { iframe } = buildMockIframe({ __hfForceTimelineRebind: undefined });
61
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
62
+ });
63
+
64
+ it("kills existing timelines, rebinds, and re-seeks on success", () => {
65
+ const { iframe, contentWindow, mockTimeline } = buildMockIframe();
66
+ const result = applySoftReload(iframe, SCRIPT_TEXT);
67
+ expect(result).toBe(true);
68
+ expect(mockTimeline.kill).toHaveBeenCalled();
69
+ expect(contentWindow.__hfForceTimelineRebind).toHaveBeenCalled();
70
+ expect(contentWindow.__player.seek).toHaveBeenCalledWith(2.0);
71
+ expect(contentWindow.__hfStudioManualEditsApply).toHaveBeenCalled();
72
+ });
73
+
74
+ it("wraps execution in __hfSuppressSceneMutations when available", () => {
75
+ let suppressionCalled = false;
76
+ const { iframe } = buildMockIframe({
77
+ __hfSuppressSceneMutations: <T>(fn: () => T): T => {
78
+ suppressionCalled = true;
79
+ return fn();
80
+ },
81
+ });
82
+ const result = applySoftReload(iframe, SCRIPT_TEXT);
83
+ expect(result).toBe(true);
84
+ expect(suppressionCalled).toBe(true);
85
+ });
86
+
87
+ it("returns false when multiple GSAP scripts exist (ambiguous)", () => {
88
+ const script1 = document.createElement("script");
89
+ script1.textContent = "const tl = gsap.timeline({ paused: true });";
90
+ const script2 = document.createElement("script");
91
+ script2.textContent = 'tl.to("#other", { x: 10 });';
92
+ const container = document.createElement("div");
93
+ container.appendChild(script1);
94
+ container.appendChild(script2);
95
+
96
+ const { iframe } = buildMockIframe();
97
+ (iframe as unknown as { contentDocument: unknown }).contentDocument = {
98
+ querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [script1, script2] : []),
99
+ createElement: (tag: string) => document.createElement(tag),
100
+ body: container,
101
+ };
102
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
103
+ });
104
+ });
@@ -0,0 +1,89 @@
1
+ type IframeWindow = Window & {
2
+ __timelines?: Record<string, { kill?: () => void; pause?: () => void }>;
3
+ __player?: { getTime?: () => number; seek?: (t: number) => void };
4
+ __hfForceTimelineRebind?: () => void;
5
+ __hfSuppressSceneMutations?: <T>(fn: () => T) => T;
6
+ __hfStudioManualEditsApply?: () => void;
7
+ gsap?: { timeline?: (...args: unknown[]) => unknown };
8
+ };
9
+
10
+ function isGsapScript(text: string): boolean {
11
+ return (
12
+ text.includes("gsap.timeline") ||
13
+ text.includes("__timelines") ||
14
+ text.includes(".to(") ||
15
+ text.includes(".set(")
16
+ );
17
+ }
18
+
19
+ function findGsapScriptElements(doc: Document): HTMLScriptElement[] {
20
+ const results: HTMLScriptElement[] = [];
21
+ const scripts = doc.querySelectorAll<HTMLScriptElement>("script:not([src])");
22
+ for (const script of scripts) {
23
+ if (isGsapScript(script.textContent || "")) results.push(script);
24
+ }
25
+ return results;
26
+ }
27
+
28
+ /**
29
+ * Replace the GSAP script in the live iframe without reloading. This preserves
30
+ * the WebGL context and shader transition cache.
31
+ *
32
+ * Scoped to root-document GSAP scripts only — scripts inside `<template>`
33
+ * elements (sub-compositions) are not visible to `querySelectorAll` and will
34
+ * fall back to a full iframe reload.
35
+ *
36
+ * Returns false (triggering a full reload fallback) when:
37
+ * - The iframe or GSAP runtime isn't available
38
+ * - Multiple GSAP scripts are found (ambiguous which to replace)
39
+ * - No matching GSAP script element exists in the live DOM
40
+ */
41
+ export function applySoftReload(iframe: HTMLIFrameElement | null, scriptText: string): boolean {
42
+ if (!iframe || !scriptText) return false;
43
+
44
+ const win = iframe.contentWindow as IframeWindow | null;
45
+ const doc = iframe.contentDocument;
46
+ if (!win || !doc) return false;
47
+ if (!win.gsap || !win.__hfForceTimelineRebind) return false;
48
+
49
+ const gsapScripts = findGsapScriptElements(doc);
50
+ if (gsapScripts.length !== 1) return false;
51
+ const oldScriptEl = gsapScripts[0]!;
52
+
53
+ const currentTime = win.__player?.getTime?.() ?? 0;
54
+
55
+ const doReload = () => {
56
+ const timelines = win.__timelines;
57
+ if (timelines) {
58
+ for (const key of Object.keys(timelines)) {
59
+ try {
60
+ timelines[key]?.kill?.();
61
+ } catch {}
62
+ delete timelines[key];
63
+ }
64
+ }
65
+
66
+ oldScriptEl.remove();
67
+ const newScript = doc.createElement("script");
68
+ // IIFE prevents const/let redeclaration errors across consecutive edits.
69
+ // Top-level declarations are scoped to the IIFE; window.* assignments
70
+ // (e.g. window.__timelines["root"] = tl) still reach the global scope.
71
+ newScript.textContent = `(function(){${scriptText}\n})();`;
72
+ doc.body.appendChild(newScript);
73
+
74
+ win.__hfForceTimelineRebind?.();
75
+ win.__player?.seek?.(currentTime);
76
+ win.__hfStudioManualEditsApply?.();
77
+ };
78
+
79
+ try {
80
+ if (win.__hfSuppressSceneMutations) {
81
+ win.__hfSuppressSceneMutations(doReload);
82
+ } else {
83
+ doReload();
84
+ }
85
+ return true;
86
+ } catch {
87
+ return false;
88
+ }
89
+ }
@@ -3,6 +3,7 @@ import { resolveVisualDomEditSelectionTarget } from "../components/editor/domEdi
3
3
  import {
4
4
  getDomLayerPatchTarget,
5
5
  isElementComputedVisible,
6
+ resolveAllVisualDomEditTargets,
6
7
  } from "../components/editor/domEditingElement";
7
8
  import { getEventTargetElement } from "./studioHelpers";
8
9
 
@@ -58,6 +59,7 @@ function removePointerEventsOverride(style: HTMLStyleElement | null): void {
58
59
  }
59
60
  }
60
61
 
62
+ // fallow-ignore-next-line complexity
61
63
  export function getPreviewTargetFromPointer(
62
64
  iframe: HTMLIFrameElement,
63
65
  clientX: number,
@@ -98,6 +100,42 @@ export function getPreviewTargetFromPointer(
98
100
  }
99
101
  }
100
102
 
103
+ /** Returns all independently-selectable elements at the pointer (topmost first). */
104
+ export function getAllPreviewTargetsFromPointer(
105
+ iframe: HTMLIFrameElement,
106
+ clientX: number,
107
+ clientY: number,
108
+ activeCompositionPath: string | null,
109
+ ): HTMLElement[] {
110
+ let doc: Document | null = null;
111
+ let win: Window | null = null;
112
+ try {
113
+ doc = iframe.contentDocument;
114
+ win = iframe.contentWindow;
115
+ } catch {
116
+ return [];
117
+ }
118
+ if (!doc || !win) return [];
119
+
120
+ const localPointer = resolvePreviewLocalPointer(iframe, doc, win, clientX, clientY);
121
+ if (!localPointer) return [];
122
+
123
+ const overrideStyle = forcePointerEventsAuto(doc);
124
+ try {
125
+ if (typeof doc.elementsFromPoint === "function") {
126
+ return resolveAllVisualDomEditTargets(doc.elementsFromPoint(localPointer.x, localPointer.y), {
127
+ activeCompositionPath,
128
+ });
129
+ }
130
+ const fallback = getEventTargetElement(doc.elementFromPoint(localPointer.x, localPointer.y));
131
+ if (!fallback || !getDomLayerPatchTarget(fallback, activeCompositionPath)) return [];
132
+ if (!isElementComputedVisible(fallback)) return [];
133
+ return [fallback];
134
+ } finally {
135
+ removePointerEventsOverride(overrideStyle);
136
+ }
137
+ }
138
+
101
139
  function objectLike(value: unknown): object | null {
102
140
  return value && (typeof value === "object" || typeof value === "function") ? value : null;
103
141
  }