@hyperframes/core 0.9.102 → 0.9.104

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 (40) hide show
  1. package/dist/atomicFile.d.ts +1 -0
  2. package/dist/atomicFile.js +1 -1
  3. package/dist/compiler/compositionScoping.js +3 -3
  4. package/dist/compiler/svgIdNamespacing.js +2 -2
  5. package/dist/fonts/authoredGoogleFonts.js +2 -2
  6. package/dist/fonts/deterministicFonts.js +2 -2
  7. package/dist/generated/position-edits-render-inline.js +1 -1
  8. package/dist/generated/runtime-inline.js +1 -1
  9. package/dist/hyperframe.runtime.iife.js +71 -71
  10. package/dist/index.d.ts +1 -1
  11. package/dist/runtime/domRealm.d.ts +1 -0
  12. package/dist/runtime/domRealm.js +3 -0
  13. package/dist/runtime/editing/canvasKeys.d.ts +63 -0
  14. package/dist/runtime/editing/canvasKeys.js +471 -0
  15. package/dist/runtime/editing/pausedFrames.js +7 -7
  16. package/dist/runtime/editing/removeElements.d.ts +10 -0
  17. package/dist/runtime/editing/removeElements.js +35 -0
  18. package/dist/runtime/editing/stageAct.d.ts +116 -0
  19. package/dist/runtime/editing/stageAct.js +531 -0
  20. package/dist/runtime/editing/stageGhost.d.ts +10 -0
  21. package/dist/runtime/editing/stageGhost.js +64 -0
  22. package/dist/runtime/editing/stageMeasure.d.ts +5 -0
  23. package/dist/runtime/editing/stageMeasure.js +35 -1
  24. package/dist/runtime/editing/textEdit.d.ts +14 -0
  25. package/dist/runtime/editing/textEdit.js +67 -9
  26. package/dist/runtime/editing/transformValues.d.ts +6 -0
  27. package/dist/runtime/editing/transformValues.js +39 -0
  28. package/dist/runtime/editing/watchGeometry.js +8 -8
  29. package/dist/runtime/editingGestureStart.d.ts +2 -0
  30. package/dist/runtime/editingGestureStart.js +1 -1
  31. package/dist/runtime/editingPort.d.ts +35 -3
  32. package/dist/runtime/editingPort.js +78 -16
  33. package/dist/runtime/playbackHealth.d.ts +8 -1
  34. package/dist/runtime/playbackHealth.js +118 -18
  35. package/dist/runtime/runtimePort.d.ts +21 -0
  36. package/dist/runtime/runtimePort.js +122 -0
  37. package/dist/runtime/types.d.ts +18 -0
  38. package/dist/studioPreviewMark.d.ts +1 -0
  39. package/dist/studioPreviewMark.js +1 -0
  40. package/package.json +4 -4
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { PlaybackHealthReport, PlaybackHeldBy, RuntimeTimelineClipIdentity, } from "./runtime/types.js";
1
+ export type { PlaybackHealthReport, PlaybackHeldBy, PlaybackMotionBlurReport, PreviewFrameShape, RuntimeTimelineClipIdentity, } from "./runtime/types.js";
2
2
  export type { ExecutionMode, Orientation, Asset, TimelineElement, TimelineElementBase, TimelineMediaElement, TimelineTextElement, TimelineCompositionElement, TimelineElementType, MediaElementType, CanvasResolution, Fps, FpsInput, FpsParseResult, MediaFile, CompositionAPI, PlayerAPI, AddElementData, ValidationResult, CompositionAsset, Keyframe, KeyframeProperties, ElementKeyframes, StageZoom, StageZoomKeyframe, CompositionVariableType, CompositionVariableBase, StringVariable, NumberVariable, ColorVariable, BooleanVariable, EnumVariable, CompositionVariable, CompositionSpec, WaveformData, OutputResolutionCompatibility, OutputResolutionIssueKind, } from "./core.types.js";
3
3
  export type { SlideshowManifest, SlideRef, SlideHotspot, SlideSequence, ResolvedSlide, ResolvedSlideSequence, ResolvedSlideshow, } from "./slideshow/index.js";
4
4
  export { parseSlideshowManifest, resolveSlideshow } from "./slideshow/index.js";
@@ -37,6 +37,7 @@
37
37
  * covered by this module or by the lint guard that keeps `src/runtime` clean
38
38
  * (`scripts/lint-runtime-preview-guards.ts`).
39
39
  */
40
+ export declare function isDomNode(value: unknown): value is Node;
40
41
  /** Any element node, from any realm (HTML, SVG, MathML). Mirrors `instanceof Element`. */
41
42
  export declare function isElementNode(value: unknown): value is Element;
42
43
  /**
@@ -38,6 +38,9 @@
38
38
  * (`scripts/lint-runtime-preview-guards.ts`).
39
39
  */
40
40
  const XHTML_NS = "http://www.w3.org/1999/xhtml";
41
+ export function isDomNode(value) {
42
+ return value != null && typeof value.nodeType === "number";
43
+ }
41
44
  /** Any element node, from any realm (HTML, SVG, MathML). Mirrors `instanceof Element`. */
42
45
  export function isElementNode(value) {
43
46
  return value != null && value.nodeType === 1;
@@ -0,0 +1,63 @@
1
+ import { type EditingElementHandle } from "../editingElement.js";
2
+ export declare const MAX_CANVAS_KEY_COMBOS = 96;
3
+ export interface CanvasKeyNudgeCombo {
4
+ combo: string;
5
+ dx: number;
6
+ dy: number;
7
+ }
8
+ export interface ArmCanvasKeysParams {
9
+ revision: number;
10
+ focus?: boolean;
11
+ deleteHandles: EditingElementHandle[];
12
+ deleteCombos: string[];
13
+ nudge: {
14
+ handles: EditingElementHandle[];
15
+ combos: CanvasKeyNudgeCombo[];
16
+ } | null;
17
+ forwardCombos: string[];
18
+ }
19
+ export interface ArmCanvasKeysResult {
20
+ armed: boolean;
21
+ revision: number;
22
+ }
23
+ export interface ResolveCanvasKeyActionParams {
24
+ actionId: number;
25
+ outcome: "commit" | "rollback";
26
+ }
27
+ export interface ResolveCanvasKeyActionResult {
28
+ resolved: boolean;
29
+ restored: number;
30
+ }
31
+ export type CanvasKeyActionEvent = {
32
+ action: "delete";
33
+ actionId: number;
34
+ revision: number;
35
+ handles: EditingElementHandle[];
36
+ } | {
37
+ action: "nudge";
38
+ actionId: number;
39
+ revision: number;
40
+ handles: EditingElementHandle[];
41
+ dx: number;
42
+ dy: number;
43
+ totalDx: number;
44
+ totalDy: number;
45
+ drafts: Record<string, number>;
46
+ /** GSAP x/y of each GSAP-owned handle before the burst's first draw. */
47
+ baseGsap: Record<string, {
48
+ x: number;
49
+ y: number;
50
+ }>;
51
+ };
52
+ export declare function parseArmCanvasKeys(params: unknown): ArmCanvasKeysParams | null;
53
+ export declare function parseResolveCanvasKeyAction(params: unknown): ResolveCanvasKeyActionParams | null;
54
+ export declare function canvasKeyActionEvent(data: unknown, version: number): CanvasKeyActionEvent | null;
55
+ export declare function listenFrameKeydown(view: Window, listener: (event: KeyboardEvent) => void): () => void;
56
+ /**
57
+ * Installs capture-first window key listeners in opaque-origin Studio preview frames only (never
58
+ * in same-origin previews, renders, captures, exports, or third-party pages). Idempotent.
59
+ */
60
+ export declare function installPreviewKeyShield(targetWindow?: Window, parentWindow?: Window): () => void;
61
+ export declare function armCanvasKeys(port: MessagePort, params: ArmCanvasKeysParams, version: number): ArmCanvasKeysResult;
62
+ export declare function resolveCanvasKeyAction({ actionId, outcome, }: ResolveCanvasKeyActionParams): ResolveCanvasKeyActionResult;
63
+ export declare function resetCanvasKeys(disposeShield?: boolean): void;
@@ -0,0 +1,471 @@
1
+ // Capture-first preview key handler: runs armed Delete/nudge in-frame and forwards Studio shortcuts.
2
+ import { matchCombo, parseCombo } from "../../keyCombo.js";
3
+ import { capturePageIn, markedForStudio } from "../../studioPreviewMark.js";
4
+ import { swallow } from "../diagnostics.js";
5
+ import { isDomNode, isElementNode, isStylableElement } from "../domRealm.js";
6
+ import { postOnPort } from "../runtimePort.js";
7
+ import { drawDrafts } from "../editingDraft.js";
8
+ import { EDITING_ELEMENT_HANDLE, MAX_DESCRIBE_HANDLES, } from "../editingElement.js";
9
+ import { gsapPosition } from "../editingGestureStart.js";
10
+ import { elementForHandle } from "../editingHandles.js";
11
+ import { isRemovableNode, outermostElements } from "./removeElements.js";
12
+ import { isActiveTextEditTarget } from "./textEdit.js";
13
+ export const MAX_CANVAS_KEY_COMBOS = 96;
14
+ const MAX_COMBO_LENGTH = 64;
15
+ const MAX_STEP_PX = 1_000;
16
+ const SHIELD_EVENTS = ["keydown", "keyup", "keypress", "beforeinput", "input"];
17
+ const isRecord = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
18
+ const isHandle = (value) => typeof value === "string" && EDITING_ELEMENT_HANDLE.test(value);
19
+ const isNonNegInt = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
20
+ const isValidComboText = (value) => typeof value === "string" && value.length <= MAX_COMBO_LENGTH && parseCombo(value) !== null;
21
+ function parseHandles(value, max = MAX_DESCRIBE_HANDLES) {
22
+ if (!Array.isArray(value) || value.length > max)
23
+ return null;
24
+ return value.every(isHandle) ? value : null;
25
+ }
26
+ function parseCombos(value, max = MAX_CANVAS_KEY_COMBOS) {
27
+ if (!Array.isArray(value) || value.length > max)
28
+ return null;
29
+ return value.every(isValidComboText) ? value : null;
30
+ }
31
+ function isStep(value) {
32
+ return typeof value === "number" && Number.isFinite(value) && Math.abs(value) <= MAX_STEP_PX;
33
+ }
34
+ function parseNudgeCombo(value) {
35
+ if (!isRecord(value) || Object.keys(value).sort().join() !== "combo,dx,dy")
36
+ return null;
37
+ if (!isValidComboText(value.combo) || !isStep(value.dx) || !isStep(value.dy))
38
+ return null;
39
+ return { combo: value.combo, dx: value.dx, dy: value.dy };
40
+ }
41
+ function parseNudge(value) {
42
+ if (value === null)
43
+ return null;
44
+ if (!isRecord(value) || Object.keys(value).sort().join() !== "combos,handles")
45
+ return undefined;
46
+ const handles = parseHandles(value.handles);
47
+ if (!handles || !handles.length || !Array.isArray(value.combos) || value.combos.length > 32)
48
+ return undefined;
49
+ const combos = value.combos.map(parseNudgeCombo);
50
+ return combos.includes(null) ? undefined : { handles, combos: combos };
51
+ }
52
+ export function parseArmCanvasKeys(params) {
53
+ if (!isRecord(params) || !isNonNegInt(params.revision))
54
+ return null;
55
+ const keys = Object.keys(params).sort().join();
56
+ const validKeys = keys === "deleteCombos,deleteHandles,forwardCombos,nudge,revision" ||
57
+ keys === "deleteCombos,deleteHandles,focus,forwardCombos,nudge,revision";
58
+ if (!validKeys)
59
+ return null;
60
+ if ("focus" in params && typeof params.focus !== "boolean")
61
+ return null;
62
+ const deleteHandles = parseHandles(params.deleteHandles);
63
+ const deleteCombos = parseCombos(params.deleteCombos, 16);
64
+ const forwardCombos = parseCombos(params.forwardCombos);
65
+ const nudge = parseNudge(params.nudge);
66
+ if (!deleteHandles || !deleteCombos || !forwardCombos || nudge === undefined)
67
+ return null;
68
+ return {
69
+ revision: params.revision,
70
+ ...("focus" in params ? { focus: params.focus } : {}),
71
+ deleteHandles,
72
+ deleteCombos,
73
+ nudge,
74
+ forwardCombos,
75
+ };
76
+ }
77
+ export function parseResolveCanvasKeyAction(params) {
78
+ if (!isRecord(params) || Object.keys(params).sort().join() !== "actionId,outcome")
79
+ return null;
80
+ if (!isNonNegInt(params.actionId))
81
+ return null;
82
+ if (params.outcome !== "commit" && params.outcome !== "rollback")
83
+ return null;
84
+ return { actionId: params.actionId, outcome: params.outcome };
85
+ }
86
+ const isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
87
+ function isBaseGsapMap(value) {
88
+ if (!isRecord(value))
89
+ return false;
90
+ return Object.entries(value).every(([k, v]) => isHandle(k) &&
91
+ isRecord(v) &&
92
+ Object.keys(v).sort().join() === "x,y" &&
93
+ isFiniteNumber(v.x) &&
94
+ isFiniteNumber(v.y));
95
+ }
96
+ function isDraftMap(value) {
97
+ if (!isRecord(value))
98
+ return false;
99
+ return Object.entries(value).every(([k, v]) => isHandle(k) && typeof v === "number" && Number.isSafeInteger(v) && v >= 0);
100
+ }
101
+ export function canvasKeyActionEvent(data, version) {
102
+ if (!isRecord(data) || data.v !== version || data.event !== "canvas-key-action")
103
+ return null;
104
+ if (!isNonNegInt(data.actionId) || !isNonNegInt(data.revision))
105
+ return null;
106
+ const handles = parseHandles(data.handles);
107
+ if (!handles || !handles.length)
108
+ return null;
109
+ if (data.action === "delete") {
110
+ if (Object.keys(data).sort().join() !== "action,actionId,event,handles,revision,v")
111
+ return null;
112
+ return { action: "delete", actionId: data.actionId, revision: data.revision, handles };
113
+ }
114
+ if (data.action === "nudge") {
115
+ const keys = Object.keys(data).sort().join();
116
+ if (keys !== "action,actionId,baseGsap,drafts,dx,dy,event,handles,revision,totalDx,totalDy,v")
117
+ return null;
118
+ if (!isStep(data.dx) ||
119
+ !isStep(data.dy) ||
120
+ !isFiniteNumber(data.totalDx) ||
121
+ !isFiniteNumber(data.totalDy) ||
122
+ !isDraftMap(data.drafts) ||
123
+ !isBaseGsapMap(data.baseGsap))
124
+ return null;
125
+ return {
126
+ action: "nudge",
127
+ actionId: data.actionId,
128
+ revision: data.revision,
129
+ handles,
130
+ dx: data.dx,
131
+ dy: data.dy,
132
+ totalDx: data.totalDx,
133
+ totalDy: data.totalDy,
134
+ drafts: data.drafts,
135
+ baseGsap: data.baseGsap,
136
+ };
137
+ }
138
+ return null;
139
+ }
140
+ // Native references captured at script evaluation time before any composition script runs.
141
+ const nativeStopImmediatePropagation = typeof Event !== "undefined" ? Event.prototype.stopImmediatePropagation : null;
142
+ const nativePreventDefault = typeof Event !== "undefined" ? Event.prototype.preventDefault : null;
143
+ const nativeIsTrustedGetter = typeof Event !== "undefined"
144
+ ? Object.getOwnPropertyDescriptor(Event.prototype, "isTrusted")?.get
145
+ : undefined;
146
+ function stopEvent(event) {
147
+ if (nativeStopImmediatePropagation)
148
+ nativeStopImmediatePropagation.call(event);
149
+ else
150
+ event.stopImmediatePropagation();
151
+ }
152
+ function preventEvent(event) {
153
+ if (nativePreventDefault)
154
+ nativePreventDefault.call(event);
155
+ else
156
+ event.preventDefault();
157
+ }
158
+ function isTrustedEvent(event) {
159
+ if (nativeIsTrustedGetter) {
160
+ try {
161
+ return nativeIsTrustedGetter.call(event) === true;
162
+ }
163
+ catch {
164
+ // Test realms may define isTrusted directly on the event instance.
165
+ }
166
+ }
167
+ return event.isTrusted === true;
168
+ }
169
+ let armed = null;
170
+ let lastActionId = 0;
171
+ const pendingDeletes = new Map();
172
+ let shieldDispose = null;
173
+ let shieldWindow = null;
174
+ const frameKeydownListeners = new Set();
175
+ export function listenFrameKeydown(view, listener) {
176
+ if (view !== shieldWindow) {
177
+ view.addEventListener("keydown", listener, true);
178
+ return () => view.removeEventListener("keydown", listener, true);
179
+ }
180
+ frameKeydownListeners.add(listener);
181
+ return () => frameKeydownListeners.delete(listener);
182
+ }
183
+ function round3(value) {
184
+ return Math.round(value * 1000) / 1000;
185
+ }
186
+ function resolveNudgeBases(handles) {
187
+ return handles.map((handle) => {
188
+ const el = elementForHandle(handle);
189
+ const owned = el ? gsapPosition(el) : null;
190
+ if (!owned || !el || !isStylableElement(el))
191
+ return { handle, gsap: null };
192
+ const rotDeg = Number.parseFloat(el.style.getPropertyValue("--hf-studio-rotation")) || 0;
193
+ const rad = (-rotDeg * Math.PI) / 180;
194
+ return {
195
+ handle,
196
+ gsap: { x: owned.x, y: owned.y, cos: Math.cos(rad), sin: Math.sin(rad) },
197
+ };
198
+ });
199
+ }
200
+ function runLocalDelete(state) {
201
+ const pairs = [];
202
+ for (const handle of state.deleteHandles) {
203
+ const element = elementForHandle(handle);
204
+ if (isRemovableNode(element) && element.isConnected)
205
+ pairs.push({ handle, element });
206
+ }
207
+ const kept = new Set(outermostElements(pairs.map((p) => p.element)));
208
+ const targets = pairs.filter((p) => kept.has(p.element));
209
+ state.deleteHandles = [];
210
+ state.nudge = null;
211
+ if (!targets.length)
212
+ return;
213
+ const detached = [];
214
+ for (const { element } of targets) {
215
+ const parent = element.parentNode;
216
+ const index = parent ? Array.prototype.indexOf.call(parent.childNodes, element) : -1;
217
+ detached.push({
218
+ element,
219
+ parent,
220
+ previousSibling: element.previousSibling,
221
+ nextSibling: element.nextSibling,
222
+ index,
223
+ });
224
+ element.remove();
225
+ }
226
+ const actionId = ++lastActionId;
227
+ pendingDeletes.set(actionId, detached);
228
+ postOnPort(state.port, {
229
+ v: state.version,
230
+ event: "canvas-key-action",
231
+ action: "delete",
232
+ actionId,
233
+ revision: state.revision,
234
+ handles: targets.map((t) => t.handle),
235
+ });
236
+ }
237
+ function runLocalNudge(state, nudge, dx, dy) {
238
+ nudge.totalDx += dx;
239
+ nudge.totalDy += dy;
240
+ nudge.bases ??= resolveNudgeBases(nudge.handles);
241
+ const drafts = nudge.bases.map((base) => {
242
+ if (!base.gsap) {
243
+ return {
244
+ handle: base.handle,
245
+ translate: { x: round3(nudge.totalDx), y: round3(nudge.totalDy) },
246
+ size: null,
247
+ };
248
+ }
249
+ const adjX = nudge.totalDx * base.gsap.cos - nudge.totalDy * base.gsap.sin;
250
+ const adjY = nudge.totalDx * base.gsap.sin + nudge.totalDy * base.gsap.cos;
251
+ return {
252
+ handle: base.handle,
253
+ translate: {
254
+ x: round3(base.gsap.x + adjX),
255
+ y: round3(base.gsap.y + adjY),
256
+ route: "gsap",
257
+ },
258
+ size: null,
259
+ };
260
+ });
261
+ const reply = drawDrafts(drafts);
262
+ const baseGsap = {};
263
+ for (const { handle, gsap } of nudge.bases)
264
+ if (gsap)
265
+ baseGsap[handle] = { x: gsap.x, y: gsap.y };
266
+ const actionId = ++lastActionId;
267
+ postOnPort(state.port, {
268
+ v: state.version,
269
+ event: "canvas-key-action",
270
+ action: "nudge",
271
+ actionId,
272
+ revision: state.revision,
273
+ handles: nudge.handles,
274
+ dx,
275
+ dy,
276
+ totalDx: nudge.totalDx,
277
+ totalDy: nudge.totalDy,
278
+ drafts: reply.drafts,
279
+ baseGsap,
280
+ });
281
+ }
282
+ function forwardTrustedKey(state, event) {
283
+ postOnPort(state.port, {
284
+ v: state.version,
285
+ event: "key",
286
+ key: {
287
+ type: event.type === "keyup" ? "keyup" : "keydown",
288
+ key: event.key,
289
+ code: event.code,
290
+ altKey: event.altKey,
291
+ ctrlKey: event.ctrlKey,
292
+ metaKey: event.metaKey,
293
+ shiftKey: event.shiftKey,
294
+ repeat: event.repeat,
295
+ },
296
+ });
297
+ }
298
+ function handleTrustedKeyDown(state, event) {
299
+ if (!event.repeat &&
300
+ state.deleteHandles.length > 0 &&
301
+ state.deleteCombos.some((combo) => matchCombo(event, combo))) {
302
+ preventEvent(event);
303
+ runLocalDelete(state);
304
+ return;
305
+ }
306
+ const nudgeHit = state.nudge?.combos.find(({ combo }) => matchCombo(event, combo));
307
+ if (state.nudge && nudgeHit) {
308
+ preventEvent(event);
309
+ runLocalNudge(state, state.nudge, nudgeHit.dx, nudgeHit.dy);
310
+ return;
311
+ }
312
+ if (state.forwardCombos.some((combo) => matchCombo(event, combo)))
313
+ preventEvent(event);
314
+ state.down.add(event.code || event.key);
315
+ forwardTrustedKey(state, event);
316
+ }
317
+ function isFilmEditableTarget(target) {
318
+ if (!isElementNode(target))
319
+ return false;
320
+ const tag = target.tagName;
321
+ if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT")
322
+ return true;
323
+ return target.isContentEditable === true;
324
+ }
325
+ function handleShieldKey(state, event) {
326
+ if (event.type === "keydown") {
327
+ if (isFilmEditableTarget(event.target))
328
+ preventEvent(event);
329
+ handleTrustedKeyDown(state, event);
330
+ return;
331
+ }
332
+ if (state.down.delete(event.code || event.key))
333
+ forwardTrustedKey(state, event);
334
+ }
335
+ function hear(listener, event) {
336
+ try {
337
+ listener(event);
338
+ }
339
+ catch (err) {
340
+ swallow("canvasKeys.frameKeydown", err);
341
+ }
342
+ }
343
+ function onShieldEvent(event) {
344
+ if (event.type === "keydown")
345
+ for (const listener of frameKeydownListeners)
346
+ hear(listener, event);
347
+ const state = armed;
348
+ if (!state || !isTrustedEvent(event) || isActiveTextEditTarget(event.target))
349
+ return;
350
+ stopEvent(event);
351
+ if (event.type !== "keydown" && event.type !== "keyup")
352
+ return preventEvent(event);
353
+ handleShieldKey(state, event);
354
+ }
355
+ function isExportRenderEnvironment() {
356
+ return Boolean(window.__HF_EXPORT_RENDER_SEEK_CONFIG);
357
+ }
358
+ /**
359
+ * Installs capture-first window key listeners in opaque-origin Studio preview frames only (never
360
+ * in same-origin previews, renders, captures, exports, or third-party pages). Idempotent.
361
+ */
362
+ export function installPreviewKeyShield(targetWindow = window, parentWindow = targetWindow.parent) {
363
+ if (shieldDispose)
364
+ return shieldDispose;
365
+ if (parentWindow === targetWindow ||
366
+ targetWindow.origin !== "null" ||
367
+ !markedForStudio(targetWindow.document) ||
368
+ capturePageIn(targetWindow.document) ||
369
+ isExportRenderEnvironment()) {
370
+ return () => { };
371
+ }
372
+ for (const type of SHIELD_EVENTS) {
373
+ targetWindow.addEventListener(type, onShieldEvent, { capture: true });
374
+ }
375
+ shieldWindow = targetWindow;
376
+ shieldDispose = () => {
377
+ for (const type of SHIELD_EVENTS) {
378
+ targetWindow.removeEventListener(type, onShieldEvent, { capture: true });
379
+ }
380
+ shieldDispose = null;
381
+ shieldWindow = null;
382
+ };
383
+ return shieldDispose;
384
+ }
385
+ function ensureBodyFocusable() {
386
+ const body = document.body;
387
+ if (!body)
388
+ return null;
389
+ if (!body.hasAttribute("tabindex"))
390
+ body.setAttribute("tabindex", "-1");
391
+ if (!body.style.outline)
392
+ body.style.outline = "none";
393
+ return body;
394
+ }
395
+ function focusFrameSurface() {
396
+ const body = ensureBodyFocusable();
397
+ if (!body || isActiveTextEditTarget(document.activeElement))
398
+ return;
399
+ body.focus({ preventScroll: true });
400
+ window.focus();
401
+ }
402
+ export function armCanvasKeys(port, params, version) {
403
+ const hasActions = params.deleteHandles.length > 0 || params.nudge !== null || params.forwardCombos.length > 0;
404
+ if (!hasActions || !shieldDispose) {
405
+ if (armed?.port === port)
406
+ armed = null;
407
+ return { armed: false, revision: params.revision };
408
+ }
409
+ ensureBodyFocusable();
410
+ const down = armed?.port === port ? armed.down : new Set();
411
+ armed = {
412
+ port,
413
+ version,
414
+ revision: params.revision,
415
+ deleteHandles: [...params.deleteHandles],
416
+ deleteCombos: params.deleteCombos.map((text) => parseCombo(text)),
417
+ nudge: params.nudge
418
+ ? {
419
+ handles: [...params.nudge.handles],
420
+ combos: params.nudge.combos.map((entry) => ({
421
+ combo: parseCombo(entry.combo),
422
+ dx: entry.dx,
423
+ dy: entry.dy,
424
+ })),
425
+ totalDx: 0,
426
+ totalDy: 0,
427
+ bases: null,
428
+ }
429
+ : null,
430
+ forwardCombos: params.forwardCombos.map((text) => parseCombo(text)),
431
+ down,
432
+ };
433
+ if (params.focus)
434
+ focusFrameSurface();
435
+ return { armed: true, revision: params.revision };
436
+ }
437
+ function restoreAnchorFor(parent, previousSibling, nextSibling, index) {
438
+ if (nextSibling && nextSibling.parentNode === parent)
439
+ return nextSibling;
440
+ if (previousSibling && previousSibling.parentNode === parent)
441
+ return previousSibling.nextSibling;
442
+ return index >= 0 ? (parent.childNodes[Math.min(index, parent.childNodes.length)] ?? null) : null;
443
+ }
444
+ function restoreDetachedNode({ element, parent, previousSibling, nextSibling, index, }) {
445
+ if (!parent || element.isConnected)
446
+ return false;
447
+ if (isDomNode(parent) && !parent.isConnected)
448
+ return false;
449
+ parent.insertBefore(element, restoreAnchorFor(parent, previousSibling, nextSibling, index));
450
+ return true;
451
+ }
452
+ export function resolveCanvasKeyAction({ actionId, outcome, }) {
453
+ const detached = pendingDeletes.get(actionId);
454
+ if (!detached)
455
+ return { resolved: false, restored: 0 };
456
+ pendingDeletes.delete(actionId);
457
+ if (outcome === "commit")
458
+ return { resolved: true, restored: 0 };
459
+ let restored = 0;
460
+ for (let i = detached.length - 1; i >= 0; i--) {
461
+ if (restoreDetachedNode(detached[i]))
462
+ restored += 1;
463
+ }
464
+ return { resolved: true, restored };
465
+ }
466
+ export function resetCanvasKeys(disposeShield = false) {
467
+ armed = null;
468
+ pendingDeletes.clear();
469
+ if (disposeShield)
470
+ shieldDispose?.();
471
+ }
@@ -1,6 +1,7 @@
1
1
  // Paused video frames for a frame Studio cannot script: the runtime asks over a port Studio handed it, and Studio
2
2
  // decodes. An ask names a path on the frame's own server and the proxy options it played, never a URL: Studio
3
3
  // resolves it against its own project before reading anything.
4
+ import { closePort, onPortMessage, postOnPort } from "../runtimePort.js";
4
5
  import { sourceFactsOfSrc } from "../mediaSourceLookup.js";
5
6
  import { isMessageList } from "../protocol.js";
6
7
  /** The query options the runtime's media proxy adds to a src; any other key never leaves the frame. */
@@ -95,21 +96,20 @@ export function portPausedVideoProvider(port) {
95
96
  if (isRecord(frame))
96
97
  frame.image?.close?.();
97
98
  };
98
- port.onmessage = (event) => {
99
- const reply = event.data;
99
+ onPortMessage(port, (reply) => {
100
100
  const entry = isRecord(reply) && isId(reply.id) ? pending.get(reply.id) : undefined;
101
101
  if (!entry)
102
102
  return closeImages(reply);
103
103
  pending.delete(reply.id);
104
104
  entry.resolve(reply);
105
- };
105
+ });
106
106
  const ask = (message, signal) => new Promise((resolve, reject) => {
107
107
  if (disposed)
108
108
  return reject(aborted());
109
109
  const onAbort = () => {
110
110
  if (!pending.delete(message.id))
111
111
  return;
112
- port.postMessage({ type: "abort", id: message.id });
112
+ postOnPort(port, { type: "abort", id: message.id });
113
113
  reject(aborted());
114
114
  };
115
115
  pending.set(message.id, {
@@ -120,7 +120,7 @@ export function portPausedVideoProvider(port) {
120
120
  reject,
121
121
  });
122
122
  signal?.addEventListener("abort", onAbort, { once: true });
123
- port.postMessage(message);
123
+ postOnPort(port, message);
124
124
  });
125
125
  const provider = {
126
126
  async decode(requests, signal) {
@@ -167,10 +167,10 @@ export function portPausedVideoProvider(port) {
167
167
  entry.reject(aborted());
168
168
  pending.clear();
169
169
  try {
170
- port.postMessage({ type: "close" });
170
+ postOnPort(port, { type: "close" });
171
171
  }
172
172
  catch { }
173
- port.close();
173
+ closePort(port);
174
174
  window.removeEventListener("pagehide", onPageHide);
175
175
  },
176
176
  };
@@ -0,0 +1,10 @@
1
+ import type { EditingElementHandle } from "../editingElement.js";
2
+ export declare function isRemovableNode(element: Element | null): element is Element;
3
+ /** Deduplicates handles so a node inside another named node leaves with it and counts once. */
4
+ export declare function outermostElements(elements: readonly Element[]): Element[];
5
+ /** Removes every named clip, or none when any handle is stale or names something that is not a clip. */
6
+ export declare function removeElements({ handles }: {
7
+ handles: EditingElementHandle[];
8
+ }): {
9
+ removed: number;
10
+ };
@@ -0,0 +1,35 @@
1
+ import { elementForHandle } from "../editingHandles.js";
2
+ import { CLIP_NODES } from "../clipNodes.js";
3
+ const NEVER_CLIPS = new Set([
4
+ "html",
5
+ "head",
6
+ "body",
7
+ "script",
8
+ "style",
9
+ "link",
10
+ "meta",
11
+ "template",
12
+ "noscript",
13
+ ]);
14
+ const isRootComposition = (element) => element.hasAttribute("data-composition-id") &&
15
+ !element.parentElement?.closest("[data-composition-id]");
16
+ export function isRemovableNode(element) {
17
+ return Boolean(element && !NEVER_CLIPS.has(element.localName) && !isRootComposition(element));
18
+ }
19
+ function removableClip(element) {
20
+ return isRemovableNode(element) && element.matches(CLIP_NODES);
21
+ }
22
+ /** Deduplicates handles so a node inside another named node leaves with it and counts once. */
23
+ export function outermostElements(elements) {
24
+ return [...new Set(elements)].filter((element, _, all) => !all.some((other) => other !== element && other.contains(element)));
25
+ }
26
+ /** Removes every named clip, or none when any handle is stale or names something that is not a clip. */
27
+ export function removeElements({ handles }) {
28
+ const elements = handles.map(elementForHandle);
29
+ if (!elements.every(removableClip))
30
+ return { removed: 0 };
31
+ const outermost = outermostElements(elements);
32
+ for (const element of outermost)
33
+ element.remove();
34
+ return { removed: outermost.length };
35
+ }