@hyperframes/core 0.9.68 → 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.
- package/dist/generated/runtime-inline.js +1 -1
- package/dist/hyperframe.runtime.iife.js +67 -66
- package/dist/runtime/editing/patchTween.js +2 -0
- package/dist/runtime/editing/softReload.d.ts +83 -0
- package/dist/runtime/editing/softReload.js +480 -0
- package/dist/runtime/editingPort.d.ts +8 -1
- package/dist/runtime/editingPort.js +3 -0
- package/package.json +8 -4
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// confidently returns false and the caller reloads. Studio runs it on a frame it can reach,
|
|
3
3
|
// a sealed frame on itself through the editing port.
|
|
4
4
|
import { applyAuthoredInlineOpacity, readStampedAuthoredOpacity } from "../../colorGrading.js";
|
|
5
|
+
import { recordLiveSet } from "./softReload.js";
|
|
5
6
|
const SET_CHANNELS = [
|
|
6
7
|
"x",
|
|
7
8
|
"y",
|
|
@@ -96,6 +97,7 @@ export function applyGlobalSet(target, selector, props) {
|
|
|
96
97
|
if (typeof target.gsap?.set !== "function" || !el || !numeric)
|
|
97
98
|
return null;
|
|
98
99
|
target.gsap.set(el, numeric);
|
|
100
|
+
recordLiveSet(el, numeric);
|
|
99
101
|
return el;
|
|
100
102
|
}
|
|
101
103
|
function keyframesAreStatic(keyframes) {
|
|
@@ -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 {};
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
// A GSAP script edit re-runs the edited composition's script in the live preview, without a reload.
|
|
2
|
+
// Studio runs it on a frame it can reach; a sealed frame runs it on itself through the editing port,
|
|
3
|
+
// with the script Studio saved and the authored styles Studio read from that file.
|
|
4
|
+
import { GSAP_BASE_URL_META, motionPathPluginUrl } from "../../gsapCdn.js";
|
|
5
|
+
import { applyAuthoredInlineOpacity, readStampedAuthoredOpacity } from "../../colorGrading.js";
|
|
6
|
+
import { escapeCssString } from "../editingElement.js";
|
|
7
|
+
import { applyAuthoredTiming } from "../../studioPreviewMark.js";
|
|
8
|
+
/** The attributes the runtime derives clip windows and the film's length from. */
|
|
9
|
+
export const TIMING_ATTRIBUTES = new Set([
|
|
10
|
+
"data-start",
|
|
11
|
+
"data-end",
|
|
12
|
+
"data-duration",
|
|
13
|
+
"data-track-index",
|
|
14
|
+
"data-media-start",
|
|
15
|
+
"data-playback-start",
|
|
16
|
+
"data-playback-rate",
|
|
17
|
+
]);
|
|
18
|
+
/** The live elements of `ownFile` by key; null when two share a key. */
|
|
19
|
+
function liveTimedElements(doc, ownFile) {
|
|
20
|
+
const live = new Map();
|
|
21
|
+
// The preview rewrites an inlined composition's own root, so the file's timing is not its own.
|
|
22
|
+
const rewritten = new Set();
|
|
23
|
+
for (const el of doc.querySelectorAll("[data-hf-id], [id]")) {
|
|
24
|
+
const key = authoringFile(el) === ownFile ? authoredKey(el) : null;
|
|
25
|
+
if (key === null)
|
|
26
|
+
continue;
|
|
27
|
+
if (el.hasAttribute("data-hf-inner-root"))
|
|
28
|
+
rewritten.add(key);
|
|
29
|
+
if (rewritten.has(key))
|
|
30
|
+
continue;
|
|
31
|
+
if (live.has(key))
|
|
32
|
+
return null;
|
|
33
|
+
live.set(key, el);
|
|
34
|
+
}
|
|
35
|
+
return { live, rewritten };
|
|
36
|
+
}
|
|
37
|
+
/** Each live element of `ownFile` with the saved timing it lacks; null when one is missing or two share a key. */
|
|
38
|
+
export function planSavedTiming(doc, ownFile, timing) {
|
|
39
|
+
const found = liveTimedElements(doc, ownFile);
|
|
40
|
+
if (!found)
|
|
41
|
+
return null;
|
|
42
|
+
const { live, rewritten } = found;
|
|
43
|
+
const plan = [];
|
|
44
|
+
for (const [key, attributes] of timing) {
|
|
45
|
+
const el = live.get(key);
|
|
46
|
+
if (!el && rewritten.has(key))
|
|
47
|
+
continue;
|
|
48
|
+
if (!el)
|
|
49
|
+
return null;
|
|
50
|
+
const changed = attributes.filter(([name, value]) => el.getAttribute(name) !== value);
|
|
51
|
+
if (changed.length > 0)
|
|
52
|
+
plan.push([el, changed]);
|
|
53
|
+
}
|
|
54
|
+
return plan;
|
|
55
|
+
}
|
|
56
|
+
export const applyTimingPlan = (plan) => {
|
|
57
|
+
for (const [el, attributes] of plan)
|
|
58
|
+
applyAuthoredTiming(el, attributes);
|
|
59
|
+
};
|
|
60
|
+
const NESTED_VARS = new Set(["css", "startAt", "keyframes"]);
|
|
61
|
+
/** The CSS names a tween's vars write; GSAP's own keys (ease, duration) are not CSS and restore as no-ops. */
|
|
62
|
+
export function tweenedProps(vars) {
|
|
63
|
+
if (!vars || typeof vars !== "object")
|
|
64
|
+
return [];
|
|
65
|
+
return Object.entries(vars).flatMap(([key, value]) => [
|
|
66
|
+
key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`),
|
|
67
|
+
...(NESTED_VARS.has(key) || /^\d|%$/.test(key) ? tweenedProps(value) : []),
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
export function elementTargets(tween) {
|
|
71
|
+
return (tween.targets?.() ?? []).filter((t) => Boolean(t?.style && t.nodeType));
|
|
72
|
+
}
|
|
73
|
+
// A nested composition's timeline sits inside the root one but only its own script rebuilds it.
|
|
74
|
+
function ownTweens(tl, nested) {
|
|
75
|
+
return (tl.getChildren?.(false) ?? []).flatMap((child) => {
|
|
76
|
+
if (nested.has(child))
|
|
77
|
+
return [];
|
|
78
|
+
return child.getChildren ? ownTweens(child, nested) : [child];
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
// The preview marks each inlined composition's host with its file; the host itself is the parent's.
|
|
82
|
+
export function authoringFile(el) {
|
|
83
|
+
return (el.parentElement?.closest("[data-composition-file]")?.getAttribute("data-composition-file") ??
|
|
84
|
+
null);
|
|
85
|
+
}
|
|
86
|
+
function addProps(targets, el, props) {
|
|
87
|
+
const seen = targets.get(el) ?? new Set();
|
|
88
|
+
for (const prop of props)
|
|
89
|
+
seen.add(prop);
|
|
90
|
+
targets.set(el, seen);
|
|
91
|
+
}
|
|
92
|
+
/** Each element the re-run rebuilds, with the CSS names its killed tweens wrote. */
|
|
93
|
+
export function collectResetTargets(win, doc, targetKeys, sets = []) {
|
|
94
|
+
const targets = new Map();
|
|
95
|
+
const timelines = (win.__timelines ?? {});
|
|
96
|
+
const others = new Set(Object.entries(timelines).flatMap(([key, tl]) => (targetKeys.includes(key) ? [] : [tl])));
|
|
97
|
+
for (const key of targetKeys) {
|
|
98
|
+
try {
|
|
99
|
+
for (const tween of ownTweens(timelines[key] ?? {}, others)) {
|
|
100
|
+
const props = tweenedProps(tween.vars);
|
|
101
|
+
for (const el of elementTargets(tween))
|
|
102
|
+
addProps(targets, el, props);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch { }
|
|
106
|
+
}
|
|
107
|
+
sweepHeldElements(doc, targetKeys, others, targets);
|
|
108
|
+
addLiveSetTargets(doc, targetKeys, targets);
|
|
109
|
+
for (const set of sets) {
|
|
110
|
+
try {
|
|
111
|
+
for (const el of doc.querySelectorAll(set.selector))
|
|
112
|
+
addProps(targets, el, set.props);
|
|
113
|
+
}
|
|
114
|
+
catch { }
|
|
115
|
+
}
|
|
116
|
+
return targets;
|
|
117
|
+
}
|
|
118
|
+
function tweenTargetsIn(timelines) {
|
|
119
|
+
const els = new Set();
|
|
120
|
+
for (const tl of timelines) {
|
|
121
|
+
try {
|
|
122
|
+
for (const tween of tl?.getChildren?.(true) ?? []) {
|
|
123
|
+
for (const el of elementTargets(tween))
|
|
124
|
+
els.add(el);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch { }
|
|
128
|
+
}
|
|
129
|
+
return els;
|
|
130
|
+
}
|
|
131
|
+
function compositionRoot(doc, key) {
|
|
132
|
+
return doc.querySelectorAll(`[data-composition-id="${escapeCssString(key)}"]`)[0];
|
|
133
|
+
}
|
|
134
|
+
export function compositionFile(doc, targetKeys) {
|
|
135
|
+
const root = targetKeys.map((key) => compositionRoot(doc, key)).find(Boolean);
|
|
136
|
+
return root?.closest("[data-composition-file]")?.getAttribute("data-composition-file") ?? null;
|
|
137
|
+
}
|
|
138
|
+
// A standalone gsap.set, or keyframes just removed, leaves GSAP state no timeline child shows.
|
|
139
|
+
// The nearest composition root says whose script set it; another timeline's targets stay its own.
|
|
140
|
+
function sweepHeldElements(doc, targetKeys, others, targets) {
|
|
141
|
+
const elsewhere = tweenTargetsIn(others);
|
|
142
|
+
const roots = new Set(targetKeys.map((k) => compositionRoot(doc, k)));
|
|
143
|
+
roots.delete(undefined);
|
|
144
|
+
for (const root of roots) {
|
|
145
|
+
for (const el of root ? [root, ...root.querySelectorAll("*")] : []) {
|
|
146
|
+
if (targets.has(el) || elsewhere.has(el) || !("_gsap" in el))
|
|
147
|
+
continue;
|
|
148
|
+
if (el === root || roots.has(el.parentElement?.closest("[data-composition-id]")))
|
|
149
|
+
targets.set(el, new Set());
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// What a live patch set on each element since its last reload; no script in the preview ran it.
|
|
154
|
+
const liveSets = new WeakMap();
|
|
155
|
+
export function recordLiveSet(el, vars) {
|
|
156
|
+
const byElement = liveSets.get(el.ownerDocument) ?? new Map();
|
|
157
|
+
liveSets.set(el.ownerDocument, byElement);
|
|
158
|
+
addProps(byElement, el, tweenedProps(vars));
|
|
159
|
+
}
|
|
160
|
+
function addLiveSetTargets(doc, targetKeys, targets) {
|
|
161
|
+
const roots = new Set(targetKeys.map((k) => compositionRoot(doc, k)));
|
|
162
|
+
for (const [el, props] of liveSets.get(doc) ?? []) {
|
|
163
|
+
if (!roots.has(el) && !roots.has(el.parentElement?.closest("[data-composition-id]")))
|
|
164
|
+
continue;
|
|
165
|
+
addProps(targets, el, props);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function forgetLiveSets(reset) {
|
|
169
|
+
for (const el of reset.keys())
|
|
170
|
+
liveSets.get(el.ownerDocument)?.delete(el);
|
|
171
|
+
}
|
|
172
|
+
function isGsapScript(text) {
|
|
173
|
+
return (text.includes("gsap.timeline") ||
|
|
174
|
+
text.includes("__timelines") ||
|
|
175
|
+
text.includes(".to(") ||
|
|
176
|
+
text.includes(".set("));
|
|
177
|
+
}
|
|
178
|
+
export function findGsapScriptElements(doc) {
|
|
179
|
+
const results = [];
|
|
180
|
+
for (const script of doc.querySelectorAll("script:not([src])")) {
|
|
181
|
+
if (isGsapScript(script.textContent || ""))
|
|
182
|
+
results.push(script);
|
|
183
|
+
}
|
|
184
|
+
return results;
|
|
185
|
+
}
|
|
186
|
+
export function timelineKeys(scriptText) {
|
|
187
|
+
return [...scriptText.matchAll(/__timelines\s*\[\s*["'`]([^"'`]+)["'`]\s*\]/g)]
|
|
188
|
+
.map((m) => m[1])
|
|
189
|
+
.filter((key) => key !== "__proxied");
|
|
190
|
+
}
|
|
191
|
+
/** The script elements that register one of `keys` on `window.__timelines`. */
|
|
192
|
+
export function scriptsRegistering(scripts, keys) {
|
|
193
|
+
return scripts.filter((script) => {
|
|
194
|
+
const text = script.textContent || "";
|
|
195
|
+
return keys.some((key) => text.includes(`__timelines["${key}"]`) || text.includes(`__timelines['${key}']`));
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
// The script's own keys, not "any key": a scoped re-run repopulates only its composition, and the
|
|
199
|
+
// global map can read empty for a moment right after it.
|
|
200
|
+
function verifyTimelinesPopulated(win, targetKeys) {
|
|
201
|
+
const timelines = win.__timelines;
|
|
202
|
+
if (!timelines)
|
|
203
|
+
return false;
|
|
204
|
+
return targetKeys.every((key) => timelines[key] != null);
|
|
205
|
+
}
|
|
206
|
+
// GSAP masks a folded CSS translate/rotate/scale with `none`, and a tween writes what it animates
|
|
207
|
+
// inline; a fresh load has only what the file authors. Opacity has its own restore.
|
|
208
|
+
export function restoreAuthoredStyle(style, authored, tweened) {
|
|
209
|
+
style.transform = authored?.transform ?? "";
|
|
210
|
+
if (!authored)
|
|
211
|
+
return;
|
|
212
|
+
for (const prop of ["translate", "rotate", "scale", ...tweened]) {
|
|
213
|
+
if (prop === "opacity")
|
|
214
|
+
continue;
|
|
215
|
+
style.setProperty(prop, authored.getPropertyValue(prop), authored.getPropertyPriority(prop));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Seek before the rebind: the rebind renders the new timeline at the runtime's scrub position, and a
|
|
219
|
+
// seek after it can be a no-op. A seek pauses the player, so an edit made while playing resumes it.
|
|
220
|
+
export function finalizeSoftReload(win, currentTime) {
|
|
221
|
+
const wasPlaying = win.__player?.isPlaying?.() === true;
|
|
222
|
+
win.__player?.seek?.(currentTime);
|
|
223
|
+
win.__hfForceTimelineRebind?.();
|
|
224
|
+
win.__hfStudioManualEditsApply?.();
|
|
225
|
+
if (wasPlaying)
|
|
226
|
+
win.__player?.play?.();
|
|
227
|
+
}
|
|
228
|
+
export function previewMotionPathUrl(doc, version) {
|
|
229
|
+
const baseUrl = doc.head.querySelector(`meta[name="${GSAP_BASE_URL_META}"]`)?.content;
|
|
230
|
+
return motionPathPluginUrl(version, baseUrl);
|
|
231
|
+
}
|
|
232
|
+
function runSuppressed(win, reload) {
|
|
233
|
+
try {
|
|
234
|
+
if (win.__hfSuppressSceneMutations)
|
|
235
|
+
win.__hfSuppressSceneMutations(reload);
|
|
236
|
+
else
|
|
237
|
+
reload();
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
console.error("[hyperframes] GSAP soft reload threw; a full reload follows", error);
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function killTimelines(win, targetKeys) {
|
|
246
|
+
const timelines = win.__timelines;
|
|
247
|
+
if (!timelines)
|
|
248
|
+
return;
|
|
249
|
+
for (const key of targetKeys) {
|
|
250
|
+
const tl = timelines[key];
|
|
251
|
+
if (!tl)
|
|
252
|
+
continue;
|
|
253
|
+
try {
|
|
254
|
+
// kill() keeps the children, and the finalize seek renders this timeline until the rebind swaps it.
|
|
255
|
+
tl.clear?.();
|
|
256
|
+
tl.kill?.();
|
|
257
|
+
}
|
|
258
|
+
catch { }
|
|
259
|
+
delete timelines[key];
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// clearProps flushes GSAP's transform cache, so from() tweens read no stale end values, but it
|
|
263
|
+
// also clears the element's own style: save, clear, restore, then put back what the file authors.
|
|
264
|
+
function resetTargetStyles(win, targets, authored) {
|
|
265
|
+
const allTargets = [...targets.keys()];
|
|
266
|
+
if (allTargets.length === 0 || !win.gsap?.set)
|
|
267
|
+
return;
|
|
268
|
+
// Another realm's nodes fail instanceof HTMLElement, so style access is duck-typed.
|
|
269
|
+
const saved = allTargets.map((el) => [el, el.style.cssText]);
|
|
270
|
+
win.gsap.set(allTargets, { clearProps: "all" });
|
|
271
|
+
for (const [el, css] of saved) {
|
|
272
|
+
const s = el.style;
|
|
273
|
+
s.cssText = css;
|
|
274
|
+
const source = authored.get(el) ?? null;
|
|
275
|
+
restoreAuthoredStyle(s, source, targets.get(el));
|
|
276
|
+
// The saved cssText holds a mid-tween opacity, which a re-initialized tween would keep as its bound.
|
|
277
|
+
const opacity = source ? source.opacity : readStampedAuthoredOpacity(el);
|
|
278
|
+
if (opacity !== null)
|
|
279
|
+
applyAuthoredInlineOpacity(s, opacity);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Runs `ready` once the MotionPath plugin is in the frame, or `failed` when it cannot load.
|
|
283
|
+
function whenMotionPathLoaded(win, doc, ready, failed) {
|
|
284
|
+
// A load already in flight is the one to wait for; a second plugin script re-flashes the frame.
|
|
285
|
+
if (win.__hfMotionPathPluginLoading) {
|
|
286
|
+
const started = Date.now();
|
|
287
|
+
const poll = win.setInterval(() => {
|
|
288
|
+
if (win.MotionPathPlugin) {
|
|
289
|
+
win.clearInterval(poll);
|
|
290
|
+
ready();
|
|
291
|
+
}
|
|
292
|
+
else if (!win.__hfMotionPathPluginLoading || Date.now() - started > 10000) {
|
|
293
|
+
win.clearInterval(poll);
|
|
294
|
+
failed();
|
|
295
|
+
}
|
|
296
|
+
}, 50);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
win.__hfMotionPathPluginLoading = true;
|
|
300
|
+
const pluginScript = doc.createElement("script");
|
|
301
|
+
pluginScript.src = previewMotionPathUrl(doc, win.gsap?.version);
|
|
302
|
+
pluginScript.onload = () => {
|
|
303
|
+
win.__hfMotionPathPluginLoading = false;
|
|
304
|
+
ready();
|
|
305
|
+
};
|
|
306
|
+
// Running the script without the plugin would leave a motionPath tween that cannot play.
|
|
307
|
+
pluginScript.onerror = () => {
|
|
308
|
+
win.__hfMotionPathPluginLoading = false;
|
|
309
|
+
failed();
|
|
310
|
+
};
|
|
311
|
+
doc.head.appendChild(pluginScript);
|
|
312
|
+
}
|
|
313
|
+
/** The timeline keys the script owns and the live scripts it replaces; null when which to replace is unclear. */
|
|
314
|
+
function reloadScope(win, doc, script) {
|
|
315
|
+
if (!script || !win.gsap || !win.__hfForceTimelineRebind)
|
|
316
|
+
return null;
|
|
317
|
+
// Each composition owns one timeline key; tearing down every timeline would revert the others.
|
|
318
|
+
const targetKeys = timelineKeys(script);
|
|
319
|
+
if (targetKeys.length === 0)
|
|
320
|
+
return null;
|
|
321
|
+
const gsapScripts = findGsapScriptElements(doc);
|
|
322
|
+
if (gsapScripts.length === 0)
|
|
323
|
+
return null;
|
|
324
|
+
const staleScripts = scriptsRegistering(gsapScripts, targetKeys);
|
|
325
|
+
// Several scripts and none registers these keys: which element to replace is ambiguous.
|
|
326
|
+
if (gsapScripts.length > 1 && staleScripts.length === 0)
|
|
327
|
+
return null;
|
|
328
|
+
return { targetKeys, staleScripts };
|
|
329
|
+
}
|
|
330
|
+
/** Each reset element's authored style; null when one's file is not at hand. */
|
|
331
|
+
function readAuthoredStyles(doc, targets, input, ownFile) {
|
|
332
|
+
const authored = new Map();
|
|
333
|
+
for (const el of targets.keys()) {
|
|
334
|
+
const text = input.authoredStyle(el, authoringFile(el), ownFile);
|
|
335
|
+
if (text === undefined)
|
|
336
|
+
return null;
|
|
337
|
+
const probe = text === null ? null : doc.createElement("div");
|
|
338
|
+
probe?.setAttribute("style", text);
|
|
339
|
+
authored.set(el, probe?.style ?? null);
|
|
340
|
+
}
|
|
341
|
+
return authored;
|
|
342
|
+
}
|
|
343
|
+
// Re-runs the script in place of the live one registering the same `__timelines` keys, tearing down
|
|
344
|
+
// only those timelines. A promise only while it waits for the MotionPath plugin.
|
|
345
|
+
export function softReloadFrame(win, doc, input) {
|
|
346
|
+
const { script } = input;
|
|
347
|
+
const scope = reloadScope(win, doc, script);
|
|
348
|
+
if (!scope)
|
|
349
|
+
return "cannot-soft-reload";
|
|
350
|
+
const { targetKeys, staleScripts } = scope;
|
|
351
|
+
const currentTime = input.time ?? win.__player?.getTime?.() ?? 0;
|
|
352
|
+
const targets = collectResetTargets(win, doc, targetKeys, input.sets);
|
|
353
|
+
const ownFile = compositionFile(doc, targetKeys);
|
|
354
|
+
const authored = readAuthoredStyles(doc, targets, input, ownFile);
|
|
355
|
+
if (!authored)
|
|
356
|
+
return "cannot-soft-reload";
|
|
357
|
+
const timing = input.timing ? planSavedTiming(doc, ownFile, input.timing) : [];
|
|
358
|
+
if (timing === null)
|
|
359
|
+
return "cannot-soft-reload";
|
|
360
|
+
let deferred = null;
|
|
361
|
+
let pending = null;
|
|
362
|
+
let builtDom = false;
|
|
363
|
+
const executeScript = () => {
|
|
364
|
+
if (win.MotionPathPlugin && win.gsap?.registerPlugin)
|
|
365
|
+
win.gsap.registerPlugin(win.MotionPathPlugin);
|
|
366
|
+
const s = doc.createElement("script");
|
|
367
|
+
s.textContent = `(function(){${script}\n})();`;
|
|
368
|
+
const before = doc.body.querySelectorAll("*").length + 1;
|
|
369
|
+
doc.body.appendChild(s);
|
|
370
|
+
// A script that builds DOM builds it again on every run; only a full load starts from the file.
|
|
371
|
+
// Not seen: text-only writes, nodes put in the head or a shadow root, and nodes added later.
|
|
372
|
+
builtDom = doc.body.querySelectorAll("*").length > before;
|
|
373
|
+
finalizeSoftReload(win, currentTime);
|
|
374
|
+
deferred?.(builtDom ? "cannot-soft-reload" : "applied");
|
|
375
|
+
};
|
|
376
|
+
const doReload = () => {
|
|
377
|
+
applyTimingPlan(timing);
|
|
378
|
+
killTimelines(win, targetKeys);
|
|
379
|
+
resetTargetStyles(win, targets, authored);
|
|
380
|
+
forgetLiveSets(targets);
|
|
381
|
+
for (const stale of staleScripts)
|
|
382
|
+
stale.remove();
|
|
383
|
+
const needsMotionPath = /motionPath\s*[:{]/.test(script);
|
|
384
|
+
if (!needsMotionPath || win.MotionPathPlugin || !win.gsap)
|
|
385
|
+
return executeScript();
|
|
386
|
+
pending = new Promise((resolve) => (deferred = resolve));
|
|
387
|
+
whenMotionPathLoaded(win, doc, executeScript, () => deferred?.("cannot-soft-reload"));
|
|
388
|
+
};
|
|
389
|
+
if (!runSuppressed(win, doReload) || builtDom)
|
|
390
|
+
return "cannot-soft-reload";
|
|
391
|
+
if (pending)
|
|
392
|
+
return pending;
|
|
393
|
+
return verifyTimelinesPopulated(win, targetKeys) ? "applied" : "verify-failed";
|
|
394
|
+
}
|
|
395
|
+
export const MAX_SOFT_RELOAD_SCRIPT = 1_000_000;
|
|
396
|
+
const MAX_SETS = 1_000;
|
|
397
|
+
const MAX_SET_PROPS = 64;
|
|
398
|
+
const MAX_SELECTOR_LENGTH = 2_000;
|
|
399
|
+
const MAX_AUTHORED = 10_000;
|
|
400
|
+
const MAX_AUTHORED_KEY = 512;
|
|
401
|
+
const MAX_AUTHORED_STYLE = 16_384;
|
|
402
|
+
const MAX_TIMED = 10_000;
|
|
403
|
+
const MAX_TIMING_VALUE = 64;
|
|
404
|
+
const isRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
|
|
405
|
+
const boundedString = (value, max) => typeof value === "string" && value.length <= max;
|
|
406
|
+
function parseSet(value) {
|
|
407
|
+
if (!isRecord(value) || Object.keys(value).sort().join() !== "props,selector")
|
|
408
|
+
return null;
|
|
409
|
+
const { selector, props } = value;
|
|
410
|
+
if (!boundedString(selector, MAX_SELECTOR_LENGTH) || selector.length === 0)
|
|
411
|
+
return null;
|
|
412
|
+
if (!Array.isArray(props) || props.length > MAX_SET_PROPS)
|
|
413
|
+
return null;
|
|
414
|
+
return props.every((p) => boundedString(p, 64)) ? { selector, props: props } : null;
|
|
415
|
+
}
|
|
416
|
+
const isAuthoredEntry = (entry) => Array.isArray(entry) &&
|
|
417
|
+
entry.length === 2 &&
|
|
418
|
+
boundedString(entry[0], MAX_AUTHORED_KEY) &&
|
|
419
|
+
/^(hf:|#)./.test(entry[0]) &&
|
|
420
|
+
boundedString(entry[1], MAX_AUTHORED_STYLE);
|
|
421
|
+
const isTimingAttribute = (value) => Array.isArray(value) &&
|
|
422
|
+
value.length === 2 &&
|
|
423
|
+
TIMING_ATTRIBUTES.has(value[0]) &&
|
|
424
|
+
boundedString(value[1], MAX_TIMING_VALUE);
|
|
425
|
+
const isTimedEntry = (entry) => Array.isArray(entry) &&
|
|
426
|
+
entry.length === 2 &&
|
|
427
|
+
boundedString(entry[0], MAX_AUTHORED_KEY) &&
|
|
428
|
+
/^(hf:|#)./.test(entry[0]) &&
|
|
429
|
+
Array.isArray(entry[1]) &&
|
|
430
|
+
entry[1].length <= TIMING_ATTRIBUTES.size &&
|
|
431
|
+
entry[1].every(isTimingAttribute);
|
|
432
|
+
export function parseSavedTiming(value) {
|
|
433
|
+
const ok = Array.isArray(value) && value.length <= MAX_TIMED && value.every(isTimedEntry);
|
|
434
|
+
return ok ? value : null;
|
|
435
|
+
}
|
|
436
|
+
export function parseSoftReload(params) {
|
|
437
|
+
const fields = "authored,script,sets,time,timing";
|
|
438
|
+
if (!isRecord(params) || Object.keys(params).sort().join() !== fields)
|
|
439
|
+
return null;
|
|
440
|
+
const { script, time, sets, authored } = params;
|
|
441
|
+
const timing = parseSavedTiming(params.timing);
|
|
442
|
+
if (!timing)
|
|
443
|
+
return null;
|
|
444
|
+
if (!boundedString(script, MAX_SOFT_RELOAD_SCRIPT) || script.length === 0)
|
|
445
|
+
return null;
|
|
446
|
+
if (typeof time !== "number" || !Number.isFinite(time) || time < 0)
|
|
447
|
+
return null;
|
|
448
|
+
if (!Array.isArray(sets) || sets.length > MAX_SETS)
|
|
449
|
+
return null;
|
|
450
|
+
if (!Array.isArray(authored) || authored.length > MAX_AUTHORED)
|
|
451
|
+
return null;
|
|
452
|
+
const parsedSets = sets.map(parseSet);
|
|
453
|
+
if (!parsedSets.every(Boolean) || !authored.every(isAuthoredEntry))
|
|
454
|
+
return null;
|
|
455
|
+
return { script, time, sets: parsedSets, authored, timing };
|
|
456
|
+
}
|
|
457
|
+
/** The key Studio files an element's authored style under: its hf-id, else its id. */
|
|
458
|
+
export function authoredKey(el) {
|
|
459
|
+
const hfId = el.getAttribute("data-hf-id");
|
|
460
|
+
if (hfId)
|
|
461
|
+
return `hf:${hfId}`;
|
|
462
|
+
return el.id ? `#${el.id}` : null;
|
|
463
|
+
}
|
|
464
|
+
/** The frame re-running its own script: only elements of the saved file have authored styles to use. */
|
|
465
|
+
export async function softReload(params) {
|
|
466
|
+
const styles = new Map(params.authored);
|
|
467
|
+
const result = await softReloadFrame(window, document, {
|
|
468
|
+
script: params.script,
|
|
469
|
+
time: params.time,
|
|
470
|
+
sets: params.sets,
|
|
471
|
+
timing: params.timing,
|
|
472
|
+
authoredStyle: (el, file, ownFile) => {
|
|
473
|
+
if (file !== ownFile)
|
|
474
|
+
return undefined;
|
|
475
|
+
const key = authoredKey(el);
|
|
476
|
+
return key === null ? null : (styles.get(key) ?? null);
|
|
477
|
+
},
|
|
478
|
+
});
|
|
479
|
+
return { result };
|
|
480
|
+
}
|
|
@@ -18,6 +18,7 @@ import type { GsapSnapshot } from "./editing/gsapSnapshot.js";
|
|
|
18
18
|
import type { MotionPathFrameHome } from "./editing/motionPathHome.js";
|
|
19
19
|
import { type GsapSetParams } from "./editing/gsapSet.js";
|
|
20
20
|
import { type PatchTweensParams } from "./editing/patchTween.js";
|
|
21
|
+
import { type SoftReloadParams, type SoftReloadResult } from "./editing/softReload.js";
|
|
21
22
|
import type { EditingLayer } from "./editingLayers.js";
|
|
22
23
|
import type { RuntimeTimelineMessage } from "./types.js";
|
|
23
24
|
import { type CaptionWordBox, type CaptionWordsParams } from "./editing/captionWords.js";
|
|
@@ -42,7 +43,7 @@ export declare const EDITING_PROTOCOL_VERSION: 1;
|
|
|
42
43
|
export declare const EDITING_CONNECT = "editing-connect";
|
|
43
44
|
/** Frame to parent, on the window: carries the port in its transfer list. */
|
|
44
45
|
export declare const EDITING_PORT = "editing-port";
|
|
45
|
-
export declare const EDITING_REQUEST_KINDS: readonly ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start", "computed-styles", "elements-at-point", "pointer-targets", "describe", "ancestors", "query", "geometry", "layers", "draft", "gesture-start", "console-errors", "watch-keys", "restore-attributes", "caption-words", "caption-transform", "color-grading-status", "color-grading-previews", "color-grading-playback", "watch-geometry", "audio-meter", "document-markup", "set-audio-group-attribute", "assign-audio-group", "gsap-snapshot", "motion-path-home", "max-z-index", "crop-lift", "edit-element", "gsap-set", "patch-tweens"];
|
|
46
|
+
export declare const EDITING_REQUEST_KINDS: readonly ["hello", "runtime-snapshot", "wait-for-seek", "set-loop-start", "computed-styles", "elements-at-point", "pointer-targets", "describe", "ancestors", "query", "geometry", "layers", "draft", "gesture-start", "console-errors", "watch-keys", "restore-attributes", "caption-words", "caption-transform", "color-grading-status", "color-grading-previews", "color-grading-playback", "watch-geometry", "audio-meter", "document-markup", "set-audio-group-attribute", "assign-audio-group", "gsap-snapshot", "motion-path-home", "max-z-index", "crop-lift", "edit-element", "gsap-set", "patch-tweens", "soft-reload"];
|
|
46
47
|
export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number];
|
|
47
48
|
export interface EditingRuntimeSnapshot {
|
|
48
49
|
/** The runtime has installed its player API. */
|
|
@@ -337,6 +338,12 @@ export interface EditingRequests {
|
|
|
337
338
|
patched: boolean;
|
|
338
339
|
};
|
|
339
340
|
};
|
|
341
|
+
"soft-reload": {
|
|
342
|
+
params: SoftReloadParams;
|
|
343
|
+
result: {
|
|
344
|
+
result: SoftReloadResult;
|
|
345
|
+
};
|
|
346
|
+
};
|
|
340
347
|
}
|
|
341
348
|
export type EditingParams<K extends EditingRequestKind> = EditingRequests[K]["params"];
|
|
342
349
|
export type EditingResult<K extends EditingRequestKind> = EditingRequests[K]["result"];
|
|
@@ -14,6 +14,7 @@ import { geometryEvent, parseWatchGeometry, } from "./editing/watchGeometry.js";
|
|
|
14
14
|
import { EDITING_ELEMENT_HANDLE, MAX_DESCRIBE_HANDLES, } from "./editingElement.js";
|
|
15
15
|
import { parseGsapSet } from "./editing/gsapSet.js";
|
|
16
16
|
import { parsePatchTweens } from "./editing/patchTween.js";
|
|
17
|
+
import { parseSoftReload, } from "./editing/softReload.js";
|
|
17
18
|
import { wallClearTimeout, wallSetTimeout } from "./wallClock.js";
|
|
18
19
|
import { parseCombo } from "../keyCombo.js";
|
|
19
20
|
import { captionWordsParams, } from "./editing/captionWords.js";
|
|
@@ -72,6 +73,7 @@ export const EDITING_REQUEST_KINDS = [
|
|
|
72
73
|
"edit-element",
|
|
73
74
|
"gsap-set",
|
|
74
75
|
"patch-tweens",
|
|
76
|
+
"soft-reload",
|
|
75
77
|
];
|
|
76
78
|
export const MAX_DRAFT_PX = 100_000;
|
|
77
79
|
export const MAX_WATCHED_KEYS = 32;
|
|
@@ -246,6 +248,7 @@ const PARAM_PARSERS = {
|
|
|
246
248
|
"edit-element": parseEditElement,
|
|
247
249
|
"gsap-set": parseGsapSet,
|
|
248
250
|
"patch-tweens": parsePatchTweens,
|
|
251
|
+
"soft-reload": parseSoftReload,
|
|
249
252
|
};
|
|
250
253
|
/** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */
|
|
251
254
|
export function parseEditingRequest(data) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperframes/core",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.69",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -325,6 +325,10 @@
|
|
|
325
325
|
"import": "./dist/runtime/editing/patchTween.js",
|
|
326
326
|
"types": "./dist/runtime/editing/patchTween.d.ts"
|
|
327
327
|
},
|
|
328
|
+
"./runtime/soft-reload": {
|
|
329
|
+
"import": "./dist/runtime/editing/softReload.js",
|
|
330
|
+
"types": "./dist/runtime/editing/softReload.d.ts"
|
|
331
|
+
},
|
|
328
332
|
"./runtime/clip-window": {
|
|
329
333
|
"import": "./dist/runtime/clipWindow.js",
|
|
330
334
|
"types": "./dist/runtime/clipWindow.d.ts"
|
|
@@ -473,9 +477,9 @@
|
|
|
473
477
|
"linkedom": "^0.18.12",
|
|
474
478
|
"postcss": "^8.5.8",
|
|
475
479
|
"wawoff2": "^2.0.1",
|
|
476
|
-
"@hyperframes/
|
|
477
|
-
"@hyperframes/
|
|
478
|
-
"@hyperframes/
|
|
480
|
+
"@hyperframes/parsers": "0.9.69",
|
|
481
|
+
"@hyperframes/lint": "0.9.69",
|
|
482
|
+
"@hyperframes/studio-server": "0.9.69"
|
|
479
483
|
},
|
|
480
484
|
"devDependencies": {
|
|
481
485
|
"@fontsource/archivo-black": "^5.2.8",
|