@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.
- package/apps/studio/access/closure.ts +156 -16
- package/apps/studio/access/filters.ts +27 -0
- package/apps/studio/actions/agents.ts +54 -0
- package/apps/studio/actions/ctx.ts +3 -0
- package/apps/studio/actions/defs/sync.ts +13 -0
- package/apps/studio/actions/defs/view.ts +59 -3
- package/apps/studio/actions/keymap.gen.ts +35 -0
- package/apps/studio/actions/menu.ts +2 -0
- package/apps/studio/actions/types.ts +3 -1
- package/apps/studio/actions.manifest.json +136 -1
- package/apps/studio/actions.parity.json +25 -1
- package/apps/studio/annotations/ui/ai-seen.tsx +4 -3
- package/apps/studio/annotations/ui/ride.ts +2 -1
- package/apps/studio/annotations-context-toolbar.tsx +5 -4
- package/apps/studio/annotations-layer.tsx +4 -3
- package/apps/studio/api.ts +383 -128
- package/apps/studio/artboard-activity-overlay.tsx +4 -4
- package/apps/studio/artboard-ai-copies.tsx +4 -4
- package/apps/studio/artboard-freeze.ts +108 -0
- package/apps/studio/artboard-live-gate.tsx +414 -45
- package/apps/studio/artboard-live.ts +526 -31
- package/apps/studio/artboard-placeholder.ts +197 -0
- package/apps/studio/artboard-still-raster.ts +485 -0
- package/apps/studio/artboard-still-warmup.ts +221 -0
- package/apps/studio/artboard-still.ts +1188 -0
- package/apps/studio/artboard-webgl.ts +2 -1
- package/apps/studio/asset-placeholder.tsx +237 -132
- package/apps/studio/asset-upload-bars.tsx +4 -4
- package/apps/studio/bin/_perf-probe-open.mjs +364 -0
- package/apps/studio/bin/_perf-shared.mjs +12 -7
- package/apps/studio/bin/_pw-launch.mjs +26 -1
- package/apps/studio/bin/perf.sh +19 -0
- package/apps/studio/bridge/dgn-protocol.ts +35 -0
- package/apps/studio/canvas-build-sandbox.ts +69 -13
- package/apps/studio/canvas-camera.ts +90 -0
- package/apps/studio/canvas-comment-mount.tsx +9 -9
- package/apps/studio/canvas-lib.tsx +160 -34
- package/apps/studio/canvas-pages-view.tsx +465 -0
- package/apps/studio/canvas-pages.ts +305 -0
- package/apps/studio/canvas-rows.ts +62 -0
- package/apps/studio/canvas-shell.tsx +62 -16
- package/apps/studio/canvas-theme-icons.tsx +41 -0
- package/apps/studio/canvas-theme-pill.tsx +15 -6
- package/apps/studio/canvas-theme.ts +12 -11
- package/apps/studio/client/app.jsx +7 -0
- package/apps/studio/client/canvas-url.js +23 -1
- package/apps/studio/client/hooks/canvas-origin-probe.js +34 -0
- package/apps/studio/client/hooks/use-action-keys.jsx +3 -1
- package/apps/studio/client/hooks/use-canvas-bridge.jsx +1 -1
- package/apps/studio/client/hooks/use-palette-and-panels.jsx +23 -6
- package/apps/studio/client/hooks/use-photo-and-timeline.jsx +16 -8
- package/apps/studio/client/hooks/use-project-data.jsx +11 -2
- package/apps/studio/client/hooks/use-shell-core.jsx +19 -9
- package/apps/studio/client/index-loader.ts +41 -21
- package/apps/studio/client/inspector/inspector-panel.jsx +35 -14
- package/apps/studio/client/shell/viewport.jsx +32 -16
- package/apps/studio/client/tree/tree.jsx +17 -0
- package/apps/studio/client/v2/canvases/canvases-panel.jsx +24 -0
- package/apps/studio/client/v2/canvases/use-canvas-index.js +24 -1
- package/apps/studio/client/v2/code/canvas-error-card.jsx +14 -2
- package/apps/studio/client/v2/code/canvas-imports-card.jsx +7 -3
- package/apps/studio/client/v2/code/canvas-imports-words.js +16 -0
- package/apps/studio/client/v2/code/canvas-waiting-card.jsx +59 -5
- package/apps/studio/client/v2/code/canvas-waiting.js +18 -1
- package/apps/studio/client/v2/code/code-consent-sheet.jsx +87 -0
- package/apps/studio/client/v2/code/code-consent.js +67 -0
- package/apps/studio/client/v2/diagnostics/sync-details/sync-details-model.js +62 -10
- package/apps/studio/client/v2/diagnostics/sync-details/sync-details.jsx +314 -5
- package/apps/studio/client/v2/layers/artboard-layers.jsx +24 -5
- package/apps/studio/client/v2/layers/layers-request.js +49 -0
- package/apps/studio/client/v2/left-panel/canvases-pinned.jsx +93 -3
- package/apps/studio/client/v2/left-panel/ds-boards.js +39 -0
- package/apps/studio/client/v2/left-panel/left-panel-model.js +13 -0
- package/apps/studio/client/v2/left-panel/left-panel.css +24 -0
- package/apps/studio/client/v2/shell/canvas-crash-guard.js +40 -5
- package/apps/studio/client/v2/shell/canvas-mode.js +17 -1
- package/apps/studio/client/v2/shell/floating-panels.css +37 -1
- package/apps/studio/client/v2/shell/mode-switch.jsx +14 -3
- package/apps/studio/client/v2/shell/opening-skeleton.js +118 -0
- package/apps/studio/client/v2/shell/opening-skeleton.jsx +103 -0
- package/apps/studio/client/v2/shell/pages-pick.js +49 -0
- package/apps/studio/client/v2/shell/project-menu-model.js +3 -0
- package/apps/studio/client/v2/shell/share-cluster-host.jsx +3 -1
- package/apps/studio/client/v2/shell/shell-parity.json +8 -0
- package/apps/studio/client/v2/shell/use-canvas-mode-lane.js +30 -0
- package/apps/studio/comments-overlay.tsx +12 -11
- package/apps/studio/config-reload-wire.ts +4 -1
- package/apps/studio/config.schema.json +6 -0
- package/apps/studio/context.ts +37 -1
- package/apps/studio/dist/client.bundle.js +720 -720
- package/apps/studio/dist/comment-mount.js +2 -2
- package/apps/studio/dist/styles.css +1 -1
- package/apps/studio/ds/board/board.css +6 -0
- package/apps/studio/ds/board/ds-bar.tsx +21 -3
- package/apps/studio/ds/board/specimen.tsx +6 -3
- package/apps/studio/ds/board/template.ts +39 -19
- package/apps/studio/ds/cli.ts +7 -1
- package/apps/studio/ds/component-list.ts +2 -1
- package/apps/studio/ds/default.ts +6 -1
- package/apps/studio/ds/ds-resolver.ts +58 -8
- package/apps/studio/ds/engine.ts +3 -1
- package/apps/studio/ds/folder-systems.ts +199 -0
- package/apps/studio/ds/pairing-css.ts +2 -1
- package/apps/studio/ds/rename.ts +10 -3
- package/apps/studio/ds/shell-fallbacks.ts +2 -1
- package/apps/studio/ds/upgrade-stage.ts +6 -2
- package/apps/studio/editing/ai-point-ring.tsx +4 -4
- package/apps/studio/editing/ai-scope-outline.tsx +4 -4
- package/apps/studio/editing/align-bar.tsx +4 -4
- package/apps/studio/editing/anchor-report.ts +3 -2
- package/apps/studio/editing/artboard-handles.tsx +4 -4
- package/apps/studio/editing/corner-dots.tsx +4 -4
- package/apps/studio/editing/crop-mode.tsx +4 -4
- package/apps/studio/editing/drop-chip.tsx +4 -4
- package/apps/studio/editing/empty-report.ts +3 -2
- package/apps/studio/editing/focus-model.tsx +4 -4
- package/apps/studio/editing/gap-bands.tsx +3 -3
- package/apps/studio/editing/hold-rings.tsx +4 -4
- package/apps/studio/editing/lock-badge.tsx +3 -3
- package/apps/studio/editing/long-page.ts +23 -1
- package/apps/studio/editing/nudge.tsx +2 -2
- package/apps/studio/editing/page-activity.ts +4 -3
- package/apps/studio/editing/paste-bar.tsx +4 -4
- package/apps/studio/editing/pen/overlay.tsx +4 -3
- package/apps/studio/editing/place-freely.tsx +3 -3
- package/apps/studio/editing/reorder-slot.tsx +3 -3
- package/apps/studio/editing/selection.tsx +4 -4
- package/apps/studio/editing/size-ghost.tsx +4 -4
- package/apps/studio/editing/tab-order.tsx +4 -4
- package/apps/studio/editing/text-in-place.tsx +4 -4
- package/apps/studio/editing/undo-preview.tsx +3 -3
- package/apps/studio/equal-spacing-handles.tsx +3 -3
- package/apps/studio/frame-clock.ts +89 -0
- package/apps/studio/http.ts +66 -32
- package/apps/studio/index/ds-usage.ts +4 -1
- package/apps/studio/index-cache.ts +272 -0
- package/apps/studio/inspect-overlay.tsx +4 -3
- package/apps/studio/inspect.ts +7 -2
- package/apps/studio/link-chip-overlay.tsx +4 -3
- package/apps/studio/measure-overlay.tsx +4 -3
- package/apps/studio/present/pointer-dot.tsx +3 -2
- package/apps/studio/present/present-media.ts +6 -2
- package/apps/studio/present/present-stage.tsx +3 -2
- package/apps/studio/preview/preview-camera.tsx +3 -2
- package/apps/studio/routes/sync.ts +176 -0
- package/apps/studio/safe-zones-overlay.tsx +3 -2
- package/apps/studio/skip-dirs.ts +103 -47
- package/apps/studio/studio-page-host.ts +70 -0
- package/apps/studio/sync/code-module-consent.ts +86 -0
- package/apps/studio/sync/file-ledger.ts +24 -0
- package/apps/studio/sync/file-plane.ts +458 -53
- package/apps/studio/sync/held-deletes.ts +230 -0
- package/apps/studio/sync/hubs-config.ts +59 -2
- package/apps/studio/sync/index.ts +99 -15
- package/apps/studio/sync/seed-progress.ts +92 -2
- package/apps/studio/sync/settings.ts +36 -1
- package/apps/studio/sync/status.ts +15 -1
- package/apps/studio/sync/writer-registry.ts +5 -0
- package/apps/studio/text-caret.ts +5 -3
- package/apps/studio/toolbars/toolbar-rect.ts +3 -2
- package/apps/studio/use-annotation-resize.tsx +4 -3
- package/apps/studio/use-artboard-drag.tsx +3 -2
- package/apps/studio/use-element-resize.tsx +7 -6
- package/apps/studio/use-grid-track-handles.tsx +5 -4
- package/apps/studio/use-selection-set.tsx +64 -4
- package/apps/studio/use-spacing-handles.tsx +7 -6
- package/apps/studio/video-comp.tsx +55 -42
- package/apps/studio/video-play-overlay.tsx +4 -3
- package/apps/studio/whats-new.json +27 -0
- package/cli/commands/design.mjs +5 -2
- package/cli/commands/hub.mjs +10 -1
- package/cli/lib/bash-guard.mjs +14 -0
- package/cli/lib/design-help.gen.mjs +1 -1
- package/cli/lib/design-link.mjs +42 -17
- package/cli/lib/hub-purge.mjs +183 -0
- package/package.json +8 -8
- package/plugins/design/templates/_shell.html +164 -1
|
@@ -0,0 +1,1188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file artboard-still.ts — 2.0.7: the overview level — small artboards show a still picture of themselves
|
|
3
|
+
* @scope apps/studio/artboard-still.ts (React-free; wired by artboard-live-gate.tsx, fed by canvas-lib.tsx)
|
|
4
|
+
* @purpose Michal (decision `v2-michal-207-overview-level`, replacing the gesture-only stills): below the overview
|
|
5
|
+
* cut — an artboard narrower than `STILL_MAX_SCREEN_PX` on screen — an artboard shows ONLY its still,
|
|
6
|
+
* settled or not. Live content appears only once the artboard is big enough on screen.
|
|
7
|
+
*
|
|
8
|
+
* Why: WebKit repaints every on-screen artboard at each scale step, and paints each artboard handed back
|
|
9
|
+
* for the first time at the new zoom. On the mascot canvas at 10 % that is ~8.5k mounted SVG/HTML
|
|
10
|
+
* elements (zoom frames 300–900 ms in Safari). A still is one bitmap; a body whose children are out of
|
|
11
|
+
* the render tree (`display: none`, the box kept) costs neither style, layout nor paint.
|
|
12
|
+
*
|
|
13
|
+
* ONE visual sequence per artboard (Michal: "the switch from still to live must be almost unnoticeable"):
|
|
14
|
+
* - `cover` — the own-colour stand-in, only while nothing better exists (never after a still or live);
|
|
15
|
+
* - `still` — the still picture; replaced only by a newer still (its content changed) or by live;
|
|
16
|
+
* - `paint` → `reveal` → live — the hand-over: the body is shown UNDER the still, and the still fades out
|
|
17
|
+
* only after the body has painted (two frames, its pictures decoded, the fonts ready).
|
|
18
|
+
* Zooming out past the cut swaps live back to the still at once (a still drawn from that very DOM).
|
|
19
|
+
* Never a still after cover→live, never cover after a still, never a blank frame: the stand-in sits
|
|
20
|
+
* outside the body, so neither its `content-visibility` nor an unpainted WebKit tile shows through.
|
|
21
|
+
*
|
|
22
|
+
* Never a still: the active artboard and the one holding the selection (`pinned` — the shell works in
|
|
23
|
+
* their DOM, and a click on an overview artboard selects it, which brings it live), video artboards
|
|
24
|
+
* (`eager`, `v2-207-eager-video-media-follows-ring`).
|
|
25
|
+
*
|
|
26
|
+
* Drawn in slices within a per-frame budget (`startStill`, artboard-still-raster.ts) — during a pan too, so
|
|
27
|
+
* an artboard that mounts mid-pan becomes its still within a few frames, never while the zoom moves, and
|
|
28
|
+
* not in the hold after a zoom settles. The children of a hidden body come back into the render tree for
|
|
29
|
+
* the walk only (`STILL_WALK_ATTR`, the body transparent); its pictures decode off the main thread.
|
|
30
|
+
*
|
|
31
|
+
* The stills are drawn in this canvas document (artboard-still-raster.ts) and kept in its
|
|
32
|
+
* memory (`StillCache`, keyed by artboard id + a content signature, bounded in bytes) — never on disk and
|
|
33
|
+
* never in the canvas origin's storage, which every canvas on that origin can read (C4's named gap).
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import type { StillJob, WalkHooks } from './artboard-still-raster.ts';
|
|
37
|
+
import { frameRaf } from './frame-clock.ts';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The overview cut: an artboard narrower than this on screen (CSS px) shows its still. One constant (Michal first
|
|
41
|
+
* said ~120; at his 10 % lane the mascot canvas's artboards are 132–160 px, so 120 changes nothing — see the contract).
|
|
42
|
+
*/
|
|
43
|
+
export const STILL_MAX_SCREEN_PX = 200;
|
|
44
|
+
/**
|
|
45
|
+
* The hand-back when the zoom crosses the cut (nearest the centre first): after `STILL_HANDBACK_QUIET_FRAMES` quiet
|
|
46
|
+
* frames, one artboard a frame, doubling up to `STILL_RESTORE_PER_FRAME` while frames stay under
|
|
47
|
+
* `STILL_HANDBACK_SLOW_MS`, back to one after a slow one. Any gesture input pauses it.
|
|
48
|
+
*/
|
|
49
|
+
export const STILL_RESTORE_PER_FRAME = 4;
|
|
50
|
+
export const STILL_HANDBACK_QUIET_FRAMES = 2;
|
|
51
|
+
export const STILL_HANDBACK_SLOW_MS = 20;
|
|
52
|
+
/** After this many frames in a row waited, the hand-back goes on (one a frame) however slow the frames are. */
|
|
53
|
+
export const STILL_HANDBACK_MAX_WAIT_FRAMES = 3;
|
|
54
|
+
/** How long the still fades out over the painted live body (ms). Short: the eye takes it as one change. */
|
|
55
|
+
export const STILL_REVEAL_MS = 140;
|
|
56
|
+
/** Stills are drawn for artboards up to this many times the cut (a zoom-out reaches them next). */
|
|
57
|
+
export const STILL_PREPARE_FACTOR = 2;
|
|
58
|
+
/**
|
|
59
|
+
* How long a still whose content changed waits before it is redrawn (ms): an edit's burst of DOM changes draws once.
|
|
60
|
+
* A cover (an artboard with no still yet) is drawn at once — during a pan too (the 2.0.7 first-pass blocks).
|
|
61
|
+
*/
|
|
62
|
+
export const STILL_TAKE_DELAY_MS = 600;
|
|
63
|
+
/**
|
|
64
|
+
* The main-thread time drawings may take per frame (ms), during a gesture and while settled. A drawing is sliced
|
|
65
|
+
* (artboard-still-raster.ts `startStill`: the walk, then the pictures decode off the main thread, then the paint),
|
|
66
|
+
* so an artboard that mounts mid-pan becomes its still within a few frames.
|
|
67
|
+
*/
|
|
68
|
+
export const STILL_PAN_BUDGET_MS = 4;
|
|
69
|
+
export const STILL_IDLE_BUDGET_MS = 8;
|
|
70
|
+
/**
|
|
71
|
+
* After a zoom settles nothing is drawn for this long (ms): WebKit repaints its tiles at the new scale in those
|
|
72
|
+
* frames, and a drawing among them pushed the fit-all zoom cycle's settle max over base. A pan in it waits too: a pan
|
|
73
|
+
* right after a zoom is still those frames (2.0.7 — drawing there kept the sweep-after-zoom lane over the bar).
|
|
74
|
+
*/
|
|
75
|
+
export const STILL_SETTLE_HOLD_MS = 1200;
|
|
76
|
+
/** At most this many drawings wait for their pictures to decode at once; the next one walks meanwhile. */
|
|
77
|
+
export const STILL_MAX_DECODING = 4;
|
|
78
|
+
/** Kept for callers: drawings are paced by the frame budget now, not by a gap. */
|
|
79
|
+
export const STILL_TAKE_GAP_MS = 50;
|
|
80
|
+
/**
|
|
81
|
+
* The stills kept in memory, in bytes of bitmap (w × h × 4). Past it the least recently used still that is not
|
|
82
|
+
* standing in right now is dropped (an artboard released far away shows its own-colour placeholder again).
|
|
83
|
+
*/
|
|
84
|
+
export const STILL_CACHE_MAX_BYTES = 96 * 1024 * 1024;
|
|
85
|
+
/** The attribute the article carries while its stand-in shows (`cover` | `still` | `paint` | `reveal`). */
|
|
86
|
+
export const STILL_ATTR = 'data-dc-still';
|
|
87
|
+
/**
|
|
88
|
+
* The attribute the article carries while its hidden body is walked for a drawing: the children are back in the
|
|
89
|
+
* render tree (laid out, so the walk can read them) and the body is transparent (not painted). Never shown.
|
|
90
|
+
*/
|
|
91
|
+
export const STILL_WALK_ATTR = 'data-dc-still-walk';
|
|
92
|
+
/** The class of the stand-in element (a `<div>` appended to the article; the still `<canvas>` is its child). */
|
|
93
|
+
export const STILL_CLASS = 'dc-artboard-still';
|
|
94
|
+
/** The frame parameter that forces the feature on or off (`?artboard-still=on|off`, tests and A/B runs). */
|
|
95
|
+
export const STILL_PARAM = 'artboard-still';
|
|
96
|
+
|
|
97
|
+
export type StillMode = 'on' | 'off';
|
|
98
|
+
/** What one artboard shows. `live` = its body, nothing over it. */
|
|
99
|
+
export type StillView = 'live' | 'cover' | 'still' | 'paint' | 'reveal';
|
|
100
|
+
|
|
101
|
+
/** On in the studio on WebKit; `?artboard-still=` wins; off in captures (the live mode is `off` there). */
|
|
102
|
+
export function resolveStillMode(env: {
|
|
103
|
+
search: string;
|
|
104
|
+
webkit: boolean;
|
|
105
|
+
liveMode: 'off' | 'sleep' | 'light';
|
|
106
|
+
}): StillMode {
|
|
107
|
+
if (env.liveMode === 'off') return 'off';
|
|
108
|
+
const p = new URLSearchParams(env.search).get(STILL_PARAM);
|
|
109
|
+
if (p === 'on' || p === 'off') return p;
|
|
110
|
+
return env.webkit ? 'on' : 'off';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Below the overview cut at this zoom (`width` in the artboard's own world px). */
|
|
114
|
+
export function inOverviewCut(width: number, zoom: number): boolean {
|
|
115
|
+
return width > 0 && zoom > 0 && width * zoom < STILL_MAX_SCREEN_PX;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Where one artboard is heading: its still (or the cover when it has none and has never shown live), or live.
|
|
120
|
+
* Mid-gesture a live artboard that shrinks under the cut swaps to its still at once (cheaper than painting it), and
|
|
121
|
+
* one that grows past it holds its still until the settle (a mid-gesture hand-back would paint it at once).
|
|
122
|
+
*/
|
|
123
|
+
export function targetView(s: {
|
|
124
|
+
view: StillView;
|
|
125
|
+
small: boolean;
|
|
126
|
+
exempt: boolean;
|
|
127
|
+
hasStill: boolean;
|
|
128
|
+
gesture: boolean;
|
|
129
|
+
}): StillView {
|
|
130
|
+
const standing = s.view === 'cover' || s.view === 'still';
|
|
131
|
+
if (s.exempt) return 'live';
|
|
132
|
+
if (s.gesture) {
|
|
133
|
+
// mid-gesture: shrinking under the cut swaps to the still; a cover takes its still the moment it exists
|
|
134
|
+
if (s.hasStill && (s.view === 'cover' || (s.small && !standing))) return 'still';
|
|
135
|
+
return s.view === 'paint' || s.view === 'reveal' ? 'live' : standing ? s.view : 'live';
|
|
136
|
+
}
|
|
137
|
+
if (!s.small) return 'live';
|
|
138
|
+
if (s.hasStill) return 'still';
|
|
139
|
+
// no still yet: what is already live stays live until its still exists (live → still is then the one change);
|
|
140
|
+
// what has shown nothing better than the cover keeps it
|
|
141
|
+
return standing ? 'cover' : 'live';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* May this artboard stand in as its still right now? (The pre-overview predicate, kept for callers and tests: the
|
|
146
|
+
* still exists, the artboard is not exempt, and it is under the cut or already showing it this gesture.)
|
|
147
|
+
*/
|
|
148
|
+
export function stillEligible(s: {
|
|
149
|
+
width: number;
|
|
150
|
+
zoom: number;
|
|
151
|
+
pinned: boolean;
|
|
152
|
+
eager: boolean;
|
|
153
|
+
hasStill: boolean;
|
|
154
|
+
showing?: boolean;
|
|
155
|
+
}): boolean {
|
|
156
|
+
if (s.pinned || s.eager || !s.hasStill) return false;
|
|
157
|
+
if (s.showing) return true;
|
|
158
|
+
return inOverviewCut(s.width, s.zoom);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Is a still worth drawing for this artboard at the settled zoom? */
|
|
162
|
+
export function stillWanted(s: {
|
|
163
|
+
width: number;
|
|
164
|
+
zoom: number;
|
|
165
|
+
pinned: boolean;
|
|
166
|
+
eager: boolean;
|
|
167
|
+
}): boolean {
|
|
168
|
+
if (s.pinned || s.eager || !(s.width > 0) || !(s.zoom > 0)) return false;
|
|
169
|
+
return s.width * s.zoom < STILL_MAX_SCREEN_PX * STILL_PREPARE_FACTOR;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* A content signature of a body: what it renders (its markup) and its size. Two equal signatures paint the same
|
|
174
|
+
* still (a picture that came in later redraws through its `load`). FNV-1a over the markup — a few ms on an 8k-node
|
|
175
|
+
* body, against 10–40 ms for a drawing.
|
|
176
|
+
*/
|
|
177
|
+
export function contentSignature(body: {
|
|
178
|
+
innerHTML: string;
|
|
179
|
+
offsetWidth: number;
|
|
180
|
+
offsetHeight: number;
|
|
181
|
+
}): string {
|
|
182
|
+
const s = body.innerHTML;
|
|
183
|
+
let h = 0x811c9dc5;
|
|
184
|
+
for (let i = 0; i < s.length; i++) {
|
|
185
|
+
h ^= s.charCodeAt(i);
|
|
186
|
+
h = Math.imul(h, 0x01000193);
|
|
187
|
+
}
|
|
188
|
+
return `${(h >>> 0).toString(36)}:${s.length}:${body.offsetWidth}x${body.offsetHeight}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** One kept still: its bitmap, the signature it was drawn from, its size in bytes, and when it was last used. */
|
|
192
|
+
export interface CachedStill {
|
|
193
|
+
el: HTMLElement;
|
|
194
|
+
sig: string;
|
|
195
|
+
bytes: number;
|
|
196
|
+
used: number;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The stills of this canvas document, by artboard id — kept when an artboard's children are released, so it comes
|
|
201
|
+
* back as its still rather than a placeholder, and across an edit's soft reload (the runtime lives on the document).
|
|
202
|
+
* Bounded by `STILL_CACHE_MAX_BYTES`; what stands in right now (`inUse`) is never dropped.
|
|
203
|
+
*/
|
|
204
|
+
export class StillCache {
|
|
205
|
+
private map = new Map<string, CachedStill>();
|
|
206
|
+
private bytes = 0;
|
|
207
|
+
private tick = 0;
|
|
208
|
+
constructor(readonly max = STILL_CACHE_MAX_BYTES) {}
|
|
209
|
+
get(id: string): CachedStill | undefined {
|
|
210
|
+
const c = this.map.get(id);
|
|
211
|
+
if (c) c.used = ++this.tick;
|
|
212
|
+
return c;
|
|
213
|
+
}
|
|
214
|
+
peek(id: string): CachedStill | undefined {
|
|
215
|
+
return this.map.get(id);
|
|
216
|
+
}
|
|
217
|
+
set(
|
|
218
|
+
id: string,
|
|
219
|
+
el: HTMLElement,
|
|
220
|
+
sig: string,
|
|
221
|
+
bytes: number,
|
|
222
|
+
inUse: (id: string) => boolean
|
|
223
|
+
): void {
|
|
224
|
+
const old = this.map.get(id);
|
|
225
|
+
if (old) {
|
|
226
|
+
this.bytes -= old.bytes;
|
|
227
|
+
if (old.el !== el) old.el.remove();
|
|
228
|
+
}
|
|
229
|
+
this.map.set(id, { el, sig, bytes, used: ++this.tick });
|
|
230
|
+
this.bytes += bytes;
|
|
231
|
+
if (this.bytes <= this.max) return;
|
|
232
|
+
const victims = [...this.map.entries()]
|
|
233
|
+
.filter(([k]) => k !== id && !inUse(k))
|
|
234
|
+
.sort((a, b) => a[1].used - b[1].used);
|
|
235
|
+
for (const [k, v] of victims) {
|
|
236
|
+
if (this.bytes <= this.max) break;
|
|
237
|
+
this.map.delete(k);
|
|
238
|
+
this.bytes -= v.bytes;
|
|
239
|
+
v.el.remove();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** The content changed under a kept still: it may still stand in (better than nothing), but is redrawn. */
|
|
243
|
+
stale(id: string): void {
|
|
244
|
+
const c = this.map.get(id);
|
|
245
|
+
if (c) c.sig = '';
|
|
246
|
+
}
|
|
247
|
+
stats(): { n: number; bytes: number } {
|
|
248
|
+
return { n: this.map.size, bytes: this.bytes };
|
|
249
|
+
}
|
|
250
|
+
ids(): string[] {
|
|
251
|
+
return [...this.map.keys()];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** What the controller needs from one artboard's DOM (an adapter, so tests need no canvas). */
|
|
256
|
+
export interface StillTarget {
|
|
257
|
+
article: HTMLElement;
|
|
258
|
+
body: HTMLElement;
|
|
259
|
+
/** the own colour the cover takes ('' = the frame's own) */
|
|
260
|
+
bg?: string;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
interface Entry {
|
|
264
|
+
id: string;
|
|
265
|
+
target: StillTarget;
|
|
266
|
+
pinned: boolean;
|
|
267
|
+
eager: boolean;
|
|
268
|
+
visible: boolean;
|
|
269
|
+
weight: number;
|
|
270
|
+
/** the stand-in element (a `<div>` child of the article; holds the still) — created on first need */
|
|
271
|
+
slot: HTMLElement | null;
|
|
272
|
+
/** the still currently inside the slot */
|
|
273
|
+
shown: HTMLElement | null;
|
|
274
|
+
dirty: boolean;
|
|
275
|
+
/** bumps on every content change: a drawing that saw an older one is shown but drawn again */
|
|
276
|
+
gen: number;
|
|
277
|
+
/** not drawn before this `now()` (a changed still waits `STILL_TAKE_DELAY_MS`; a cover is drawn at once) */
|
|
278
|
+
readyAt: number;
|
|
279
|
+
view: StillView;
|
|
280
|
+
/** bumps on every view change: a hand-over in flight checks it before its next step */
|
|
281
|
+
seq: number;
|
|
282
|
+
observer: MutationObserver | null;
|
|
283
|
+
/** has this entry's body been shown live since it registered (the cover is never shown after that) */
|
|
284
|
+
wasLive: boolean;
|
|
285
|
+
/** its drawing in flight */
|
|
286
|
+
job: Current | null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The drawing in flight (one at a time). */
|
|
290
|
+
interface Current {
|
|
291
|
+
e: Entry;
|
|
292
|
+
job: StillJob;
|
|
293
|
+
sig: string;
|
|
294
|
+
gen: number;
|
|
295
|
+
t0: number;
|
|
296
|
+
waiting: boolean;
|
|
297
|
+
/** diagnostics: slices, the longest one, when each phase ended (ms after t0) */
|
|
298
|
+
slices: number;
|
|
299
|
+
sliceMax: number;
|
|
300
|
+
phaseMax: Record<string, number>;
|
|
301
|
+
marks: Record<string, number>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** One finished drawing (diagnostics, `__maudeStillJobs`). */
|
|
305
|
+
export interface StillJobTrace {
|
|
306
|
+
id: string;
|
|
307
|
+
/** what the artboard showed when the drawing ended (before its new still) */
|
|
308
|
+
view: StillView;
|
|
309
|
+
/** the longest slice per phase it started in (ms) */
|
|
310
|
+
phaseMax: Record<string, number>;
|
|
311
|
+
slices: number;
|
|
312
|
+
sliceMax: number;
|
|
313
|
+
/** ms after the start: the walk done, the pictures decoded, the end */
|
|
314
|
+
marks: Record<string, number>;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface StillControllerOptions {
|
|
318
|
+
/** Start a sliced drawing of `body` (artboard-still-raster.ts `startStill`). Wins over `take`. */
|
|
319
|
+
job?: (body: HTMLElement, walk: WalkHooks) => StillJob;
|
|
320
|
+
/** Draw a still of `body` whole (null when it cannot) — tests and callers without slicing. */
|
|
321
|
+
take?: (body: HTMLElement) => Promise<HTMLElement | null>;
|
|
322
|
+
later?: (fn: () => void, ms: number) => unknown;
|
|
323
|
+
/** the next animation frame (drawings, the staggered hand-back, the hand-over's two frames) */
|
|
324
|
+
frame?: (fn: () => void) => unknown;
|
|
325
|
+
/** a clock (ms) — the hand-back paces itself on frame times, drawings on the frame budget */
|
|
326
|
+
now?: () => number;
|
|
327
|
+
cancel?: (handle: unknown) => void;
|
|
328
|
+
/** observe DOM changes (null in tests that do not need it); `why` is 'load' for a picture that came in */
|
|
329
|
+
observe?: ((el: HTMLElement, onChange: (why?: string) => void) => MutationObserver | null) | null;
|
|
330
|
+
/** on screen + mounted weight, from the mount ring (default: what `update` was told) */
|
|
331
|
+
info?: (id: string) => { visible: boolean; weight: number } | null;
|
|
332
|
+
/** the body has painted what it shows: its pictures decoded, the fonts ready (default: those two promises) */
|
|
333
|
+
painted?: (body: HTMLElement) => Promise<void>;
|
|
334
|
+
/** make a stand-in element (default: a `<div>`; tests pass a fake) */
|
|
335
|
+
makeSlot?: () => HTMLElement;
|
|
336
|
+
/** the content signature of a body (default `contentSignature`) */
|
|
337
|
+
signature?: (body: HTMLElement) => string;
|
|
338
|
+
/** the bytes a still holds (default: canvas width × height × 4) */
|
|
339
|
+
sizeOf?: (still: HTMLElement) => number;
|
|
340
|
+
cache?: StillCache;
|
|
341
|
+
/** a view change, for the trace (tests, `__maudeStillStats`) */
|
|
342
|
+
onView?: (id: string, view: StillView) => void;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** A whole (unsliced) drawing as a job: one walk, then the wait for its promise. */
|
|
346
|
+
function wholeJob(take: (body: HTMLElement) => Promise<HTMLElement | null>) {
|
|
347
|
+
return (body: HTMLElement, walk: WalkHooks): StillJob => {
|
|
348
|
+
let phase: StillJob['phase'] = 'collect';
|
|
349
|
+
let ready: Promise<unknown> | null = null;
|
|
350
|
+
let result: HTMLElement | null = null;
|
|
351
|
+
let settled = false;
|
|
352
|
+
return {
|
|
353
|
+
get ready() {
|
|
354
|
+
return ready;
|
|
355
|
+
},
|
|
356
|
+
get result() {
|
|
357
|
+
return result;
|
|
358
|
+
},
|
|
359
|
+
get phase() {
|
|
360
|
+
return phase;
|
|
361
|
+
},
|
|
362
|
+
abort() {
|
|
363
|
+
phase = 'done';
|
|
364
|
+
},
|
|
365
|
+
step() {
|
|
366
|
+
if (phase === 'collect') {
|
|
367
|
+
let p: Promise<HTMLElement | null>;
|
|
368
|
+
walk.enter();
|
|
369
|
+
try {
|
|
370
|
+
p = take(body);
|
|
371
|
+
} catch {
|
|
372
|
+
p = Promise.resolve(null);
|
|
373
|
+
} finally {
|
|
374
|
+
walk.leave();
|
|
375
|
+
}
|
|
376
|
+
phase = 'decode';
|
|
377
|
+
ready = p
|
|
378
|
+
.then(
|
|
379
|
+
(r) => {
|
|
380
|
+
result = r;
|
|
381
|
+
},
|
|
382
|
+
() => {
|
|
383
|
+
result = null;
|
|
384
|
+
}
|
|
385
|
+
)
|
|
386
|
+
.then(() => {
|
|
387
|
+
settled = true;
|
|
388
|
+
});
|
|
389
|
+
return 'wait';
|
|
390
|
+
}
|
|
391
|
+
if (phase === 'decode' && !settled) return 'wait';
|
|
392
|
+
phase = 'done';
|
|
393
|
+
return 'done';
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* One per canvas document. Artboards register while their children are mounted (in a layout effect, so the body is
|
|
401
|
+
* hidden before its first paint when it is under the cut); the viewport reports every zoom it writes (`noteZoom`)
|
|
402
|
+
* and the settle (`settle`). Stills are drawn one at a time in slices, within a per-frame budget — during a pan too,
|
|
403
|
+
* never while the zoom is moving, and not in the `STILL_SETTLE_HOLD_MS` after a zoom settles (a pan in it waits too).
|
|
404
|
+
*/
|
|
405
|
+
export class StillController {
|
|
406
|
+
private entries = new Map<string, Entry>();
|
|
407
|
+
private zoom = 1;
|
|
408
|
+
private gestureZoom: number | null = null;
|
|
409
|
+
/** the gesture in flight changed the zoom (drawings wait; a pure pan draws) */
|
|
410
|
+
private zooming = false;
|
|
411
|
+
private holdUntil = 0;
|
|
412
|
+
/** a zoom was published before (the first one is the canvas opening, never held) */
|
|
413
|
+
private zoomKnown = false;
|
|
414
|
+
private holdTimer: unknown = null;
|
|
415
|
+
private readyTimer: unknown = null;
|
|
416
|
+
private pumpQueued = false;
|
|
417
|
+
/** drawings that can take a slice now (decoded ones first) — one walks at a time, the queue's head */
|
|
418
|
+
private queue: Current[] = [];
|
|
419
|
+
/** drawings whose pictures are decoding (off the main thread): the next one walks meanwhile */
|
|
420
|
+
private decoding = new Set<Current>();
|
|
421
|
+
/** drawings so far, their total and longest wall time (ms), the longest slice (ms), draws skipped by an equal signature */
|
|
422
|
+
private takes = { n: 0, ms: 0, max: 0, slice: 0, reused: 0 };
|
|
423
|
+
private readonly job: ((body: HTMLElement, walk: WalkHooks) => StillJob) | null;
|
|
424
|
+
private readonly later: (fn: () => void, ms: number) => unknown;
|
|
425
|
+
private readonly frame: (fn: () => void) => unknown;
|
|
426
|
+
private restoring: Entry[] = [];
|
|
427
|
+
private restoreScheduled = false;
|
|
428
|
+
private readonly now: () => number;
|
|
429
|
+
private handbackQuiet = 0;
|
|
430
|
+
private handbackStep = 1;
|
|
431
|
+
private lastFrameAt = 0;
|
|
432
|
+
private handbackWaited = 0;
|
|
433
|
+
private readonly observe: StillControllerOptions['observe'];
|
|
434
|
+
private readonly info: StillControllerOptions['info'];
|
|
435
|
+
private readonly painted: (body: HTMLElement) => Promise<void>;
|
|
436
|
+
private readonly makeSlot: () => HTMLElement;
|
|
437
|
+
private readonly signature: (body: HTMLElement) => string;
|
|
438
|
+
private readonly sizeOf: (still: HTMLElement) => number;
|
|
439
|
+
private readonly onView: StillControllerOptions['onView'];
|
|
440
|
+
readonly cache: StillCache;
|
|
441
|
+
/** view changes per artboard since it first registered — the flicker count (diagnostics) */
|
|
442
|
+
private changes = new Map<string, StillView[]>();
|
|
443
|
+
/** the last drawings (≤ 64) — diagnostics */
|
|
444
|
+
private traces: StillJobTrace[] = [];
|
|
445
|
+
/** the last camera write or settle (ms, `now()`) — the warm-up waits for quiet after it */
|
|
446
|
+
private activityAt = Number.NEGATIVE_INFINITY;
|
|
447
|
+
private stillListeners = new Set<(id: string, ok: boolean) => void>();
|
|
448
|
+
|
|
449
|
+
constructor(o: StillControllerOptions) {
|
|
450
|
+
this.job = o.job ?? (o.take ? wholeJob(o.take) : null);
|
|
451
|
+
this.later = o.later ?? ((fn, ms) => setTimeout(fn, ms));
|
|
452
|
+
this.now =
|
|
453
|
+
o.now ?? (() => (typeof performance !== 'undefined' ? performance.now() : Date.now()));
|
|
454
|
+
this.frame =
|
|
455
|
+
o.frame ??
|
|
456
|
+
((fn) => (typeof requestAnimationFrame === 'function' ? frameRaf(fn) : setTimeout(fn, 16)));
|
|
457
|
+
this.observe = o.observe === undefined ? defaultObserve : o.observe;
|
|
458
|
+
this.info = o.info;
|
|
459
|
+
this.painted = o.painted ?? defaultPainted;
|
|
460
|
+
this.makeSlot = o.makeSlot ?? (() => document.createElement('div'));
|
|
461
|
+
this.signature = o.signature ?? ((b) => contentSignature(b));
|
|
462
|
+
this.sizeOf = o.sizeOf ?? defaultSize;
|
|
463
|
+
this.cache = o.cache ?? new StillCache();
|
|
464
|
+
this.onView = o.onView;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private visibleOf(e: Entry): boolean {
|
|
468
|
+
return this.info ? !!this.info(e.id)?.visible : e.visible;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** The settled (published) zoom — the one a gesture starts from. Ignored mid-gesture. */
|
|
472
|
+
setZoom(zoom: number): void {
|
|
473
|
+
if (!(zoom > 0) || this.gestureZoom !== null) return;
|
|
474
|
+
if (zoom === this.zoom) return;
|
|
475
|
+
this.activityAt = this.now();
|
|
476
|
+
this.zoom = zoom;
|
|
477
|
+
// a programmatic zoom (fit, a jump) repaints like a gesture's settle — not the first one (the canvas opening)
|
|
478
|
+
if (this.zoomKnown) this.holdUntil = this.now() + STILL_SETTLE_HOLD_MS;
|
|
479
|
+
this.zoomKnown = true;
|
|
480
|
+
this.reconcileAll();
|
|
481
|
+
this.schedulePump();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
register(
|
|
485
|
+
id: string,
|
|
486
|
+
target: StillTarget,
|
|
487
|
+
flags: { pinned?: boolean; eager?: boolean } = {}
|
|
488
|
+
): () => void {
|
|
489
|
+
this.unregister(id);
|
|
490
|
+
const e: Entry = {
|
|
491
|
+
id,
|
|
492
|
+
target,
|
|
493
|
+
pinned: !!flags.pinned,
|
|
494
|
+
eager: !!flags.eager,
|
|
495
|
+
visible: false,
|
|
496
|
+
weight: 0,
|
|
497
|
+
slot: null,
|
|
498
|
+
shown: null,
|
|
499
|
+
dirty: true,
|
|
500
|
+
gen: 0,
|
|
501
|
+
readyAt: 0,
|
|
502
|
+
view: 'live',
|
|
503
|
+
seq: 0,
|
|
504
|
+
observer: null,
|
|
505
|
+
wasLive: false,
|
|
506
|
+
job: null,
|
|
507
|
+
};
|
|
508
|
+
e.observer = this.observe ? this.observe(target.body, (why) => this.markDirty(id, why)) : null;
|
|
509
|
+
this.entries.set(id, e);
|
|
510
|
+
// A fresh mount: it has painted nothing yet. Under the cut it starts as its kept still (or the cover) with the
|
|
511
|
+
// body hidden before its first paint; over the cut it starts as a hand-over from that stand-in.
|
|
512
|
+
const kept = this.cache.get(id);
|
|
513
|
+
if (kept) this.putStill(e, kept.el);
|
|
514
|
+
const small = this.small(e, this.zoom);
|
|
515
|
+
if (small && !e.pinned && !e.eager) this.setView(e, kept ? 'still' : 'cover');
|
|
516
|
+
else if (kept) this.handOver(e, true);
|
|
517
|
+
else e.wasLive = true;
|
|
518
|
+
this.schedulePump();
|
|
519
|
+
return () => {
|
|
520
|
+
if (this.entries.get(id) === e) this.unregister(id);
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
private unregister(id: string): void {
|
|
525
|
+
const e = this.entries.get(id);
|
|
526
|
+
if (!e) return;
|
|
527
|
+
e.seq += 1;
|
|
528
|
+
e.observer?.disconnect();
|
|
529
|
+
if (e.job) this.dropJob(e.job);
|
|
530
|
+
e.target.article.removeAttribute(STILL_WALK_ATTR);
|
|
531
|
+
if (e.shown) e.shown.remove(); // stays in the cache
|
|
532
|
+
e.slot?.remove();
|
|
533
|
+
e.target.article.removeAttribute(STILL_ATTR);
|
|
534
|
+
this.restoring = this.restoring.filter((x) => x !== e);
|
|
535
|
+
this.entries.delete(id);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
update(
|
|
539
|
+
id: string,
|
|
540
|
+
s: Partial<{ pinned: boolean; eager: boolean; visible: boolean; weight: number; bg: string }>
|
|
541
|
+
): void {
|
|
542
|
+
const e = this.entries.get(id);
|
|
543
|
+
if (!e) return;
|
|
544
|
+
if (s.pinned !== undefined) e.pinned = s.pinned;
|
|
545
|
+
if (s.eager !== undefined) e.eager = s.eager;
|
|
546
|
+
if (s.visible !== undefined) e.visible = s.visible;
|
|
547
|
+
if (s.weight !== undefined && Number.isFinite(s.weight)) e.weight = s.weight;
|
|
548
|
+
if (s.bg !== undefined) {
|
|
549
|
+
e.target.bg = s.bg;
|
|
550
|
+
if (e.slot) e.slot.style.backgroundColor = s.bg || '';
|
|
551
|
+
}
|
|
552
|
+
// the selection lands (mid-gesture too): its artboard goes live at once — the shell works in its DOM
|
|
553
|
+
this.reconcile(e, this.gestureZoom !== null);
|
|
554
|
+
this.schedulePump();
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* The content changed (a DOM mutation, a picture that came in, a wake or a sleep): the still is redrawn before it
|
|
559
|
+
* is trusted again — after `STILL_TAKE_DELAY_MS` when a still stands in, at once for a cover. A picture that came
|
|
560
|
+
* in while a drawing of this artboard waits for its pictures is part of that drawing, not a change.
|
|
561
|
+
*/
|
|
562
|
+
markDirty(id: string, why?: string): void {
|
|
563
|
+
const e = this.entries.get(id);
|
|
564
|
+
if (!e) return;
|
|
565
|
+
if (why === 'load' && e.job && e.job.job.phase !== 'paint') return;
|
|
566
|
+
e.gen += 1;
|
|
567
|
+
if (e.shown || !hiddenView(e.view)) e.readyAt = this.now() + STILL_TAKE_DELAY_MS;
|
|
568
|
+
if (!e.dirty) {
|
|
569
|
+
e.dirty = true;
|
|
570
|
+
this.cache.stale(id);
|
|
571
|
+
}
|
|
572
|
+
this.schedulePump();
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/** The viewport wrote this zoom (every frame of a gesture, and every programmatic move). */
|
|
576
|
+
noteZoom(zoom: number): void {
|
|
577
|
+
if (!(zoom > 0)) return;
|
|
578
|
+
this.activityAt = this.now();
|
|
579
|
+
if (this.gestureZoom === null) {
|
|
580
|
+
// the first write of a gesture: remember where it started; nothing changes for a pure pan
|
|
581
|
+
this.gestureZoom = this.zoom;
|
|
582
|
+
this.zooming = false;
|
|
583
|
+
}
|
|
584
|
+
if (zoom === this.zoom) {
|
|
585
|
+
this.schedulePump(); // a pan: drawings go on, within the frame budget
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
this.zoom = zoom;
|
|
589
|
+
if (!this.zooming) {
|
|
590
|
+
this.zooming = true;
|
|
591
|
+
// a walk in progress puts its children back out of the render tree; it starts again after the settle
|
|
592
|
+
for (const c of [...this.queue]) if (c.job.phase === 'collect') this.dropJob(c);
|
|
593
|
+
}
|
|
594
|
+
for (const e of this.entries.values()) this.reconcile(e, true);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** The gesture settled: what is now over the cut is handed back (paced), what is under it shows its still. */
|
|
598
|
+
settle(zoom?: number): void {
|
|
599
|
+
this.activityAt = this.now();
|
|
600
|
+
if (zoom !== undefined && zoom > 0) this.zoom = zoom;
|
|
601
|
+
if (this.zooming) this.holdUntil = this.now() + STILL_SETTLE_HOLD_MS;
|
|
602
|
+
this.gestureZoom = null;
|
|
603
|
+
this.zooming = false;
|
|
604
|
+
this.reconcileAll();
|
|
605
|
+
this.schedulePump();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** Is the zoom in flight (diagnostics)? */
|
|
609
|
+
engaged(): boolean {
|
|
610
|
+
return this.gestureZoom !== null;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** The settled zoom (the warm-up asks which artboards would be under the cut). */
|
|
614
|
+
zoomNow(): number {
|
|
615
|
+
return this.zoom;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/** The last camera write or settle (ms on this controller's clock). */
|
|
619
|
+
lastActivity(): number {
|
|
620
|
+
return this.activityAt;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Nothing moves and nothing is held: no gesture, past the settle hold, no hand-back. `drawing` also asks that no
|
|
625
|
+
* drawing is in flight or waiting to start for an artboard on screen.
|
|
626
|
+
*/
|
|
627
|
+
quiet(drawing = false): boolean {
|
|
628
|
+
if (this.gestureZoom !== null || this.zooming || this.restoring.length > 0) return false;
|
|
629
|
+
if (this.now() < this.holdUntil) return false;
|
|
630
|
+
return !drawing || (this.queue.length === 0 && this.decoding.size === 0);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** Has this artboard a kept still (current or stale)? */
|
|
634
|
+
hasStill(id: string): boolean {
|
|
635
|
+
return !!this.cache.peek(id);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** A drawing of `id` ended (`ok`: a still came of it, or its kept still was reused). */
|
|
639
|
+
onStill(fn: (id: string, ok: boolean) => void): () => void {
|
|
640
|
+
this.stillListeners.add(fn);
|
|
641
|
+
return () => this.stillListeners.delete(fn);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
private small(e: Entry, zoom: number): boolean {
|
|
645
|
+
return inOverviewCut(e.target.article.offsetWidth, zoom);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
private reconcileAll(): void {
|
|
649
|
+
const handBack: Entry[] = [];
|
|
650
|
+
for (const e of this.entries.values()) {
|
|
651
|
+
const t = this.reconcile(e, false, true);
|
|
652
|
+
if (t) handBack.push(e);
|
|
653
|
+
}
|
|
654
|
+
if (handBack.length) this.queueHandBack(handBack);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Move one entry toward its target. Returns true when it should be handed back by the paced queue (only when
|
|
659
|
+
* `queue` — the settle's batch); otherwise a hand-back starts at once.
|
|
660
|
+
*/
|
|
661
|
+
private reconcile(e: Entry, gesture: boolean, queue = false): boolean {
|
|
662
|
+
const exempt = e.pinned || e.eager;
|
|
663
|
+
const t = targetView({
|
|
664
|
+
view: e.view,
|
|
665
|
+
small: this.small(e, this.zoom),
|
|
666
|
+
exempt,
|
|
667
|
+
hasStill: !!e.shown,
|
|
668
|
+
gesture,
|
|
669
|
+
});
|
|
670
|
+
if (t === 'still' && e.view !== 'still') {
|
|
671
|
+
this.setView(e, 'still');
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
if (t === 'cover' && e.view !== 'cover') {
|
|
675
|
+
this.setView(e, 'cover');
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
if (t === 'live' && (e.view === 'still' || e.view === 'cover')) {
|
|
679
|
+
if (exempt) {
|
|
680
|
+
this.handOver(e, false);
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
if (queue) return !this.restoring.includes(e);
|
|
684
|
+
if (!gesture) this.handOver(e, false);
|
|
685
|
+
}
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
private queueHandBack(list: Entry[]): void {
|
|
690
|
+
const W = typeof innerWidth === 'number' ? innerWidth : 0;
|
|
691
|
+
const H = typeof innerHeight === 'number' ? innerHeight : 0;
|
|
692
|
+
const dist = new Map<Entry, number>();
|
|
693
|
+
for (const e of list) {
|
|
694
|
+
const r = e.target.article.getBoundingClientRect();
|
|
695
|
+
dist.set(e, Math.hypot(r.left + r.width / 2 - W / 2, r.top + r.height / 2 - H / 2) || 0);
|
|
696
|
+
}
|
|
697
|
+
list.sort((a, b) => (dist.get(a) ?? 0) - (dist.get(b) ?? 0));
|
|
698
|
+
this.restoring = [...this.restoring.filter((x) => !list.includes(x)), ...list];
|
|
699
|
+
this.handbackQuiet = STILL_HANDBACK_QUIET_FRAMES;
|
|
700
|
+
this.handbackStep = 1;
|
|
701
|
+
this.lastFrameAt = 0;
|
|
702
|
+
this.handbackWaited = 0;
|
|
703
|
+
this.scheduleHandback();
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
private scheduleHandback(): void {
|
|
707
|
+
if (!this.restoring.length || this.restoreScheduled) {
|
|
708
|
+
if (!this.restoring.length) this.schedulePump(); // the hand-back is done: drawings go on
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
this.restoreScheduled = true;
|
|
712
|
+
this.frame(() => {
|
|
713
|
+
this.restoreScheduled = false;
|
|
714
|
+
this.handbackTick();
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** One frame of the hand-back: wait for quiet frames, then a few artboards, fewer after a slow frame. */
|
|
719
|
+
private handbackTick(): void {
|
|
720
|
+
if (this.gestureZoom !== null) return; // paused: the gesture's settle queues again
|
|
721
|
+
const t = this.now();
|
|
722
|
+
const dt = this.lastFrameAt ? t - this.lastFrameAt : 0;
|
|
723
|
+
this.lastFrameAt = t;
|
|
724
|
+
const slow = dt > STILL_HANDBACK_SLOW_MS;
|
|
725
|
+
const stalled = this.handbackWaited >= STILL_HANDBACK_MAX_WAIT_FRAMES;
|
|
726
|
+
if (this.handbackQuiet > 0 && !stalled) {
|
|
727
|
+
if (!slow) this.handbackQuiet -= 1;
|
|
728
|
+
else this.handbackWaited += 1;
|
|
729
|
+
} else if (slow && !stalled) {
|
|
730
|
+
this.handbackStep = 1;
|
|
731
|
+
this.handbackWaited += 1;
|
|
732
|
+
} else {
|
|
733
|
+
this.handbackQuiet = 0;
|
|
734
|
+
this.handbackWaited = 0;
|
|
735
|
+
if (slow) this.handbackStep = 1;
|
|
736
|
+
const batch = this.restoring.splice(0, this.handbackStep);
|
|
737
|
+
for (const e of batch) {
|
|
738
|
+
// still heading live? (a zoom-out in between, or it was released)
|
|
739
|
+
if (this.entries.get(e.id) !== e) continue;
|
|
740
|
+
if (e.view !== 'still' && e.view !== 'cover') continue;
|
|
741
|
+
if (this.small(e, this.zoom) && !(e.pinned || e.eager)) continue;
|
|
742
|
+
this.handOver(e, false);
|
|
743
|
+
}
|
|
744
|
+
this.handbackStep = Math.min(STILL_RESTORE_PER_FRAME, this.handbackStep * 2);
|
|
745
|
+
}
|
|
746
|
+
this.scheduleHandback();
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* The hand-over to live: the body is shown under its stand-in (`paint`); two frames later, with its pictures
|
|
751
|
+
* decoded and the fonts ready, the stand-in fades out (`reveal`) and is gone (`live`). A view change in between
|
|
752
|
+
* (a zoom back out) cancels it.
|
|
753
|
+
*/
|
|
754
|
+
private handOver(e: Entry, fresh: boolean): void {
|
|
755
|
+
if (fresh && !e.shown && !e.slot) {
|
|
756
|
+
e.wasLive = true;
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
this.setView(e, 'paint');
|
|
760
|
+
const seq = e.seq;
|
|
761
|
+
const ok = () => this.entries.get(e.id) === e && e.seq === seq;
|
|
762
|
+
this.frame(() =>
|
|
763
|
+
this.frame(() => {
|
|
764
|
+
if (!ok()) return;
|
|
765
|
+
void this.painted(e.target.body)
|
|
766
|
+
.catch(() => {})
|
|
767
|
+
.then(() => {
|
|
768
|
+
if (!ok()) return;
|
|
769
|
+
this.setView(e, 'reveal');
|
|
770
|
+
const seq2 = e.seq;
|
|
771
|
+
this.later(() => {
|
|
772
|
+
if (this.entries.get(e.id) === e && e.seq === seq2) this.setView(e, 'live');
|
|
773
|
+
}, STILL_REVEAL_MS + 20);
|
|
774
|
+
});
|
|
775
|
+
})
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
private setView(e: Entry, v: StillView): void {
|
|
780
|
+
if (e.view === v) return;
|
|
781
|
+
const prev = e.view;
|
|
782
|
+
e.view = v;
|
|
783
|
+
e.seq += 1;
|
|
784
|
+
if (v === 'live') {
|
|
785
|
+
e.wasLive = true;
|
|
786
|
+
e.target.article.removeAttribute(STILL_ATTR);
|
|
787
|
+
} else {
|
|
788
|
+
if (v === 'paint' || v === 'reveal') e.wasLive = true;
|
|
789
|
+
else if (!hiddenView(prev)) {
|
|
790
|
+
// its box keeps the height it has now while its children are out of the render tree
|
|
791
|
+
const h = e.target.body.offsetHeight;
|
|
792
|
+
if (h > 0) e.target.body.style?.setProperty?.('--dc-still-h', `${h}px`);
|
|
793
|
+
}
|
|
794
|
+
this.ensureSlot(e);
|
|
795
|
+
e.target.article.setAttribute(STILL_ATTR, v);
|
|
796
|
+
}
|
|
797
|
+
const h = this.changes.get(e.id) ?? [];
|
|
798
|
+
h.push(v);
|
|
799
|
+
if (h.length > 32) h.shift();
|
|
800
|
+
this.changes.set(e.id, h);
|
|
801
|
+
this.onView?.(e.id, v);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
private ensureSlot(e: Entry): HTMLElement {
|
|
805
|
+
if (!e.slot) {
|
|
806
|
+
const s = this.makeSlot();
|
|
807
|
+
s.classList.add(STILL_CLASS);
|
|
808
|
+
s.setAttribute?.('aria-hidden', 'true');
|
|
809
|
+
if (e.target.bg) s.style.backgroundColor = e.target.bg;
|
|
810
|
+
e.slot = s;
|
|
811
|
+
}
|
|
812
|
+
place(e.slot, e.target);
|
|
813
|
+
if (e.slot.parentNode !== e.target.article) e.target.article.appendChild(e.slot);
|
|
814
|
+
return e.slot;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
private putStill(e: Entry, still: HTMLElement): void {
|
|
818
|
+
const slot = this.ensureSlot(e);
|
|
819
|
+
if (e.shown === still) return;
|
|
820
|
+
if (e.shown && e.shown.parentNode === slot) e.shown.remove();
|
|
821
|
+
slot.appendChild(still);
|
|
822
|
+
e.shown = still;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/** Test / diagnostics view. */
|
|
826
|
+
stats(): {
|
|
827
|
+
registered: number;
|
|
828
|
+
withStill: number;
|
|
829
|
+
showing: number;
|
|
830
|
+
covers: number;
|
|
831
|
+
engaged: boolean;
|
|
832
|
+
takes: { n: number; ms: number; max: number; slice: number; reused: number };
|
|
833
|
+
cache: { n: number; bytes: number };
|
|
834
|
+
views: Record<string, number>;
|
|
835
|
+
} {
|
|
836
|
+
let withStill = 0;
|
|
837
|
+
let showing = 0;
|
|
838
|
+
let covers = 0;
|
|
839
|
+
const views: Record<string, number> = {};
|
|
840
|
+
for (const e of this.entries.values()) {
|
|
841
|
+
if (e.shown) withStill += 1;
|
|
842
|
+
if (e.view === 'still') showing += 1;
|
|
843
|
+
if (e.view === 'cover') covers += 1;
|
|
844
|
+
views[e.view] = (views[e.view] ?? 0) + 1;
|
|
845
|
+
}
|
|
846
|
+
return {
|
|
847
|
+
registered: this.entries.size,
|
|
848
|
+
withStill,
|
|
849
|
+
showing,
|
|
850
|
+
covers,
|
|
851
|
+
engaged: this.gestureZoom !== null,
|
|
852
|
+
takes: {
|
|
853
|
+
...this.takes,
|
|
854
|
+
ms: Math.round(this.takes.ms),
|
|
855
|
+
max: Math.round(this.takes.max),
|
|
856
|
+
slice: Math.round(this.takes.slice * 10) / 10,
|
|
857
|
+
},
|
|
858
|
+
cache: this.cache.stats(),
|
|
859
|
+
views,
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/** The views each artboard went through since it first registered (oldest first, ≤ 32). */
|
|
864
|
+
history(id?: string): Record<string, StillView[]> {
|
|
865
|
+
if (id) return { [id]: [...(this.changes.get(id) ?? [])] };
|
|
866
|
+
return Object.fromEntries([...this.changes].map(([k, v]) => [k, [...v]]));
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/** The last drawings, oldest first (diagnostics). */
|
|
870
|
+
jobs(): StillJobTrace[] {
|
|
871
|
+
return this.traces.map((t) => ({ ...t, phaseMax: { ...t.phaseMax }, marks: { ...t.marks } }));
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
isShowing(id: string): boolean {
|
|
875
|
+
return this.entries.get(id)?.view === 'still';
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
viewOf(id: string): StillView | null {
|
|
879
|
+
return this.entries.get(id)?.view ?? null;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/** Drawings wait while the zoom moves and while the hand-back paints. */
|
|
883
|
+
private blocked(): boolean {
|
|
884
|
+
return this.zooming || this.restoring.length > 0 || !this.job;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/** Draw on the next frame, if there is anything to draw and drawing is allowed now. */
|
|
888
|
+
private schedulePump(): void {
|
|
889
|
+
if (this.pumpQueued || this.blocked()) return;
|
|
890
|
+
const t = this.now();
|
|
891
|
+
if (t < this.holdUntil) {
|
|
892
|
+
if (this.holdTimer === null)
|
|
893
|
+
this.holdTimer = this.later(() => {
|
|
894
|
+
this.holdTimer = null;
|
|
895
|
+
this.schedulePump();
|
|
896
|
+
}, this.holdUntil - t);
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
this.pumpQueued = true;
|
|
900
|
+
this.frame(() => {
|
|
901
|
+
this.pumpQueued = false;
|
|
902
|
+
this.pump();
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/** One frame's drawing: slices of the drawing in flight (and the next ones) until the frame budget is spent. */
|
|
907
|
+
private pump(): void {
|
|
908
|
+
if (this.blocked()) return;
|
|
909
|
+
const t0 = this.now();
|
|
910
|
+
if (t0 < this.holdUntil) {
|
|
911
|
+
this.schedulePump();
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
const deadline = t0 + (this.gestureZoom !== null ? STILL_PAN_BUDGET_MS : STILL_IDLE_BUDGET_MS);
|
|
915
|
+
for (let guard = 0; guard < 64; guard++) {
|
|
916
|
+
const c = this.queue[0];
|
|
917
|
+
if (!c) {
|
|
918
|
+
// nothing to step: start the next drawing (a kept still reused counts as one), unless enough are decoding
|
|
919
|
+
if (this.decoding.size >= STILL_MAX_DECODING || !this.startNext()) return;
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
const s0 = this.now();
|
|
923
|
+
const p0 = c.job.phase;
|
|
924
|
+
let r: 'more' | 'wait' | 'done';
|
|
925
|
+
try {
|
|
926
|
+
r = c.job.step(deadline);
|
|
927
|
+
} catch {
|
|
928
|
+
c.job.abort();
|
|
929
|
+
r = 'done';
|
|
930
|
+
}
|
|
931
|
+
const sd = this.now() - s0;
|
|
932
|
+
this.takes.slice = Math.max(this.takes.slice, sd);
|
|
933
|
+
c.slices += 1;
|
|
934
|
+
c.sliceMax = Math.max(c.sliceMax, sd);
|
|
935
|
+
c.phaseMax[p0] = Math.round(Math.max(c.phaseMax[p0] ?? 0, sd) * 10) / 10;
|
|
936
|
+
const ph = c.job.phase;
|
|
937
|
+
if (ph !== 'collect' && c.marks.walk === undefined)
|
|
938
|
+
c.marks.walk = Math.round(this.now() - c.t0);
|
|
939
|
+
if ((ph === 'paint' || ph === 'done') && c.marks.decode === undefined)
|
|
940
|
+
c.marks.decode = Math.round(this.now() - c.t0);
|
|
941
|
+
if (r === 'wait') {
|
|
942
|
+
this.queue.shift();
|
|
943
|
+
this.decoding.add(c);
|
|
944
|
+
void Promise.resolve(c.job.ready).then(() => {
|
|
945
|
+
if (!this.decoding.delete(c)) return; // dropped meanwhile
|
|
946
|
+
this.queue.unshift(c); // decoded: it paints before the next walk goes on
|
|
947
|
+
this.schedulePump();
|
|
948
|
+
});
|
|
949
|
+
} else if (r === 'done') {
|
|
950
|
+
this.queue.shift();
|
|
951
|
+
this.finishJob(c);
|
|
952
|
+
}
|
|
953
|
+
if (this.now() >= deadline) break;
|
|
954
|
+
}
|
|
955
|
+
this.schedulePump();
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/** Forget a drawing in flight (its artboard left, or a zoom started mid-walk): its walk state goes. */
|
|
959
|
+
private dropJob(c: Current): void {
|
|
960
|
+
c.job.abort();
|
|
961
|
+
this.queue = this.queue.filter((x) => x !== c);
|
|
962
|
+
this.decoding.delete(c);
|
|
963
|
+
if (c.e.job === c) c.e.job = null;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/** Pick the next artboard to draw and start it (or reuse its kept still). False when there is none now. */
|
|
967
|
+
private startNext(): boolean {
|
|
968
|
+
const e = this.nextToTake();
|
|
969
|
+
if (!e || !this.job) return false;
|
|
970
|
+
// the same content as the kept still: nothing to draw
|
|
971
|
+
let sig = '';
|
|
972
|
+
try {
|
|
973
|
+
sig = this.signature(e.target.body);
|
|
974
|
+
} catch {
|
|
975
|
+
sig = '';
|
|
976
|
+
}
|
|
977
|
+
const kept = this.cache.peek(e.id);
|
|
978
|
+
if (sig && kept && kept.sig === sig) {
|
|
979
|
+
e.dirty = false;
|
|
980
|
+
this.takes.reused += 1;
|
|
981
|
+
if (e.shown !== kept.el) this.putStill(e, kept.el);
|
|
982
|
+
this.afterTake(e);
|
|
983
|
+
for (const fn of this.stillListeners) fn(e.id, true);
|
|
984
|
+
return true;
|
|
985
|
+
}
|
|
986
|
+
const a = e.target.article;
|
|
987
|
+
const walk: WalkHooks = {
|
|
988
|
+
// a body under its stand-in is out of the render tree: back in (transparent) for the walk only
|
|
989
|
+
enter: () => {
|
|
990
|
+
if (hiddenView(e.view)) a.setAttribute(STILL_WALK_ATTR, '');
|
|
991
|
+
},
|
|
992
|
+
leave: () => a.removeAttribute(STILL_WALK_ATTR),
|
|
993
|
+
};
|
|
994
|
+
const c: Current = {
|
|
995
|
+
e,
|
|
996
|
+
job: this.job(e.target.body, walk),
|
|
997
|
+
sig,
|
|
998
|
+
gen: e.gen,
|
|
999
|
+
t0: this.now(),
|
|
1000
|
+
waiting: false,
|
|
1001
|
+
slices: 0,
|
|
1002
|
+
sliceMax: 0,
|
|
1003
|
+
phaseMax: {},
|
|
1004
|
+
marks: {},
|
|
1005
|
+
};
|
|
1006
|
+
e.job = c;
|
|
1007
|
+
this.queue.push(c);
|
|
1008
|
+
return true;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
private finishJob(c: Current): void {
|
|
1012
|
+
const e = c.e;
|
|
1013
|
+
if (e.job === c) e.job = null;
|
|
1014
|
+
const still = c.job.result;
|
|
1015
|
+
const dt = this.now() - c.t0;
|
|
1016
|
+
this.takes.n += 1;
|
|
1017
|
+
this.takes.ms += dt;
|
|
1018
|
+
this.takes.max = Math.max(this.takes.max, dt);
|
|
1019
|
+
c.marks.end = Math.round(dt);
|
|
1020
|
+
this.traces.push({
|
|
1021
|
+
id: e.id,
|
|
1022
|
+
view: e.view,
|
|
1023
|
+
slices: c.slices,
|
|
1024
|
+
sliceMax: Math.round(c.sliceMax * 10) / 10,
|
|
1025
|
+
phaseMax: c.phaseMax,
|
|
1026
|
+
marks: c.marks,
|
|
1027
|
+
});
|
|
1028
|
+
if (this.traces.length > 64) this.traces.shift();
|
|
1029
|
+
if (this.entries.get(e.id) !== e) {
|
|
1030
|
+
still?.remove();
|
|
1031
|
+
for (const fn of this.stillListeners) fn(e.id, false);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
// changed while it was drawn: shown (better than a cover), drawn again once the change settles
|
|
1035
|
+
const current = e.gen === c.gen;
|
|
1036
|
+
if (current) e.dirty = false;
|
|
1037
|
+
if (still) {
|
|
1038
|
+
this.cache.set(e.id, still, current ? c.sig : '', this.sizeOf(still), (k) =>
|
|
1039
|
+
this.entries.has(k)
|
|
1040
|
+
);
|
|
1041
|
+
this.putStill(e, still);
|
|
1042
|
+
}
|
|
1043
|
+
this.afterTake(e);
|
|
1044
|
+
for (const fn of this.stillListeners) fn(e.id, !!still);
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
private nextToTake(): Entry | null {
|
|
1048
|
+
const t = this.now();
|
|
1049
|
+
let soonest = Number.POSITIVE_INFINITY;
|
|
1050
|
+
let bestRank = 99;
|
|
1051
|
+
let best: Entry[] = [];
|
|
1052
|
+
for (const e of this.entries.values()) {
|
|
1053
|
+
if (!e.dirty || e.job) continue;
|
|
1054
|
+
const covered = e.view === 'cover';
|
|
1055
|
+
if (
|
|
1056
|
+
!covered &&
|
|
1057
|
+
!stillWanted({
|
|
1058
|
+
width: e.target.article.offsetWidth,
|
|
1059
|
+
zoom: this.zoom,
|
|
1060
|
+
pinned: e.pinned,
|
|
1061
|
+
eager: e.eager,
|
|
1062
|
+
})
|
|
1063
|
+
)
|
|
1064
|
+
continue;
|
|
1065
|
+
if (e.readyAt > t) {
|
|
1066
|
+
soonest = Math.min(soonest, e.readyAt);
|
|
1067
|
+
continue;
|
|
1068
|
+
}
|
|
1069
|
+
// a cover on screen first, then a missing still on screen, a stale one, then the prefetch ring
|
|
1070
|
+
const rank = (this.visibleOf(e) ? 0 : 3) + (covered ? 0 : e.shown ? 2 : 1);
|
|
1071
|
+
if (rank < bestRank) {
|
|
1072
|
+
bestRank = rank;
|
|
1073
|
+
best = [e];
|
|
1074
|
+
} else if (rank === bestRank) best.push(e);
|
|
1075
|
+
}
|
|
1076
|
+
if (!best.length) {
|
|
1077
|
+
if (soonest < Number.POSITIVE_INFINITY && this.readyTimer === null)
|
|
1078
|
+
this.readyTimer = this.later(() => {
|
|
1079
|
+
this.readyTimer = null;
|
|
1080
|
+
this.schedulePump();
|
|
1081
|
+
}, soonest - t);
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
if (best.length === 1) return best[0] as Entry;
|
|
1085
|
+
// nearest the centre of the view first
|
|
1086
|
+
const W = typeof innerWidth === 'number' ? innerWidth : 0;
|
|
1087
|
+
const H = typeof innerHeight === 'number' ? innerHeight : 0;
|
|
1088
|
+
let pick = best[0] as Entry;
|
|
1089
|
+
let d0 = Number.POSITIVE_INFINITY;
|
|
1090
|
+
for (const e of best) {
|
|
1091
|
+
const r = e.target.article.getBoundingClientRect();
|
|
1092
|
+
const d = Math.hypot(r.left + r.width / 2 - W / 2, r.top + r.height / 2 - H / 2) || 0;
|
|
1093
|
+
if (d < d0) {
|
|
1094
|
+
d0 = d;
|
|
1095
|
+
pick = e;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return pick;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/** A still came in: a cover becomes it (mid-pan too), a live artboard under the cut swaps to it (the one change). */
|
|
1102
|
+
private afterTake(e: Entry): void {
|
|
1103
|
+
if (this.zooming) return;
|
|
1104
|
+
this.reconcile(e, this.gestureZoom !== null);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
const hiddenView = (v: StillView) => v === 'still' || v === 'cover';
|
|
1109
|
+
|
|
1110
|
+
/** Put the stand-in over the body: same offset and size inside the (positioned) article. */
|
|
1111
|
+
function place(slot: HTMLElement, t: StillTarget): void {
|
|
1112
|
+
const st = slot.style;
|
|
1113
|
+
st.left = `${t.body.offsetLeft}px`;
|
|
1114
|
+
st.top = `${t.body.offsetTop}px`;
|
|
1115
|
+
st.width = `${t.body.offsetWidth}px`;
|
|
1116
|
+
st.height = `${t.body.offsetHeight}px`;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
function defaultSize(still: HTMLElement): number {
|
|
1120
|
+
const c = still as unknown as { width?: number; height?: number };
|
|
1121
|
+
return Math.max(1, (c.width ?? 0) * (c.height ?? 0) * 4);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/** The body's pictures decoded and the fonts ready (each bounded: a picture that never loads does not hold it). */
|
|
1125
|
+
async function defaultPainted(body: HTMLElement): Promise<void> {
|
|
1126
|
+
const waits: Promise<unknown>[] = [];
|
|
1127
|
+
for (const img of Array.from(body.getElementsByTagName('img'))) {
|
|
1128
|
+
if (img.complete && img.naturalWidth === 0) continue; // a missing picture: nothing to wait for
|
|
1129
|
+
waits.push(img.decode().catch(() => {}));
|
|
1130
|
+
}
|
|
1131
|
+
const fonts = (document as Document & { fonts?: { ready: Promise<unknown> } }).fonts;
|
|
1132
|
+
if (fonts?.ready) waits.push(fonts.ready);
|
|
1133
|
+
await Promise.race([Promise.all(waits), new Promise((r) => setTimeout(r, 400))]);
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
/**
|
|
1137
|
+
* A change to what the body paints. A picture's `src` changing is not one yet (the asset layer retries a missing
|
|
1138
|
+
* picture every few seconds, which redrew those stills forever): its `load` is.
|
|
1139
|
+
*/
|
|
1140
|
+
export function isPaintMutation(
|
|
1141
|
+
m: Pick<MutationRecord, 'type' | 'attributeName' | 'target'>
|
|
1142
|
+
): boolean {
|
|
1143
|
+
if (m.type !== 'attributes') return true;
|
|
1144
|
+
const t = m.target as Element;
|
|
1145
|
+
return !(
|
|
1146
|
+
(m.attributeName === 'src' || m.attributeName === 'srcset') &&
|
|
1147
|
+
(t.tagName === 'IMG' || t.tagName === 'VIDEO' || t.tagName === 'SOURCE')
|
|
1148
|
+
);
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
function defaultObserve(
|
|
1152
|
+
el: HTMLElement,
|
|
1153
|
+
onChange: (why?: string) => void
|
|
1154
|
+
): MutationObserver | null {
|
|
1155
|
+
if (typeof MutationObserver !== 'function') return null;
|
|
1156
|
+
const mo = new MutationObserver((list) => {
|
|
1157
|
+
if (list.some(isPaintMutation)) onChange();
|
|
1158
|
+
});
|
|
1159
|
+
mo.observe(el, { subtree: true, childList: true, attributes: true, characterData: true });
|
|
1160
|
+
// a picture that came in (load does not bubble: capture)
|
|
1161
|
+
const onLoad = () => onChange('load');
|
|
1162
|
+
el.addEventListener('load', onLoad, true);
|
|
1163
|
+
const disconnect = mo.disconnect.bind(mo);
|
|
1164
|
+
mo.disconnect = () => {
|
|
1165
|
+
el.removeEventListener('load', onLoad, true);
|
|
1166
|
+
disconnect();
|
|
1167
|
+
};
|
|
1168
|
+
return mo;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* The stand-in's CSS (folded into the canvas-lib stylesheet with ARTBOARD_LIVE_CSS). Under its still or cover the
|
|
1173
|
+
* body's children leave the render tree (`display: none` — no style, layout or paint; the DOM, ids and React state
|
|
1174
|
+
* stay) and the body keeps its box (`--dc-still-h`). Measured in Safari on the mascot canvas at 10 %: zoom p95 44 ms
|
|
1175
|
+
* this way, 89 with `visibility: hidden`, 150 with `content-visibility: hidden`, 526 live. During the hand-over the
|
|
1176
|
+
* body paints under the stand-in, which then fades.
|
|
1177
|
+
*/
|
|
1178
|
+
export const ARTBOARD_STILL_CSS = `
|
|
1179
|
+
.dc-artboard > .${STILL_CLASS} { display: none; position: absolute; pointer-events: none; overflow: hidden; z-index: 1; }
|
|
1180
|
+
.dc-artboard[${STILL_ATTR}] > .${STILL_CLASS} { display: block; }
|
|
1181
|
+
.dc-artboard > .${STILL_CLASS} > canvas { display: block; width: 100%; height: 100%; }
|
|
1182
|
+
.dc-artboard[${STILL_ATTR}='still'] > .dc-artboard-body,
|
|
1183
|
+
.dc-artboard[${STILL_ATTR}='cover'] > .dc-artboard-body { min-height: var(--dc-still-h, 0px); }
|
|
1184
|
+
.dc-artboard[${STILL_ATTR}='still']:not([${STILL_WALK_ATTR}]) > .dc-artboard-body > *,
|
|
1185
|
+
.dc-artboard[${STILL_ATTR}='cover']:not([${STILL_WALK_ATTR}]) > .dc-artboard-body > * { display: none !important; }
|
|
1186
|
+
.dc-artboard[${STILL_WALK_ATTR}] > .dc-artboard-body { opacity: 0; }
|
|
1187
|
+
.dc-artboard[${STILL_ATTR}='reveal'] > .${STILL_CLASS} { opacity: 0; transition: opacity ${STILL_REVEAL_MS}ms linear; }
|
|
1188
|
+
`;
|