@pairdraw/core 0.22.0 → 0.22.2

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 (44) hide show
  1. package/dist/canvas/index.d.ts +2 -1
  2. package/dist/canvas/overlays/presence.d.ts +16 -1
  3. package/dist/canvas/overlays/presence.d.ts.map +1 -1
  4. package/dist/canvas/overlays/presence.js +90 -3
  5. package/dist/canvas/overlays/presence.js.map +1 -1
  6. package/dist/canvas/renderer/picture.d.ts +4 -1
  7. package/dist/canvas/renderer/picture.d.ts.map +1 -1
  8. package/dist/canvas/renderer/picture.js +13 -1
  9. package/dist/canvas/renderer/picture.js.map +1 -1
  10. package/dist/canvas/renderer/transient-previews.d.ts +9 -1
  11. package/dist/canvas/renderer/transient-previews.d.ts.map +1 -1
  12. package/dist/canvas/renderer/transient-previews.js +29 -3
  13. package/dist/canvas/renderer/transient-previews.js.map +1 -1
  14. package/dist/canvas/renderer/types.d.ts +11 -0
  15. package/dist/canvas/renderer/types.d.ts.map +1 -1
  16. package/dist/canvas/renderer.d.ts +1 -1
  17. package/dist/design-jsx/preview/picture.d.ts +4 -0
  18. package/dist/design-jsx/preview/picture.d.ts.map +1 -1
  19. package/dist/design-jsx/preview/picture.js +13 -1
  20. package/dist/design-jsx/preview/picture.js.map +1 -1
  21. package/dist/design-jsx/preview/stage.d.ts +2 -0
  22. package/dist/design-jsx/preview/stage.d.ts.map +1 -1
  23. package/dist/design-jsx/preview/stage.js +11 -2
  24. package/dist/design-jsx/preview/stage.js.map +1 -1
  25. package/dist/text/font/batch.d.ts +26 -0
  26. package/dist/text/font/batch.d.ts.map +1 -0
  27. package/dist/text/font/batch.js +31 -0
  28. package/dist/text/font/batch.js.map +1 -0
  29. package/dist/text/font/manager.d.ts.map +1 -1
  30. package/dist/text/font/manager.js +14 -1
  31. package/dist/text/font/manager.js.map +1 -1
  32. package/dist/text/index.d.ts +2 -1
  33. package/dist/text/index.js +2 -1
  34. package/package.json +9 -9
  35. package/src/canvas/index.ts +1 -0
  36. package/src/canvas/overlays/presence.ts +114 -13
  37. package/src/canvas/renderer/picture.ts +17 -0
  38. package/src/canvas/renderer/transient-previews.ts +38 -4
  39. package/src/canvas/renderer/types.ts +8 -0
  40. package/src/design-jsx/preview/picture.ts +27 -1
  41. package/src/design-jsx/preview/stage.ts +11 -3
  42. package/src/text/font/batch.ts +58 -0
  43. package/src/text/font/manager.ts +17 -1
  44. package/src/text/index.ts +1 -0
@@ -1,4 +1,4 @@
1
- import type { Canvas, Image } from 'canvaskit-wasm'
1
+ import type { Canvas, Image, Paint } from 'canvaskit-wasm'
2
2
 
3
3
  import type { SceneGraph } from '@pairdraw/scene-graph'
4
4
  import { getWorldMatrix } from '@pairdraw/scene-graph/coordinate'
@@ -17,28 +17,122 @@ const LABEL_MAX_WIDTH = 160
17
17
  /** The provider mark before an agent's name, and the gap after it. */
18
18
  const LABEL_LOGO_SIZE = 9
19
19
  const LABEL_LOGO_GAP = 3
20
+ /** The newest outline's soft pulse, and how long a replaced one takes to fade. */
21
+ const FOCUS_PULSE_MS = 1200
22
+ const FOCUS_FADE_MS = 200
23
+ /** One sheen across what a finished preview built. */
24
+ const SHEEN_MS = 400
20
25
 
21
26
  /** Decoded once per logo byte array; the app keeps one array per provider, so this is tiny. */
22
27
  const labelLogoImages = new WeakMap<Uint8Array, Image | null>()
28
+ /** What a cursor's focus outline showed, so a newer element fades the old one out. */
29
+ export interface FocusTransition {
30
+ current: Rect | null
31
+ previous: Rect | null
32
+ /** When the previous outline was replaced. */
33
+ since: number
34
+ }
35
+
36
+ export const NO_FOCUS: FocusTransition = { current: null, previous: null, since: 0 }
37
+
38
+ /** Draw a world-space rectangle in the overlay's screen coordinates. */
39
+ function drawWorldRect(r: SkiaRenderer, canvas: Canvas, rect: Rect, paint: Paint) {
40
+ canvas.drawRect(
41
+ r.ck.XYWHRect(
42
+ rect.x * r.zoom + r.panX,
43
+ rect.y * r.zoom + r.panY,
44
+ rect.width * r.zoom,
45
+ rect.height * r.zoom
46
+ ),
47
+ paint
48
+ )
49
+ }
50
+
51
+ function sameRect(a: Rect, b: Rect): boolean {
52
+ return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height
53
+ }
54
+
55
+ /** Move the outline to `focus`, keeping the one it replaces for its fade. */
56
+ export function nextFocus(
57
+ state: FocusTransition,
58
+ focus: Rect | undefined,
59
+ now: number
60
+ ): FocusTransition {
61
+ if (!focus) return NO_FOCUS
62
+ if (state.current && sameRect(state.current, focus)) return state
63
+ return { current: focus, previous: state.current, since: now }
64
+ }
65
+
66
+ /** The outline to draw now: the current one, pulsing, and the replaced one fading out. */
67
+ export function focusOutlines(
68
+ state: FocusTransition,
69
+ pulse: boolean | undefined,
70
+ now: number
71
+ ): Array<{ rect: Rect; alpha: number }> {
72
+ const outlines: Array<{ rect: Rect; alpha: number }> = []
73
+ const age = now - state.since
74
+ if (pulse && state.previous && age < FOCUS_FADE_MS) {
75
+ outlines.push({ rect: state.previous, alpha: 0.75 * (1 - age / FOCUS_FADE_MS) })
76
+ }
77
+ if (state.current) {
78
+ const alpha = pulse
79
+ ? 0.35 + 0.65 * (0.5 + 0.5 * Math.sin((now / FOCUS_PULSE_MS) * 2 * Math.PI))
80
+ : 0.75
81
+ outlines.push({ rect: state.current, alpha })
82
+ }
83
+ return outlines
84
+ }
85
+
86
+ /** Each canvas keeps its own focus transitions, so two documents' agents never collide. */
87
+ const focusHistories = new WeakMap<SkiaRenderer, Map<string, FocusTransition>>()
88
+
89
+ function focusHistoryFor(r: SkiaRenderer): Map<string, FocusTransition> {
90
+ const existing = focusHistories.get(r)
91
+ if (existing) return existing
92
+ const history = new Map<string, FocusTransition>()
93
+ focusHistories.set(r, history)
94
+ return history
95
+ }
96
+
97
+ /** The element being written, pulsing in the accent color, and the one it replaced fading out. */
98
+ function drawFocus(r: SkiaRenderer, canvas: Canvas, cursor: PresenceCursor) {
99
+ if (cursor.kind !== 'agent') return
100
+ const key = cursor.id ?? cursor.name
101
+ const history = focusHistoryFor(r)
102
+ const now = performance.now()
103
+ const state = nextFocus(history.get(key) ?? NO_FOCUS, cursor.focus, now)
104
+ if (state === NO_FOCUS) history.delete(key)
105
+ else history.set(key, state)
106
+ const { r: red, g, b } = cursor.color
107
+ r.auxStroke.setStrokeWidth(2)
108
+ r.auxStroke.setPathEffect(null)
109
+ for (const { rect, alpha } of focusOutlines(state, cursor.pulse, now)) {
110
+ if (alpha <= 0) continue
111
+ r.auxStroke.setColor(r.ck.Color4f(red, g, b, alpha))
112
+ drawWorldRect(r, canvas, rect, r.auxStroke)
113
+ }
114
+ }
115
+
116
+ /** One soft pass of light across what a finished preview built. */
117
+ function drawSheen(r: SkiaRenderer, canvas: Canvas, cursor: PresenceCursor) {
118
+ if (!cursor.sheen) return
119
+ const age = performance.now() - cursor.sheen.at
120
+ if (age < 0 || age >= SHEEN_MS) return
121
+ const { rect } = cursor.sheen
122
+ r.auxFill.setColor(r.ck.Color4f(1, 1, 1, 0.07 * Math.sin((age / SHEEN_MS) * Math.PI)))
123
+ drawWorldRect(r, canvas, rect, r.auxFill)
124
+ }
23
125
 
24
126
  function drawSelection(r: SkiaRenderer, canvas: Canvas, graph: SceneGraph, cursor: PresenceCursor) {
127
+ drawSheen(r, canvas, cursor)
128
+ drawFocus(r, canvas, cursor)
25
129
  if (!cursor.selection?.length && !cursor.outline?.length) return
26
130
  const { r: red, g, b } = cursor.color
27
131
  r.auxStroke.setColor(r.ck.Color4f(red, g, b, 0.6))
28
132
  r.auxStroke.setStrokeWidth(1.5)
29
133
  r.auxStroke.setPathEffect(null)
30
134
  // Outlines of what is not a layer yet, such as JSX an agent is still streaming.
31
- for (const rect of cursor.outline ?? []) {
32
- canvas.drawRect(
33
- r.ck.XYWHRect(
34
- rect.x * r.zoom + r.panX,
35
- rect.y * r.zoom + r.panY,
36
- rect.width * r.zoom,
37
- rect.height * r.zoom
38
- ),
39
- r.auxStroke
40
- )
41
- }
135
+ for (const rect of cursor.outline ?? []) drawWorldRect(r, canvas, rect, r.auxStroke)
42
136
  for (const nodeId of cursor.selection ?? []) {
43
137
  const node = graph.getNode(nodeId)
44
138
  if (!node) continue
@@ -115,6 +209,8 @@ function drawLabel(r: SkiaRenderer, canvas: Canvas, x: number, y: number, cursor
115
209
  const agent = cursor.kind === 'agent'
116
210
  const logo = agent ? labelLogo(r, cursor.logo) : null
117
211
  const badge = logo ? LABEL_LOGO_SIZE + LABEL_LOGO_GAP : 0
212
+ // The status climbs through its dots while the agent rests; the name stays first.
213
+ const label = cursor.status ? `${cursor.name} ${cursor.status}` : cursor.name
118
214
  const pillX = x + LABEL_OFFSET_X - LABEL_PADDING_X
119
215
  const pillY = y + LABEL_OFFSET_Y - LABEL_FONT_SIZE - LABEL_PADDING_Y + 2
120
216
  const pillHeight = LABEL_FONT_SIZE + LABEL_PADDING_Y * 2
@@ -122,7 +218,7 @@ function drawLabel(r: SkiaRenderer, canvas: Canvas, x: number, y: number, cursor
122
218
  r.labelParagraphCache.use(
123
219
  r.ck,
124
220
  provider,
125
- cursor.name,
221
+ label,
126
222
  LABEL_FONT_SIZE,
127
223
  LABEL_MAX_WIDTH,
128
224
  agent ? color : r.ck.WHITE,
@@ -171,11 +267,16 @@ export function drawPresenceCursors(
171
267
  graph: SceneGraph,
172
268
  cursors?: PresenceCursor[]
173
269
  ): void {
270
+ const history = focusHistoryFor(r)
271
+ const drawn = new Set<string>()
174
272
  for (const cursor of cursors ?? []) {
273
+ drawn.add(cursor.id ?? cursor.name)
175
274
  const x = cursor.x * r.zoom + r.panX
176
275
  const y = cursor.y * r.zoom + r.panY
177
276
  drawSelection(r, canvas, graph, cursor)
178
277
  drawArrow(r, canvas, x, y, cursor)
179
278
  drawLabel(r, canvas, x, y, cursor)
180
279
  }
280
+ // A cursor no longer drawn (an agent at rest) leaves no outline to flash on its return.
281
+ for (const key of history.keys()) if (!drawn.has(key)) history.delete(key)
181
282
  }
@@ -1,5 +1,6 @@
1
1
  import type { Canvas, SkPicture } from 'canvaskit-wasm'
2
2
 
3
+ import type { SceneGraph } from '@pairdraw/scene-graph'
3
4
  import type { VisualBounds } from '@pairdraw/scene-graph/geometry'
4
5
 
5
6
  import type { SkiaRenderer } from '#core/canvas/renderer'
@@ -20,6 +21,22 @@ export function withWorldViewport<T>(r: SkiaRenderer, bounds: VisualBounds, draw
20
21
  }
21
22
  }
22
23
 
24
+ /** Record one node, its children included, into a picture of world-space `bounds`. */
25
+ export function recordNodePicture(
26
+ r: SkiaRenderer,
27
+ graph: SceneGraph,
28
+ nodeId: string,
29
+ bounds: VisualBounds,
30
+ signal?: AbortSignal
31
+ ): SkPicture {
32
+ return recordWorldPicture(r, bounds, (canvas) => {
33
+ signal?.throwIfAborted()
34
+ const parentId = graph.getNode(nodeId)?.parentId
35
+ const parent = parentId ? graph.getAbsolutePosition(parentId) : { x: 0, y: 0 }
36
+ r.renderNode(canvas, graph, nodeId, {}, parent.x, parent.y)
37
+ })
38
+ }
39
+
23
40
  /** Record `draw` into a picture of world-space `bounds`. The caller owns the picture. */
24
41
  export function recordWorldPicture(
25
42
  r: SkiaRenderer,
@@ -8,11 +8,16 @@ import type { RenderOverlays, SkiaRenderer } from '#core/canvas/renderer'
8
8
  export interface TransientCanvasPreview {
9
9
  graph: SceneGraph
10
10
  pageId: string
11
- picture: SkPicture
11
+ /** The settled preview; absent while every element enters for the first time. */
12
+ picture?: SkPicture
12
13
  /** Substitute a whole page child, including its proposed descendant layout. */
13
14
  replaceId?: string
14
15
  /** Insert before this page-child index, or append when omitted. */
15
16
  insertIndex?: number
17
+ /** New elements fading in over the picture, with how far their entrance has come (0 to 1). */
18
+ entrance?: { pictures: SkPicture[]; progress: number }
19
+ /** How opaque the whole preview is while a finished call hands off to the committed nodes. */
20
+ fade?: number
16
21
  }
17
22
 
18
23
  function activePreviews(r: SkiaRenderer, graph: SceneGraph): TransientCanvasPreview[] {
@@ -25,6 +30,31 @@ export function hasTransientPreviews(r: SkiaRenderer, graph: SceneGraph): boolea
25
30
  return activePreviews(r, graph).length > 0
26
31
  }
27
32
 
33
+ /** One preview picture, with new elements easing up 4 screen pixels while they fade in. */
34
+ function drawPreview(r: SkiaRenderer, canvas: Canvas, preview: TransientCanvasPreview): void {
35
+ const fade = preview.fade ?? 1
36
+ if (fade <= 0) return
37
+ try {
38
+ if (preview.picture) {
39
+ r.auxFill.setAlphaf(fade)
40
+ canvas.saveLayer(r.auxFill)
41
+ canvas.drawPicture(preview.picture)
42
+ canvas.restore()
43
+ }
44
+ const entrance = preview.entrance
45
+ if (!entrance || entrance.progress <= 0) return
46
+ r.auxFill.setAlphaf(fade * entrance.progress)
47
+ canvas.save()
48
+ canvas.translate(0, ((1 - entrance.progress) * 4) / (r.zoom || 1))
49
+ canvas.saveLayer(r.auxFill)
50
+ for (const picture of entrance.pictures) canvas.drawPicture(picture)
51
+ canvas.restore()
52
+ canvas.restore()
53
+ } finally {
54
+ r.auxFill.setAlphaf(1)
55
+ }
56
+ }
57
+
28
58
  /** Draw at the normal scene z-order, not over the top of unchanged originals. Never used for export. */
29
59
  export function renderPageWithPreviews(
30
60
  r: SkiaRenderer,
@@ -48,12 +78,16 @@ export function renderPageWithPreviews(
48
78
  }
49
79
  }
50
80
  for (let index = 0; index <= children.length; index++) {
51
- for (const preview of insertions.get(index) ?? []) canvas.drawPicture(preview.picture)
81
+ for (const preview of insertions.get(index) ?? []) drawPreview(r, canvas, preview)
52
82
  const id = children[index]
53
83
  if (!id) continue
54
84
  const replacement = replacements.get(id)
55
- if (replacement) canvas.drawPicture(replacement.picture)
56
- else r.renderNode(canvas, graph, id, overlays)
85
+ if (replacement) {
86
+ // A fading replacement uncovers the committed node it replaced, so no frame is blank.
87
+ if (replacement.fade !== undefined && replacement.fade < 1)
88
+ r.renderNode(canvas, graph, id, overlays)
89
+ drawPreview(r, canvas, replacement)
90
+ } else r.renderNode(canvas, graph, id, overlays)
57
91
  }
58
92
  return true
59
93
  }
@@ -34,8 +34,16 @@ export interface PresenceCursor {
34
34
  x: number
35
35
  y: number
36
36
  selection?: string[]
37
+ /** What the label says after the name while the agent works, such as `Writing...`. */
38
+ status?: string
37
39
  /** Outlines in world coordinates of what is not a layer yet, such as streamed JSX. */
38
40
  outline?: Rect[]
41
+ /** The element being written, outlined with a soft pulse while the agent rests on it. */
42
+ focus?: Rect
43
+ /** Whether the focus outline may pulse; false under reduced motion. */
44
+ pulse?: boolean
45
+ /** One sheen over what a finished preview built, drawn once at `at`. */
46
+ sheen?: { rect: Rect; at: number }
39
47
  /** PNG bytes of the mark of what an agent runs on, drawn in its label. */
40
48
  logo?: Uint8Array
41
49
  }
@@ -1,9 +1,10 @@
1
1
  import type { CanvasKit, SkPicture } from 'canvaskit-wasm'
2
2
 
3
+ import type { SceneGraph } from '@pairdraw/scene-graph'
3
4
  import { computeDescendantVisualBounds, type VisualBounds } from '@pairdraw/scene-graph/geometry'
4
5
 
5
6
  import { SkiaRenderer } from '#core/canvas/renderer'
6
- import { recordWorldPicture } from '#core/canvas/renderer/picture'
7
+ import { recordNodePicture, recordWorldPicture } from '#core/canvas/renderer/picture'
7
8
 
8
9
  import type { StagedJSXPreview } from './stage'
9
10
 
@@ -43,6 +44,13 @@ async function recordStaged(
43
44
  export interface JSXPreviewRecorder {
44
45
  /** Record `staged` as a picture the caller owns, even when its request becomes stale. */
45
46
  record(staged: StagedJSXPreview, signal: AbortSignal): Promise<SkPicture | null>
47
+ /** Record one layer of a staged graph, for an entrance drawn apart from the rest. */
48
+ recordNode(
49
+ graph: SceneGraph,
50
+ nodeId: string,
51
+ bounds: VisualBounds,
52
+ signal: AbortSignal
53
+ ): Promise<SkPicture | null>
46
54
  /** Free the private renderer once the records already asked for finish. */
47
55
  destroy(): void
48
56
  }
@@ -89,12 +97,30 @@ export function createJSXPreviewRecorder(ck: CanvasKit): JSXPreviewRecorder {
89
97
  return recordStaged(current, staged, signal)
90
98
  }
91
99
 
100
+ async function recordNodeNext(
101
+ graph: SceneGraph,
102
+ nodeId: string,
103
+ bounds: VisualBounds,
104
+ signal: AbortSignal
105
+ ): Promise<SkPicture | null> {
106
+ signal.throwIfAborted()
107
+ const current = destroyed ? null : await rendererFor()
108
+ if (!current) return null
109
+ signal.throwIfAborted()
110
+ return recordNodePicture(current, graph, nodeId, bounds, signal)
111
+ }
112
+
92
113
  return {
93
114
  record(staged, signal) {
94
115
  const result = queue.then(() => recordNext(staged, signal))
95
116
  queue = result.catch(() => undefined)
96
117
  return result
97
118
  },
119
+ recordNode(graph, nodeId, bounds, signal) {
120
+ const result = queue.then(() => recordNodeNext(graph, nodeId, bounds, signal))
121
+ queue = result.catch(() => undefined)
122
+ return result
123
+ },
98
124
  destroy() {
99
125
  destroyed = true
100
126
  const release = () => {
@@ -1,4 +1,4 @@
1
- import type { RenderResult, TreeNode } from '@pairdraw/design-jsx'
1
+ import type { JSXPreviewNode, RenderResult, TreeNode } from '@pairdraw/design-jsx'
2
2
  import type { SceneGraph } from '@pairdraw/scene-graph'
3
3
 
4
4
  import {
@@ -21,6 +21,8 @@ export interface StagedJSXPreview {
21
21
  replaceId?: string
22
22
  /** Page insertion slot when the preview adds new top-level nodes. */
23
23
  insertIndex?: number
24
+ /** The source position each rendered layer came from, so restages keep an element's identity. */
25
+ sources: ReadonlyMap<string, number>
24
26
  }
25
27
 
26
28
  function contextFits(graph: SceneGraph, rootId: string): boolean {
@@ -57,10 +59,15 @@ export async function stageJSXPreview(
57
59
  graph.variables = structuredClone(source.variables)
58
60
  graph.variableCollections = structuredClone(source.variableCollections)
59
61
  const results: RenderResult[] = []
62
+ const sources = new Map<string, number>()
63
+ const onNode = (element: TreeNode, node: { id: string }) => {
64
+ const sourceStart = (element as JSXPreviewNode).sourceStart
65
+ if (typeof sourceStart === 'number') sources.set(node.id, sourceStart)
66
+ }
60
67
  for (const root of tree.type === '' ? tree.children : [tree]) {
61
68
  if (typeof root === 'string') continue
62
69
  signal?.throwIfAborted()
63
- results.push(await renderTree(graph, root, placement))
70
+ results.push(await renderTree(graph, root, { ...placement, onNode }))
64
71
  }
65
72
  signal?.throwIfAborted()
66
73
  if (results.length === 0) return null
@@ -71,6 +78,7 @@ export async function stageJSXPreview(
71
78
  nodeIds: owner && graph.getNode(owner) ? [owner] : results.map((result) => result.id),
72
79
  renderedIds: results.map((result) => result.id),
73
80
  replaceId: owner,
74
- insertIndex: placement.insertIndex
81
+ insertIndex: placement.insertIndex,
82
+ sources
75
83
  }
76
84
  }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Adds faces that arrive together to the page in one task. WebKit clears its style resolver on
3
+ * every `document.fonts.add` and rebuilds it on the next style update, walking each registered
4
+ * `@property` rule across every live animation, so a burst of font loads that adds one task per
5
+ * face pays that rebuild once per face instead of once per burst.
6
+ */
7
+ export interface FontFaceBatch<T> {
8
+ /** Queues an item and schedules the batch's single add when none is scheduled. */
9
+ push(item: T): void
10
+ /** Adds everything queued now; a no-op when nothing is queued. */
11
+ flush(): void
12
+ }
13
+
14
+ export interface FontFaceBatchOptions<T> {
15
+ add: (item: T) => void
16
+ /** Gets an item whose add failed; the rest of the batch is still added. */
17
+ onError?: (item: T, error: unknown) => void
18
+ /** Runs `flush` once after the caller's batch window, which a test drives by hand. */
19
+ schedule: (run: () => void) => void
20
+ }
21
+
22
+ /** How long fonts loaded for the editor wait, so a burst reaches the page as one change. */
23
+ export const FONT_FACE_BATCH_DELAY_MS = 120
24
+
25
+ export function createFontFaceBatch<T>({
26
+ add,
27
+ onError,
28
+ schedule
29
+ }: FontFaceBatchOptions<T>): FontFaceBatch<T> {
30
+ let queued: T[] = []
31
+ let scheduled = false
32
+
33
+ function flush(): void {
34
+ scheduled = false
35
+ if (queued.length === 0) return
36
+ const batch = queued
37
+ queued = []
38
+ // One face the page refuses must not take the rest of the burst with it, or throw from the
39
+ // timer that called this.
40
+ for (const item of batch) {
41
+ try {
42
+ add(item)
43
+ } catch (error) {
44
+ onError?.(item, error)
45
+ }
46
+ }
47
+ }
48
+
49
+ return {
50
+ push(item: T): void {
51
+ queued.push(item)
52
+ if (scheduled) return
53
+ scheduled = true
54
+ schedule(flush)
55
+ },
56
+ flush
57
+ }
58
+ }
@@ -25,6 +25,7 @@ import {
25
25
  type FontFallbackScript,
26
26
  type LetterFallbackScript
27
27
  } from '#core/text/fallbacks'
28
+ import { createFontFaceBatch, FONT_FACE_BATCH_DELAY_MS } from '#core/text/font/batch'
28
29
  import { BundledFonts, type BundledFontLocator } from '#core/text/font/bundled'
29
30
  import { FamilyFaceCatalog, type HostFontFamily } from '#core/text/font/catalog'
30
31
  import { makeParagraphBuilder, resetFontCollection } from '#core/text/font/collection'
@@ -1104,7 +1105,7 @@ export class FontManager {
1104
1105
  const face = new FontFace(family, data, browserFaceDescriptors(style, data))
1105
1106
  face
1106
1107
  .load()
1107
- .then(() => document.fonts.add(face))
1108
+ .then(() => browserFaces?.push(face))
1108
1109
  .catch(() => {
1109
1110
  console.warn(`Failed to load font "${family}" (${style})`)
1110
1111
  })
@@ -1123,6 +1124,21 @@ function pageDrawsByName(source: FontLoadedSource | undefined): boolean {
1123
1124
 
1124
1125
  export const fontManager = new FontManager()
1125
1126
 
1127
+ /**
1128
+ * Faces loaded for the page's own text, added once per burst. Each `document.fonts.add` makes
1129
+ * WebKit rebuild its style resolver, and a rebuild registers every `@property` rule across every
1130
+ * live animation, so a burst of font loads must not clear the resolver once per face.
1131
+ */
1132
+ const browserFaces = IS_BROWSER
1133
+ ? createFontFaceBatch<FontFace>({
1134
+ add: (face) => document.fonts.add(face),
1135
+ onError: (face) => console.warn(`Failed to add font "${face.family}" to the page`),
1136
+ schedule: (run) => {
1137
+ setTimeout(run, FONT_FACE_BATCH_DELAY_MS)
1138
+ }
1139
+ })
1140
+ : null
1141
+
1126
1142
  function isLetterFallbackScript(script: FontFallbackScript): script is LetterFallbackScript {
1127
1143
  return LETTER_FALLBACK_SCRIPTS.some((letter) => letter === script)
1128
1144
  }
package/src/text/index.ts CHANGED
@@ -3,6 +3,7 @@ export * from './style-runs'
3
3
  export * from './case'
4
4
  export * from './coverage'
5
5
  export * from './font/features'
6
+ export * from './font/batch'
6
7
  export * from './font/manager'
7
8
  export * from './font/sources'
8
9
  export * from './font/style'