@graview/pages 0.1.74 → 0.2.1

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 (58) hide show
  1. package/dist/context-menu.d.ts.map +1 -1
  2. package/dist/context-menu.js +11 -1
  3. package/dist/context-menu.js.map +1 -1
  4. package/dist/face-controls.d.ts.map +1 -1
  5. package/dist/face-controls.js +17 -2
  6. package/dist/face-controls.js.map +1 -1
  7. package/dist/held-element.d.ts +12 -0
  8. package/dist/held-element.d.ts.map +1 -0
  9. package/dist/held-element.js +90 -0
  10. package/dist/held-element.js.map +1 -0
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/index.js.map +1 -1
  15. package/dist/one-place-city.d.ts +9 -0
  16. package/dist/one-place-city.d.ts.map +1 -0
  17. package/dist/one-place-city.js +1892 -0
  18. package/dist/one-place-city.js.map +1 -0
  19. package/dist/one-place-control.d.ts +37 -0
  20. package/dist/one-place-control.d.ts.map +1 -0
  21. package/dist/one-place-control.js +14 -0
  22. package/dist/one-place-control.js.map +1 -0
  23. package/dist/one-place-geometry.d.ts +297 -0
  24. package/dist/one-place-geometry.d.ts.map +1 -0
  25. package/dist/one-place-geometry.js +532 -0
  26. package/dist/one-place-geometry.js.map +1 -0
  27. package/dist/one-place-signs.d.ts +59 -0
  28. package/dist/one-place-signs.d.ts.map +1 -0
  29. package/dist/one-place-signs.js +358 -0
  30. package/dist/one-place-signs.js.map +1 -0
  31. package/dist/one-place.d.ts +87 -0
  32. package/dist/one-place.d.ts.map +1 -0
  33. package/dist/one-place.js +1325 -0
  34. package/dist/one-place.js.map +1 -0
  35. package/dist/page-context.d.ts +28 -0
  36. package/dist/page-context.d.ts.map +1 -1
  37. package/dist/page-context.js +3 -0
  38. package/dist/page-context.js.map +1 -1
  39. package/dist/page-problems.d.ts.map +1 -1
  40. package/dist/page-problems.js +18 -6
  41. package/dist/page-problems.js.map +1 -1
  42. package/dist/page-record.d.ts.map +1 -1
  43. package/dist/page-record.js +3 -1
  44. package/dist/page-record.js.map +1 -1
  45. package/dist/page-search.d.ts.map +1 -1
  46. package/dist/page-search.js +32 -4
  47. package/dist/page-search.js.map +1 -1
  48. package/dist/page-shell.d.ts.map +1 -1
  49. package/dist/page-shell.js +72 -11
  50. package/dist/page-shell.js.map +1 -1
  51. package/dist/page-typography.d.ts.map +1 -1
  52. package/dist/page-typography.js +3 -2
  53. package/dist/page-typography.js.map +1 -1
  54. package/dist/router.d.ts +2 -0
  55. package/dist/router.d.ts.map +1 -1
  56. package/dist/router.js +69 -3
  57. package/dist/router.js.map +1 -1
  58. package/package.json +6 -6
@@ -0,0 +1,1892 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { labelOf, ONE_FACE_LAYERS } from "@graview/core";
3
+ import { RECORD_BOXES_ATTRIBUTE, RECORD_BOXES_MOVED, recordBoxesOf } from "@graview/core/record-boxes";
4
+ import { EMPTY_VIEW, toUrl, withOverview } from "@graview/layout/view";
5
+ import { Scene } from "@graview/react";
6
+ import { useGraview, useNavigation, useViolations } from "@graview/react/provider";
7
+ import { useOneFace } from "./one-place-control.js";
8
+ import { useEffect, useLayoutEffect, useMemo, useRef } from "react";
9
+ import { aimAt, anchorOf, BOARD_ASPECT, frameSpacingAt, boardFaceWidth, boardLeast, boardMost, boardWidthFor, fitBeside, fitCamera, SIGN_GAP, SIGN_POST, signRoom, cameraBetween, cameraPutting, CITY, cityArea, footOf, leaveFrom, routeLine, LEVEL_LIVE, levelZooms, PAGE, pageBox, PANEL, panelBox, PHONE, project, restingLevel, SHEET_ORIGIN, standingAt, standingCss, unionOf } from "./one-place-geometry.js";
10
+ import { placeSigns } from "./one-place-signs.js";
11
+ import { pluralOf } from "./page-typography.js";
12
+ /*
13
+ * THE CITY UNDER THE PAGE, AND ONE CAMERA OVER BOTH
14
+ * (Graview HQ feature:f-page-zooms-out-to-the-city).
15
+ *
16
+ * The Scene itself, in the provider the pages draw with, at its overview:
17
+ * `layout` → `placeCity`, its plots, its buildings, its streets. Over it,
18
+ * nothing but the page the person was reading — the same element at every
19
+ * altitude — and the lines from its parts to their buildings.
20
+ *
21
+ * ONE FRAME, ONE NUMBER. Each animation frame reads the Scene's own camera
22
+ * (its committed zoom and the move between stops it is painting, `live`)
23
+ * and turns it into the ALTITUDE — or, while this face is moving the
24
+ * camera itself (Up, Down, a pinch on the page), sets that camera from the
25
+ * altitude. From the altitude in that frame follow the page's box, how it
26
+ * stands, how much of the Connected list has grown in, the city's fade,
27
+ * and the lines: each line's building end is the building's place in the
28
+ * picture carried through that same camera, and its page end is read from
29
+ * the page after its own transform for that frame is written. Nothing is
30
+ * read from a frame before.
31
+ *
32
+ * Between levels the camera is the Scene's move between stops, painted on
33
+ * its layers outside React; at a level it is written into the view once,
34
+ * and the city laid out again at that zoom.
35
+ */
36
+ /*
37
+ * WHAT A DRAWN PART SAYS IT SHOWS. A page, a lens and a view each mark the
38
+ * record a part of them shows, and the line from that part to the record's
39
+ * building is read off the mark, whatever drew it: the pick every shipped
40
+ * lens and the Scene's own views carry (`data-graview-pick`, what a press
41
+ * there chooses), a routed page's row (`data-graview-record`), and a view's
42
+ * own link to a record (`data-record`). One line a record, from the first
43
+ * of its parts in sight: a task drawn on three days of a calendar is one
44
+ * line, not three. A part scrolled out of the page draws none.
45
+ */
46
+ const RECORD_PARTS = "[data-graview-pick], [data-graview-record], [data-record]";
47
+ /*
48
+ * AND WHAT A VIEW DRAWN WHERE THE PAGE CANNOT READ SAYS IT SHOWS (Graview
49
+ * HQ feature:f-one-face-lines-from-frame-views): a worker view's shadow
50
+ * root, a frame guest's document. The element it is drawn in holds its
51
+ * records' boxes, judged by its host (`RECORD_BOXES`), in its own pixels;
52
+ * each is carried through that element's box in this frame, so a line
53
+ * leaves it as it leaves a part of the page's own. A record page drawn by
54
+ * a view that names none of the records it draws still has its one line,
55
+ * from the view to the record it is about.
56
+ */
57
+ const VIEW_PARTS = `[${RECORD_BOXES_ATTRIBUTE}]`;
58
+ const recordShownBy = (element) => element.getAttribute("data-graview-pick") ?? element.getAttribute("data-graview-record") ?? element.getAttribute("data-record");
59
+ /*
60
+ * THE CITY ON THIS FACE. The Scene's own zoom and its signs past the edge
61
+ * are not this face's: the control at the foot and the camera are. Nor are
62
+ * the Scene's lines and words from a chosen record to what it is linked
63
+ * to: the page beside the city says those, and its lines leave from where
64
+ * it says them. The record the page is about stands out from the rest —
65
+ * lit in the accent and outlined, where it stands: lit changes no geometry,
66
+ * so a building pressed keeps its box to the pixel — while what it is linked to
67
+ * stays lit as the Scene lights it, and everything else is quiet.
68
+ */
69
+ const CITY_CSS = `
70
+ [data-testid="one-place-city"] .graview-zoom, [data-testid="one-place-city"] .graview-edge-sign, [data-testid="one-place-city"] .graview-record-links, [data-testid="one-place-city"] .graview-drive-in { display: none; }
71
+ [data-testid="one-place-city"] .graview-building[data-graview-here] polygon { stroke: var(--graview-ink); stroke-width: 1.2; stroke-opacity: 1; }
72
+ [data-testid="one-place-city"] .graview-building[data-graview-here] .graview-iso-roof { fill: var(--graview-accent); }
73
+ [data-testid="one-place-city"] .graview-building[data-graview-here] .graview-iso-left { fill: color-mix(in srgb, var(--graview-accent) 55%, black); }
74
+ [data-testid="one-place-city"] .graview-building[data-graview-here] .graview-iso-right { fill: color-mix(in srgb, var(--graview-accent) 72%, black); }
75
+ [data-testid="one-place-city"] .graview-kind-face { filter: opacity(var(--graview-one-face-plates, 1)); }
76
+ [data-testid="one-place-city"][data-graview-names-said] .graview-kind-face { visibility: hidden !important; pointer-events: none !important; }
77
+ .graview-one-place-district { position: fixed; left: 0; top: 0; display: flex; gap: 4px; align-items: stretch; width: max-content; max-width: none; font-size: 0.75rem; line-height: 1.4; white-space: nowrap; }
78
+ .graview-one-place-district > button { touch-action: none; display: inline-flex; align-items: center; gap: 0.4em; box-sizing: border-box; min-height: 24px; min-width: 24px; margin: 0; padding: 1px 6px 2px; border: 1px solid var(--graview-edge); border-radius: 5px; background: var(--graview-panel, Canvas); color: var(--graview-ink); font: inherit; letter-spacing: 0.08em; text-transform: uppercase; white-space: nowrap; box-shadow: 0 1px 2px rgba(0,0,0,0.25); cursor: pointer; }
79
+ .graview-one-place-district > button:hover { border-color: var(--graview-accent); }
80
+ .graview-one-place-district > button:focus-visible { outline: 2px solid var(--graview-accent); outline-offset: 1px; }
81
+ .graview-one-place-district > [data-graview-district-problems] { color: var(--graview-warn); font-weight: 600; letter-spacing: 0; font-variant-numeric: tabular-nums; }
82
+ .graview-one-place-district [data-graview-district-tag-count] { color: var(--graview-ink-muted); font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
83
+ @media (max-width: 600px) { .graview-one-place-district { font-size: 0.5625rem; } .graview-one-place-district > button { padding: 0 4px 1px; letter-spacing: 0.04em; } }
84
+ `;
85
+ const zoomNowOf = (cam) => cam.zoom().toFixed(4);
86
+ /** The most lines drawn from a page's parts: past this a list's lines are a hatch. */
87
+ const MOST_LINES = 40;
88
+ /** As many lines into one district as this, and they are one line into it with their count on it. */
89
+ const GATHER = 4;
90
+ /** What a district's "+n" is kept under among the picture's buildings: never a record's id. */
91
+ const REST_KEY = "\u0000rest:";
92
+ /** How long one level's move takes. A person who asked for less motion is cut straight there. */
93
+ export const MOVE_MS = 560;
94
+ /** A hand that has stopped zooming for this long has let go: the altitude settles to the nearest level. */
95
+ const LET_GO_MS = 240;
96
+ const NS = "http://www.w3.org/2000/svg";
97
+ const kindOfId = (id) => (id === null ? null : id.startsWith("kind:") ? id.slice(5) : id.startsWith("aggregate:") && !id.includes("|") && !id.includes("+") ? id.slice(10) : null);
98
+ const boxOf = (rect) => ({ x: rect.left, y: rect.top, width: rect.width, height: rect.height });
99
+ const middle = (box) => ({ x: box.x + box.width / 2, y: box.y + box.height / 2 });
100
+ const keyOf = (where) => (where.type === "record" ? `record:${where.id}` : where.type === "list" ? `list:${where.kind}` : where.type === "place" ? `place:${where.kinds.join(",")}` : where.type);
101
+ const ease = (t) => (t <= 0 ? 0 : t >= 1 ? 1 : t * t * (3 - 2 * t));
102
+ const level = (live) => Math.abs(live.s - 1) < 1e-4 && Math.abs(live.x) < 0.05 && Math.abs(live.y) < 0.05;
103
+ const clamp01 = (t) => Math.max(0, Math.min(1, t));
104
+ /** The same ids, in the same order. */
105
+ const sameIds = (a, b) => a.length === b.length && a.every((id, at) => id === b[at]);
106
+ /** Whether a point stands inside a box. */
107
+ const inside = (p, box) => p.x >= box.x && p.x <= box.x + box.width && p.y >= box.y && p.y <= box.y + box.height;
108
+ export function OnePlaceCity({ where, out, sheet, connected, boards, current, boardEls, boardLayer, fab, view, lessMotion, resolve, openRecord, openKind, onGround, motion, onAltitude }) {
109
+ const { store } = useGraview();
110
+ const { view: seen, go } = useNavigation();
111
+ const box = useRef(null);
112
+ const overlay = useRef(null);
113
+ const rings = useRef(null);
114
+ const camera = useRef(null);
115
+ /*
116
+ * THE SCENE'S STOP: always the overview — its lens over the grid and its
117
+ * shelf of districts are not drawn on this face — and nothing chosen on
118
+ * it, so it draws no lines of its own from a choice and no outline of
119
+ * one: this face draws the one set of lines. (A record its plot has no
120
+ * room to draw — one of a "+415" — stands on its district.)
121
+ */
122
+ const wanted = useMemo(() => withOverview(EMPTY_VIEW, true), []);
123
+ const asked = useRef("");
124
+ /*
125
+ * ONLY ONCE THE PAGE GOES OUT. While it is read, the view the provider
126
+ * holds is the page's own (what the page's quick ask is about, say), and
127
+ * the Scene is not drawn; out, the Scene's view is the overview; in
128
+ * again, the page's own view comes back.
129
+ */
130
+ const kept = useRef(null);
131
+ useEffect(() => {
132
+ if (out) {
133
+ kept.current ??= seen;
134
+ const next = { ...wanted, ...(seen.overview && seen.pan ? { pan: seen.pan } : {}) };
135
+ asked.current = toUrl(next);
136
+ go(next);
137
+ return;
138
+ }
139
+ if (kept.current) {
140
+ const back = kept.current;
141
+ kept.current = null;
142
+ asked.current = toUrl(back);
143
+ go(back);
144
+ }
145
+ // eslint-disable-next-line react-hooks/exhaustive-deps
146
+ }, [out]);
147
+ /* The Scene's own way down — a record focused, a district gone into — is that place, in the panel. A pan is the camera's own and is kept. */
148
+ const latestOut = useRef(out);
149
+ latestOut.current = out;
150
+ const latest = useRef({ openRecord, openKind });
151
+ latest.current = { openRecord, openKind };
152
+ /*
153
+ * NOTHING CHOSEN ON THE SCENE, AFTER A PRESS AS ON A FRESH LOAD. A page
154
+ * opened while the page is out (a building pressed, a link, a pill) says
155
+ * its subject to the provider as the page's choice: the Scene took it for
156
+ * its own, lit that building alone, faded the rest of the city, put the
157
+ * record at the head of its row and wrote its links' words over the city.
158
+ * Out, the Scene keeps nothing chosen — one rule, whichever way the page
159
+ * came — and the page's choice is kept for the page to have back. Read
160
+ * before the frame is painted, so nothing faded is ever drawn.
161
+ */
162
+ useLayoutEffect(() => {
163
+ // While the page is read the view is the page's own, and none of this is the Scene's.
164
+ if (!latestOut.current || toUrl(seen) === asked.current)
165
+ return;
166
+ const picked = seen.selection ?? [];
167
+ // What the page chose is the page's — none, too, on a page of no one record — had back when it comes in again.
168
+ if (kept.current && (picked.length > 0 || (kept.current.selection ?? []).length > 0) && !sameIds(picked, kept.current.selection ?? []))
169
+ kept.current = { ...kept.current, selection: picked };
170
+ if (seen.overview && seen.focusId === null && picked.length === 0)
171
+ return;
172
+ const focus = seen.focusId;
173
+ const back = { ...wanted, ...(seen.pan ? { pan: seen.pan } : {}) };
174
+ asked.current = toUrl(back);
175
+ go(back);
176
+ const record = focus && store.graph.getNode(focus) ? focus : null;
177
+ const kind = kindOfId(focus);
178
+ // A record chosen in the city is its own page, beside the city: never a view of the Scene's own about it.
179
+ if (record)
180
+ latest.current.openRecord(record, PANEL);
181
+ else if (kind)
182
+ latest.current.openKind(kind, PANEL);
183
+ // eslint-disable-next-line react-hooks/exhaustive-deps
184
+ }, [seen]);
185
+ /*
186
+ * THE PLAZA'S ROOM AT THE TOP: the views of no one kind, in rows, under
187
+ * the bar. A kind's views find their own room beside its district
188
+ * (`placeSigns`), clear of every name the face says there.
189
+ */
190
+ const plaza = useMemo(() => {
191
+ const count = boards.filter((board) => board.where.type !== "record" && signKindOf(board.where) === null).length;
192
+ return count === 0 ? 0 : Math.ceil(signRoom(count, boardWidthFor(view)).height + 12);
193
+ }, [boards, view]);
194
+ const sceneOptions = useMemo(() => ({ plaza: plaza + view.top }), [plaza, view.top]);
195
+ /*
196
+ * EACH DISTRICT'S NAME, said by this face: its kind's plural and how many
197
+ * it holds, a size on the screen at every level, placed with the views so
198
+ * nothing stands on it (`placeSigns`). The Scene's own names stand down
199
+ * as these come up: they were laid out for the Scene's own fit, and at
200
+ * this face's City most of them said nothing.
201
+ */
202
+ const violations = useViolations();
203
+ const face = useOneFace();
204
+ // A district's problems: each broken rule that names one of its records, once.
205
+ const problemsOf = useMemo(() => {
206
+ const out = new Map();
207
+ for (const violation of violations) {
208
+ const kinds = new Set(violation.nodeIds.flatMap((id) => { const node = store.graph.getNode(id); return node ? [node.kind] : []; }));
209
+ for (const kind of kinds)
210
+ out.set(kind, (out.get(kind) ?? 0) + 1);
211
+ }
212
+ return out;
213
+ }, [violations, store]);
214
+ const districtNames = store.schema.kinds.map((kind) => ({ kind, plural: pluralOf(store, kind), count: store.graph.count(kind), problems: problemsOf.get(kind) ?? 0 }));
215
+ const tagEls = useRef(new Map());
216
+ const tagLayer = useRef(null);
217
+ const state = useRef({ where, out, view, lessMotion, resolve, onAltitude, boards, current });
218
+ /*
219
+ * WHAT THE FRAME READS IS WHAT IS DRAWN. Written as the render ran, a
220
+ * render React paused part way (a route drawn as a transition) handed the
221
+ * frame a place whose page was not yet on the screen: the move began from
222
+ * the new place's board while the old page still stood there, and the new
223
+ * page came in a long task later, a leap inside the move. Written once the
224
+ * render is committed, the frame moves only what is drawn.
225
+ */
226
+ useLayoutEffect(() => {
227
+ state.current = { where, out, view, lessMotion, resolve, onAltitude, boards, current };
228
+ });
229
+ useEffect(() => {
230
+ let frame = 0;
231
+ let world = { plots: new Map(), buildings: new Map() };
232
+ /*
233
+ * NEARBY FRAMES WHAT THE RECORD IS CONNECTED TO (Graview HQ brief
234
+ * b-cloud-2026-11-13-connected-under-a-view): a record's page at Nearby
235
+ * is aimed beside the panel at the level's own zoom, unless the
236
+ * buildings of what it is linked to and what its page shows would fall
237
+ * outside the city's side of the window; then the camera stands back,
238
+ * no further than it must, to take them all in. That zoom is this
239
+ * page's Nearby, so the altitude read from the camera is the level's
240
+ * still, and the move there is the one move, as any level's is.
241
+ */
242
+ let nearbyZoom = null;
243
+ /* What the last Nearby framing took in; a view that says its records after the move began is framed again, once, from where the camera is. */
244
+ let framedKey = "";
245
+ let reframeAsked = false;
246
+ /*
247
+ * ONCE A MOVE, AND NEVER OVER A HAND: armed as a move to a record's Nearby
248
+ * begins, taken by the first word from a view of what it shows after
249
+ * that, and put away once taken or the moment the person moves the
250
+ * camera themselves. A list a view grows later never takes the camera
251
+ * from where somebody left it.
252
+ */
253
+ let reframeArmed = false;
254
+ const askReframe = () => {
255
+ if (reframeArmed)
256
+ reframeAsked = true;
257
+ };
258
+ const byHand = () => {
259
+ reframeArmed = false;
260
+ reframeAsked = false;
261
+ };
262
+ const framingKeyNow = (id) => `${id}|${[...new Set([id, ...store.graph.neighbors(id).map((node) => node.id), ...markedOnPage()])].sort().join(" ")}`;
263
+ const zoomsOf = (where, view) => {
264
+ const zooms = levelZooms(where, view);
265
+ return nearbyZoom !== null && where.type === "record" ? [zooms[0], nearbyZoom, zooms[2]] : zooms;
266
+ };
267
+ /** The records a page shows, by what its parts mark and what a view drawn in it says. */
268
+ const markedOnPage = () => {
269
+ const plane = sheet.current;
270
+ if (!plane)
271
+ return [];
272
+ const ids = [];
273
+ for (const part of plane.querySelectorAll(`${RECORD_PARTS}, ${VIEW_PARTS}`)) {
274
+ const view = recordBoxesOf(part);
275
+ if (view)
276
+ ids.push(...view.shows());
277
+ else {
278
+ const named = recordShownBy(part);
279
+ if (named)
280
+ ids.push(named);
281
+ }
282
+ if (ids.length > 400)
283
+ break;
284
+ }
285
+ return ids;
286
+ };
287
+ const nearbyFraming = (id, anchor, most, zoomNow) => {
288
+ const now = state.current;
289
+ const view = now.view;
290
+ const base = cameraPutting(anchor, aimAt(PANEL, view, footRoom()), most);
291
+ const ids = new Set([id, ...store.graph.neighbors(id).map((node) => node.id), ...markedOnPage()]);
292
+ framedKey = `${id}|${[...ids].sort().join(" ")}`;
293
+ const kinds = new Set();
294
+ const boxes = [...ids].flatMap((one) => {
295
+ const building = world.buildings.get(one);
296
+ if (!building)
297
+ return [];
298
+ const kind = store.graph.getNode(one)?.kind;
299
+ if (kind)
300
+ kinds.add(kind);
301
+ return [building];
302
+ });
303
+ // And each of their districts, with its name: a district's marks stay in sight with its buildings.
304
+ const named = [...kinds].flatMap((kind) => {
305
+ const plot = world.plots.get(kind);
306
+ return plot ? [plot] : [];
307
+ });
308
+ const around = boxes.length > 1 ? unionOf([...boxes, ...named]) : null;
309
+ if (!around)
310
+ return base;
311
+ // The city's side of the window at Nearby: beside the panel above what stands at the foot, or above a phone's sheet and the control riding on it.
312
+ const bar = view.top ?? 0;
313
+ const panel = panelBox(view);
314
+ const dock = footNow().rest[0];
315
+ const area = view.width <= PHONE ? { x: 0, y: bar, width: view.width, height: Math.max(1, panel.y - bar - footRoom()) } : { x: 0, y: bar, width: panel.x, height: Math.max(1, (dock ? dock.y : view.height) - bar) };
316
+ const margin = 28;
317
+ /*
318
+ * ALREADY IN SIGHT, THE CAMERA STAYS: a record opened from the city
319
+ * beside the page, where it and everything it is connected to already
320
+ * stand in the city's side as the camera rests, moves no camera.
321
+ */
322
+ const buildings = unionOf(boxes);
323
+ const inSight = (cam, box) => {
324
+ const lo = project(cam, { x: box.x, y: box.y });
325
+ const hi = project(cam, { x: box.x + box.width, y: box.y + box.height });
326
+ return lo.x >= area.x && lo.y >= area.y && hi.x <= area.x + area.width && hi.y <= area.y + area.height;
327
+ };
328
+ if (rest === PANEL && !move && level(painted) && buildings && inSight(LEVEL_LIVE, buildings))
329
+ return LEVEL_LIVE;
330
+ const a = project(base, { x: around.x, y: around.y });
331
+ const b = project(base, { x: around.x + around.width, y: around.y + around.height });
332
+ if (a.x >= area.x + margin && a.y >= area.y + margin && b.x <= area.x + area.width - margin && b.y <= area.y + area.height - margin)
333
+ return base;
334
+ const fit = fitCamera(around, area, margin, most);
335
+ // Never as far back as the City, nor past what the camera can be written at.
336
+ const least = Math.max(1.02 / zoomNow, (camera.current?.range.min ?? 0) / zoomNow);
337
+ if (fit.s >= least)
338
+ return fit;
339
+ /* More than the least zoom holds: the record's own building first, in the city's side, and as much else around it as fits. */
340
+ const middleOf = { x: around.x + around.width / 2, y: around.y + around.height / 2 };
341
+ const held = cameraPutting(middleOf, project(fit, middleOf), least);
342
+ const own = world.buildings.get(id);
343
+ if (!own)
344
+ return held;
345
+ const lo = project(held, { x: own.x, y: own.y });
346
+ const hi = project(held, { x: own.x + own.width, y: own.y + own.height });
347
+ const shift = (low, high, from, to) => (low < from ? from - low : high > to ? to - high : 0);
348
+ return { s: held.s, x: held.x + shift(lo.x, hi.x, area.x + margin, area.x + area.width - margin), y: held.y + shift(lo.y, hi.y, area.y + margin, area.y + area.height - margin) };
349
+ };
350
+ /** The camera between stops this frame, as painted — the last one seen while the view takes in a move just let go. */
351
+ let painted = LEVEL_LIVE;
352
+ /** The level the camera rests at, and the altitude this frame. */
353
+ let rest = PAGE;
354
+ let altitude = PAGE;
355
+ /** A move of the camera this face is making: from one camera to another, about the place. */
356
+ let move = null;
357
+ /** What stood at the foot when the City was last fitted: the legend opened or put away there fits it again. */
358
+ let cityFoot = "";
359
+ /** Where the camera stood when it came to rest at the City: a hand that moved it since keeps it. */
360
+ let cityAt = "";
361
+ /** The camera at the level rested at as a scrub began: the person's own, kept for the scrub to leave from. */
362
+ let restCam = null;
363
+ /*
364
+ * THE ZOOM THIS FACE LAST LEFT THE CAMERA AT. Only a zoom that differs
365
+ * from it is a hand's: a new address at rest asks the camera for a
366
+ * different zoom for the same level (a list's, not a record's), and read
367
+ * against that level's zoom it looked like a pinch — the altitude was
368
+ * read off it and settled to another level: the page came down to the
369
+ * page after Back, or went back up to the city after a view was chosen.
370
+ */
371
+ let ownZoom = null;
372
+ /** The panel as it was last frame, and whether a hand has moved it since the camera was written. */
373
+ let lastPanel = "";
374
+ /** When the city was last read, and what it was read for. */
375
+ let lastRead = -Infinity;
376
+ let lastReadKey = "";
377
+ let resized = false;
378
+ let handSince = 0;
379
+ let lastPlace = "";
380
+ let lastReported = "";
381
+ /* When the camera was last written into the view: the Scene settles what is painted a beat after (its wheel's rule), so nothing new is painted until that beat has passed. */
382
+ let committedAt = -Infinity;
383
+ /** When the last frame was drawn: a move does not count a stall between two as time gone by. */
384
+ let lastTick = 0;
385
+ /*
386
+ * THE PAGE CHOSEN IS DRAWN BEFORE IT MOVES. A new place's page is drawn
387
+ * by its route — on a slow machine, in tasks long enough to stall the
388
+ * frames after it — so a move to another place begins only once two
389
+ * frames running have come quickly (or a while has gone by, whatever the
390
+ * page is still doing): the page's drawing lands before the move, not
391
+ * inside it.
392
+ */
393
+ let calmRun = 0;
394
+ /** The page a board was last placed for at once: one with no place to stand is not placed again every frame. */
395
+ let placedFor = "";
396
+ let placeSeenAt = 0;
397
+ let placeSeen = "";
398
+ const drawnSince = (placeKey, now) => {
399
+ if (placeKey !== placeSeen) {
400
+ placeSeen = placeKey;
401
+ placeSeenAt = now;
402
+ calmRun = 0;
403
+ }
404
+ return calmRun >= 2 || now - placeSeenAt > 700;
405
+ };
406
+ /*
407
+ * A PLACE READ AGAIN AT REST IS GONE TO, NOT JUMPED TO. Once the camera
408
+ * is written the Scene lays the city out again at that zoom, and a board
409
+ * or a name placed from it may stand a little elsewhere: it goes there
410
+ * over a moment, from where it was drawn.
411
+ */
412
+ let settle = null;
413
+ /** A camera written in the frame before: what stood where the move ended is carried into the new pixels as this frame begins, the view drawing them now. */
414
+ let rebaseNext = null;
415
+ const SETTLE_MS = 300;
416
+ /*
417
+ * A name's box is carried whole: its size too, which differs between
418
+ * levels (a size on the screen is many times wider in the picture far
419
+ * out). Carried with the size it is going to, a box caught part way —
420
+ * a move begun again when what stands at the foot changed — had its
421
+ * middle moved by half the difference in one frame: a name leapt a
422
+ * hundred pixels sideways.
423
+ */
424
+ const blend = (from, to, t) => {
425
+ if (!to || !from)
426
+ return to;
427
+ const at = { ...to, x: from.x + (to.x - from.x) * t, y: from.y + (to.y - from.y) * t };
428
+ if ("width" in to && "width" in from && "height" in to && "height" in from) {
429
+ const [a, b] = [from, to];
430
+ return { ...at, width: a.width + (b.width - a.width) * t, height: a.height + (b.height - a.height) * t };
431
+ }
432
+ return at;
433
+ };
434
+ /** How far a move or a settle has gone, this frame: 1 when nothing is under way. */
435
+ const goneAt = (now) => move ? { t: ease(clamp01((now - move.start) / MOVE_MS)), slots: move.slots, names: move.names } : settle && now - settle.start < SETTLE_MS ? { t: ease((now - settle.start) / SETTLE_MS), slots: settle.slots, names: settle.names } : { t: 1, slots: null, names: null };
436
+ /** Every board and name where it is drawn this frame. */
437
+ const drawnSlots = (now) => { const g = goneAt(now); return new Map([...slots].map(([key, at]) => [key, blend(g.slots?.get(key), at, g.t) ?? at])); };
438
+ const drawnNames = (now) => { const g = goneAt(now); return new Map([...names].map(([key, at]) => [key, blend(g.names?.get(key), at, g.t) ?? at])); };
439
+ const QUIET_MS = 220;
440
+ const lines = new Map();
441
+ /** The picture as laid out now, read while nothing is painted over it. */
442
+ const readWorld = (holder) => {
443
+ const plots = new Map();
444
+ for (const plot of holder.querySelectorAll("[data-graview-plot]")) {
445
+ const kind = kindOfId(plot.getAttribute("data-graview-plot"));
446
+ const tile = plot.querySelector(".graview-plot-tile") ?? plot;
447
+ if (kind)
448
+ plots.set(kind, boxOf(tile.getBoundingClientRect()));
449
+ }
450
+ const buildings = new Map();
451
+ for (const building of holder.querySelectorAll("[data-graview-building]"))
452
+ buildings.set(building.getAttribute("data-graview-building"), boxOf(building.getBoundingClientRect()));
453
+ /*
454
+ * A district's "+n" — the buildings its plot has no room for — is words
455
+ * in the picture, as a building's name is: stood clear of as a building
456
+ * is, so no district's name runs into it ("+43PLANTS 10").
457
+ */
458
+ holder.querySelectorAll(".graview-village-rest").forEach((rest, i) => {
459
+ const box = boxOf(rest.getBoundingClientRect());
460
+ if (box.width > 0)
461
+ buildings.set(`${REST_KEY}${i}`, box);
462
+ });
463
+ return { plots, buildings };
464
+ };
465
+ /** What a page stands on in the picture as it is laid out. */
466
+ const placeIn = (at, w) => ({
467
+ building: at.type === "record" ? (w.buildings.get(at.id) ?? null) : null,
468
+ district: at.type === "record" || at.type === "list" ? (w.plots.get(at.kind) ?? null) : null,
469
+ districts: at.type === "place" ? at.kinds.flatMap((kind) => (w.plots.has(kind) ? [w.plots.get(kind)] : [])) : [],
470
+ all: [...w.plots.values()],
471
+ });
472
+ /**
473
+ * WHERE EACH VIEW AND EACH DISTRICT'S NAME STANDS (`placeSigns`): a view
474
+ * of one kind on its district, its post coming down onto its ground, the
475
+ * rest on the plaza at the city's back edge, a record's own page on its
476
+ * building; each district's name beside it or under it. And the box
477
+ * around all of it, which the city's camera fits.
478
+ *
479
+ * PLACED ON THE SCREEN AS THE CAMERA WILL HAVE IT. Names and boards are a
480
+ * size on the screen, so the picture is carried through the camera the
481
+ * level is headed for (`cam`, against the view's own zoom; none at rest)
482
+ * and placed there, in the screen's own pixels, then carried back: the
483
+ * place a move heads for and the place read again once it has arrived
484
+ * are one answer, never a second that the boards then jump to.
485
+ *
486
+ * AND ONCE. The same city, the same window and the same views place the
487
+ * same way: kept, and carried along a pan (a pan moves the camera, not
488
+ * the city) until what is placed changes or the pan has gone far enough
489
+ * to change what is in sight.
490
+ */
491
+ let slots = new Map();
492
+ let posts = new Map();
493
+ let scales = new Map();
494
+ let names = new Map();
495
+ /** The city and every view standing in it, as last read: what the city's camera fits. */
496
+ let whole = null;
497
+ let kept = null;
498
+ /** A window too small for a board beside every district: a phone, or one turned on its side. */
499
+ const cramped = () => state.current.view.width <= PHONE || state.current.view.height <= 600;
500
+ const standAll = (w, zoomNow, cam, bounds, lvl) => {
501
+ const view = state.current.view;
502
+ const onScreen = (box) => ({ x: box.x * cam.s + cam.x, y: box.y * cam.s + cam.y, width: box.width * cam.s, height: box.height * cam.s });
503
+ const tiles = new Map([...w.plots].map(([kind, box]) => [kind, onScreen(box)]));
504
+ const city = unionOf([...tiles.values()]);
505
+ const origin = city ? { x: city.x, y: city.y } : { x: 0, y: 0 };
506
+ // A size on the screen at most, and on a small window at least: what it says is read.
507
+ const natural = boardWidthFor(view) * zoomNow * cam.s;
508
+ const width = Math.max(Math.min(natural, boardMost(view, state.current.where, zoomNow * cam.s)), boardLeast(view));
509
+ const k = width / Math.max(1e-6, natural);
510
+ const sizes = new Map();
511
+ for (const [kind, el] of tagEls.current) {
512
+ if (!tiles.has(kind) || el.offsetWidth === 0)
513
+ continue;
514
+ sizes.set(kind, { width: el.offsetWidth, height: el.offsetHeight });
515
+ }
516
+ const signs = state.current.boards.map((board) => ({
517
+ key: board.key,
518
+ kind: board.where.type === "record" ? board.where.kind : signKindOf(board.where),
519
+ building: board.where.type === "record" && w.buildings.has(board.where.id) ? onScreen(w.buildings.get(board.where.id)) : null,
520
+ }));
521
+ const reserved = footNow().now.map((box) => ({ x: box.x - 6, y: box.y - 6, width: box.width + 12, height: box.height + 12 }));
522
+ const round = (n) => Math.round(n / 2) * 2;
523
+ const rel = (box) => [round(box.x - origin.x), round(box.y - origin.y), round(box.width), round(box.height)];
524
+ const buildingsOnScreen = [...w.buildings.values()].map(onScreen);
525
+ const restsOnScreen = [...w.buildings].filter(([key]) => key.startsWith(REST_KEY)).map(([, box]) => onScreen(box));
526
+ // What decides the placing, the city's own lattice taken from where it stands: a pan of fewer than 24 pixels places nothing again.
527
+ const key = JSON.stringify([lvl, view.width, view.height, round(width), [...tiles].map(([kind, box]) => [kind, ...rel(box)]), buildingsOnScreen.length, buildingsOnScreen.reduce((sum, box) => sum + rel(box)[0] + rel(box)[1] * 3, 0), [...sizes].map(([kind, size]) => [kind, size.width, size.height]), signs.map((one) => [one.key, one.kind, one.building ? rel(one.building) : null]), bounds ? [Math.round((bounds.x - origin.x) / 24), Math.round((bounds.y - origin.y) / 24), Math.round(bounds.width / 24), Math.round(bounds.height / 24)] : null, reserved.map((box) => rel(box).map((n) => Math.round(n / 12)))]);
528
+ let placed;
529
+ let shift = { x: 0, y: 0 };
530
+ if (kept && kept.key === key) {
531
+ placed = kept.placed;
532
+ shift = { x: origin.x - kept.origin.x, y: origin.y - kept.origin.y };
533
+ }
534
+ else {
535
+ placed = placeSigns({
536
+ tiles,
537
+ buildings: buildingsOnScreen,
538
+ names: sizes,
539
+ words: restsOnScreen,
540
+ signs,
541
+ board: { width, height: width / BOARD_ASPECT, post: SIGN_POST * zoomNow * cam.s * k, gap: SIGN_GAP * zoomNow * cam.s * k },
542
+ ...(bounds ? { bounds } : {}),
543
+ // Nor under what stands at the foot: the control, the legend over it and the timeline.
544
+ reserved,
545
+ // Beside the page on a small window a board too small to read, or with no clear place on its district, is not drawn: the bar reaches every view.
546
+ // Beside a page a board with no clear place on its district stands nowhere: the City stands every view, on a small window as high over its district as it takes (the City pans there).
547
+ dropCovered: lvl === PANEL,
548
+ // Beside a page on a desk a view's board may stand higher over its own district before it stands nowhere.
549
+ reach: lvl === CITY ? (cramped() ? 30 : 16) : cramped() ? 12 : 24,
550
+ // A board drawn smaller where it must; beside a page as small as its floor (`boardLeast`) before it stands nowhere.
551
+ shrink: lvl === PANEL || !cramped(),
552
+ ...(lvl === PANEL ? { least: boardLeast(view) } : {}),
553
+ // Beside the page the plaza is behind the city: only the views that stand on a district stand there.
554
+ plaza: lvl !== PANEL,
555
+ });
556
+ kept = { key, origin, placed };
557
+ }
558
+ // Back into the picture's own pixels, as the view lays it out now.
559
+ const back = (p) => ({ x: (p.x + shift.x - cam.x) / cam.s, y: (p.y + shift.y - cam.y) / cam.s });
560
+ const backBox = (box) => ({ ...back(box), width: box.width / cam.s, height: box.height / cam.s });
561
+ const next = new Map([...placed.feet].map(([one, foot]) => [one, back(foot)]));
562
+ // A record's page whose building is not drawn stands where its district is.
563
+ for (const board of state.current.boards) {
564
+ if (next.has(board.key) || board.where.type !== "record")
565
+ continue;
566
+ const at = footOf(board.where, placeIn(board.where, w));
567
+ if (at)
568
+ next.set(board.key, at);
569
+ }
570
+ slots = next;
571
+ posts = new Map([...placed.posts].map(([one, length]) => [one, length / cam.s]));
572
+ scales = new Map(placed.scales);
573
+ names = new Map([...placed.names].map(([kind, box]) => [kind, backBox(box)]));
574
+ whole = placed.whole ? backBox(placed.whole) : null;
575
+ };
576
+ /*
577
+ * WHAT STANDS AT THE FOOT: the control, the legend over it when it is
578
+ * open, and the timeline beside them — each its own rect, read in the
579
+ * frame that uses it. `now` is where they are this frame (a phone's
580
+ * control rides on the sheet at Nearby); `rest` is where they stand at
581
+ * the window's foot, which is where they are at the City.
582
+ */
583
+ const footNow = () => {
584
+ const holder = fab.current;
585
+ if (!holder)
586
+ return { now: [], rest: [], key: "" };
587
+ const bottom = holder.getBoundingClientRect().bottom;
588
+ const height = state.current.view.height;
589
+ const now = [holder.querySelector("[data-graview-one-face-dock]"), holder.querySelector("[data-graview-past-slot] > *")].flatMap((el) => {
590
+ const r = el?.getBoundingClientRect();
591
+ return r && r.width > 0 && r.height > 0 ? [boxOf(r)] : [];
592
+ });
593
+ // At rest the region stands 16 pixels off the window's foot: each rect where it would be there.
594
+ const rest = now.map((box) => ({ ...box, y: height - 16 - (bottom - box.y) }));
595
+ // What changes the fit: the control and the legend over it (the first rect). The timeline's width follows the window's, and a resize refits on its own.
596
+ const dock = rest[0];
597
+ return { now, rest, key: dock ? `${Math.round(dock.x)},${Math.round(dock.y)},${Math.round(dock.width)},${Math.round(dock.height)}` : "" };
598
+ };
599
+ /** The part of the window the City is seen in, at rest there: under the bar, over what stands at the foot. */
600
+ const cityShown = () => {
601
+ const view = state.current.view;
602
+ const top = (view.top ?? 0) + 12;
603
+ const foot = Math.min(view.height, ...footNow().rest.map((box) => box.y)) - 12;
604
+ return { x: 12, y: top, width: view.width - 24, height: Math.max(1, foot - top) };
605
+ };
606
+ /** And the part it is seen in beside the page, at Nearby: left of the panel on a desk, over the sheet on a phone. */
607
+ const nearbyShown = () => {
608
+ const view = state.current.view;
609
+ const panel = panelBox(view);
610
+ const top = (view.top ?? 0) + 8;
611
+ // On a phone the control rides above the sheet at Nearby: the city is seen above both.
612
+ if (view.width <= PHONE)
613
+ return { x: 8, y: top, width: view.width - 16, height: Math.max(1, panel.y - footRoom() - 8 - top) };
614
+ return { x: 8, y: top, width: Math.max(1, panel.x - 16), height: Math.max(1, view.height - 8 - top) };
615
+ };
616
+ /** What everything is placed inside, at a level: at the City on a small window, nothing — the City pans there rather than shrink its words past reading. */
617
+ const shownAt = (lvl) => (lvl === CITY ? (cramped() ? undefined : cityShown()) : lvl === PANEL ? nearbyShown() : undefined);
618
+ /** Placed for where a move ends, through the camera it ends at: the same answer as read again there. */
619
+ const placeFor = (lvl, to, zoomNow) => {
620
+ if (lvl === PAGE)
621
+ return;
622
+ standAll(world, zoomNow, to, shownAt(lvl), lvl);
623
+ };
624
+ /** How much of the window above a phone's sheet the control and the legend keep, at Nearby. */
625
+ const footRoom = () => {
626
+ const dock = fab.current?.querySelector("[data-graview-one-face-dock]")?.getBoundingClientRect();
627
+ return dock ? dock.height + 24 : 72;
628
+ };
629
+ /** The place the page stands on, in the picture. */
630
+ const anchorIn = (at, w) => anchorOf(at, {
631
+ building: at.type === "record" ? (w.buildings.get(at.id) ?? null) : null,
632
+ district: at.type === "record" || at.type === "list" ? (w.plots.get(at.kind) ?? null) : null,
633
+ districts: at.type === "place" ? at.kinds.flatMap((kind) => (w.plots.has(kind) ? [w.plots.get(kind)] : [])) : [],
634
+ all: [...w.plots.values()],
635
+ });
636
+ /** The camera a level asks for, against the view's own: how much closer than now, and the place where that level aims it. */
637
+ const cameraFor = (lvl, anchor, zoomNow) => {
638
+ const now = state.current;
639
+ const zooms = zoomsOf(now.where, now.view);
640
+ const s = zooms[lvl] / zoomNow;
641
+ if (lvl === CITY) {
642
+ /*
643
+ * THE CITY AT ITS OWN FIT (zoom 1: the Scene's, with room kept under
644
+ * each name for its views and at the top for the plaza), its middle
645
+ * and the plaza above it in the middle of what the bar and the
646
+ * control at the foot leave. Worked out once, before the move, and
647
+ * held: the camera goes straight there and stops.
648
+ */
649
+ if (whole) {
650
+ /*
651
+ * The names are a size on the screen: at the City they take less of
652
+ * the picture than where it was read. Placed again for the City's
653
+ * own scale, and fitted again, until the two agree.
654
+ */
655
+ let fitted = fitBeside(whole, now.view, footNow().rest, 20, s);
656
+ for (let again = 0; again < 2; again++) {
657
+ standAll(world, zoomNow, fitted, undefined, CITY);
658
+ if (!whole)
659
+ break;
660
+ fitted = fitBeside(whole, now.view, footNow().rest, 20, s);
661
+ }
662
+ if (!whole)
663
+ return fitted;
664
+ // Never past what the camera can be written at: a fit below its reach was painted, then put back up when written — an overshoot.
665
+ const least = (camera.current?.range.min ?? 0) / zoomNow;
666
+ if (fitted.s >= least)
667
+ return fitted;
668
+ /*
669
+ * AS FAR OUT AS THE CAMERA GOES, AND EVERYTHING INSIDE WHAT IT SHOWS
670
+ * from there: the city in the middle of the window's part for it,
671
+ * and the names and views placed again inside that part — closer
672
+ * together, rather than past the window's edge (a phone's City).
673
+ */
674
+ const city = unionOf([...world.plots.values()]) ?? whole;
675
+ const area = cityShown();
676
+ const at = cameraPutting(middle(city), middle(area), least);
677
+ standAll(world, zoomNow, at, shownAt(CITY), CITY);
678
+ return at;
679
+ }
680
+ }
681
+ /*
682
+ * THE HOME BESIDE THE CITY: the city framed in the part the page leaves
683
+ * it — left of the panel on a desk, over the sheet on a phone — never
684
+ * the whole window, where the districts nearest the page stood behind it.
685
+ */
686
+ if (lvl === PANEL && (now.where.type === "home" || now.where.type === "overview")) {
687
+ const city = unionOf([...world.plots.values()]);
688
+ if (city) {
689
+ const shown = nearbyShown();
690
+ const fit = fitCamera(city, shown, 16, s);
691
+ // Never past what the camera can be written at: there the whole city does not fit beside the page, and the districts the home's views show are what stands in the city's side.
692
+ const least = (camera.current?.range.min ?? 0) / zoomNow;
693
+ // Said on the city, for a reader of the page: whether the whole city stands beside the page here.
694
+ box.current?.toggleAttribute("data-graview-nearby-holds-the-city", fit.s >= least);
695
+ if (fit.s >= least)
696
+ return fit;
697
+ const kinds = new Set(markedOnPage().flatMap((id) => {
698
+ const kind = store.graph.getNode(id)?.kind;
699
+ return kind ? [kind] : [];
700
+ }));
701
+ const own = unionOf([...kinds].flatMap((kind) => {
702
+ const plot = world.plots.get(kind);
703
+ return plot ? [plot] : [];
704
+ }));
705
+ const what = own ?? city;
706
+ const close = fitCamera(what, shown, 16, s);
707
+ if (close.s >= least)
708
+ return close;
709
+ // As far out as the camera goes: the city as near the middle of its side as its views' districts let it, they in the side whole.
710
+ const held = cameraPutting(middle(city), middle(shown), least);
711
+ const lo = project(held, { x: what.x, y: what.y });
712
+ const hi = project(held, { x: what.x + what.width, y: what.y + what.height });
713
+ const shift = (low, high, from, to) => (low < from ? from - low : high > to ? to - high : 0);
714
+ return { s: held.s, x: held.x + shift(lo.x, hi.x, shown.x, shown.x + shown.width), y: held.y + shift(lo.y, hi.y, shown.y, shown.y + shown.height) };
715
+ }
716
+ }
717
+ if (lvl === CITY || now.where.type === "home" || now.where.type === "overview") {
718
+ const city = unionOf([...world.plots.values()]);
719
+ const middleOfCity = city ? middle(city) : anchor;
720
+ return cameraPutting(middleOfCity, aimAt(CITY, now.view), s);
721
+ }
722
+ if (lvl === PANEL && now.where.type === "record") {
723
+ const framed = nearbyFraming(now.where.id, anchor, s, zoomNow);
724
+ nearbyZoom = zoomNow * framed.s;
725
+ return framed;
726
+ }
727
+ if (lvl === PANEL)
728
+ nearbyZoom = null;
729
+ return cameraPutting(anchor, aimAt(lvl === PAGE ? PANEL : lvl, now.view, footRoom()), s);
730
+ };
731
+ /** Start a move of the camera to a level. */
732
+ /*
733
+ * THE CAMERA WRITTEN, THE PICTURE'S PIXELS ARE NEW: the Scene lays the
734
+ * city out again at the zoom written, and what stood where the move
735
+ * ended — through the camera it ended at — stands there in the new
736
+ * pixels: carried over at once, so the frame after the move draws every
737
+ * board and name where the frame before did.
738
+ */
739
+ const rebase = (to) => {
740
+ const at = (p) => project(to, p);
741
+ const box = (b) => ({ ...at(b), width: b.width * to.s, height: b.height * to.s });
742
+ slots = new Map([...slots].map(([key, p]) => [key, at(p)]));
743
+ posts = new Map([...posts].map(([key, length]) => [key, length * to.s]));
744
+ names = new Map([...names].map(([key, b]) => [key, box(b)]));
745
+ whole = whole ? box(whole) : null;
746
+ settle = null;
747
+ world = { plots: new Map([...world.plots].map(([key, b]) => [key, box(b)])), buildings: new Map([...world.buildings].map(([key, b]) => [key, box(b)])) };
748
+ };
749
+ const moveTo = (cam, toLevel, anchor, now, arm = true) => {
750
+ reframeArmed = arm && toLevel === PANEL && state.current.where.type === "record";
751
+ const from = painted;
752
+ /*
753
+ * A PAGE CHOSEN AT REST LEAVES FROM WHERE ITS BOARD STANDS THERE. A
754
+ * building pressed at the City makes its record the page, and that page's
755
+ * board was placed nowhere yet: it would have started from where it will
756
+ * stand beside the page, forty pixels from the building pressed. Placed
757
+ * first where the camera rests, so the move carries it from there.
758
+ */
759
+ // Where everything is drawn now is where it moves from: the page's own board, placed only now, joins them from where it stands at rest.
760
+ const namesFrom = drawnNames(now);
761
+ const slotsFrom = drawnSlots(now);
762
+ const own = state.current.current;
763
+ if (rest !== PAGE && !move && !slots.has(own) && level(painted)) {
764
+ standAll(world, cam.zoom(), LEVEL_LIVE, shownAt(rest), rest);
765
+ const at = slots.get(own);
766
+ if (at)
767
+ slotsFrom.set(own, at);
768
+ }
769
+ settle = null;
770
+ const to = cameraFor(toLevel, anchor, cam.zoom());
771
+ placeFor(toLevel, to, cam.zoom());
772
+ const fromAltitude = altitude;
773
+ if (state.current.lessMotion) {
774
+ cam.liveTo(to);
775
+ painted = to;
776
+ altitude = toLevel;
777
+ rest = toLevel;
778
+ cityFoot = "";
779
+ cam.commit();
780
+ rebaseNext = to;
781
+ committedAt = performance.now();
782
+ ownZoom = cam.zoom();
783
+ return;
784
+ }
785
+ move = {
786
+ from,
787
+ to,
788
+ fromAltitude,
789
+ toAltitude: toLevel,
790
+ about: anchor,
791
+ start: now,
792
+ foot: footNow().key,
793
+ names: namesFrom,
794
+ slots: slotsFrom,
795
+ done: () => {
796
+ rest = toLevel;
797
+ altitude = toLevel;
798
+ cityFoot = "";
799
+ cam.commit();
800
+ rebaseNext = to;
801
+ committedAt = performance.now();
802
+ ownZoom = null;
803
+ },
804
+ };
805
+ };
806
+ const tick = (now) => {
807
+ frame = requestAnimationFrame(tick);
808
+ /*
809
+ * A STALL IS NOT A LEAP. A move is clocked by the wall, and a frame the
810
+ * page could not draw (a chosen page's route drawn in one long task)
811
+ * left the next to catch the whole stall up at once: the page leapt a
812
+ * third of its way. A gap past 34 ms is taken out of the move's clock,
813
+ * so the move slows through it rather than jumps.
814
+ */
815
+ if (lastTick > 0 && now - lastTick > 34) {
816
+ // And a settle after a re-read the same way: on a slow machine it goes over as many frames as on a quick one.
817
+ const lost = now - lastTick - 1000 / 60;
818
+ if (move)
819
+ move.start += lost;
820
+ if (settle)
821
+ settle.start += lost;
822
+ }
823
+ calmRun = lastTick > 0 && now - lastTick < 50 ? calmRun + 1 : 0;
824
+ lastTick = now;
825
+ if (rebaseNext) {
826
+ rebase(rebaseNext);
827
+ rebaseNext = null;
828
+ }
829
+ const holder = box.current;
830
+ const plane = sheet.current;
831
+ const cam = camera.current;
832
+ if (!holder || !plane || !cam)
833
+ return;
834
+ const at = state.current;
835
+ const asking = motion.current;
836
+ /*
837
+ * WHILE THE PAGE IS READ the Scene is not drawn at all: no room, no
838
+ * scroll, no pointer, no keyboard. Asked out, it is drawn a frame
839
+ * before anything moves, laid out, and the camera aimed out of sight
840
+ * where stepping out begins; only then does the page move.
841
+ */
842
+ const drawn = at.out || rest !== PAGE || move !== null || asking.scrubbing || altitude > 0.001;
843
+ if (!drawn) {
844
+ if (holder.style.display !== "none") {
845
+ holder.style.display = "none";
846
+ holder.style.visibility = "hidden";
847
+ plane.style.transform = "";
848
+ plane.style.opacity = "";
849
+ plane.style.borderRadius = "";
850
+ // A full page's room above and below again: the shell's own (the gutter the frame sets itself).
851
+ plane.style.removeProperty("--graview-frame-top");
852
+ plane.style.removeProperty("--graview-frame-foot");
853
+ draw([], 0);
854
+ }
855
+ return;
856
+ }
857
+ if (holder.style.display === "none") {
858
+ holder.style.display = "block";
859
+ return;
860
+ }
861
+ /* THE CAMERA AS IT IS: a move between stops painted now, or the view's own. */
862
+ const live = cam.live();
863
+ const between = !level(live);
864
+ // Settling: a move written into the view not yet laid out, or written too recently for a new one to be painted safely.
865
+ const settling = cam.moving() && !between;
866
+ // Written too recently for a new move to be painted safely: the Scene settles what is painted a beat after a commit.
867
+ const calm = now - committedAt >= QUIET_MS;
868
+ if (between)
869
+ painted = live;
870
+ else if (!settling) {
871
+ painted = LEVEL_LIVE;
872
+ /*
873
+ * THE CITY READ AGAIN ONLY WHEN IT MAY HAVE MOVED: a camera written,
874
+ * the window, the views or what stands at the foot changed, or a while gone by (a record added
875
+ * lays its district out again) — never every frame of a page at rest.
876
+ */
877
+ const readKey = `${at.view.width}x${at.view.height}|${at.boards.length}|${at.current}|${zoomNowOf(cam)}|${holder.querySelector(".graview-plots > g")?.getAttribute("transform") ?? ""}|${footNow().key}`;
878
+ // An empty city is read again every frame until it has laid out; a pan moves the city's own transform, which is in the key.
879
+ /*
880
+ * NEVER PLACED AGAIN FOR WHERE A MOVE LEFT. The first frame of a move
881
+ * paints the camera it starts from, which is a level's: read again
882
+ * there (a slow machine's frame long past the half-second), the
883
+ * level being left was placed again over the one headed for, without
884
+ * the part beside the page it is placed inside — and the names, slid
885
+ * along to stay in it, leapt sideways. Under way, the city is placed
886
+ * only for where the move ends.
887
+ */
888
+ if (world.plots.size === 0 || (!move && (readKey !== lastReadKey || committedAt > lastRead || now - lastRead > 500))) {
889
+ world = readWorld(holder);
890
+ if (move)
891
+ placeFor(move.toAltitude, move.to, cam.zoom());
892
+ else {
893
+ const drawnBefore = { slots: drawnSlots(now), names: drawnNames(now) };
894
+ // At the City everything stands inside what the camera shows; the camera was fitted to it.
895
+ standAll(world, cam.zoom(), LEVEL_LIVE, shownAt(rest), rest);
896
+ settle = { ...drawnBefore, start: now };
897
+ }
898
+ lastRead = now;
899
+ lastReadKey = readKey;
900
+ }
901
+ }
902
+ /*
903
+ * A NEW PAGE'S BOARD IS PLACED THE FRAME IT IS DRAWN. Chosen by Find, a
904
+ * link or a building, the page is its own board until it moves — and
905
+ * with no place yet it stood a frame on its district's middle, then on
906
+ * its board: a jump before the move began. Placed at once, where the
907
+ * camera rests.
908
+ */
909
+ if (world.plots.size > 0 && rest !== PAGE && !move && level(painted) && !slots.has(at.current) && placedFor !== at.current) {
910
+ placedFor = at.current;
911
+ standAll(world, cam.zoom(), LEVEL_LIVE, shownAt(rest), rest);
912
+ }
913
+ const anchor = world.plots.size > 0 ? anchorIn(at.where, world) : null;
914
+ const zoomNow = cam.zoom();
915
+ const placeKey = `${keyOf(at.where)}|${at.view.width}x${at.view.height}`;
916
+ /* At the page, the camera waits where stepping out begins: aimed for each place before anything moves. */
917
+ let ready = rest !== PAGE || move !== null || asking.scrubbing;
918
+ if (!ready && anchor && !settling) {
919
+ // Painted, not written into the view: the move starts from it in this same frame, and only its end is written.
920
+ const want = cameraFor(PAGE, anchor, zoomNow);
921
+ if (Math.hypot(want.x - painted.x, want.y - painted.y) + Math.abs(want.s - painted.s) * 100 > 0.5) {
922
+ cam.liveTo(want);
923
+ painted = want;
924
+ }
925
+ ready = true;
926
+ }
927
+ /* WHAT IS ASKED: a level (Up, Down, a press), or a scrub of the altitude by a hand on the page. */
928
+ if (ready && calm && anchor && asking.level !== null && !move && !settling && (placeKey === lastPlace || drawnSince(placeKey, now))) {
929
+ const toLevel = asking.level;
930
+ asking.level = null;
931
+ // The level it rests at, asked again: back to that level's framing, if a hand has moved the camera from it.
932
+ const offFraming = () => {
933
+ const want = cameraFor(rest, anchor, zoomNow);
934
+ return Math.hypot(want.x - painted.x, want.y - painted.y) + Math.abs(want.s - painted.s) * 100 > 2;
935
+ };
936
+ if (toLevel !== rest || placeKey !== lastPlace || (rest !== PAGE && offFraming()))
937
+ moveTo(cam, toLevel, anchor, now);
938
+ lastPlace = placeKey;
939
+ }
940
+ if (ready && calm && anchor && asking.scrub !== 0 && !settling) {
941
+ // As far as the hand has gone, but no more than a step a frame: a hand ahead of the camera is caught up with, never jumped to.
942
+ const delta = Math.max(-0.12, Math.min(0.12, asking.scrub));
943
+ asking.scrub -= delta;
944
+ move = null;
945
+ const target = Math.max(PAGE, Math.min(CITY, altitude + delta));
946
+ // The cameras of the levels either side, the one rested at as it is (a pan the person made is kept).
947
+ const lower = Math.floor(target);
948
+ const upper = Math.min(CITY, lower + 1);
949
+ // The level rested at is the camera as the person left it there — a pinch or a pan over the city beside the page kept — so the scrub leaves from where it is, never from a jump back to the framing.
950
+ const camOf = (lvl) => (lvl === rest && rest !== PAGE ? (restCam ??= painted) : cameraFor(lvl, anchor, zoomNow));
951
+ const placed = cameraBetween(camOf(lower), camOf(upper), anchor, target - lower);
952
+ cam.liveTo(placed);
953
+ painted = placed;
954
+ altitude = target;
955
+ handSince = now;
956
+ asking.scrubbing = true;
957
+ }
958
+ if (asking.scrubbing && !asking.held && now - handSince > LET_GO_MS && !settling && calm) {
959
+ asking.scrubbing = false;
960
+ restCam = null;
961
+ if (anchor)
962
+ moveTo(cam, restingLevel(altitude), anchor, now, false);
963
+ }
964
+ /* A VIEW'S RECORDS SAID AFTER NEARBY WAS FRAMED: framed again, the one camera moving there from where it is. */
965
+ if (reframeAsked && !move && !asking.scrubbing && !settling && calm && anchor) {
966
+ reframeAsked = false;
967
+ if (rest === PANEL && at.where.type === "record" && framingKeyNow(at.where.id) !== framedKey) {
968
+ reframeArmed = false;
969
+ moveTo(cam, PANEL, anchor, now, false);
970
+ }
971
+ }
972
+ /* A MOVE UNDER WAY: the camera set from how far it has got. */
973
+ if (move) {
974
+ /*
975
+ * WHAT STANDS AT THE FOOT CHANGED UNDER WAY — the legend opens as the
976
+ * level it is shown at is headed for — and the camera's end is
977
+ * worked out again from it, in this frame: the fit and the aim read
978
+ * the rects as they are, never as they were when the move began.
979
+ */
980
+ const foot = footNow().key;
981
+ if (foot !== move.foot) {
982
+ // From where the camera is painted now, toward the new end: never a jump in the middle of the move.
983
+ move.foot = foot;
984
+ move.from = painted;
985
+ move.fromAltitude = altitude;
986
+ move.start = now;
987
+ move.names = drawnNames(now);
988
+ move.slots = drawnSlots(now);
989
+ move.to = cameraFor(move.toAltitude, move.about, cam.zoom());
990
+ placeFor(move.toAltitude, move.to, cam.zoom());
991
+ }
992
+ const t = clamp01((now - move.start) / MOVE_MS);
993
+ const e = ease(t);
994
+ const placed = cameraBetween(move.from, move.to, move.about, e);
995
+ cam.liveTo(placed);
996
+ painted = placed;
997
+ altitude = move.fromAltitude + (move.toAltitude - move.fromAltitude) * e;
998
+ if (t >= 1) {
999
+ const finished = move;
1000
+ move = null;
1001
+ finished.done();
1002
+ }
1003
+ }
1004
+ else if (!asking.scrubbing && !settling) {
1005
+ /*
1006
+ * AT REST, OR THE PERSON'S OWN HAND ON THE SCENE: a pinch or a
1007
+ * ctrl+wheel over the city zooms its camera and moves no level — at
1008
+ * Nearby and the City alike. A level is the page's to step.
1009
+ */
1010
+ const effective = zoomNow * painted.s;
1011
+ // The zoom a move just written into the view settles a beat later: it is the face's own once it is still.
1012
+ if (ownZoom === null && calm)
1013
+ ownZoom = effective;
1014
+ /*
1015
+ * AT THE CITY THE CAMERA IS FREE. A pinch or a wheel there moves
1016
+ * closer to whatever is under the hand and back out — the views
1017
+ * standing there seen nearer — and never goes down into one: that is
1018
+ * choosing a view, or the control. The Scene keeps the camera within
1019
+ * its reach, so the city is never lost.
1020
+ */
1021
+ if (rest === CITY && !move) {
1022
+ altitude = CITY;
1023
+ /*
1024
+ * The legend opened or put away at the City: the city is fitted
1025
+ * again beside it — unless the person has moved the camera there
1026
+ * since it came to the City, which is theirs to keep.
1027
+ */
1028
+ const foot = footNow().key;
1029
+ const where = `${zoomNowOf(cam)}|${holder.querySelector(".graview-plots > g")?.getAttribute("transform") ?? ""}`;
1030
+ if (cityFoot === "") {
1031
+ // Recorded once the camera written at the City has settled.
1032
+ if (calm) {
1033
+ cityFoot = foot;
1034
+ cityAt = where;
1035
+ }
1036
+ }
1037
+ else if (foot !== cityFoot && calm && anchor) {
1038
+ cityFoot = foot;
1039
+ if (where === cityAt)
1040
+ moveTo(cam, CITY, anchor, now);
1041
+ }
1042
+ if (ownZoom !== null && Math.abs(Math.log(effective / ownZoom)) > 0.001)
1043
+ ownZoom = effective;
1044
+ }
1045
+ else if (rest === PANEL) {
1046
+ /*
1047
+ * AT NEARBY THE CAMERA IS FREE TOO (Graview HQ
1048
+ * feature:f-one-face-explore-city-beside-panel). A pinch or a
1049
+ * ctrl+wheel over the city beside the page moves closer to what is
1050
+ * under the hand and back out, a drag or a scroll pans it, and the
1051
+ * page and the level stay where they are: a level is stepped by a
1052
+ * pinch over the page, the control or the keys — and the control
1053
+ * and the keys at Nearby bring the camera back to its framing.
1054
+ */
1055
+ if (ownZoom !== null && Math.abs(Math.log(effective / ownZoom)) > 0.001)
1056
+ ownZoom = effective;
1057
+ if (placeKey === lastPlace)
1058
+ altitude = PANEL;
1059
+ }
1060
+ else if (!(rest !== PAGE && placeKey !== lastPlace))
1061
+ altitude = rest;
1062
+ }
1063
+ /*
1064
+ * THE PANEL SIZED BY HAND: the city beside it follows on the one
1065
+ * camera in the same frame — painted while the edge moves, written
1066
+ * into the view once when it is let go, never fitted again after.
1067
+ */
1068
+ const panelNow = `${panelBox(at.view).x},${panelBox(at.view).y},${panelBox(at.view).width}`;
1069
+ if (rest === PANEL && !move && !asking.scrubbing && anchor && !settling && panelNow !== lastPanel) {
1070
+ if (lastPanel !== "") {
1071
+ const want = cameraFor(PANEL, anchor, zoomNow);
1072
+ cam.liveTo(want);
1073
+ painted = want;
1074
+ resized = true;
1075
+ }
1076
+ lastPanel = panelNow;
1077
+ }
1078
+ else if (rest !== PANEL)
1079
+ lastPanel = "";
1080
+ if (resized && !asking.resizing && !move && rest === PANEL) {
1081
+ resized = false;
1082
+ cam.commit();
1083
+ committedAt = performance.now();
1084
+ ownZoom = null;
1085
+ }
1086
+ // Moving to another place at a level above the page: the camera flies there and stays at that level.
1087
+ if (rest !== PAGE && !move && !asking.scrubbing && anchor && placeKey !== lastPlace && !settling && calm && holder.querySelector("[data-graview-settled]") && drawnSince(placeKey, now)) {
1088
+ lastPlace = placeKey;
1089
+ /*
1090
+ * A RECORD OPENED BESIDE THE CITY WHERE IT IS SEEN IS LIT WHERE IT
1091
+ * STANDS. Pressed at Nearby (or opened by a link or a pill) with its
1092
+ * building in the part of the city the page leaves, the camera does
1093
+ * not move: every building keeps its box to the pixel, and only the
1094
+ * boards and names go over a moment to where they stand for it.
1095
+ */
1096
+ const building = at.where.type === "record" ? world.buildings.get(at.where.id) : undefined;
1097
+ if (rest === PANEL && building && level(painted) && inside(project(painted, { x: building.x + building.width / 2, y: building.y + building.height / 2 }), nearbyShown())) {
1098
+ settle = { slots: drawnSlots(now), names: drawnNames(now), start: now };
1099
+ standAll(world, zoomNow, LEVEL_LIVE, shownAt(rest), rest);
1100
+ }
1101
+ else {
1102
+ // Chosen at the city — a Find result, a link — a page lands beside the city, as a view chosen there does.
1103
+ moveTo(cam, rest === CITY ? PANEL : rest, anchor, now);
1104
+ }
1105
+ }
1106
+ /* THE PAGE: its box from the altitude, then — past the panel — moved and scaled into its board, as the camera has it this frame. */
1107
+ const a = altitude;
1108
+ // Out, or asked out and waiting for the camera to be aimed: the page is not back in until nothing asks it out.
1109
+ const away = a > 0.001 || move !== null || asking.scrubbing || (asking.level !== null && asking.level > PAGE) || asking.scrub !== 0;
1110
+ holder.style.visibility = away ? "visible" : "hidden";
1111
+ // Drawn but not yet moved out, or on its way between levels, the city takes no pointer and no keyboard, as the boards do not: a press lands where the move ends.
1112
+ const reachable = away && !move && !(rest === PAGE && !asking.scrubbing);
1113
+ if (holder.inert === reachable)
1114
+ holder.inert = !reachable;
1115
+ holder.style.opacity = String(a >= PANEL ? 1 : ease(clamp01(a * 1.3)));
1116
+ const phone = at.view.width <= PHONE;
1117
+ const panel = panelBox(at.view);
1118
+ const pageAt = pageBox(a, at.view);
1119
+ /* The boards' size this frame: their width in the picture, carried through the camera painted now. */
1120
+ // A size on the screen at most, as `standAll` placed them: at Nearby the camera is close.
1121
+ const boardWidth = Math.max(Math.min(boardWidthFor(at.view) * zoomNow * painted.s, boardMost(at.view, at.where, zoomNow * painted.s)), boardLeast(at.view));
1122
+ // The view the page goes into is as wide as it will be where the move ends: held, so the page shrinks one way only.
1123
+ const intoWidth = move && move.toAltitude >= CITY ? Math.max(Math.min(boardWidthFor(at.view) * zoomNow * move.to.s, boardMost(at.view, at.where, zoomNow * move.to.s)), boardLeast(at.view)) : boardWidth;
1124
+ // Across a move, each board goes from where it stood to where it will stand, as the names do: the page's own with them, so a page chosen at the City leaves from the board pressed.
1125
+ const gone = goneAt(now);
1126
+ const carried = gone.t;
1127
+ const slotNow = (key) => blend(gone.slots?.get(key), slots.get(key), carried);
1128
+ const ownSlot = slotNow(at.current);
1129
+ const ownFoot = ownSlot ? project(painted, ownSlot) : anchor ? project(painted, anchor) : { x: at.view.width / 2, y: at.view.height / 2 };
1130
+ const standing = away && a > PANEL ? standingAt(a, panel, ownFoot, intoWidth) : null;
1131
+ if (away) {
1132
+ plane.style.left = `${pageAt.x.toFixed(2)}px`;
1133
+ plane.style.top = `${pageAt.y.toFixed(2)}px`;
1134
+ plane.style.width = `${pageAt.width.toFixed(2)}px`;
1135
+ plane.style.height = `${(standing ? standing.height : pageAt.height).toFixed(2)}px`;
1136
+ plane.style.transformOrigin = SHEET_ORIGIN;
1137
+ plane.style.transform = standing ? standingCss(standing) : "none";
1138
+ plane.style.opacity = "1";
1139
+ // As a board its corners are a board's, the camera's scale undone.
1140
+ plane.style.borderRadius = `${(a >= PANEL ? 10 / (standing?.scale ?? 1) : 10 * a).toFixed(2)}px`;
1141
+ if (standing && plane.scrollTop !== 0 && a > 1.9)
1142
+ plane.scrollTop = 0;
1143
+ // Its gutter and its room above and below, a full page's at the page and a panel's beside the city, every step between drawn (written only when a pixel changes).
1144
+ for (const [name, value] of Object.entries(frameSpacingAt(a)))
1145
+ if (plane.style.getPropertyValue(name) !== value)
1146
+ plane.style.setProperty(name, value);
1147
+ }
1148
+ else {
1149
+ plane.style.transform = "";
1150
+ plane.style.opacity = "";
1151
+ plane.style.borderRadius = "";
1152
+ }
1153
+ /* The Connected list grows into the page as it becomes the panel, and folds away as the page becomes its board. */
1154
+ const grown = a <= PANEL ? clamp01((a - 0.55) / 0.45) : 1 - clamp01((a - PANEL) / 0.4);
1155
+ const list = connected.current;
1156
+ if (list) {
1157
+ const inner = list.firstElementChild;
1158
+ list.style.height = `${((inner?.scrollHeight ?? 0) * ease(grown)).toFixed(2)}px`;
1159
+ list.style.opacity = String(ease(grown));
1160
+ }
1161
+ /*
1162
+ * THE BOARDS: each on its slot, through this frame's camera, at the
1163
+ * page's own size scaled — a size on the screen at most. The views
1164
+ * stand at Nearby as at the City; the page's own board is the page
1165
+ * itself, and is drawn only once the page has become it.
1166
+ */
1167
+ const shownBoards = a >= PANEL ? 1 : ease(clamp01((a - 0.6) / 0.4));
1168
+ const shownOwn = ease(clamp01((a - 1.25) / 0.6));
1169
+ const layer = boardLayer.current;
1170
+ if (layer) {
1171
+ const reachable = a > 0.95 && !move;
1172
+ if (layer.inert === reachable)
1173
+ layer.inert = !reachable;
1174
+ layer.setAttribute("data-graview-boards-shown", shownBoards.toFixed(3));
1175
+ }
1176
+ /*
1177
+ * NOTHING OF THE CITY'S UNDER THE BAR, THE PAGE OR THE LEVEL CONTROL —
1178
+ * after a hand's pan too, which carries what was placed with the city
1179
+ * until it is placed again: a board or a district's name that a pan
1180
+ * has brought under one of them is put away (unseen, unreached) until
1181
+ * it comes out again.
1182
+ */
1183
+ const dockRect = fab.current?.querySelector("[data-graview-one-face-dock]")?.getBoundingClientRect();
1184
+ const keepOut = move
1185
+ ? []
1186
+ : [
1187
+ { x: 0, y: 0, width: at.view.width, height: at.view.top ?? 0 },
1188
+ ...(a > 0.5 && a < 1.5 ? [boxOf(plane.getBoundingClientRect())] : []),
1189
+ ...(dockRect && dockRect.width > 0 ? [boxOf(dockRect)] : []),
1190
+ ];
1191
+ const under = (b) => keepOut.some((k) => Math.min(b.x + b.width, k.x + k.width) - Math.max(b.x, k.x) > 1 && Math.min(b.y + b.height, k.y + k.height) - Math.max(b.y, k.y) > 1);
1192
+ // Every board and name a press reaches this frame: a line's ring under or against one of them is not offered.
1193
+ pressable.length = 0;
1194
+ for (const [key, button] of boardEls.current ?? []) {
1195
+ /*
1196
+ * A board that had no place where the move began (its district out
1197
+ * of sight) comes up where it will stand as the move goes on, and one
1198
+ * that will have none goes down where it stood: never one frame from
1199
+ * nowhere to full.
1200
+ */
1201
+ const goal = slots.get(key);
1202
+ const was = gone.slots?.get(key);
1203
+ const arriving = gone.slots !== null && goal !== undefined && was === undefined ? carried : 1;
1204
+ const leaving = gone.slots !== null && goal === undefined && was !== undefined ? 1 - carried : 1;
1205
+ const slot = goal ? slotNow(key) : gone.slots && was ? was : undefined;
1206
+ const w = boardWidth * (scales.get(key) ?? 1);
1207
+ const h = w / BOARD_ASPECT;
1208
+ const footAt = slot ? project(painted, slot) : null;
1209
+ const covered = key !== at.current && footAt !== null && under({ x: footAt.x - w / 2, y: footAt.y - h, width: w, height: h });
1210
+ // Put away under the bar or the control by a pan (a phone's City is panned to reach it): said, for whoever reads the page.
1211
+ button.toggleAttribute("data-graview-under", covered);
1212
+ const seen = slot && !covered ? (key === at.current ? shownOwn : shownBoards) * arriving * leaving : 0;
1213
+ // Not seen, it is not reached: out of the keyboard's order and the pointer's, as a board going away is.
1214
+ const reached = seen > 0.5 && (key === at.current ? a > 1.5 : a > 0.95) && !move;
1215
+ if (button.inert === reached)
1216
+ button.inert = !reached;
1217
+ if (!slot || seen <= 0.001) {
1218
+ if (button.style.opacity !== "0")
1219
+ button.style.opacity = "0";
1220
+ button.style.pointerEvents = "none";
1221
+ continue;
1222
+ }
1223
+ const foot = footAt ?? project(painted, slot);
1224
+ button.style.left = `${(foot.x - w / 2).toFixed(2)}px`;
1225
+ button.style.top = `${(foot.y - h).toFixed(2)}px`;
1226
+ button.style.width = `${w.toFixed(2)}px`;
1227
+ button.style.height = `${h.toFixed(2)}px`;
1228
+ button.style.opacity = key === at.current ? (shownOwn > 0.001 ? "1" : "0") : seen.toFixed(3);
1229
+ button.style.setProperty("--graview-sign-post", `${((posts.get(key) ?? SIGN_POST * zoomNow) * painted.s).toFixed(2)}px`);
1230
+ button.style.pointerEvents = reached ? "auto" : "none";
1231
+ if (reached)
1232
+ pressable.push({ x: foot.x - w / 2, y: foot.y - h, width: w, height: h });
1233
+ const face = button.firstElementChild;
1234
+ if (face)
1235
+ face.style.transform = `scale(${(w / boardFaceWidth(at.view)).toFixed(5)})`;
1236
+ }
1237
+ /*
1238
+ * THE DISTRICTS' NAMES come up with the views, and the Scene's own
1239
+ * names stand down as they do: one name a district at every altitude.
1240
+ * Each is placed through this frame's camera, a size on the screen.
1241
+ */
1242
+ const shownNames = a >= PANEL ? 1 : ease(clamp01((a - 0.6) / 0.4));
1243
+ holder.style.setProperty("--graview-one-face-plates", (1 - shownNames).toFixed(3));
1244
+ holder.toggleAttribute("data-graview-names-said", shownNames > 0.999);
1245
+ // Across a move, each name goes from where it stood to where it will stand, as far as the camera has gone: never a jump; one with nowhere to come from comes up where it stands, one going away goes down where it stood.
1246
+ const along = gone.t;
1247
+ for (const [kind, el] of tagEls.current) {
1248
+ const goal = names.get(kind);
1249
+ const from = gone.names?.get(kind);
1250
+ const was = from ?? goal;
1251
+ const fade = gone.names && goal && !from ? along : gone.names && !goal && from ? 1 - along : 1;
1252
+ const place = goal ?? (gone.names ? from : undefined);
1253
+ const seen = place ? shownNames * fade : 0;
1254
+ const reached = seen > 0.95 && !move && a > 0.95;
1255
+ if (el.inert === reached)
1256
+ el.inert = !reached;
1257
+ if (!place || !was || seen <= 0.001) {
1258
+ if (el.style.visibility !== "hidden")
1259
+ el.style.visibility = "hidden";
1260
+ continue;
1261
+ }
1262
+ el.style.visibility = "visible";
1263
+ el.style.opacity = seen.toFixed(3);
1264
+ /*
1265
+ * The name's middle goes from where it was to where it will be. Its box
1266
+ * at the end of a move is the size it is in the picture there — a size
1267
+ * on the screen taken through a camera much further out is many times
1268
+ * wider in the picture — so the middle is carried, never the left edge
1269
+ * with the width at the end: that put a name a hundred pixels off in
1270
+ * the first frame of a move to the City.
1271
+ */
1272
+ const top = project(painted, { x: was.x + was.width / 2 + (place.x + place.width / 2 - (was.x + was.width / 2)) * along, y: was.y + (place.y - was.y) * along });
1273
+ el.style.left = `${(top.x - el.offsetWidth / 2).toFixed(2)}px`;
1274
+ el.style.top = `${top.y.toFixed(2)}px`;
1275
+ const covered = under({ x: top.x - el.offsetWidth / 2, y: top.y, width: el.offsetWidth, height: el.offsetHeight });
1276
+ el.toggleAttribute("data-graview-under", covered);
1277
+ if (!covered && reached)
1278
+ pressable.push({ x: top.x - el.offsetWidth / 2, y: top.y, width: el.offsetWidth, height: el.offsetHeight });
1279
+ if (covered) {
1280
+ el.style.visibility = "hidden";
1281
+ el.inert = true;
1282
+ }
1283
+ }
1284
+ /* The button that moves a level: on a phone it rides above the sheet while the page is one, and comes back to the foot at the city. */
1285
+ const button = fab.current;
1286
+ if (button) {
1287
+ const lift = phone ? (a <= PANEL ? ease(a) : 1 - ease(clamp01(a - PANEL))) : 0;
1288
+ button.style.bottom = `${(16 + lift * Math.max(0, at.view.height - panel.y + 10 - 16)).toFixed(2)}px`;
1289
+ }
1290
+ /* THE LINES, from this frame's numbers. */
1291
+ const shown = a <= PANEL ? ease(clamp01((a - 0.6) / 0.4)) : 1 - ease(clamp01((a - PANEL) / 0.45));
1292
+ const found = [];
1293
+ if (shown > 0.01) {
1294
+ const pageRect = boxOf(plane.getBoundingClientRect());
1295
+ const controlRect = fab.current?.querySelector('[data-graview-one-face-dock]')?.getBoundingClientRect();
1296
+ const area = cityArea(pageRect, at.view, controlRect ? boxOf(controlRect) : null, phone);
1297
+ // Everything that stands at the foot this frame, each its own rect: no line runs under any of them.
1298
+ const reserved = footNow().now;
1299
+ const subjectId = at.where.type === "record" ? at.where.id : null;
1300
+ const taken = new Set();
1301
+ // A district the Scene draws in this frame: one it has let go of off the window has no end to give.
1302
+ const drawnPlot = (kind) => holder.querySelector(`[data-graview-plot="kind:${CSS.escape(kind)}"]`) !== null;
1303
+ const endOf = (target) => {
1304
+ if (target.type === "record") {
1305
+ // Still standing in this frame (a plot shows fewer buildings as it changes): its place in the picture; else its district.
1306
+ const building = holder.querySelector(`[data-graview-building="${CSS.escape(target.id)}"]`) ? world.buildings.get(target.id) : undefined;
1307
+ if (building)
1308
+ return { at: project(painted, { x: building.x + building.width / 2, y: building.y + building.height / 2 }), on: "building" };
1309
+ const kind = store.graph.getNode(target.id)?.kind;
1310
+ const district = kind && drawnPlot(kind) ? world.plots.get(kind) : undefined;
1311
+ return district ? { at: project(painted, middle(district)), on: `kind:${kind}` } : null;
1312
+ }
1313
+ const district = drawnPlot(target.kind) ? world.plots.get(target.kind) : undefined;
1314
+ return district ? { at: project(painted, middle(district)), on: `kind:${target.kind}` } : null;
1315
+ };
1316
+ const labelFor = (target) => {
1317
+ if (target.type !== "record")
1318
+ return target.kind;
1319
+ const node = store.graph.getNode(target.id);
1320
+ return node ? labelOf(store.schema.tryDefinition(node.kind), node) : target.id;
1321
+ };
1322
+ /* The page's box in sight, read once a frame for every part on it. */
1323
+ const ports = new Map();
1324
+ const portOf = (element) => {
1325
+ const port = element.closest("[data-graview-one-place]");
1326
+ if (!port)
1327
+ return null;
1328
+ let box = ports.get(port);
1329
+ if (!box)
1330
+ ports.set(port, (box = port.getBoundingClientRect()));
1331
+ return box;
1332
+ };
1333
+ /* One line a record: from where it is shown, in sight on the page, to where it stands — gathered first, routed once every part is known. */
1334
+ const asked = [];
1335
+ /** Every part's box on the page in sight: what a line inside the page goes round. */
1336
+ const words = [];
1337
+ const offer = (target, part, visible, partBox) => {
1338
+ const key = target.type === "record" ? target.id : `kind:${target.kind}`;
1339
+ // Only what is in sight on the page: a part scrolled past, or under the page's own edge, draws none.
1340
+ if (part.x < visible.left || part.x > visible.right || part.y < visible.top || part.y > visible.bottom)
1341
+ return;
1342
+ if (partBox.width > 0 && partBox.height > 0)
1343
+ words.push(partBox);
1344
+ if (taken.has(key))
1345
+ return;
1346
+ const end = endOf(target);
1347
+ if (!end)
1348
+ return;
1349
+ taken.add(key);
1350
+ asked.push({ key, target, part, box: partBox, visible, end });
1351
+ };
1352
+ /* The record page drawn by a view that names no other record: where its one line to the record leaves from. */
1353
+ let aboutPart = null;
1354
+ // The page's own parts first — a link to a record's page, a row that names one, a view's records — then the Connected rows for what the page does not show.
1355
+ const parts = [...plane.querySelectorAll(`a[href], ${RECORD_PARTS}, ${VIEW_PARTS}`)].sort((x, y) => Number(x.closest("[data-graview-connected]") !== null) - Number(y.closest("[data-graview-connected]") !== null));
1356
+ for (const element of parts) {
1357
+ if (asked.length >= MOST_LINES)
1358
+ break;
1359
+ if (element.closest("nav, header, [role=navigation], [role=banner]"))
1360
+ continue;
1361
+ const visible = portOf(element);
1362
+ if (!visible)
1363
+ continue;
1364
+ const boxes = recordBoxesOf(element);
1365
+ if (boxes) {
1366
+ const rect = element.getBoundingClientRect();
1367
+ if (rect.width < 1 || rect.height < 1)
1368
+ continue;
1369
+ // Its pixels carried through its box as this frame paints it: the page scales as it moves a level.
1370
+ const scale = element.offsetWidth > 0 ? rect.width / element.offsetWidth : 1;
1371
+ let named = false;
1372
+ let about = null;
1373
+ for (const box of boxes.boxes()) {
1374
+ if (asked.length >= MOST_LINES)
1375
+ break;
1376
+ if (!store.graph.getNode(box.id))
1377
+ continue;
1378
+ const part = { x: rect.left + (box.x + box.width / 2) * scale, y: rect.top + (box.y + box.height / 2) * scale };
1379
+ const partBox = { x: rect.left + box.x * scale, y: rect.top + box.y * scale, width: box.width * scale, height: box.height * scale };
1380
+ if (box.id === subjectId) {
1381
+ about ??= part;
1382
+ if (partBox.width > 0 && partBox.height > 0)
1383
+ words.push(partBox);
1384
+ continue;
1385
+ }
1386
+ named = true;
1387
+ offer({ type: "record", id: box.id }, part, visible, partBox);
1388
+ }
1389
+ if (!named && subjectId !== null && aboutPart === null) {
1390
+ // From the middle of what is in sight of it, or the record's own part where it drew one.
1391
+ const top = Math.max(rect.top, visible.top);
1392
+ const bottom = Math.min(rect.bottom, visible.bottom);
1393
+ const left = Math.max(rect.left, visible.left);
1394
+ const right = Math.min(rect.right, visible.right);
1395
+ if (about)
1396
+ aboutPart = { part: about, visible, box: { ...about, width: 0, height: 0 } };
1397
+ else if (bottom - top >= 1 && right - left >= 1)
1398
+ aboutPart = { part: { x: (left + right) / 2, y: (top + bottom) / 2 }, visible, box: { x: left, y: top, width: right - left, height: bottom - top } };
1399
+ }
1400
+ continue;
1401
+ }
1402
+ const named = recordShownBy(element);
1403
+ let target = null;
1404
+ if (named !== null && store.graph.getNode(named))
1405
+ target = { type: "record", id: named };
1406
+ else if (element instanceof HTMLAnchorElement && element.href) {
1407
+ const resolved = at.resolve(element.pathname);
1408
+ target = resolved?.type === "record" ? { type: "record", id: resolved.id } : resolved?.type === "list" ? { type: "list", kind: resolved.kind } : null;
1409
+ }
1410
+ if (!target || (target.type === "record" && target.id === subjectId))
1411
+ continue;
1412
+ const rect = element.getBoundingClientRect();
1413
+ if (rect.width < 1 || rect.height < 1)
1414
+ continue;
1415
+ offer(target, { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }, visible, boxOf(rect));
1416
+ }
1417
+ if (aboutPart && subjectId !== null && asked.length < MOST_LINES)
1418
+ offer({ type: "record", id: subjectId }, aboutPart.part, aboutPart.visible, aboutPart.box);
1419
+ /*
1420
+ * INSIDE THE PAGE A LINE CROSSES NO ONE ELSE'S WORDS (Graview Cloud's
1421
+ * Deep Time: a row's later pills struck through the earlier ones on
1422
+ * their way to the panel's edge). Each line leaves its own part; where
1423
+ * the straight way to the edge would cross another part, it drops
1424
+ * into the gap above or below its row — the one toward where it is
1425
+ * going first — runs along it to the edge, and goes on from there.
1426
+ * Lines along one gap stand a pixel or two apart.
1427
+ */
1428
+ const hits = (a, b, own) => words.some((o) => o !== own && Math.max(a.x, b.x) > o.x + 1 && Math.min(a.x, b.x) < o.x + o.width - 1 && Math.max(a.y, b.y) > o.y + 1 && Math.min(a.y, b.y) < o.y + o.height - 1);
1429
+ const edgeX = pageRect.x;
1430
+ const wayIn = (one) => {
1431
+ const b = one.box;
1432
+ const from = leaveFrom(one.part, pageRect, phone);
1433
+ const straight = phone ? { x: one.part.x, y: Math.max(b.y, one.visible.top, from.y) } : { x: Math.max(b.x, one.visible.left, from.x), y: one.part.y };
1434
+ if (phone || !hits(straight, from, b))
1435
+ return { via: [straight, from], gap: null };
1436
+ const beside = words.filter((o) => o !== b && o.x < b.x + b.width && o.x + o.width > edgeX);
1437
+ const above = beside.filter((o) => o.y + o.height <= b.y + 1).reduce((most, o) => Math.max(most, o.y + o.height), -Infinity);
1438
+ const below = beside.filter((o) => o.y >= b.y + b.height - 1).reduce((least, o) => Math.min(least, o.y), Infinity);
1439
+ const gaps = [
1440
+ { y: Number.isFinite(above) ? (above + b.y) / 2 : b.y - 4, leave: b.y },
1441
+ { y: Number.isFinite(below) ? (b.y + b.height + below) / 2 : b.y + b.height + 4, leave: b.y + b.height },
1442
+ ].filter((g) => g.y > one.visible.top && g.y < one.visible.bottom);
1443
+ // Toward where it is going first: up for a building above the part, down for one below.
1444
+ if (one.end.at.y > one.part.y)
1445
+ gaps.reverse();
1446
+ const x = b.x + Math.min(10, b.width / 2);
1447
+ for (const gap of gaps) {
1448
+ const leave = { x, y: gap.leave };
1449
+ const down = { x, y: gap.y };
1450
+ const out = { x: edgeX, y: gap.y };
1451
+ if (!hits(leave, down, b) && !hits(down, out, b))
1452
+ return { via: [leave, down, out], gap: Math.round(gap.y) };
1453
+ }
1454
+ return { via: [straight, from], gap: null };
1455
+ };
1456
+ const ways = asked.map(wayIn);
1457
+ // Lines along one gap: a pixel and a half apart, about its middle.
1458
+ const along = new Map();
1459
+ ways.forEach((way, at) => {
1460
+ if (way.gap !== null)
1461
+ along.set(way.gap, [...(along.get(way.gap) ?? []), at]);
1462
+ });
1463
+ for (const ats of along.values()) {
1464
+ ats.forEach((at, nth) => {
1465
+ const shift = (nth - (ats.length - 1) / 2) * 1.5;
1466
+ const via = ways[at].via;
1467
+ ways[at] = { ...ways[at], via: [via[0], ...via.slice(1).map((p) => ({ x: p.x, y: p.y + shift }))] };
1468
+ });
1469
+ }
1470
+ asked.forEach((one, at) => {
1471
+ const via = ways[at].via;
1472
+ const route = routeLine(via[via.length - 1], one.end.at, area, phone, reserved);
1473
+ if (!route)
1474
+ return;
1475
+ found.push({ key: one.key, from: route.from, to: route.to, bend: route.bend, under: route.toward, on: one.end.on, target: one.target, label: labelFor(one.target), part: one.part, start: via[0], via: [...via.slice(0, -1), route.from] });
1476
+ });
1477
+ /*
1478
+ * MANY LINES INTO ONE DISTRICT ARE ONE LINE (Graview HQ, Cloud's test
1479
+ * of Deep Time): each part keeps its own short line to the page's
1480
+ * edge, where they meet, level with the middle of them, and one line
1481
+ * goes on from there into the district with how many it carries.
1482
+ */
1483
+ const into = new Map();
1484
+ found.forEach((line, at) => {
1485
+ const kind = line.target.type === "record" ? store.graph.getNode(line.target.id)?.kind : line.target.kind;
1486
+ if (kind)
1487
+ into.set(kind, [...(into.get(kind) ?? []), at]);
1488
+ });
1489
+ for (const [kind, ats] of into) {
1490
+ if (ats.length < GATHER)
1491
+ continue;
1492
+ const end = endOf({ type: "list", kind });
1493
+ if (!end)
1494
+ continue;
1495
+ const parts = ats.map((at) => found[at].part);
1496
+ const meet = leaveFrom({ x: parts.reduce((sum, p) => sum + p.x, 0) / parts.length, y: parts.reduce((sum, p) => sum + p.y, 0) / parts.length }, pageRect, phone);
1497
+ const route = routeLine(meet, end.at, area, phone, reserved);
1498
+ if (!route)
1499
+ continue;
1500
+ ats.forEach((at, nth) => {
1501
+ const line = found[at];
1502
+ // Its own way to the page's edge, then along the edge to where they meet.
1503
+ found[at] = { ...line, via: [...line.via, route.from], from: route.from, to: route.to, bend: route.bend, under: route.toward, on: end.on, gathered: { kind, count: ats.length, lead: nth === 0 } };
1504
+ });
1505
+ }
1506
+ }
1507
+ draw(found, shown);
1508
+ /* Told what the frame says, when it changes: the level rested at or headed for, and whether the page is out of the window. */
1509
+ const said = `${move ? move.toAltitude : restingLevel(a)}|${away ? 1 : 0}|${move || asking.scrubbing ? 1 : 0}`;
1510
+ if (said !== lastReported) {
1511
+ lastReported = said;
1512
+ at.onAltitude({ level: move ? move.toAltitude : restingLevel(a), away, moving: move !== null || asking.scrubbing });
1513
+ }
1514
+ /* The record the page is about, marked where it stands: the one building that is "you are here". */
1515
+ const hereId = at.where.type === "record" ? at.where.id : null;
1516
+ const marked = holder.querySelector("[data-graview-here]");
1517
+ if (marked && marked.getAttribute("data-graview-building") !== hereId)
1518
+ marked.removeAttribute("data-graview-here");
1519
+ if (hereId && (!marked || marked.getAttribute("data-graview-building") !== hereId))
1520
+ holder.querySelector(`[data-graview-building="${CSS.escape(hereId)}"]`)?.setAttribute("data-graview-here", "");
1521
+ holder.setAttribute("data-graview-altitude-now", a.toFixed(3));
1522
+ // The camera's zoom as painted in this frame, for a reader of the move (the harness holds it to going one way).
1523
+ holder.setAttribute("data-graview-camera-zoom", (zoomNow * painted.s).toFixed(4));
1524
+ };
1525
+ /*
1526
+ * A RING IS NEVER UNDER A BOARD OR A NAME, NOR AGAINST ONE: a line's end
1527
+ * that a board or a district's name stands over (or within a fingertip
1528
+ * of) offers no ring of its own — a press there landed on the board, and
1529
+ * one beside it was a small target crowded by a larger one. The line
1530
+ * still ends there; the board or the name is what is pressed.
1531
+ */
1532
+ const pressable = [];
1533
+ const besidePressable = (to) => pressable.some((b) => to.x > b.x - 20 && to.x < b.x + b.width + 20 && to.y > b.y - 20 && to.y < b.y + b.height + 20);
1534
+ /* The overlay, written in the frame it is measured in: no React state per frame. */
1535
+ const draw = (found, opacity) => {
1536
+ const control = fab.current?.querySelector('[data-graview-one-face-dock]')?.getBoundingClientRect() ?? null;
1537
+ const svg = overlay.current;
1538
+ const ringLayer = rings.current;
1539
+ if (!svg || !ringLayer)
1540
+ return;
1541
+ svg.style.opacity = String(opacity);
1542
+ ringLayer.style.opacity = String(opacity);
1543
+ // No lines, no group of their ends: nothing of it outside the page's landmarks while the page is read.
1544
+ ringLayer.hidden = found.length === 0;
1545
+ const keep = new Set(found.map((line) => line.key));
1546
+ for (const [key, drawn] of lines) {
1547
+ if (keep.has(key))
1548
+ continue;
1549
+ drawn.g.remove();
1550
+ drawn.ring.remove();
1551
+ lines.delete(key);
1552
+ }
1553
+ /* One marker where several lines end together: the first one's, the rest drawn without a ring of their own. */
1554
+ const ringsAt = [];
1555
+ for (const line of found) {
1556
+ let drawn = lines.get(line.key);
1557
+ if (!drawn) {
1558
+ const g = document.createElementNS(NS, "g");
1559
+ const path = document.createElementNS(NS, "path");
1560
+ path.setAttribute("fill", "none");
1561
+ path.setAttribute("stroke", "var(--graview-accent)");
1562
+ path.setAttribute("stroke-width", "1.5");
1563
+ path.setAttribute("stroke-opacity", "0.8");
1564
+ path.setAttribute("stroke-linecap", "round");
1565
+ const dot = document.createElementNS(NS, "circle");
1566
+ dot.setAttribute("r", "2.5");
1567
+ dot.setAttribute("fill", "var(--graview-accent)");
1568
+ const arrow = document.createElementNS(NS, "path");
1569
+ arrow.setAttribute("fill", "var(--graview-accent)");
1570
+ g.append(path, dot, arrow);
1571
+ svg.append(g);
1572
+ const ring = document.createElement("button");
1573
+ ring.type = "button";
1574
+ ring.setAttribute("data-testid", "one-place-ring");
1575
+ ring.style.cssText = "position:fixed;width:16px;height:16px;padding:0;border-radius:8px;border:2px solid var(--graview-accent);background:color-mix(in srgb, var(--graview-accent) 18%, transparent);cursor:pointer;";
1576
+ const key = line.key;
1577
+ ring.addEventListener("click", () => lines.get(key)?.open());
1578
+ ringLayer.append(ring);
1579
+ drawn = { g, path, dot, arrow, ring, open: () => { } };
1580
+ lines.set(line.key, drawn);
1581
+ }
1582
+ const target = line.target;
1583
+ const gathered = line.gathered;
1584
+ drawn.open = gathered ? () => latest.current.openKind(gathered.kind, PANEL) : () => (target.type === "record" ? latest.current.openRecord(target.id, PANEL) : latest.current.openKind(target.kind, PANEL));
1585
+ // Gathered, only the first of them draws the line they share, and its count; the rest draw their own short line to where they meet.
1586
+ const shared = !gathered || gathered.lead;
1587
+ const { from, to } = line;
1588
+ const bend = line.bend;
1589
+ drawn.g.setAttribute("data-graview-line", line.key);
1590
+ drawn.g.setAttribute("data-graview-line-on", line.on);
1591
+ drawn.g.toggleAttribute("data-graview-line-under", line.under !== null);
1592
+ drawn.g.setAttribute("data-graview-line-from", `${from.x.toFixed(2)},${from.y.toFixed(2)}`);
1593
+ drawn.g.setAttribute("data-graview-line-start", `${line.start.x.toFixed(2)},${line.start.y.toFixed(2)}`);
1594
+ if (gathered)
1595
+ drawn.g.setAttribute("data-graview-line-gathered", `kind:${gathered.kind}`);
1596
+ else
1597
+ drawn.g.removeAttribute("data-graview-line-gathered");
1598
+ if (gathered?.lead)
1599
+ drawn.g.setAttribute("data-graview-line-count", String(gathered.count));
1600
+ else
1601
+ drawn.g.removeAttribute("data-graview-line-count");
1602
+ drawn.g.setAttribute("data-graview-line-part", `${line.part.x.toFixed(2)},${line.part.y.toFixed(2)}`);
1603
+ drawn.g.setAttribute("data-graview-line-to", `${to.x.toFixed(2)},${to.y.toFixed(2)}`);
1604
+ drawn.g.setAttribute("data-graview-line-bend", `${bend.x.toFixed(2)},${bend.y.toFixed(2)}`);
1605
+ if (line.under)
1606
+ drawn.g.setAttribute("data-graview-line-toward", `${line.under.x.toFixed(2)},${line.under.y.toFixed(2)}`);
1607
+ else
1608
+ drawn.g.removeAttribute("data-graview-line-toward");
1609
+ const leg = line.via.map((p, i) => `${i === 0 ? "M" : "L"}${p.x.toFixed(2)},${p.y.toFixed(2)}`).join(" ");
1610
+ drawn.path.setAttribute("d", shared ? `${leg} Q${bend.x.toFixed(2)},${bend.y.toFixed(2)} ${to.x.toFixed(2)},${to.y.toFixed(2)}` : leg);
1611
+ drawn.dot.setAttribute("cx", line.start.x.toFixed(2));
1612
+ drawn.dot.setAttribute("cy", line.start.y.toFixed(2));
1613
+ // The shared line's marker says how many it carries: wide enough for the number, and a fingertip's size.
1614
+ const size = gathered?.lead ? 22 : 16;
1615
+ const wide = gathered?.lead ? Math.max(22, 10 + 7 * String(gathered.count).length) : 16;
1616
+ drawn.ring.textContent = gathered?.lead ? String(gathered.count) : "";
1617
+ drawn.ring.style.width = `${wide}px`;
1618
+ drawn.ring.style.height = `${size}px`;
1619
+ drawn.ring.style.borderRadius = `${size / 2}px`;
1620
+ drawn.ring.style.font = gathered?.lead ? "600 11px/1 system-ui, sans-serif" : "";
1621
+ drawn.ring.style.color = gathered?.lead ? "var(--graview-ink)" : "";
1622
+ drawn.ring.style.background = gathered?.lead ? "var(--graview-panel, Canvas)" : "color-mix(in srgb, var(--graview-accent) 18%, transparent)";
1623
+ if (!shared) {
1624
+ drawn.arrow.setAttribute("d", "");
1625
+ drawn.ring.style.display = "none";
1626
+ }
1627
+ else if (line.under) {
1628
+ // An arrow at the page's edge, pointing on toward the building under the page.
1629
+ const angle = Math.atan2(line.under.y - to.y, line.under.x - to.x);
1630
+ const tip = { x: to.x + Math.cos(angle) * 7, y: to.y + Math.sin(angle) * 7 };
1631
+ const left = { x: to.x + Math.cos(angle + 2.4) * 6, y: to.y + Math.sin(angle + 2.4) * 6 };
1632
+ const right = { x: to.x + Math.cos(angle - 2.4) * 6, y: to.y + Math.sin(angle - 2.4) * 6 };
1633
+ drawn.arrow.setAttribute("d", `M${tip.x.toFixed(2)},${tip.y.toFixed(2)} L${left.x.toFixed(2)},${left.y.toFixed(2)} L${right.x.toFixed(2)},${right.y.toFixed(2)} Z`);
1634
+ // The marker toward a building out of sight is the way to it: its name said, a press flies there.
1635
+ const crowded = ringsAt.some((p) => Math.hypot(p.x - to.x, p.y - to.y) < 28) || besidePressable(to);
1636
+ drawn.ring.style.display = crowded ? "none" : "";
1637
+ if (!crowded)
1638
+ ringsAt.push(to);
1639
+ drawn.ring.style.left = `${(to.x - wide / 2).toFixed(2)}px`;
1640
+ drawn.ring.style.top = `${(to.y - size / 2).toFixed(2)}px`;
1641
+ }
1642
+ else {
1643
+ drawn.arrow.setAttribute("d", "");
1644
+ /*
1645
+ * A SHARED LINE'S COUNT IS ALWAYS SAID: where its district's middle
1646
+ * has a board or a name beside it (which takes the press there), the
1647
+ * count stands back along the line, at the nearest place clear of them.
1648
+ */
1649
+ let at = to;
1650
+ if (gathered?.lead && besidePressable(to)) {
1651
+ for (let t = 0.95; t >= 0.4; t -= 0.05) {
1652
+ const p = { x: (1 - t) ** 2 * from.x + 2 * (1 - t) * t * bend.x + t * t * to.x, y: (1 - t) ** 2 * from.y + 2 * (1 - t) * t * bend.y + t * t * to.y };
1653
+ if (!besidePressable(p)) {
1654
+ at = p;
1655
+ break;
1656
+ }
1657
+ }
1658
+ }
1659
+ // An end under the bar, or under the control at the foot, is not offered: they stand over it.
1660
+ const under = control && at.x > control.left - 10 && at.x < control.right + 10 && at.y > control.top - 10 && at.y < control.bottom + 10;
1661
+ const crowded = ringsAt.some((p) => Math.hypot(p.x - at.x, p.y - at.y) < 28) || besidePressable(at);
1662
+ drawn.ring.style.display = at.y < (state.current.view.top ?? 0) + 8 || under || crowded ? "none" : "";
1663
+ if (!crowded)
1664
+ ringsAt.push(at);
1665
+ drawn.ring.style.left = `${(at.x - wide / 2).toFixed(2)}px`;
1666
+ drawn.ring.style.top = `${(at.y - size / 2).toFixed(2)}px`;
1667
+ }
1668
+ if (gathered) {
1669
+ const plural = pluralOf(store, gathered.kind);
1670
+ drawn.ring.setAttribute("data-graview-to", `kind:${gathered.kind}`);
1671
+ drawn.ring.setAttribute("aria-label", `${gathered.count} ${plural.toLowerCase()} here: open the ${plural} list`);
1672
+ drawn.ring.title = `${gathered.count} ${plural.toLowerCase()}`;
1673
+ }
1674
+ else {
1675
+ drawn.ring.setAttribute("data-graview-to", line.target.type === "record" ? line.target.id : `kind:${line.target.kind}`);
1676
+ drawn.ring.setAttribute("aria-label", line.target.type === "record" ? `Open ${line.label}` : `Open the ${line.label} list`);
1677
+ drawn.ring.title = line.label;
1678
+ }
1679
+ }
1680
+ };
1681
+ frame = requestAnimationFrame(tick);
1682
+ const said = sheet.current;
1683
+ said?.addEventListener(RECORD_BOXES_MOVED, askReframe);
1684
+ // The person's own hand on the city: a press, a drag, a wheel, a pinch.
1685
+ const city = box.current;
1686
+ const hands = ["pointerdown", "wheel", "touchstart"];
1687
+ for (const type of hands)
1688
+ city?.addEventListener(type, byHand, { capture: true, passive: true });
1689
+ return () => {
1690
+ cancelAnimationFrame(frame);
1691
+ said?.removeEventListener(RECORD_BOXES_MOVED, askReframe);
1692
+ for (const type of hands)
1693
+ city?.removeEventListener(type, byHand, { capture: true });
1694
+ for (const drawn of lines.values()) {
1695
+ drawn.g.remove();
1696
+ drawn.ring.remove();
1697
+ }
1698
+ };
1699
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1700
+ }, []);
1701
+ /*
1702
+ * A HAND ON A SIGN MOVES THE CITY. The boards, the districts' names and
1703
+ * their chips, and the rings at the lines' ends stand over the city in a
1704
+ * layer of their own, so a drag, a wheel or a pinch that began on one of
1705
+ * them reached nothing: the city stalled under the pointer. Begun there, a
1706
+ * drag past a few pixels pans the city's own camera by as far as the hand
1707
+ * goes (and the press that began it opens nothing), two fingers zoom it
1708
+ * about their middle, and the wheel pans or, with ctrl, zooms it — as over
1709
+ * open ground. A press that does not move still opens what it is on.
1710
+ */
1711
+ useEffect(() => {
1712
+ const layers = [boardLayer.current, tagLayer.current, rings.current].filter((el) => el !== null);
1713
+ const DRAG = 6;
1714
+ const down = new Map();
1715
+ let dragging = false;
1716
+ let pinchFrom = null;
1717
+ let swallow = false;
1718
+ const spread = () => {
1719
+ const [a, b] = [...down.values()];
1720
+ return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
1721
+ };
1722
+ const onDown = (event) => {
1723
+ if (event.pointerType === "mouse" && event.button !== 0)
1724
+ return;
1725
+ down.set(event.pointerId, { x: event.clientX, y: event.clientY });
1726
+ dragging = false;
1727
+ pinchFrom = down.size === 2 ? spread() : null;
1728
+ };
1729
+ const onMove = (event) => {
1730
+ const was = down.get(event.pointerId);
1731
+ const cam = camera.current;
1732
+ if (!was || !cam)
1733
+ return;
1734
+ const dx = event.clientX - was.x;
1735
+ const dy = event.clientY - was.y;
1736
+ if (down.size >= 2) {
1737
+ down.set(event.pointerId, { x: event.clientX, y: event.clientY });
1738
+ const now = spread();
1739
+ if (pinchFrom && now > 0) {
1740
+ const [a, b] = [...down.values()];
1741
+ cam.zoomTo(cam.zoom() * (now / pinchFrom), (a.x + b.x) / 2, (a.y + b.y) / 2);
1742
+ pinchFrom = now;
1743
+ dragging = true;
1744
+ }
1745
+ return;
1746
+ }
1747
+ if (!dragging && Math.hypot(dx, dy) < DRAG)
1748
+ return;
1749
+ if (!dragging) {
1750
+ dragging = true;
1751
+ try {
1752
+ event.target?.setPointerCapture?.(event.pointerId);
1753
+ }
1754
+ catch {
1755
+ // A pointer the page did not see go down keeps no capture; the moves still come here.
1756
+ }
1757
+ }
1758
+ cam.panBy(dx, dy);
1759
+ down.set(event.pointerId, { x: event.clientX, y: event.clientY });
1760
+ };
1761
+ const onUp = (event) => {
1762
+ if (!down.has(event.pointerId))
1763
+ return;
1764
+ down.delete(event.pointerId);
1765
+ if (dragging)
1766
+ swallow = true;
1767
+ if (down.size === 0) {
1768
+ dragging = false;
1769
+ pinchFrom = null;
1770
+ }
1771
+ };
1772
+ // The press that ended a drag opens nothing.
1773
+ const onClick = (event) => {
1774
+ if (!swallow)
1775
+ return;
1776
+ swallow = false;
1777
+ event.preventDefault();
1778
+ event.stopPropagation();
1779
+ };
1780
+ const onWheel = (event) => {
1781
+ const cam = camera.current;
1782
+ if (!cam)
1783
+ return;
1784
+ event.preventDefault();
1785
+ const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? 400 : 1;
1786
+ if (event.ctrlKey || event.metaKey)
1787
+ cam.zoomTo(cam.zoom() * Math.exp(-event.deltaY * unit * 0.01), event.clientX, event.clientY);
1788
+ else
1789
+ cam.panBy(-event.deltaX * unit, -event.deltaY * unit);
1790
+ };
1791
+ /*
1792
+ * A SCROLL OVER A DISTRICT PANS TOO. The Scene leaves a lens's wheel to
1793
+ * the lens, and a district's plot is a view of its own there, so a
1794
+ * two-finger scroll that happened to start over a district moved
1795
+ * nothing. On this face a district scrolls nothing of its own: the
1796
+ * scroll pans the city, as over open ground. A part that does scroll (a
1797
+ * scroll region, a pane, a worker view drawn there, whose own scroll this
1798
+ * face cannot see into) keeps its own wheel.
1799
+ */
1800
+ const holder = box.current;
1801
+ const onPlotWheel = (event) => {
1802
+ const target = event.target;
1803
+ if (event.ctrlKey || event.metaKey || !target?.closest("[data-graview-plot]") || target.closest(".graview-scroll, [data-graview-overlay], .graview-zoom, [data-worker-view]"))
1804
+ return;
1805
+ const cam = camera.current;
1806
+ if (!cam)
1807
+ return;
1808
+ event.preventDefault();
1809
+ event.stopPropagation();
1810
+ const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? 400 : 1;
1811
+ cam.panBy(-event.deltaX * unit, -event.deltaY * unit);
1812
+ };
1813
+ holder?.addEventListener("wheel", onPlotWheel, { passive: false, capture: true });
1814
+ for (const layer of layers) {
1815
+ layer.addEventListener("pointerdown", onDown, true);
1816
+ layer.addEventListener("pointermove", onMove, true);
1817
+ layer.addEventListener("pointerup", onUp, true);
1818
+ layer.addEventListener("pointercancel", onUp, true);
1819
+ layer.addEventListener("click", onClick, true);
1820
+ layer.addEventListener("wheel", onWheel, { passive: false });
1821
+ }
1822
+ return () => {
1823
+ holder?.removeEventListener("wheel", onPlotWheel, { capture: true });
1824
+ for (const layer of layers) {
1825
+ layer.removeEventListener("pointerdown", onDown, true);
1826
+ layer.removeEventListener("pointermove", onMove, true);
1827
+ layer.removeEventListener("pointerup", onUp, true);
1828
+ layer.removeEventListener("pointercancel", onUp, true);
1829
+ layer.removeEventListener("click", onClick, true);
1830
+ layer.removeEventListener("wheel", onWheel);
1831
+ }
1832
+ };
1833
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1834
+ }, []);
1835
+ /*
1836
+ * A building pressed, above the page: its record, in the panel, the camera
1837
+ * flying to it. The Scene's blocks take no press of their own (their tile
1838
+ * does, for the district), so the building is found by where each stands.
1839
+ * The end of a drag is the camera's, not a press.
1840
+ */
1841
+ const downAt = useRef(null);
1842
+ const pressed = (event) => {
1843
+ const from = downAt.current;
1844
+ if (from && Math.hypot(event.clientX - from.x, event.clientY - from.y) > 6)
1845
+ return;
1846
+ let id = null;
1847
+ let front = -Infinity;
1848
+ for (const building of box.current?.querySelectorAll("[data-graview-building]") ?? []) {
1849
+ const r = building.getBoundingClientRect();
1850
+ if (event.clientX < r.left || event.clientX > r.right || event.clientY < r.top || event.clientY > r.bottom)
1851
+ continue;
1852
+ if (r.bottom > front) {
1853
+ front = r.bottom;
1854
+ id = building.getAttribute("data-graview-building");
1855
+ }
1856
+ }
1857
+ if (!id || !store.graph.getNode(id)) {
1858
+ // Bare ground, or a district's own tile, which the Scene takes: at the city, no view is chosen any more.
1859
+ if (!event.target?.closest?.("[data-graview-plot], .graview-kind-card, button, a"))
1860
+ onGround();
1861
+ return;
1862
+ }
1863
+ event.preventDefault();
1864
+ event.stopPropagation();
1865
+ // A building is a record: its own page, beside the city, the camera coming down to it.
1866
+ openRecord(id, PANEL);
1867
+ };
1868
+ return (_jsxs(_Fragment, { children: [_jsxs("div", { ref: box, "data-testid": "one-place-city", onPointerDownCapture: (event) => {
1869
+ downAt.current = { x: event.clientX, y: event.clientY };
1870
+ }, onClickCapture: pressed,
1871
+ /*
1872
+ * While the page is read the city is laid out, out of sight, so the
1873
+ * first frame out has it to move: no pointer, no keyboard, no room
1874
+ * in the page — and what of it reaches past the window is inside a
1875
+ * box that scrolls, as a phone's sideways row would be.
1876
+ */
1877
+ style: { position: "fixed", inset: 0, zIndex: ONE_FACE_LAYERS.city, display: "none", visibility: "hidden", overflow: "hidden", containerType: "size" }, children: [_jsx("style", { children: CITY_CSS }), _jsx(Scene, { renderer: "dom", blur: false, animate: false, camera: camera, options: sceneOptions, style: { width: "100%", height: "100%" } })] }), _jsx("svg", { ref: overlay, "aria-hidden": "true", "data-testid": "one-place-lines", width: view.width, height: view.height, style: { position: "fixed", left: 0, top: 0, zIndex: ONE_FACE_LAYERS.marks, pointerEvents: "none", overflow: "visible", opacity: 0 } }), _jsx("div", { ref: rings, hidden: true, role: "group", "aria-label": "What this page shows, in the city", style: { position: "fixed", left: 0, top: 0, width: 0, height: 0, zIndex: ONE_FACE_LAYERS.marks, opacity: 0 } }), _jsx("div", { ref: tagLayer, role: "region", "aria-label": "The districts", "data-testid": "one-place-names", style: { position: "fixed", left: 0, top: 0, width: 0, height: 0, zIndex: ONE_FACE_LAYERS.marks }, children: districtNames.map((one) => (_jsxs("span", { ref: (el) => {
1878
+ if (el)
1879
+ tagEls.current.set(one.kind, el);
1880
+ else
1881
+ tagEls.current.delete(one.kind);
1882
+ }, "data-graview-district-tag": one.kind, className: "graview-one-place-district", style: { visibility: "hidden" }, children: [_jsxs("button", { type: "button", "data-graview-district-open": one.kind, "aria-label": `${one.plural}, ${one.count === 0 ? "none yet" : one.count.toLocaleString()}: open the list`, title: `${one.plural}, ${one.count}`, onClick: () => openKind(one.kind, PANEL), children: [_jsx("span", { "data-graview-district-tag-name": "", children: one.plural }), " ", _jsx("span", { "data-graview-district-tag-count": "", children: one.count === 0 ? "none yet" : one.count.toLocaleString() })] }), one.problems > 0 ? (_jsxs("button", { type: "button", "data-graview-district-problems": one.kind, "aria-label": `${one.problems} ${one.problems === 1 ? "problem" : "problems"} in ${one.plural}`, title: `${one.problems} ${one.problems === 1 ? "problem" : "problems"} in ${one.plural}: press to see them`, onClick: () => face?.openPath(`/problems?kind=${encodeURIComponent(one.kind)}`, PANEL), children: ["\u26A0 ", one.problems] })) : null] }, one.kind))) })] }));
1883
+ }
1884
+ /** The one kind a view stands on: a kind's list, or a view of one kind; null for the home, a view of several kinds and the connections. */
1885
+ export function signKindOf(where) {
1886
+ if (where.type === "list")
1887
+ return where.kind;
1888
+ if (where.type === "place" && where.kinds.length === 1)
1889
+ return where.kinds[0];
1890
+ return null;
1891
+ }
1892
+ //# sourceMappingURL=one-place-city.js.map