@1agh/maude 2.0.6 → 2.0.8

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 (177) hide show
  1. package/apps/studio/access/closure.ts +156 -16
  2. package/apps/studio/access/filters.ts +27 -0
  3. package/apps/studio/actions/agents.ts +54 -0
  4. package/apps/studio/actions/ctx.ts +3 -0
  5. package/apps/studio/actions/defs/sync.ts +13 -0
  6. package/apps/studio/actions/defs/view.ts +59 -3
  7. package/apps/studio/actions/keymap.gen.ts +35 -0
  8. package/apps/studio/actions/menu.ts +2 -0
  9. package/apps/studio/actions/types.ts +3 -1
  10. package/apps/studio/actions.manifest.json +136 -1
  11. package/apps/studio/actions.parity.json +25 -1
  12. package/apps/studio/annotations/ui/ai-seen.tsx +4 -3
  13. package/apps/studio/annotations/ui/ride.ts +2 -1
  14. package/apps/studio/annotations-context-toolbar.tsx +5 -4
  15. package/apps/studio/annotations-layer.tsx +4 -3
  16. package/apps/studio/api.ts +383 -128
  17. package/apps/studio/artboard-activity-overlay.tsx +4 -4
  18. package/apps/studio/artboard-ai-copies.tsx +4 -4
  19. package/apps/studio/artboard-freeze.ts +108 -0
  20. package/apps/studio/artboard-live-gate.tsx +414 -45
  21. package/apps/studio/artboard-live.ts +526 -31
  22. package/apps/studio/artboard-placeholder.ts +197 -0
  23. package/apps/studio/artboard-still-raster.ts +485 -0
  24. package/apps/studio/artboard-still-warmup.ts +221 -0
  25. package/apps/studio/artboard-still.ts +1188 -0
  26. package/apps/studio/artboard-webgl.ts +2 -1
  27. package/apps/studio/asset-placeholder.tsx +237 -132
  28. package/apps/studio/asset-upload-bars.tsx +4 -4
  29. package/apps/studio/bin/_perf-probe-open.mjs +364 -0
  30. package/apps/studio/bin/_perf-shared.mjs +12 -7
  31. package/apps/studio/bin/_pw-launch.mjs +26 -1
  32. package/apps/studio/bin/perf.sh +19 -0
  33. package/apps/studio/bridge/dgn-protocol.ts +35 -0
  34. package/apps/studio/canvas-build-sandbox.ts +69 -13
  35. package/apps/studio/canvas-camera.ts +90 -0
  36. package/apps/studio/canvas-comment-mount.tsx +9 -9
  37. package/apps/studio/canvas-lib.tsx +160 -34
  38. package/apps/studio/canvas-pages-view.tsx +465 -0
  39. package/apps/studio/canvas-pages.ts +305 -0
  40. package/apps/studio/canvas-rows.ts +62 -0
  41. package/apps/studio/canvas-shell.tsx +62 -16
  42. package/apps/studio/canvas-theme-icons.tsx +41 -0
  43. package/apps/studio/canvas-theme-pill.tsx +15 -6
  44. package/apps/studio/canvas-theme.ts +12 -11
  45. package/apps/studio/client/app.jsx +7 -0
  46. package/apps/studio/client/canvas-url.js +23 -1
  47. package/apps/studio/client/hooks/canvas-origin-probe.js +34 -0
  48. package/apps/studio/client/hooks/use-action-keys.jsx +3 -1
  49. package/apps/studio/client/hooks/use-canvas-bridge.jsx +1 -1
  50. package/apps/studio/client/hooks/use-palette-and-panels.jsx +23 -6
  51. package/apps/studio/client/hooks/use-photo-and-timeline.jsx +16 -8
  52. package/apps/studio/client/hooks/use-project-data.jsx +11 -2
  53. package/apps/studio/client/hooks/use-shell-core.jsx +19 -9
  54. package/apps/studio/client/index-loader.ts +41 -21
  55. package/apps/studio/client/inspector/inspector-panel.jsx +35 -14
  56. package/apps/studio/client/shell/viewport.jsx +32 -16
  57. package/apps/studio/client/tree/tree.jsx +17 -0
  58. package/apps/studio/client/v2/canvases/canvases-panel.jsx +24 -0
  59. package/apps/studio/client/v2/canvases/use-canvas-index.js +24 -1
  60. package/apps/studio/client/v2/code/canvas-error-card.jsx +14 -2
  61. package/apps/studio/client/v2/code/canvas-imports-card.jsx +7 -3
  62. package/apps/studio/client/v2/code/canvas-imports-words.js +16 -0
  63. package/apps/studio/client/v2/code/canvas-waiting-card.jsx +59 -5
  64. package/apps/studio/client/v2/code/canvas-waiting.js +18 -1
  65. package/apps/studio/client/v2/code/code-consent-sheet.jsx +87 -0
  66. package/apps/studio/client/v2/code/code-consent.js +67 -0
  67. package/apps/studio/client/v2/diagnostics/sync-details/sync-details-model.js +62 -10
  68. package/apps/studio/client/v2/diagnostics/sync-details/sync-details.jsx +314 -5
  69. package/apps/studio/client/v2/layers/artboard-layers.jsx +24 -5
  70. package/apps/studio/client/v2/layers/layers-request.js +49 -0
  71. package/apps/studio/client/v2/left-panel/canvases-pinned.jsx +93 -3
  72. package/apps/studio/client/v2/left-panel/ds-boards.js +39 -0
  73. package/apps/studio/client/v2/left-panel/left-panel-model.js +13 -0
  74. package/apps/studio/client/v2/left-panel/left-panel.css +24 -0
  75. package/apps/studio/client/v2/shell/canvas-crash-guard.js +40 -5
  76. package/apps/studio/client/v2/shell/canvas-mode.js +17 -1
  77. package/apps/studio/client/v2/shell/floating-panels.css +37 -1
  78. package/apps/studio/client/v2/shell/mode-switch.jsx +14 -3
  79. package/apps/studio/client/v2/shell/opening-skeleton.js +118 -0
  80. package/apps/studio/client/v2/shell/opening-skeleton.jsx +103 -0
  81. package/apps/studio/client/v2/shell/pages-pick.js +49 -0
  82. package/apps/studio/client/v2/shell/project-menu-model.js +3 -0
  83. package/apps/studio/client/v2/shell/share-cluster-host.jsx +3 -1
  84. package/apps/studio/client/v2/shell/shell-parity.json +8 -0
  85. package/apps/studio/client/v2/shell/use-canvas-mode-lane.js +30 -0
  86. package/apps/studio/comments-overlay.tsx +12 -11
  87. package/apps/studio/config-reload-wire.ts +4 -1
  88. package/apps/studio/config.schema.json +6 -0
  89. package/apps/studio/context.ts +37 -1
  90. package/apps/studio/dist/client.bundle.js +720 -720
  91. package/apps/studio/dist/comment-mount.js +2 -2
  92. package/apps/studio/dist/styles.css +1 -1
  93. package/apps/studio/ds/board/board.css +6 -0
  94. package/apps/studio/ds/board/ds-bar.tsx +21 -3
  95. package/apps/studio/ds/board/specimen.tsx +6 -3
  96. package/apps/studio/ds/board/template.ts +39 -19
  97. package/apps/studio/ds/cli.ts +7 -1
  98. package/apps/studio/ds/component-list.ts +2 -1
  99. package/apps/studio/ds/default.ts +6 -1
  100. package/apps/studio/ds/ds-resolver.ts +58 -8
  101. package/apps/studio/ds/engine.ts +3 -1
  102. package/apps/studio/ds/folder-systems.ts +199 -0
  103. package/apps/studio/ds/pairing-css.ts +2 -1
  104. package/apps/studio/ds/rename.ts +10 -3
  105. package/apps/studio/ds/shell-fallbacks.ts +2 -1
  106. package/apps/studio/ds/upgrade-stage.ts +6 -2
  107. package/apps/studio/editing/ai-point-ring.tsx +4 -4
  108. package/apps/studio/editing/ai-scope-outline.tsx +4 -4
  109. package/apps/studio/editing/align-bar.tsx +4 -4
  110. package/apps/studio/editing/anchor-report.ts +3 -2
  111. package/apps/studio/editing/artboard-handles.tsx +4 -4
  112. package/apps/studio/editing/corner-dots.tsx +4 -4
  113. package/apps/studio/editing/crop-mode.tsx +4 -4
  114. package/apps/studio/editing/drop-chip.tsx +4 -4
  115. package/apps/studio/editing/empty-report.ts +3 -2
  116. package/apps/studio/editing/focus-model.tsx +4 -4
  117. package/apps/studio/editing/gap-bands.tsx +3 -3
  118. package/apps/studio/editing/hold-rings.tsx +4 -4
  119. package/apps/studio/editing/lock-badge.tsx +3 -3
  120. package/apps/studio/editing/long-page.ts +23 -1
  121. package/apps/studio/editing/nudge.tsx +2 -2
  122. package/apps/studio/editing/page-activity.ts +4 -3
  123. package/apps/studio/editing/paste-bar.tsx +4 -4
  124. package/apps/studio/editing/pen/overlay.tsx +4 -3
  125. package/apps/studio/editing/place-freely.tsx +3 -3
  126. package/apps/studio/editing/reorder-slot.tsx +3 -3
  127. package/apps/studio/editing/selection.tsx +4 -4
  128. package/apps/studio/editing/size-ghost.tsx +4 -4
  129. package/apps/studio/editing/tab-order.tsx +4 -4
  130. package/apps/studio/editing/text-in-place.tsx +4 -4
  131. package/apps/studio/editing/undo-preview.tsx +3 -3
  132. package/apps/studio/equal-spacing-handles.tsx +3 -3
  133. package/apps/studio/frame-clock.ts +89 -0
  134. package/apps/studio/http.ts +66 -32
  135. package/apps/studio/index/ds-usage.ts +4 -1
  136. package/apps/studio/index-cache.ts +272 -0
  137. package/apps/studio/inspect-overlay.tsx +4 -3
  138. package/apps/studio/inspect.ts +7 -2
  139. package/apps/studio/link-chip-overlay.tsx +4 -3
  140. package/apps/studio/measure-overlay.tsx +4 -3
  141. package/apps/studio/present/pointer-dot.tsx +3 -2
  142. package/apps/studio/present/present-media.ts +6 -2
  143. package/apps/studio/present/present-stage.tsx +3 -2
  144. package/apps/studio/preview/preview-camera.tsx +3 -2
  145. package/apps/studio/routes/sync.ts +176 -0
  146. package/apps/studio/safe-zones-overlay.tsx +3 -2
  147. package/apps/studio/skip-dirs.ts +103 -47
  148. package/apps/studio/studio-page-host.ts +70 -0
  149. package/apps/studio/sync/code-module-consent.ts +86 -0
  150. package/apps/studio/sync/file-ledger.ts +24 -0
  151. package/apps/studio/sync/file-plane.ts +458 -53
  152. package/apps/studio/sync/held-deletes.ts +230 -0
  153. package/apps/studio/sync/hubs-config.ts +59 -2
  154. package/apps/studio/sync/index.ts +99 -15
  155. package/apps/studio/sync/seed-progress.ts +92 -2
  156. package/apps/studio/sync/settings.ts +36 -1
  157. package/apps/studio/sync/status.ts +15 -1
  158. package/apps/studio/sync/writer-registry.ts +5 -0
  159. package/apps/studio/text-caret.ts +5 -3
  160. package/apps/studio/toolbars/toolbar-rect.ts +3 -2
  161. package/apps/studio/use-annotation-resize.tsx +4 -3
  162. package/apps/studio/use-artboard-drag.tsx +3 -2
  163. package/apps/studio/use-element-resize.tsx +7 -6
  164. package/apps/studio/use-grid-track-handles.tsx +5 -4
  165. package/apps/studio/use-selection-set.tsx +64 -4
  166. package/apps/studio/use-spacing-handles.tsx +7 -6
  167. package/apps/studio/video-comp.tsx +55 -42
  168. package/apps/studio/video-play-overlay.tsx +4 -3
  169. package/apps/studio/whats-new.json +27 -0
  170. package/cli/commands/design.mjs +5 -2
  171. package/cli/commands/hub.mjs +10 -1
  172. package/cli/lib/bash-guard.mjs +14 -0
  173. package/cli/lib/design-help.gen.mjs +1 -1
  174. package/cli/lib/design-link.mjs +42 -17
  175. package/cli/lib/hub-purge.mjs +183 -0
  176. package/package.json +8 -8
  177. package/plugins/design/templates/_shell.html +164 -1
@@ -1,37 +1,74 @@
1
1
  /**
2
2
  * @file artboard-live-gate.tsx — 2.0.5: the React half of artboard-live.ts
3
3
  * @scope apps/studio/artboard-live-gate.tsx (used by canvas-lib.tsx's DCArtboardBody + DesignCanvas)
4
- * @purpose One `LiveRegistry` + one `WebglBudget` per canvas document, fed by two shared
5
- * IntersectionObservers. `ArtboardLiveGate` wraps an artboard's children: it hands them
6
- * `useArtboardActive()` / `useArtboardSeen()`, pauses the artboard's `<video>`s and CSS
7
- * animations while it sleeps, and in `light` mode mounts the children only while it is near
8
- * (a placeholder of the last measured height stands in, so nothing reflows).
9
- * `holdAllLive()` is the in-frame export's guarantee: every artboard live for the capture.
4
+ * @purpose One `LiveRegistry` + one `WebglBudget` per canvas document, fed by three shared
5
+ * IntersectionObservers (prefetch ring, on screen, keep ring). `ArtboardLiveGate` wraps an
6
+ * artboard's children: it hands them `useArtboardActive()` / `useArtboardSeen()`, pauses the
7
+ * artboard's `<video>`s and CSS animations while it sleeps, and mounts the children only while the
8
+ * registry admits them. `holdAllLive()` is the in-frame export's guarantee: every artboard live
9
+ * for the capture.
10
+ *
11
+ * 2.0.7: in `sleep` too the children mount only while admitted (the mount ring: on screen + a
12
+ * prefetch ring, released when far, within a DOM budget). First mounts go through the registry's
13
+ * queue paced per frame, so it never blocks a pan or a zoom. What stands in is a same-size
14
+ * placeholder in the artboard's own colours (artboard-placeholder.ts) — never the bare frame,
15
+ * which on a dark design system is a black box.
10
16
  */
11
17
 
12
18
  import {
13
19
  createContext,
14
20
  type ReactNode,
15
21
  type RefObject,
16
- useCallback,
17
22
  useContext,
18
23
  useEffect,
19
24
  useLayoutEffect,
20
25
  useRef,
26
+ useState,
21
27
  useSyncExternalStore,
22
28
  } from 'react';
29
+ import { ARTBOARD_FREEZE_CSS, ArtboardFreeze } from './artboard-freeze.ts';
23
30
  import {
24
31
  type ArtboardLiveState,
32
+ KEEP_MARGIN,
25
33
  type LiveMode,
26
34
  LiveRegistry,
27
35
  NEAR_MARGIN,
28
36
  resolveLiveMode,
29
37
  } from './artboard-live.ts';
38
+ import {
39
+ backgroundOf,
40
+ canvasBackground,
41
+ choosePlaceholder,
42
+ knownBackground,
43
+ rememberBackground,
44
+ rememberPicture,
45
+ subscribeCanvasBackground,
46
+ takePicture,
47
+ } from './artboard-placeholder.ts';
48
+ import {
49
+ ARTBOARD_STILL_CSS,
50
+ inOverviewCut,
51
+ resolveStillMode,
52
+ StillController,
53
+ } from './artboard-still.ts';
54
+ import { rasterTimings, startStill } from './artboard-still-raster.ts';
55
+ import { StillWarmup, WARM_ENABLED } from './artboard-still-warmup.ts';
30
56
  import { WebglBudget } from './artboard-webgl.ts';
57
+ import { frameFreezeApi, frameFrozen, frameRaf, onFrameFreeze } from './frame-clock.ts';
31
58
 
32
- const ALL_LIVE: ArtboardLiveState = { live: true, seen: true };
59
+ const ALL_LIVE: ArtboardLiveState = { live: true, seen: true, media: true };
33
60
  const LiveContext = createContext<ArtboardLiveState>(ALL_LIVE);
34
61
 
62
+ /**
63
+ * May this artboard's media load (a Player, a `<video>` with its src)? Follows the mount ring: near or on screen,
64
+ * kept inside the keep ring. A video artboard is mounted wherever it is (its comp registers itself), so a
65
+ * `VideoComp` mounts its Player only while this holds — far away it fetches and decodes nothing (2.0.7). Always
66
+ * `true` outside an artboard, in captures and exports.
67
+ */
68
+ export function useArtboardMedia(): boolean {
69
+ return useContext(LiveContext).media;
70
+ }
71
+
35
72
  /**
36
73
  * Is this artboard running live? `false` while it is offscreen or the canvas is zoomed out to its overview —
37
74
  * pause a render loop, a video, a simulation. Always `true` outside an artboard, in captures and exports.
@@ -41,23 +78,60 @@ export function useArtboardActive(): boolean {
41
78
  }
42
79
 
43
80
  /**
44
- * Has this artboard been near the viewport yet? Gate an expensive first mount on it (the design-system
45
- * board's specimens do). Always `true` in captures and exports.
81
+ * Are this artboard's children mounted now (it is on screen or near it)? Gate an expensive first mount on it
82
+ * (the design-system board's specimens do). Always `true` in captures and exports.
46
83
  */
47
84
  export function useArtboardSeen(): boolean {
48
85
  return useContext(LiveContext).seen;
49
86
  }
50
87
 
88
+ /**
89
+ * Is the camera moving right now (a pan, a zoom)? While it is, Maude holds the artboards' frame callbacks and
90
+ * pauses their CSS animations and silent videos; a timer (setInterval / setTimeout) is the canvas's own — skip
91
+ * a tick while this is true. Opt-in: only a component that calls it re-renders when a gesture starts and stops.
92
+ * Always `false` outside the canvas frame's shell (tests, captures never move the camera).
93
+ */
94
+ export function useArtboardGesture(): boolean {
95
+ return useSyncExternalStore(onFrameFreeze, frameFrozen, () => false);
96
+ }
97
+
51
98
  interface Runtime {
52
99
  registry: LiveRegistry;
53
100
  budget: WebglBudget | null;
54
101
  near: IntersectionObserver | null;
55
102
  visible: IntersectionObserver | null;
103
+ keep: IntersectionObserver | null;
56
104
  ids: WeakMap<Element, string>;
105
+ /** 2.0.7 — the overview level: artboards under the cut show their still (WebKit). */
106
+ stills: StillController | null;
107
+ /** 2.0.7 — draws the stills of artboards that never mounted, one at a time while nothing moves. */
108
+ warmup: StillWarmup | null;
109
+ /** the article of each observed artboard (the warm-up's distance and cut, the gesture freeze) */
110
+ articles: Map<string, HTMLElement>;
111
+ /** 2.0.7 — pauses the awake artboards' own animations and silent videos while the camera moves. */
112
+ freeze: ArtboardFreeze | null;
57
113
  }
58
114
 
59
- let runtime: Runtime | null = null;
60
- let runtimeSearch: string | null = null;
115
+ /**
116
+ * The runtime lives on the canvas DOCUMENT, not in this module. Each canvas module bundles its own copy of
117
+ * canvas-lib (Bun.build inlines `@maude/canvas-lib`), so an edit's soft reload evaluates a fresh copy of this file:
118
+ * a module-level runtime started every artboard of the remounted canvas as a placeholder again, just as the shell
119
+ * reselected after `loaded` — the selected clip was gone (2.0.7 video regression). Bump `RUNTIME_SHAPE` when
120
+ * `Runtime` or `LiveRegistry` changes shape (a canvas-lib change hard-reloads the frame anyway).
121
+ */
122
+ const RUNTIME_SHAPE = 5;
123
+ const RUNTIME_KEY = '__maudeArtboardLive';
124
+ interface RuntimeSlot {
125
+ shape: number;
126
+ search: string;
127
+ runtime: Runtime;
128
+ }
129
+ function runtimeSlot(): RuntimeSlot | null {
130
+ const slot = (window as unknown as Record<string, unknown>)[RUNTIME_KEY] as
131
+ | RuntimeSlot
132
+ | undefined;
133
+ return slot && slot.shape === RUNTIME_SHAPE ? slot : null;
134
+ }
61
135
 
62
136
  function embedded(): boolean {
63
137
  try {
@@ -69,32 +143,60 @@ function embedded(): boolean {
69
143
 
70
144
  /** Test hook: forget the per-document runtime (the URL decides the mode at first use). */
71
145
  export function resetArtboardLive(): void {
72
- runtime = null;
73
- runtimeSearch = null;
146
+ if (typeof window === 'undefined') return;
147
+ delete (window as unknown as Record<string, unknown>)[RUNTIME_KEY];
148
+ }
149
+
150
+ /** WebKit / WKWebView — the same test as canvas-lib's `IS_WEBKIT`. */
151
+ function isWebkit(): boolean {
152
+ return (
153
+ typeof navigator !== 'undefined' &&
154
+ (/apple/i.test(navigator.vendor || '') ||
155
+ (typeof window !== 'undefined' && 'GestureEvent' in window))
156
+ );
157
+ }
158
+
159
+ /** Distance (px) of an observed artboard's centre to the viewport centre — the mount queue's order. */
160
+ function centreDistance(en: IntersectionObserverEntry): number {
161
+ const b = en.boundingClientRect;
162
+ if (!b) return Number.POSITIVE_INFINITY;
163
+ const W = typeof innerWidth === 'number' ? innerWidth : 0;
164
+ const H = typeof innerHeight === 'number' ? innerHeight : 0;
165
+ return Math.hypot(b.left + b.width / 2 - W / 2, b.top + b.height / 2 - H / 2);
74
166
  }
75
167
 
76
168
  function getRuntime(): Runtime | null {
77
169
  if (typeof window === 'undefined') return null;
78
- if (runtime && runtimeSearch === window.location.search) return runtime;
170
+ const held = runtimeSlot();
171
+ if (held && held.search === window.location.search) return held.runtime;
79
172
  const mode: LiveMode = resolveLiveMode({ search: window.location.search, embedded: embedded() });
80
- runtimeSearch = window.location.search;
81
173
  const schedule = (fn: () => void) =>
82
- typeof requestAnimationFrame === 'function' ? requestAnimationFrame(fn) : setTimeout(fn, 16);
174
+ typeof requestAnimationFrame === 'function' ? frameRaf(fn) : setTimeout(fn, 16);
83
175
  const registry = new LiveRegistry(mode, schedule);
84
176
  const ids = new WeakMap<Element, string>();
85
177
  let near: IntersectionObserver | null = null;
86
178
  let visible: IntersectionObserver | null = null;
179
+ let keep: IntersectionObserver | null = null;
87
180
  let budget: WebglBudget | null = null;
88
181
  if (mode !== 'off' && typeof IntersectionObserver === 'function') {
89
182
  near = new IntersectionObserver(
90
183
  (entries) => {
91
184
  for (const en of entries) {
92
185
  const id = ids.get(en.target);
93
- if (id) registry.setNear(id, en.isIntersecting);
186
+ if (id) registry.setNear(id, en.isIntersecting, centreDistance(en));
94
187
  }
95
188
  },
96
189
  { rootMargin: NEAR_MARGIN }
97
190
  );
191
+ keep = new IntersectionObserver(
192
+ (entries) => {
193
+ for (const en of entries) {
194
+ const id = ids.get(en.target);
195
+ if (id) registry.setKeep(id, en.isIntersecting);
196
+ }
197
+ },
198
+ { rootMargin: KEEP_MARGIN }
199
+ );
98
200
  visible = new IntersectionObserver((entries) => {
99
201
  for (const en of entries) {
100
202
  const id = ids.get(en.target);
@@ -104,16 +206,139 @@ function getRuntime(): Runtime | null {
104
206
  budget = new WebglBudget((id) => (id ? registry.isLive(id) : true));
105
207
  budget.install(window as Window & typeof globalThis);
106
208
  registry.onChange(() => budget?.update());
209
+ // 2.0.7 — WebKit: with many artboards on screen (`crowded`, LAYER_MAX_VISIBLE) they stop being a compositing
210
+ // layer each. WebKit rasterizes every such layer at full size whatever the world's scale, so 40 artboards on
211
+ // screen at 8 % were 40 full-size rasters per frame (seconds per pan frame on the mascot canvas, ~40 ms
212
+ // without). Switched only outside a gesture, so a pinch never re-rasters mid-flight. Blink keeps its layers.
213
+ if (isWebkit()) {
214
+ const sync = () => {
215
+ if (registry.isInteracting()) return;
216
+ document.documentElement.toggleAttribute('data-dc-crowded', registry.crowded());
217
+ };
218
+ registry.onChange(sync);
219
+ registry.onSettle(sync);
220
+ }
107
221
  (window as unknown as { __maudeWebglStats?: () => unknown }).__maudeWebglStats = () => ({
108
222
  ...budget?.stats(),
109
223
  mode,
110
224
  visible: registry.visibleCount(),
225
+ mounted: registry.mountedCount(),
226
+ mountedNodes: Math.round(registry.mountedWeight()),
227
+ // 2.0.7 — a light canvas mounts every artboard (v2-207-light-canvas-mounts-all); -1 = not known yet
228
+ light: registry.isLight(),
229
+ estimatedNodes: Math.round(registry.estimatedWeight()),
230
+ churn: registry.churnStats(),
111
231
  });
112
232
  }
113
- runtime = { registry, budget, near, visible, ids };
233
+ const articles = new Map<string, HTMLElement>();
234
+ // 2.0.7 — the gesture freeze: while the camera moves the awake artboards' CSS animations, scripted Web
235
+ // Animations and silent videos pause (artboard-freeze.ts); their frame callbacks wait in the shell's gate.
236
+ let freeze: ArtboardFreeze | null = null;
237
+ if (mode !== 'off') {
238
+ const fz = new ArtboardFreeze();
239
+ onFrameFreeze((on) => (on ? fz.freeze(articles.values()) : fz.thaw()));
240
+ freeze = fz;
241
+ (window as unknown as { __maudeFreezeStats?: () => unknown }).__maudeFreezeStats = () => ({
242
+ ...fz.stats(),
243
+ frame: frameFreezeApi()?.stats() ?? null,
244
+ });
245
+ }
246
+ let stills: StillController | null = null;
247
+ let warmup: StillWarmup | null = null;
248
+ if (
249
+ resolveStillMode({ search: window.location.search, webkit: isWebkit(), liveMode: mode }) ===
250
+ 'on'
251
+ ) {
252
+ const st = new StillController({ job: startStill, info: (id) => registry.slotInfo(id) });
253
+ registry.onSettle(() => st.settle());
254
+ // 2.0.7 — no mid-gesture wait for artboards under the cut (decision v2-207-drop-undercut-midgesture-wait): in Safari
255
+ // the wait alone put the 10 % sweep's tail over base (p99 ~80 -> ~120 ms, worst second 52 -> 31 frames), since the
256
+ // waiting artboards stayed on screen as placeholders through the pan; the ring's mid-gesture hold stays
257
+ stills = st;
258
+ const w = window as unknown as Record<string, unknown>;
259
+ w.__maudeStillStats = () => ({ ...st.stats(), raster: { ...rasterTimings } });
260
+ // the views each artboard went through (the flicker count of the overview level)
261
+ w.__maudeStillHistory = (id?: string) => st.history(id);
262
+ // the last drawings: slices, the longest one, when the walk / the decode / the drawing ended
263
+ w.__maudeStillJobs = () => st.jobs();
264
+ if (mode === 'sleep' && WARM_ENABLED) {
265
+ warmup = startWarmup(registry, st, articles);
266
+ const wu = warmup;
267
+ w.__maudeStillWarm = () => ({ ...wu.stats(), cache: st.cache.stats() });
268
+ }
269
+ }
270
+ const runtime: Runtime = {
271
+ registry,
272
+ budget,
273
+ near,
274
+ visible,
275
+ keep,
276
+ ids,
277
+ stills,
278
+ warmup,
279
+ articles,
280
+ freeze,
281
+ };
282
+ const slot: RuntimeSlot = { shape: RUNTIME_SHAPE, search: window.location.search, runtime };
283
+ (window as unknown as Record<string, unknown>)[RUNTIME_KEY] = slot;
114
284
  return runtime;
115
285
  }
116
286
 
287
+ /** The ring's own mounts are done when it mounted nothing for this long (ms) — the warm-up waits for them. */
288
+ const WARM_RING_QUIET_MS = 300;
289
+
290
+ /** The stills' warm-up (artboard-still-warmup.ts) on this document's registry and still controller. */
291
+ function startWarmup(
292
+ registry: LiveRegistry,
293
+ st: StillController,
294
+ articles: Map<string, HTMLElement>
295
+ ): StillWarmup {
296
+ const now = () => performance.now();
297
+ const W = () => (typeof innerWidth === 'number' ? innerWidth : 0);
298
+ const H = () => (typeof innerHeight === 'number' ? innerHeight : 0);
299
+ const wu = new StillWarmup({
300
+ now,
301
+ later: (fn, ms) => setTimeout(fn, ms),
302
+ cancel: (h) => clearTimeout(h as ReturnType<typeof setTimeout>),
303
+ candidates: () =>
304
+ registry.warmCandidates().map((k) => {
305
+ const a = articles.get(k.id);
306
+ if (!a) return k;
307
+ const r = a.getBoundingClientRect();
308
+ return {
309
+ ...k,
310
+ dist: Math.hypot(r.left + r.width / 2 - W() / 2, r.top + r.height / 2 - H() / 2),
311
+ };
312
+ }),
313
+ wanted: (id) => {
314
+ const a = articles.get(id);
315
+ return !!a && inOverviewCut(a.offsetWidth, st.zoomNow());
316
+ },
317
+ hasStill: (id) => st.hasStill(id),
318
+ mount: (id) => registry.setWarm(id, true),
319
+ release: (id) => registry.setWarm(id, false),
320
+ mounted: (id) => registry.isMounted(id),
321
+ quiet: () =>
322
+ st.quiet(true) &&
323
+ !registry.isInteracting() &&
324
+ now() - registry.lastMountAt() >= WARM_RING_QUIET_MS,
325
+ lastActivity: () => st.lastActivity(),
326
+ cacheRoom: () => {
327
+ const c = st.cache.stats();
328
+ const each = c.n ? c.bytes / c.n : 0;
329
+ return c.bytes + 2 * each <= st.cache.max;
330
+ },
331
+ });
332
+ st.onStill((id, ok) => wu.stillDone(id, ok));
333
+ registry.onSettle(() => wu.wake());
334
+ // any input pauses it at once (capture: before the canvas's own handlers; passive: never delays them)
335
+ const onInput = () => wu.input();
336
+ for (const type of ['pointerdown', 'pointermove', 'wheel', 'keydown', 'touchstart'])
337
+ window.addEventListener(type, onInput, { capture: true, passive: true });
338
+ wu.kick();
339
+ return wu;
340
+ }
341
+
117
342
  /** The mode this canvas document runs in. */
118
343
  export function artboardLiveMode(): LiveMode {
119
344
  return getRuntime()?.registry.mode ?? 'off';
@@ -129,16 +354,26 @@ export async function holdAllLive(): Promise<() => void> {
129
354
  const release = rt.registry.holdAllLive();
130
355
  rt.budget?.apply();
131
356
  if (typeof requestAnimationFrame === 'function')
132
- await new Promise<void>((r) => requestAnimationFrame(() => requestAnimationFrame(() => r())));
357
+ await new Promise<void>((r) => frameRaf(() => frameRaf(() => r())));
133
358
  return release;
134
359
  }
135
360
 
361
+ /**
362
+ * The viewport wrote this zoom (canvas-lib's applyViewport, every frame of a gesture): small artboards swap to
363
+ * their stills on the first scale step of a gesture. Cheap — a comparison per registered artboard.
364
+ */
365
+ export function noteGestureZoom(zoom: number): void {
366
+ getRuntime()?.stills?.noteZoom(zoom);
367
+ }
368
+
136
369
  /** Feeds the published (settle-cadence) zoom and the gesture flag. Renders nothing. */
137
370
  export function ArtboardLiveFeed({ zoom, interacting }: { zoom: number; interacting: boolean }) {
138
371
  useEffect(() => {
139
372
  const rt = getRuntime();
140
373
  if (!rt || rt.registry.mode === 'off') return;
141
374
  rt.registry.setZoom(zoom);
375
+ rt.stills?.setZoom(zoom);
376
+ rt.warmup?.wake();
142
377
  }, [zoom]);
143
378
  useEffect(() => {
144
379
  const rt = getRuntime();
@@ -182,32 +417,62 @@ function useMediaSleep(articleRef: RefObject<HTMLElement | null>, live: boolean,
182
417
  }, [articleRef, live, on]);
183
418
  }
184
419
 
420
+ /** How long after a first mount the placeholder's picture is taken (images and fonts have come in). */
421
+ const PICTURE_DELAY_MS = 900;
422
+
185
423
  export function ArtboardLiveGate({
186
424
  id,
187
425
  articleRef,
188
426
  bodyRef,
189
427
  heightFloor,
428
+ background,
429
+ pinned = false,
430
+ eager = false,
190
431
  children,
191
432
  }: {
192
433
  id: string;
193
434
  articleRef: RefObject<HTMLElement | null>;
194
435
  bodyRef: RefObject<HTMLElement | null>;
195
436
  heightFloor: number;
437
+ /** the artboard's own `background` prop — the colour a never-rendered placeholder takes */
438
+ background?: string;
439
+ /** the active artboard, or the one holding the selection: never released (the shell works in its DOM) */
440
+ pinned?: boolean;
441
+ /** a video artboard: always mounted (its comp registers itself only while mounted) */
442
+ eager?: boolean;
196
443
  children: ReactNode;
197
444
  }) {
198
445
  const rt = getRuntime();
199
446
  const on = !!rt && rt.registry.mode !== 'off';
200
- // Stable per (runtime, id): a new subscribe each render would drop the slot (and its near state) in between.
201
- const subscribe = useCallback(
202
- (fn: () => void) => (on && rt ? rt.registry.subscribe(id, fn) : () => {}),
203
- [on, rt, id]
204
- );
205
- const state = useSyncExternalStore(
206
- subscribe,
207
- () => (on && rt ? rt.registry.get(id) : ALL_LIVE),
208
- () => ALL_LIVE
447
+ const [state, setState] = useState<ArtboardLiveState>(() =>
448
+ on && rt ? rt.registry.get(id) : ALL_LIVE
209
449
  );
450
+ // Subscribe before the observers report (layout effects run before effects). Every change applies at once:
451
+ // a first mount as a React transition was measured and dropped — the canvas's own sync updates kept
452
+ // restarting it, so an on-screen artboard waited for the transition lane's ~5 s expiry (first paint 1.3 s →
453
+ // 15 s on math-learning-spaces). The queue's pacing (one per frame, one per GESTURE_MOUNT_GAP_MS mid-gesture)
454
+ // is what keeps a mount from blocking a pan.
455
+ useLayoutEffect(() => {
456
+ if (!on || !rt) return;
457
+ const off = rt.registry.subscribe(id, setState);
458
+ setState(rt.registry.get(id));
459
+ return off;
460
+ }, [on, rt, id]);
210
461
  const { live, seen } = state;
462
+ // A skeleton drawn before any artboard of the canvas rendered took the mid tone: it repaints once the canvas
463
+ // colour is known (only while it is a placeholder — a mounted artboard never re-renders for it).
464
+ useSyncExternalStore(
465
+ subscribeCanvasBackground,
466
+ () => (on && !seen ? canvasBackground() : ''),
467
+ () => ''
468
+ );
469
+
470
+ useEffect(() => {
471
+ if (on && rt) rt.registry.setPinned(id, pinned);
472
+ }, [on, rt, id, pinned]);
473
+ useEffect(() => {
474
+ if (on && rt) rt.registry.setEager(id, eager);
475
+ }, [on, rt, id, eager]);
211
476
 
212
477
  // Observe the artboard (the article — its size is stable whether its children are mounted or not).
213
478
  useEffect(() => {
@@ -216,22 +481,50 @@ export function ArtboardLiveGate({
216
481
  if (!rt.near || !rt.visible) {
217
482
  // No IntersectionObserver: fail open, everything near and on screen.
218
483
  rt.registry.setNear(id, true);
484
+ rt.registry.setVisible(id, true);
219
485
  return;
220
486
  }
221
487
  rt.ids.set(el, id);
488
+ rt.articles.set(id, el);
222
489
  rt.near.observe(el);
223
490
  rt.visible.observe(el);
491
+ rt.keep?.observe(el);
224
492
  return () => {
225
493
  rt.near?.unobserve(el);
226
494
  rt.visible?.unobserve(el);
227
- rt.registry.setNear(id, false);
228
- rt.registry.setVisible(id, false);
495
+ rt.keep?.unobserve(el);
496
+ rt.ids.delete(el);
497
+ if (rt.articles.get(id) === el) rt.articles.delete(id);
498
+ // an edit's remount: the new gate already holds the id and its observers report on its own article —
499
+ // clearing near/visible here would put the artboard to sleep (and pause its video) for a frame
500
+ rt.registry.unobserve(id);
229
501
  };
230
502
  }, [articleRef, id, on, rt]);
231
503
 
232
504
  useMediaSleep(articleRef, live, on);
233
505
 
234
- // light mode: remember the height the children had, so the placeholder holds it.
506
+ // 2.0.7 — the overview level (artboard-still.ts): registered while the children are mounted; under the cut the
507
+ // artboard shows its still. The selected / active artboard and a video one never do (`pinned`, `eager`).
508
+ const stills = on && rt ? rt.stills : null;
509
+ const flags = useRef({ pinned, eager, bg: background });
510
+ flags.current = { pinned, eager, bg: background };
511
+ // A layout effect: an artboard mounting under the cut is hidden behind its still (or cover) before it paints.
512
+ useLayoutEffect(() => {
513
+ const article = articleRef.current;
514
+ const body = bodyRef.current;
515
+ if (!stills || !seen || !article || !body) return;
516
+ const f = flags.current;
517
+ return stills.register(id, { article, body, bg: knownBackground(id, f.bg) }, f);
518
+ }, [stills, seen, id, articleRef, bodyRef]);
519
+ useEffect(() => {
520
+ stills?.update(id, { pinned, eager });
521
+ }, [stills, id, pinned, eager]);
522
+ // a wake or a sleep changes what is painted (paused animations, a WebGL picture): draw it again
523
+ useEffect(() => {
524
+ stills?.markDirty(id, live ? 'woke' : 'slept');
525
+ }, [stills, id, live]);
526
+
527
+ // Remember the height the children had, so the placeholder holds it.
235
528
  const lastHeight = useRef(0);
236
529
  useLayoutEffect(() => {
237
530
  if (!seen) return;
@@ -241,27 +534,103 @@ export function ArtboardLiveGate({
241
534
  };
242
535
  }, [seen, bodyRef]);
243
536
 
537
+ // Mounted: weigh it (the DOM budget's unit) and, once it settled, take its picture for the placeholder.
538
+ useEffect(() => {
539
+ if (!on || !rt || !seen) return;
540
+ const b = bodyRef.current;
541
+ if (!b) return;
542
+ rt.registry.setWeight(id, b.getElementsByTagName('*').length);
543
+ // its colour at once (four elements at most): the next placeholders of this canvas take it
544
+ rememberBackground(id, backgroundOf(b));
545
+ const own = knownBackground(id, background);
546
+ if (own) {
547
+ articleRef.current?.style.setProperty('--dc-own-bg', own);
548
+ stills?.update(id, { bg: own });
549
+ }
550
+ let timer: ReturnType<typeof setTimeout> | undefined;
551
+ const shoot = () => {
552
+ if (!b.isConnected) return;
553
+ if (rt.registry.isInteracting()) {
554
+ timer = setTimeout(shoot, PICTURE_DELAY_MS);
555
+ return;
556
+ }
557
+ rt.registry.setWeight(id, b.getElementsByTagName('*').length);
558
+ const pic = takePicture(b);
559
+ if (pic) rememberPicture(id, pic);
560
+ };
561
+ timer = setTimeout(shoot, PICTURE_DELAY_MS);
562
+ return () => {
563
+ if (timer) clearTimeout(timer);
564
+ };
565
+ }, [on, rt, id, seen, bodyRef, articleRef, background, stills]);
566
+
567
+ // Not mounted: the frame takes the colour known so far (an unpainted body is never var(--bg-0)).
568
+ const ownBg = on && !seen ? knownBackground(id, background) : '';
569
+ useLayoutEffect(() => {
570
+ if (ownBg) articleRef.current?.style.setProperty('--dc-own-bg', ownBg);
571
+ }, [ownBg, articleRef]);
572
+
244
573
  const value = on ? state : ALL_LIVE;
245
- const mountChildren = !on || rt?.registry.mode !== 'light' || seen;
574
+ if (!on || seen) return <LiveContext.Provider value={value}>{children}</LiveContext.Provider>;
575
+ const ph = choosePlaceholder(id, background);
576
+ const kept = stills?.cache.peek(id);
577
+ // a kept still stands in for a released artboard (the same picture it shows when mounted under the cut)
578
+ const pic = kept ? undefined : ph.picture;
246
579
  return (
247
580
  <LiveContext.Provider value={value}>
248
- {mountChildren ? (
249
- children
250
- ) : (
251
- <div
252
- className="dc-artboard-sleeping"
253
- aria-hidden="true"
254
- style={{ minHeight: Math.max(lastHeight.current, heightFloor) }}
255
- />
256
- )}
581
+ <div
582
+ className="dc-artboard-sleeping"
583
+ data-dc-placeholder={ph.kind}
584
+ aria-hidden="true"
585
+ style={{
586
+ minHeight: Math.max(lastHeight.current, pic?.h ?? 0, heightFloor),
587
+ backgroundColor: ph.bg || undefined,
588
+ }}
589
+ >
590
+ {kept ? <StillCopy still={kept.el} /> : null}
591
+ {pic?.blocks.map((b, i) => (
592
+ <i
593
+ // biome-ignore lint/suspicious/noArrayIndexKey: a still picture; its blocks never reorder
594
+ key={i}
595
+ data-media={b.c ? undefined : ''}
596
+ style={{
597
+ left: `${b.x * 100}%`,
598
+ top: `${b.y * 100}%`,
599
+ width: `${b.w * 100}%`,
600
+ height: `${b.h * 100}%`,
601
+ background: b.c || undefined,
602
+ }}
603
+ />
604
+ ))}
605
+ </div>
257
606
  </LiveContext.Provider>
258
607
  );
259
608
  }
260
609
 
261
- /** Paused CSS animations while asleep. (A released WebGL canvas's picture is an overlay: artboard-webgl.ts.) */
262
- export const ARTBOARD_LIVE_CSS = `
610
+ /** A copy of a kept still (the cache's own canvas stays the cache's): drawn once, on mount. */
611
+ function StillCopy({ still }: { still: HTMLElement }) {
612
+ return (
613
+ <canvas
614
+ className="dc-artboard-still-copy"
615
+ ref={(c) => {
616
+ const src = still as HTMLCanvasElement;
617
+ if (!c || !src.width || c.width === src.width) return;
618
+ c.width = src.width;
619
+ c.height = src.height;
620
+ c.getContext('2d')?.drawImage(src, 0, 0);
621
+ }}
622
+ />
623
+ );
624
+ }
625
+
626
+ /** Paused CSS animations while asleep, the placeholder and the still. (A released WebGL canvas's picture is artboard-webgl.ts's.) */
627
+ export const ARTBOARD_LIVE_CSS = `${ARTBOARD_STILL_CSS}${ARTBOARD_FREEZE_CSS}
263
628
  .dc-artboard[data-dc-asleep] .dc-artboard-body *,
264
629
  .dc-artboard[data-dc-asleep] .dc-artboard-body *::before,
265
630
  .dc-artboard[data-dc-asleep] .dc-artboard-body *::after { animation-play-state: paused !important; }
266
- .dc-artboard-sleeping { width: 100%; }
631
+ html[data-dc-crowded] .dc-canvas .dc-artboard { will-change: auto; }
632
+ .dc-artboard-sleeping { width: 100%; position: relative; overflow: hidden; }
633
+ .dc-artboard-sleeping > i { position: absolute; display: block; opacity: 0.45; border-radius: 2px; }
634
+ .dc-artboard-sleeping > i[data-media] { background: color-mix(in srgb, currentColor 16%, transparent); }
635
+ .dc-artboard-sleeping > .dc-artboard-still-copy { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
267
636
  `;