@hyperframes/core 0.9.67 → 0.9.69

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,88 @@
1
+ export interface SetPatchProps {
2
+ x?: number;
3
+ y?: number;
4
+ rotation?: number;
5
+ rotationX?: number;
6
+ rotationY?: number;
7
+ rotationZ?: number;
8
+ z?: number;
9
+ transformPerspective?: number;
10
+ scaleX?: number;
11
+ scaleY?: number;
12
+ scale?: number;
13
+ opacity?: number;
14
+ width?: number;
15
+ height?: number;
16
+ }
17
+ /** A single keyframe step's numeric channels (the GSAP array-keyframe form). */
18
+ export type KeyframeStep = Record<string, number>;
19
+ export type RuntimeTweenChange = {
20
+ kind: "set";
21
+ props: SetPatchProps;
22
+ } | {
23
+ kind: "keyframes";
24
+ keyframes: KeyframeStep[];
25
+ } | {
26
+ kind: "keyframe-rebuild";
27
+ pct: number;
28
+ props: KeyframeStep;
29
+ } | {
30
+ kind: "global-set";
31
+ props: SetPatchProps;
32
+ };
33
+ export interface PatchableTween {
34
+ targets?: () => Element[];
35
+ vars?: Record<string, unknown>;
36
+ duration?: () => number;
37
+ startTime?: () => number;
38
+ invalidate?: () => unknown;
39
+ kill?: () => void;
40
+ parent?: PatchableTimeline;
41
+ }
42
+ export interface PatchableTimeline {
43
+ getChildren?: (deep: boolean) => PatchableTween[];
44
+ time?: () => number;
45
+ to?: (targets: Element[], vars: Record<string, unknown>, position?: number) => unknown;
46
+ }
47
+ export interface PatchTarget {
48
+ timelines: Record<string, PatchableTimeline> | null | undefined;
49
+ document: Document | null | undefined;
50
+ gsap: {
51
+ set?: (target: Element, vars: Record<string, number>) => void;
52
+ } | null | undefined;
53
+ player: {
54
+ getTime?: () => number;
55
+ seek?: (t: number) => void;
56
+ } | null | undefined;
57
+ }
58
+ export declare function matchesElement(tween: PatchableTween, el: Element): boolean;
59
+ /** Whether a keyframe tween's steps carry one of `channels`, in the object, array or object-of-arrays form. */
60
+ export declare function keyframeVarsCarryChannel(vars: Record<string, unknown> | undefined, channels: string[]): boolean;
61
+ export interface ResolvedTween {
62
+ tween: PatchableTween;
63
+ timeline: PatchableTimeline;
64
+ }
65
+ /** The live tween on `selector`; `channels` keeps an {x,y} patch off a co-located rotation-only set. */
66
+ export declare function resolveTween(target: PatchTarget, selector: string, kind: "keyframe" | "set", compositionId?: string, channels?: string[]): ResolvedTween | null;
67
+ /** The element a base `gsap.set` was applied to, or null when it could not be applied. */
68
+ export declare function applyGlobalSet(target: PatchTarget, selector: string, props: SetPatchProps): Element | null;
69
+ /** `deferSeek`: a group commit renders once after its last patch, or queued members repaint stale. */
70
+ export declare function patchTween(target: PatchTarget, selector: string, change: Exclude<RuntimeTweenChange, {
71
+ kind: "global-set";
72
+ }>, compositionId?: string, deferSeek?: boolean): boolean;
73
+ /** A base value never renders, so a batch renders on its last tween patch, whatever follows it. */
74
+ export declare function lastTweenPatch(patches: ReadonlyArray<{
75
+ change: RuntimeTweenChange;
76
+ }>): number;
77
+ export type PatchTweensParams = {
78
+ patches: Array<{
79
+ selector: string;
80
+ change: RuntimeTweenChange;
81
+ }>;
82
+ seek: boolean;
83
+ };
84
+ export declare function parsePatchTweens(params: unknown): PatchTweensParams | null;
85
+ /** The frame patching its own timelines: every patch must land, and it renders once at the end. */
86
+ export declare function patchTweens({ patches, seek }: PatchTweensParams): {
87
+ patched: boolean;
88
+ };
@@ -0,0 +1,331 @@
1
+ // A value-only edit patches its one live GSAP tween in place; anything it cannot place
2
+ // confidently returns false and the caller reloads. Studio runs it on a frame it can reach,
3
+ // a sealed frame on itself through the editing port.
4
+ import { applyAuthoredInlineOpacity, readStampedAuthoredOpacity } from "../../colorGrading.js";
5
+ import { recordLiveSet } from "./softReload.js";
6
+ const SET_CHANNELS = [
7
+ "x",
8
+ "y",
9
+ "rotation",
10
+ "rotationX",
11
+ "rotationY",
12
+ "rotationZ",
13
+ "z",
14
+ "transformPerspective",
15
+ "scaleX",
16
+ "scaleY",
17
+ "scale",
18
+ "opacity",
19
+ ];
20
+ const PATCH_CHANNELS = new Set([...SET_CHANNELS, "width", "height"]);
21
+ export function matchesElement(tween, el) {
22
+ if (!tween.targets)
23
+ return false;
24
+ for (const t of tween.targets()) {
25
+ if (t === el || (el.id && t.id === el.id))
26
+ return true;
27
+ }
28
+ return false;
29
+ }
30
+ function varsCarryChannel(vars, channels) {
31
+ if (!vars)
32
+ return false;
33
+ return channels.some((ch) => Object.prototype.hasOwnProperty.call(vars, ch));
34
+ }
35
+ /** Whether a keyframe tween's steps carry one of `channels`, in the object, array or object-of-arrays form. */
36
+ export function keyframeVarsCarryChannel(vars, channels) {
37
+ const kf = vars?.keyframes;
38
+ if (!kf || typeof kf !== "object")
39
+ return false;
40
+ if (!Array.isArray(kf) && channels.some((ch) => Object.prototype.hasOwnProperty.call(kf, ch)))
41
+ return true;
42
+ const steps = Array.isArray(kf) ? kf : Object.values(kf);
43
+ return steps.some((step) => step != null &&
44
+ typeof step === "object" &&
45
+ channels.some((ch) => Object.prototype.hasOwnProperty.call(step, ch)));
46
+ }
47
+ const tweenDuration = (tween) => typeof tween.duration === "function" ? tween.duration() : 0;
48
+ const isKind = (tween, kind) => tweenDuration(tween) > 0 === (kind === "keyframe");
49
+ function playsNow(tween, timeline) {
50
+ if (typeof timeline.time !== "function")
51
+ return false;
52
+ const now = timeline.time();
53
+ const start = typeof tween.startTime === "function" ? tween.startTime() : 0;
54
+ return now >= start - 1e-3 && now <= start + tweenDuration(tween) + 1e-3;
55
+ }
56
+ const carriesChannels = (vars, kind, channels) => (kind === "set" ? varsCarryChannel(vars, channels) : keyframeVarsCarryChannel(vars, channels));
57
+ function liveTimelines(timelines, compositionId) {
58
+ const ids = compositionId ? [compositionId] : Object.keys(timelines);
59
+ return ids
60
+ .map((id) => timelines[id])
61
+ .filter((tl) => typeof tl?.getChildren === "function");
62
+ }
63
+ function candidatesFor(target, selector, kind, compositionId) {
64
+ const targetEl = target.timelines ? target.document?.querySelector(selector) : null;
65
+ if (!target.timelines || !targetEl)
66
+ return [];
67
+ const found = [];
68
+ for (const timeline of liveTimelines(target.timelines, compositionId))
69
+ for (const tween of timeline.getChildren(true))
70
+ if (tween.vars && matchesElement(tween, targetEl) && isKind(tween, kind))
71
+ found.push({ tween: tween, timeline });
72
+ return found;
73
+ }
74
+ /** The live tween on `selector`; `channels` keeps an {x,y} patch off a co-located rotation-only set. */
75
+ export function resolveTween(target, selector, kind, compositionId, channels) {
76
+ const found = candidatesFor(target, selector, kind, compositionId);
77
+ const preferred = channels && channels.length > 0
78
+ ? found.find(({ tween }) => carriesChannels(tween.vars, kind, channels))
79
+ : kind === "keyframe"
80
+ ? found.find(({ tween, timeline }) => playsNow(tween, timeline))
81
+ : undefined;
82
+ return preferred ?? found[0] ?? null;
83
+ }
84
+ function finiteNumericProps(props) {
85
+ const numeric = {};
86
+ for (const [k, v] of Object.entries(props)) {
87
+ if (typeof v !== "number" || !Number.isFinite(v))
88
+ return null;
89
+ numeric[k] = v;
90
+ }
91
+ return Object.keys(numeric).length > 0 ? numeric : null;
92
+ }
93
+ /** The element a base `gsap.set` was applied to, or null when it could not be applied. */
94
+ export function applyGlobalSet(target, selector, props) {
95
+ const el = target.document?.querySelector(selector) ?? null;
96
+ const numeric = finiteNumericProps(props);
97
+ if (typeof target.gsap?.set !== "function" || !el || !numeric)
98
+ return null;
99
+ target.gsap.set(el, numeric);
100
+ recordLiveSet(el, numeric);
101
+ return el;
102
+ }
103
+ function keyframesAreStatic(keyframes) {
104
+ if (keyframes.length === 0)
105
+ return false;
106
+ return keyframes.every((step) => !!step &&
107
+ typeof step === "object" &&
108
+ Object.values(step).every((v) => typeof v === "number" && Number.isFinite(v)));
109
+ }
110
+ function patchSet(tween, props) {
111
+ const vars = tween.vars;
112
+ if (!vars || "motionPath" in vars)
113
+ return false;
114
+ let touched = false;
115
+ for (const ch of SET_CHANNELS) {
116
+ const next = props[ch];
117
+ if (next === undefined)
118
+ continue;
119
+ if (typeof next !== "number" || !Number.isFinite(next))
120
+ return false;
121
+ // A string here is a computed GSAP expression ("+=100"); a number would drop it.
122
+ if (typeof vars[ch] === "string")
123
+ return false;
124
+ vars[ch] = next;
125
+ touched = true;
126
+ }
127
+ return touched;
128
+ }
129
+ function patchKeyframes(tween, keyframes) {
130
+ const vars = tween.vars;
131
+ if (!vars || "motionPath" in vars)
132
+ return false;
133
+ if (!Array.isArray(vars.keyframes) || !keyframesAreStatic(keyframes))
134
+ return false;
135
+ vars.keyframes = keyframes.map((step) => ({ ...step }));
136
+ return true;
137
+ }
138
+ function mergeKeyframeStep(map, pct, props) {
139
+ const next = {};
140
+ for (const [k, step] of Object.entries(map))
141
+ next[k] = { ...step };
142
+ let key = null;
143
+ for (const k of Object.keys(next)) {
144
+ const n = parseFloat(k);
145
+ if (Number.isFinite(n) && Math.abs(n - pct) < 0.05) {
146
+ key = k;
147
+ break;
148
+ }
149
+ }
150
+ key ??= `${pct}%`;
151
+ next[key] = { ...(next[key] ?? {}), ...props };
152
+ return next;
153
+ }
154
+ const replaceable = (tween) => !!tween.vars &&
155
+ typeof tween.startTime === "function" &&
156
+ typeof tween.kill === "function" &&
157
+ typeof tween.parent?.to === "function";
158
+ function keyframeObject(vars) {
159
+ const kf = vars.keyframes;
160
+ if ("motionPath" in vars || !kf || typeof kf !== "object" || Array.isArray(kf))
161
+ return null;
162
+ return kf;
163
+ }
164
+ const finiteStep = (props) => Object.values(props).every((v) => typeof v === "number" && Number.isFinite(v));
165
+ function rebuildKeyframeTween(tween, pct, props) {
166
+ if (!replaceable(tween) || !finiteStep(props))
167
+ return false;
168
+ const kf = keyframeObject(tween.vars);
169
+ const targets = tween.targets?.();
170
+ if (!kf || !targets || targets.length === 0)
171
+ return false;
172
+ const next = mergeKeyframeStep(kf, pct, props);
173
+ const position = tween.startTime();
174
+ tween.kill();
175
+ tween.parent.to(targets, { ...tween.vars, keyframes: next }, position);
176
+ return true;
177
+ }
178
+ function changeChannels(change) {
179
+ if (change.kind === "set")
180
+ return Object.keys(change.props).filter((k) => change.props[k] !== undefined);
181
+ if (change.kind === "keyframe-rebuild")
182
+ return Object.keys(change.props);
183
+ return undefined;
184
+ }
185
+ function changeTouchesOpacity(change) {
186
+ if (change.kind === "set" || change.kind === "global-set")
187
+ return change.props.opacity !== undefined;
188
+ if (change.kind === "keyframes")
189
+ return change.keyframes.some((step) => "opacity" in step);
190
+ return "opacity" in change.props;
191
+ }
192
+ /** A re-init captures the current inline opacity, which mid-grade is the engine's 0 hide. */
193
+ function restoreAuthoredOpacityForCapture(tween) {
194
+ for (const target of tween.targets?.() ?? []) {
195
+ const el = target;
196
+ if (!el?.style || typeof el.getAttribute !== "function")
197
+ continue;
198
+ const authored = readStampedAuthoredOpacity(el);
199
+ if (authored !== null)
200
+ applyAuthoredInlineOpacity(el.style, authored);
201
+ }
202
+ }
203
+ function applyChange(tween, change) {
204
+ if (change.kind === "set")
205
+ return patchSet(tween, change.props);
206
+ if (change.kind === "keyframes")
207
+ return patchKeyframes(tween, change.keyframes);
208
+ if (change.kind === "keyframe-rebuild")
209
+ return rebuildKeyframeTween(tween, change.pct, change.props);
210
+ return false;
211
+ }
212
+ function seekToCurrent(target, timeline) {
213
+ const player = target.player;
214
+ const currentTime = typeof player?.getTime === "function"
215
+ ? player.getTime()
216
+ : typeof timeline.time === "function"
217
+ ? timeline.time()
218
+ : 0;
219
+ player?.seek?.(Number.isFinite(currentTime) ? currentTime : 0);
220
+ }
221
+ /** `deferSeek`: a group commit renders once after its last patch, or queued members repaint stale. */
222
+ export function patchTween(target, selector, change, compositionId, deferSeek = false) {
223
+ try {
224
+ const kind = change.kind === "set" ? "set" : "keyframe";
225
+ const resolved = resolveTween(target, selector, kind, compositionId, changeChannels(change));
226
+ if (!resolved)
227
+ return false;
228
+ const { tween, timeline } = resolved;
229
+ if (changeTouchesOpacity(change))
230
+ restoreAuthoredOpacityForCapture(tween);
231
+ if (!applyChange(tween, change))
232
+ return false;
233
+ // Only the edited tween: a timeline-wide invalidate re-captures every from() against
234
+ // the grading engine's opacity-0 hide, and graded elements vanish.
235
+ if (change.kind !== "keyframe-rebuild")
236
+ tween.invalidate?.();
237
+ if (!deferSeek)
238
+ seekToCurrent(target, timeline);
239
+ return true;
240
+ }
241
+ catch {
242
+ return false;
243
+ }
244
+ }
245
+ const MAX_PATCHES = 64;
246
+ const MAX_KEYFRAME_STEPS = 64;
247
+ const MAX_SELECTOR_LENGTH = 2_000;
248
+ /** A base value never renders, so a batch renders on its last tween patch, whatever follows it. */
249
+ export function lastTweenPatch(patches) {
250
+ for (let i = patches.length - 1; i >= 0; i--)
251
+ if (patches[i].change.kind !== "global-set")
252
+ return i;
253
+ return -1;
254
+ }
255
+ const isRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
256
+ function numericChannels(value) {
257
+ if (!isRecord(value))
258
+ return null;
259
+ const entries = Object.entries(value);
260
+ const ok = entries.every(([k, v]) => PATCH_CHANNELS.has(k) && typeof v === "number" && Number.isFinite(v));
261
+ return ok && entries.length > 0 ? value : null;
262
+ }
263
+ const CHANGE_KEYS = {
264
+ set: "kind,props",
265
+ "global-set": "kind,props",
266
+ "keyframe-rebuild": "kind,pct,props",
267
+ keyframes: "keyframes,kind",
268
+ };
269
+ function parseKeyframes(steps) {
270
+ if (!Array.isArray(steps) || steps.length === 0 || steps.length > MAX_KEYFRAME_STEPS)
271
+ return null;
272
+ const parsed = steps.map(numericChannels);
273
+ return parsed.every(Boolean) ? { kind: "keyframes", keyframes: parsed } : null;
274
+ }
275
+ function parseRebuild(pct, value) {
276
+ const props = numericChannels(value);
277
+ const okPct = typeof pct === "number" && pct >= 0 && pct <= 100;
278
+ return props && okPct ? { kind: "keyframe-rebuild", pct, props } : null;
279
+ }
280
+ function parseChange(value) {
281
+ if (!isRecord(value) || typeof value.kind !== "string")
282
+ return null;
283
+ const kind = value.kind;
284
+ // An unknown kind, prototype names included, has no key list here, so it fails this match.
285
+ if (Object.keys(value).sort().join() !== CHANGE_KEYS[kind])
286
+ return null;
287
+ if (kind === "keyframes")
288
+ return parseKeyframes(value.keyframes);
289
+ if (kind === "keyframe-rebuild")
290
+ return parseRebuild(value.pct, value.props);
291
+ const props = numericChannels(value.props);
292
+ return props ? { kind, props } : null;
293
+ }
294
+ export function parsePatchTweens(params) {
295
+ if (!isRecord(params) || Object.keys(params).sort().join() !== "patches,seek")
296
+ return null;
297
+ const { patches, seek } = params;
298
+ if (typeof seek !== "boolean" || !Array.isArray(patches))
299
+ return null;
300
+ if (patches.length === 0 || patches.length > MAX_PATCHES)
301
+ return null;
302
+ const parsed = [];
303
+ for (const patch of patches) {
304
+ if (!isRecord(patch) || Object.keys(patch).sort().join() !== "change,selector")
305
+ return null;
306
+ const { selector } = patch;
307
+ const change = parseChange(patch.change);
308
+ const okSelector = typeof selector === "string" && selector.length > 0 && selector.length <= MAX_SELECTOR_LENGTH;
309
+ if (!okSelector || !change)
310
+ return null;
311
+ parsed.push({ selector, change });
312
+ }
313
+ return { patches: parsed, seek };
314
+ }
315
+ /** The frame patching its own timelines: every patch must land, and it renders once at the end. */
316
+ export function patchTweens({ patches, seek }) {
317
+ const win = window;
318
+ const target = {
319
+ timelines: win.__timelines,
320
+ document,
321
+ gsap: win.gsap,
322
+ player: win.__player,
323
+ };
324
+ const renderAt = lastTweenPatch(patches);
325
+ const missed = patches.find(({ selector, change }, index) => {
326
+ if (change.kind === "global-set")
327
+ return applyGlobalSet(target, selector, change.props) === null;
328
+ return !patchTween(target, selector, change, undefined, !(seek && index === renderAt));
329
+ });
330
+ return { patched: missed === undefined };
331
+ }
@@ -0,0 +1,83 @@
1
+ export type SoftReloadWindow = Window & {
2
+ __timelines?: Record<string, unknown>;
3
+ __player?: {
4
+ getTime?: () => number;
5
+ seek?: (t: number) => void;
6
+ isPlaying?: () => boolean;
7
+ play?: () => void;
8
+ };
9
+ __hfForceTimelineRebind?: () => void;
10
+ __hfSuppressSceneMutations?: <T>(fn: () => T) => T;
11
+ __hfStudioManualEditsApply?: () => void;
12
+ __hfMotionPathPluginLoading?: boolean;
13
+ gsap?: {
14
+ version?: string;
15
+ registerPlugin?: (...plugins: unknown[]) => unknown;
16
+ set?: (targets: Element | Element[], vars: Record<string, unknown>) => void;
17
+ };
18
+ MotionPathPlugin?: unknown;
19
+ };
20
+ /** What one standalone `gsap.set` in the outgoing script wrote: its selector and the CSS names. */
21
+ export type StandaloneSet = {
22
+ selector: string;
23
+ props: string[];
24
+ };
25
+ export type SoftReloadResult = "applied" | "verify-failed" | "cannot-soft-reload";
26
+ export interface SoftReloadInput {
27
+ script: string;
28
+ /** The time to seek the rebuilt timeline to; the player's own time when absent. */
29
+ time?: number;
30
+ /** What the outgoing script's standalone sets wrote, which no timeline child records. */
31
+ sets: readonly StandaloneSet[];
32
+ /** An element's authored style text: null when unknown, undefined when its file is not at hand. */
33
+ authoredStyle: (el: Element, file: string | null, ownFile: string | null) => string | null | undefined;
34
+ /** A sealed timing edit's saved timing, which a reachable preview's commit already patched live. */
35
+ timing?: SavedTiming;
36
+ }
37
+ /** The attributes the runtime derives clip windows and the film's length from. */
38
+ export declare const TIMING_ATTRIBUTES: Set<string>;
39
+ /** `[key, attributes]` per element of the saved file, keyed as `authoredKey` keys a live one. */
40
+ export type SavedTiming = Array<[string, Array<[string, string]>]>;
41
+ type TimingPlan = Array<[Element, Array<[string, string]>]>;
42
+ /** Each live element of `ownFile` with the saved timing it lacks; null when one is missing or two share a key. */
43
+ export declare function planSavedTiming(doc: Document, ownFile: string | null, timing: SavedTiming): TimingPlan | null;
44
+ export declare const applyTimingPlan: (plan: TimingPlan) => void;
45
+ /** The CSS names a tween's vars write; GSAP's own keys (ease, duration) are not CSS and restore as no-ops. */
46
+ export declare function tweenedProps(vars: unknown): string[];
47
+ export declare function elementTargets(tween: {
48
+ targets?: () => unknown[];
49
+ }): Element[];
50
+ export declare function authoringFile(el: Element): string | null;
51
+ /** Each element the re-run rebuilds, with the CSS names its killed tweens wrote. */
52
+ export declare function collectResetTargets(win: {
53
+ __timelines?: Record<string, unknown>;
54
+ }, doc: Document, targetKeys: string[], sets?: readonly StandaloneSet[]): Map<Element, Set<string>>;
55
+ export declare function compositionFile(doc: Document, targetKeys: string[]): string | null;
56
+ export declare function recordLiveSet(el: Element, vars: Record<string, unknown>): void;
57
+ export declare function findGsapScriptElements(doc: ParentNode): HTMLScriptElement[];
58
+ export declare function timelineKeys(scriptText: string): string[];
59
+ /** The script elements that register one of `keys` on `window.__timelines`. */
60
+ export declare function scriptsRegistering(scripts: readonly HTMLScriptElement[], keys: readonly string[]): HTMLScriptElement[];
61
+ export declare function restoreAuthoredStyle(style: CSSStyleDeclaration, authored: CSSStyleDeclaration | null, tweened: Set<string>): void;
62
+ export declare function finalizeSoftReload(win: SoftReloadWindow, currentTime: number): void;
63
+ export declare function previewMotionPathUrl(doc: Document, version?: string): string;
64
+ export declare function softReloadFrame(win: SoftReloadWindow, doc: Document, input: SoftReloadInput): SoftReloadResult | Promise<SoftReloadResult>;
65
+ export declare const MAX_SOFT_RELOAD_SCRIPT = 1000000;
66
+ export type SoftReloadParams = {
67
+ script: string;
68
+ time: number;
69
+ sets: StandaloneSet[];
70
+ /** `[key, style]`: key `hf:<data-hf-id>` or `#<id>`, style the element's authored style text. */
71
+ authored: Array<[string, string]>;
72
+ /** Empty unless a timing edit: the saved file's timing, applied before the re-run's rebind. */
73
+ timing: SavedTiming;
74
+ };
75
+ export declare function parseSavedTiming(value: unknown): SavedTiming | null;
76
+ export declare function parseSoftReload(params: unknown): SoftReloadParams | null;
77
+ /** The key Studio files an element's authored style under: its hf-id, else its id. */
78
+ export declare function authoredKey(el: Element): string | null;
79
+ /** The frame re-running its own script: only elements of the saved file have authored styles to use. */
80
+ export declare function softReload(params: SoftReloadParams): Promise<{
81
+ result: SoftReloadResult;
82
+ }>;
83
+ export {};