@hyperframes/core 0.9.92 → 0.9.93

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.
@@ -37,6 +37,7 @@ export interface GsapSnapshot {
37
37
  time: number | null;
38
38
  defaultEase: string | null;
39
39
  timelines: GsapSnapshotTimeline[];
40
+ globalTweens: GsapSnapshotTween[];
40
41
  targets: GsapSnapshotTarget[];
41
42
  }
42
43
  /** Every tween on the frame's timelines, after any seek in flight has painted. Throws past a bound. */
@@ -33,10 +33,13 @@ export async function gsapSnapshot() {
33
33
  const gsap = frame.gsap;
34
34
  const reader = new SnapshotReader();
35
35
  const timelines = [];
36
+ const held = new Set();
36
37
  for (const [id, live] of Object.entries(frame.__timelines ?? {})) {
37
38
  if (typeof live?.getChildren !== "function")
38
39
  continue;
39
40
  const tweens = live.getChildren(true).filter((child) => typeof child.targets === "function");
41
+ for (const tween of tweens)
42
+ held.add(tween);
40
43
  if (timelines.length >= MAX_SNAPSHOT_TWEENS)
41
44
  throw new SnapshotBound("timelines");
42
45
  timelines.push({
@@ -46,11 +49,15 @@ export async function gsapSnapshot() {
46
49
  tweens: tweens.map((tween) => reader.tween(tween)),
47
50
  });
48
51
  }
52
+ const globalTweens = (gsap?.globalTimeline?.getChildren?.(true, true, false) ?? [])
53
+ .filter((child) => typeof child.targets === "function" && !held.has(child))
54
+ .map((tween) => reader.tween(tween));
49
55
  const time = frame.__player?.getTime?.();
50
56
  return {
51
57
  time: typeof time === "number" && Number.isFinite(time) ? time : null,
52
58
  defaultEase: text(gsap?.defaults?.().ease),
53
59
  timelines,
60
+ globalTweens,
54
61
  targets: reader.targets(gsap),
55
62
  };
56
63
  }
@@ -3,6 +3,7 @@ type Point = {
3
3
  x: number;
4
4
  y: number;
5
5
  };
6
+ type Style = Record<string, string | null>;
6
7
  export type TextEditParams = {
7
8
  handle: EditingElementHandle;
8
9
  action: "start";
@@ -11,6 +12,10 @@ export type TextEditParams = {
11
12
  handle: EditingElementHandle;
12
13
  action: "end";
13
14
  keep: boolean;
15
+ } | {
16
+ handle: EditingElementHandle;
17
+ action: "style";
18
+ style: Style;
14
19
  };
15
20
  export type TextEditResult = {
16
21
  applied: boolean;
@@ -23,7 +28,25 @@ export type TextEditEndedEvent = {
23
28
  html: string | null;
24
29
  previous: string;
25
30
  };
31
+ /** The selection in an open session, posted on its port as it changes; null when nothing is selected in it. */
32
+ export type TextEditSelection = {
33
+ rect: {
34
+ x: number;
35
+ y: number;
36
+ width: number;
37
+ height: number;
38
+ };
39
+ viewportWidth: number;
40
+ styles: Record<string, string>;
41
+ colours: string[];
42
+ painted: string | null;
43
+ };
44
+ export type TextEditSelectionEvent = {
45
+ handle: EditingElementHandle;
46
+ selection: TextEditSelection | null;
47
+ };
26
48
  export declare function parseTextEdit(params: unknown): TextEditParams | null;
49
+ export declare function textEditSelectionEvent(data: unknown, version: number): TextEditSelectionEvent | null;
27
50
  export declare function textEditEndedEvent(data: unknown, version: number): TextEditEndedEvent | null;
28
51
  export declare function textEditResult(reply: unknown): TextEditResult | null;
29
52
  /** Ends an open session with its original markup back; the agent's dispose and a page hide call it. */
@@ -6,7 +6,16 @@ import { isStylableElement } from "../domRealm.js";
6
6
  import { sanitizeRichTextChildren } from "../../utils/richTextSanitize.js";
7
7
  import { openTextEdits, isTextMarkup, MAX_EDIT_VALUE } from "./editElement.js";
8
8
  import { isEditableText } from "./textMarkup.js";
9
+ import { applyInlineStyle, INLINE_STYLE_PROPERTIES, readFirstPaintedElement, readInlineStyle, readInlineStyleSpread, } from "../../utils/inlineTextStyleRead.js";
9
10
  const EDITING_OUTLINE = "2px solid #3CE6AC";
11
+ const MAX_STYLE_VALUE = 256;
12
+ const MAX_COLOURS = 16;
13
+ const STYLE_VALUES = {
14
+ color: (value) => value.length <= MAX_STYLE_VALUE,
15
+ "font-weight": (value) => value === "700",
16
+ "font-style": (value) => value === "italic",
17
+ "text-decoration-line": (value) => value === "underline",
18
+ };
10
19
  const isRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
11
20
  const keysAre = (value, keys) => Object.keys(value).sort().join() === keys;
12
21
  const isHandle = (value) => typeof value === "string" && EDITING_ELEMENT_HANDLE.test(value);
@@ -21,8 +30,41 @@ export function parseTextEdit(params) {
21
30
  }
22
31
  if (action === "end" && keysAre(params, "action,handle,keep") && typeof params.keep === "boolean")
23
32
  return { handle, action, keep: params.keep };
33
+ if (action === "style" && keysAre(params, "action,handle,style") && isStyle(params.style))
34
+ return { handle, action, style: params.style };
24
35
  return null;
25
36
  }
37
+ const isStyle = (value) => isRecord(value) &&
38
+ Object.keys(value).length > 0 &&
39
+ Object.entries(value).every(([key, set]) => Object.hasOwn(STYLE_VALUES, key) &&
40
+ (set === null || (typeof set === "string" && STYLE_VALUES[key](set))));
41
+ const isShortText = (value) => typeof value === "string" && value.length <= MAX_STYLE_VALUE;
42
+ const isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
43
+ const isRect = (value) => isRecord(value) &&
44
+ keysAre(value, "height,width,x,y") &&
45
+ Object.values(value).every(isFiniteNumber);
46
+ const isShownStyles = (value) => isRecord(value) &&
47
+ Object.entries(value).every(([key, set]) => Object.hasOwn(STYLE_VALUES, key) && isShortText(set));
48
+ const isColours = (value) => Array.isArray(value) && value.length <= MAX_COLOURS && value.every(isShortText);
49
+ function isSelection(value) {
50
+ if (!isRecord(value) || !keysAre(value, "colours,painted,rect,styles,viewportWidth"))
51
+ return false;
52
+ const { rect, viewportWidth, styles, colours, painted } = value;
53
+ const shown = isFiniteNumber(viewportWidth) && viewportWidth > 0;
54
+ return (shown &&
55
+ isRect(rect) &&
56
+ isShownStyles(styles) &&
57
+ isColours(colours) &&
58
+ (painted === null || isShortText(painted)));
59
+ }
60
+ export function textEditSelectionEvent(data, version) {
61
+ if (!isRecord(data) || !keysAre(data, "event,handle,selection,v"))
62
+ return null;
63
+ if (data.v !== version || data.event !== "text-edit-selection" || !isHandle(data.handle))
64
+ return null;
65
+ const { handle, selection } = data;
66
+ return selection === null || isSelection(selection) ? { handle, selection } : null;
67
+ }
26
68
  const isValue = (value) => value === null || (typeof value === "string" && value.length <= MAX_EDIT_VALUE);
27
69
  const isTextMarkupOrNull = (value) => value === null || (typeof value === "string" && isTextMarkup(value));
28
70
  export function textEditEndedEvent(data, version) {
@@ -55,6 +97,30 @@ function clearSelectionWithin(element) {
55
97
  if (element.contains(range.startContainer) || element.contains(range.endContainer))
56
98
  selection.removeAllRanges();
57
99
  }
100
+ function selectedRange(element) {
101
+ const selection = element.ownerDocument.defaultView?.getSelection();
102
+ if (!selection || selection.rangeCount === 0 || selection.isCollapsed)
103
+ return null;
104
+ const range = selection.getRangeAt(0);
105
+ return element.contains(range.commonAncestorContainer) ? range : null;
106
+ }
107
+ const shortStyles = (styles) => Object.fromEntries(Object.entries(styles).filter(([, value]) => isShortText(value)));
108
+ function selectionIn(element) {
109
+ const range = selectedRange(element);
110
+ const view = element.ownerDocument.defaultView;
111
+ if (!range || !view)
112
+ return null;
113
+ const { left: x, top: y, width, height } = range.getBoundingClientRect();
114
+ const painted = readFirstPaintedElement(range, "color");
115
+ const paintedColour = painted ? view.getComputedStyle(painted).color : null;
116
+ return {
117
+ rect: { x, y, width, height },
118
+ viewportWidth: view.innerWidth || 1,
119
+ styles: shortStyles(readInlineStyle(range, INLINE_STYLE_PROPERTIES)),
120
+ colours: readInlineStyleSpread(range, "color").filter(isShortText).slice(0, MAX_COLOURS),
121
+ painted: isShortText(paintedColour) ? paintedColour : null,
122
+ };
123
+ }
58
124
  function keptMarkup(element) {
59
125
  sanitizeRichTextChildren(element);
60
126
  const html = element.innerHTML;
@@ -133,7 +199,7 @@ export function takePlainTextOnly(element) {
133
199
  element.removeEventListener("drop", onDrop);
134
200
  };
135
201
  }
136
- function listen(element, endInFrame) {
202
+ function listen(element, endInFrame, report) {
137
203
  const onKeyDown = (event) => {
138
204
  if ((event.key !== "Enter" || event.shiftKey) && event.key !== "Escape")
139
205
  return;
@@ -149,8 +215,10 @@ function listen(element, endInFrame) {
149
215
  const releasePlainText = takePlainTextOnly(element);
150
216
  element.addEventListener("keydown", onKeyDown);
151
217
  element.addEventListener("blur", endInFrame);
218
+ element.ownerDocument.addEventListener("selectionchange", report);
152
219
  view?.addEventListener("pagehide", endTextEdit);
153
220
  return () => {
221
+ element.ownerDocument.removeEventListener("selectionchange", report);
154
222
  releasePlainText();
155
223
  element.removeEventListener("keydown", onKeyDown);
156
224
  element.removeEventListener("blur", endInFrame);
@@ -172,14 +240,21 @@ function start(handle, caret, port, version) {
172
240
  outline: editable.style.outline,
173
241
  outlineOffset: editable.style.outlineOffset,
174
242
  stop: () => { },
243
+ report: () => {
244
+ if (session !== open)
245
+ return;
246
+ const selection = selectionIn(editable);
247
+ port.postMessage({ v: version, event: "text-edit-selection", handle, selection });
248
+ },
175
249
  };
176
- open.stop = listen(editable, () => {
250
+ const endInFrame = () => {
177
251
  if (session !== open)
178
252
  return;
179
253
  const ended = close(true);
180
254
  if (ended)
181
255
  port.postMessage({ v: version, event: "text-edit-ended", handle, ...ended });
182
- });
256
+ };
257
+ open.stop = listen(editable, endInFrame, open.report);
183
258
  session = open;
184
259
  openTextEdits.add(editable);
185
260
  editable.style.outline = EDITING_OUTLINE;
@@ -194,6 +269,16 @@ export function textEdit(params, port, version) {
194
269
  return start(params.handle, params.caret, port, version);
195
270
  if (session?.handle !== params.handle || session.port !== port)
196
271
  return NOTHING;
272
+ if (params.action === "style")
273
+ return styleSelection(session, params.style);
197
274
  const ended = close(params.keep);
198
275
  return ended ? { applied: true, ...ended } : NOTHING;
199
276
  }
277
+ function styleSelection(open, style) {
278
+ const range = selectedRange(open.element);
279
+ if (!range)
280
+ return NOTHING;
281
+ applyInlineStyle(range, style);
282
+ open.report();
283
+ return { applied: true, html: null, previous: null };
284
+ }
@@ -33,7 +33,7 @@ import { type AssignAudioGroupParams } from "./editing/assignAudioGroup.js";
33
33
  import { type AudioMeterLevels } from "./editing/audioMeter.js";
34
34
  import { type SetAudioGroupAttributeParams } from "./editing/setAudioGroupAttribute.js";
35
35
  import { type EditElementParams, type EditElementResult } from "./editing/editElement.js";
36
- import { type TextEditEndedEvent, type TextEditParams, type TextEditResult } from "./editing/textEdit.js";
36
+ import { type TextEditEndedEvent, type TextEditSelectionEvent, type TextEditParams, type TextEditResult } from "./editing/textEdit.js";
37
37
  export { buildStableSelector, escapeCssString, getPreferredClassSelector, isEditableTextLeaf, readElementSource, readTextLayer, EDITING_ELEMENT_HANDLE, MAX_DESCRIBE_HANDLES, MAX_ELEMENT_ENTRIES, MAX_ELEMENT_TEXT, MAX_ELEMENT_VALUE, MAX_TIMELINE_TEXT, MAX_POINT_STACK, MAX_QUERY_MATCHES, MAX_SOURCE_GROUPS, type EditingElementDescription, type EditingElementHandle, type EditingElementHit, type EditingElementMatch, type EditingElementSource, type EditingRect, type TextLayerRead, } from "./editingElement.js";
38
38
  export { elementCornerPoints, isElementVisibleThroughAncestors, readElementTransformSnapshot, rotationDegreesFromMatrix, snapCandidates, MAX_GEOMETRY_CHILDREN, MAX_SNAP_TARGETS, ROTATION_GATE_EPSILON_DEG, type EditingCorner, type EditingCorners, type EditingElementBox, type EditingElementGeometry, type ElementTransformSnapshot, } from "./editingGeometry.js";
39
39
  export type { EditingGestureBase, EditingGestureStart } from "./editingGestureStart.js";
@@ -490,6 +490,7 @@ export interface EditingPortClient {
490
490
  onKey(listener: (key: EditingKeyEvent) => void): () => void;
491
491
  onGeometry(listener: (event: EditingGeometryEvent) => void): () => void;
492
492
  onTextEditEnded(listener: (event: TextEditEndedEvent) => void): () => void;
493
+ onTextEditSelection(listener: (event: TextEditSelectionEvent) => void): () => void;
493
494
  /** Rejects every pending request with `disposed` and closes the port. */
494
495
  dispose(): void;
495
496
  }
@@ -504,5 +505,5 @@ export declare function createEditingPortClient(options: {
504
505
  host?: Window;
505
506
  }): EditingPortClient;
506
507
  export { elementHome, transformWDivisor, type MotionPathHome } from "./editing/motionPathHome.js";
507
- export { caretRangeAt, takePlainTextOnly, textEditResult, type TextEditEndedEvent, type TextEditResult, } from "./editing/textEdit.js";
508
+ export { caretRangeAt, takePlainTextOnly, textEditResult, type TextEditEndedEvent, type TextEditResult, type TextEditSelection, type TextEditSelectionEvent, } from "./editing/textEdit.js";
508
509
  export { parsePausedFrameAsk, PAUSED_FRAME_BYTES, PAUSED_FRAME_PARAMS, PAUSED_FRAME_SOURCES, type PausedFrameAsk, type PausedFrameAskMessage, type PausedFrameImage, type PausedFrameReply, } from "./editing/pausedFrames.js";
@@ -17,7 +17,7 @@ import { parseAssignAudioGroup } from "./editing/assignAudioGroup.js";
17
17
  import { parseAudioMeter } from "./editing/audioMeter.js";
18
18
  import { parseSetAudioGroupAttribute, } from "./editing/setAudioGroupAttribute.js";
19
19
  import { parseEditElement, } from "./editing/editElement.js";
20
- import { parseTextEdit, textEditEndedEvent, } from "./editing/textEdit.js";
20
+ import { parseTextEdit, textEditEndedEvent, textEditSelectionEvent, } from "./editing/textEdit.js";
21
21
  export { buildStableSelector, escapeCssString, getPreferredClassSelector, isEditableTextLeaf, readElementSource, readTextLayer, EDITING_ELEMENT_HANDLE, MAX_DESCRIBE_HANDLES, MAX_ELEMENT_ENTRIES, MAX_ELEMENT_TEXT, MAX_ELEMENT_VALUE, MAX_TIMELINE_TEXT, MAX_POINT_STACK, MAX_QUERY_MATCHES, MAX_SOURCE_GROUPS, } from "./editingElement.js";
22
22
  export { elementCornerPoints, isElementVisibleThroughAncestors, readElementTransformSnapshot, rotationDegreesFromMatrix, snapCandidates, MAX_GEOMETRY_CHILDREN, MAX_SNAP_TARGETS, ROTATION_GATE_EPSILON_DEG, } from "./editingGeometry.js";
23
23
  export { MAX_CAPTION_TRANSFORM } from "./editing/captionTransform.js";
@@ -406,6 +406,7 @@ export function createEditingPortClient(options) {
406
406
  const keyListeners = new Set();
407
407
  const geometryListeners = new Set();
408
408
  const textEditListeners = new Set();
409
+ const selectionListeners = new Set();
409
410
  let watchedGeometry = false;
410
411
  let port = null;
411
412
  let disposed = false;
@@ -437,7 +438,10 @@ export function createEditingPortClient(options) {
437
438
  return geometryListeners.forEach((listener) => listener(geometry));
438
439
  const ended = textEditEndedEvent(data, EDITING_PROTOCOL_VERSION);
439
440
  if (ended)
440
- textEditListeners.forEach((listener) => listener(ended));
441
+ return textEditListeners.forEach((listener) => listener(ended));
442
+ const selected = textEditSelectionEvent(data, EDITING_PROTOCOL_VERSION);
443
+ if (selected)
444
+ selectionListeners.forEach((listener) => listener(selected));
441
445
  };
442
446
  const onReply = (event) => {
443
447
  const data = event.data;
@@ -508,6 +512,10 @@ export function createEditingPortClient(options) {
508
512
  textEditListeners.add(listener);
509
513
  return () => textEditListeners.delete(listener);
510
514
  },
515
+ onTextEditSelection(listener) {
516
+ selectionListeners.add(listener);
517
+ return () => selectionListeners.delete(listener);
518
+ },
511
519
  dispose() {
512
520
  if (disposed)
513
521
  return;
@@ -515,6 +523,7 @@ export function createEditingPortClient(options) {
515
523
  keyListeners.clear();
516
524
  geometryListeners.clear();
517
525
  textEditListeners.clear();
526
+ selectionListeners.clear();
518
527
  host.removeEventListener("message", onWindowMessage);
519
528
  if (watchedGeometry) {
520
529
  const stop = { kind: "watch-geometry", params: { handles: [] } };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Styling a run of characters inside an element being edited in place.
3
+ *
4
+ * Not `document.execCommand`. That is deprecated, and what it emits varies by
5
+ * browser between `<font>`, a class, and an inline style depending on
6
+ * `styleWithCSS`. The output of this goes into the user's composition file, so
7
+ * it has to be one predictable shape, and the shape is a `<span>` carrying an
8
+ * inline style.
9
+ *
10
+ * Not DOM range surgery either, which is the obvious way and the wrong one.
11
+ * Wrapping a range in a span is three lines and then every interesting case is
12
+ * a special case: recolouring nests spans that shadow each other, removing a
13
+ * style cannot reach the ancestor that set it, and styling across an existing
14
+ * run's boundary has to split it. Each fix is a new branch and the branches
15
+ * interact.
16
+ *
17
+ * So the element is read into a flat list of styled runs, the styling is
18
+ * applied to a span of characters in that list, and the element is rebuilt
19
+ * from it. Replacing, removing, splitting and merging all stop being cases:
20
+ * the rebuild emits one span per distinct run and cannot nest or duplicate,
21
+ * whatever was there before. Text elements in a composition are a headline or
22
+ * a sentence, so reading and rebuilding one is not a cost worth avoiding.
23
+ */
24
+ export type InlineStyleDelta = Record<string, string | null>;
25
+ /** Apply `style` to the characters the range covers, then rebuild the element. */
26
+ export declare function applyInlineStyle(range: Range, style: InlineStyleDelta): void;
27
+ export interface InlineStyleChar {
28
+ char: string;
29
+ style: Readonly<Record<string, string>>;
30
+ painter: Element | null;
31
+ }
32
+ /** Every character the range covers with its style, or null when it covers none. */
33
+ export declare function readCoveredInlineStyleChars(range: Range): readonly InlineStyleChar[] | null;