@loadmap/react 0.13.0 → 0.15.0

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.
@@ -0,0 +1,609 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
+ import { createRoot } from "react-dom/client";
4
+ // src/viewpoint.ts
5
+ /** The zoom's range in board pixels a scene unit, and how much of the diagram always stays in view. */
6
+ const ZOOM = {
7
+ min: .1,
8
+ max: 4,
9
+ keep: 64
10
+ };
11
+ /** The free part of the board, in board pixels; the whole board when the insets leave too little. */
12
+ function free(vp) {
13
+ const { top, right, bottom, left } = vp.inset;
14
+ const w = vp.w - left - right;
15
+ const h = vp.h - top - bottom;
16
+ return w > 0 && h > 0 ? {
17
+ x: left,
18
+ y: top,
19
+ w,
20
+ h
21
+ } : {
22
+ x: 0,
23
+ y: 0,
24
+ w: vp.w,
25
+ h: vp.h
26
+ };
27
+ }
28
+ /** The part of the scene the board shows: its viewBox, at the board's shape. */
29
+ function shown(v, vp) {
30
+ const w = vp.w / v.k;
31
+ const h = vp.h / v.k;
32
+ return {
33
+ x: v.x - w / 2,
34
+ y: v.y - h / 2,
35
+ w,
36
+ h
37
+ };
38
+ }
39
+ /** The view that shows `r` centred in the free part, as large as fits with the margin, no closer than `max`. */
40
+ function frame(r, vp, max = vp.fitMax) {
41
+ const f = free(vp);
42
+ const m = Math.min(vp.margin, f.w / 4, f.h / 4);
43
+ const k = Math.min((f.w - 2 * m) / r.w, (f.h - 2 * m) / r.h, max);
44
+ return at(r.x + r.w / 2, r.y + r.h / 2, f.x + f.w / 2, f.y + f.h / 2, k, vp);
45
+ }
46
+ /** The view at scale `k` that puts the scene point (sx, sy) at the board point (px, py). */
47
+ function at(sx, sy, px, py, k, vp) {
48
+ return {
49
+ x: sx + (vp.w / 2 - px) / k,
50
+ y: sy + (vp.h / 2 - py) / k,
51
+ k
52
+ };
53
+ }
54
+ /** The scene point under a board point. */
55
+ function toScene(v, vp, px, py) {
56
+ return {
57
+ x: v.x + (px - vp.w / 2) / v.k,
58
+ y: v.y + (py - vp.h / 2) / v.k
59
+ };
60
+ }
61
+ /** The zoom's range for this diagram: down to half of Fit when that is further out than the usual least. */
62
+ function range(fit) {
63
+ return {
64
+ min: Math.min(ZOOM.min, fit.k / 2),
65
+ max: Math.max(ZOOM.max, fit.k)
66
+ };
67
+ }
68
+ /** `v` scaled by `factor` about the board point (px, py), which keeps the scene point under it. */
69
+ function zoomAt(v, vp, px, py, factor) {
70
+ const p = toScene(v, vp, px, py);
71
+ return at(p.x, p.y, px, py, v.k * factor, vp);
72
+ }
73
+ /** `v` moved by a drag of (dx, dy) board pixels: the scene follows the pointer. */
74
+ function panBy(v, dx, dy) {
75
+ return {
76
+ x: v.x - dx / v.k,
77
+ y: v.y - dy / v.k,
78
+ k: v.k
79
+ };
80
+ }
81
+ /**
82
+ * `v` inside the zoom's range, and moved back until at least `ZOOM.keep` pixels of the diagram (`bounds`) show in the
83
+ * free part on each axis, or all of it where it is smaller: a diagram can never be lost off the board, nor under what
84
+ * floats over it.
85
+ */
86
+ function clamp(v, vp, bounds, lim) {
87
+ const k = Math.min(lim.max, Math.max(lim.min, v.k));
88
+ const f = free(vp);
89
+ const axis = (c, size, from, to, lo, len) => {
90
+ const keep = Math.min(ZOOM.keep, len * k);
91
+ const start = size / 2 + (lo - c) * k;
92
+ const end = start + len * k;
93
+ if (end < from + keep) return c - (from + keep - end) / k;
94
+ if (start > to - keep) return c + (start - (to - keep)) / k;
95
+ return c;
96
+ };
97
+ return {
98
+ x: axis(v.x, vp.w, f.x, f.x + f.w, bounds.x, bounds.w),
99
+ y: axis(v.y, vp.h, f.y, f.y + f.h, bounds.y, bounds.h),
100
+ k
101
+ };
102
+ }
103
+ /**
104
+ * The view for a camera of the story, a part of the scene at the scene's shape: the whole scene goes where `fit`
105
+ * puts it, and a closer camera as much closer, centred in the free part.
106
+ */
107
+ function shot(camera, scene, fit, vp) {
108
+ const f = free(vp);
109
+ return at(camera.x + camera.w / 2, camera.y + camera.h / 2, f.x + f.w / 2, f.y + f.h / 2, fit.k * (scene.width / camera.w), vp);
110
+ }
111
+ /** The story camera that `shot` turns into `v`: where Play flies from when the reader had moved the view. */
112
+ function cameraOf(v, scene, fit, vp) {
113
+ const f = free(vp);
114
+ const w = scene.width * (fit.k / v.k);
115
+ const h = w * (scene.height / scene.width);
116
+ const c = toScene(v, vp, f.x + f.w / 2, f.y + f.h / 2);
117
+ return {
118
+ x: c.x - w / 2,
119
+ y: c.y - h / 2,
120
+ w,
121
+ h
122
+ };
123
+ }
124
+ /** The view that brings `r` (a box) into the middle of the free part at the same scale; null when it shows whole. */
125
+ function bring(v, vp, r) {
126
+ const f = free(vp);
127
+ const left = vp.w / 2 + (r.x - v.x) * v.k;
128
+ const top = vp.h / 2 + (r.y - v.y) * v.k;
129
+ return left >= f.x && top >= f.y && left + r.w * v.k <= f.x + f.w && top + r.h * v.k <= f.y + f.h ? null : at(r.x + r.w / 2, r.y + r.h / 2, f.x + f.w / 2, f.y + f.h / 2, v.k, vp);
130
+ }
131
+ /** Where a part of the scene (a viewBox) draws on a board of w × h pixels: scaled to fit and centred, as SVG does. */
132
+ function placement(box, w, h) {
133
+ const k = Math.min(w / box.w, h / box.h);
134
+ return {
135
+ k,
136
+ dx: (w - box.w * k) / 2 - box.x * k,
137
+ dy: (h - box.h * k) / 2 - box.y * k
138
+ };
139
+ }
140
+ /** `t` of the way from `a` to `b`, the scale changing by the same factor each moment. */
141
+ function towards(a, b, t) {
142
+ const s = (1 - Math.cos(Math.PI * Math.max(0, Math.min(1, t)))) / 2;
143
+ return {
144
+ x: a.x + (b.x - a.x) * s,
145
+ y: a.y + (b.y - a.y) * s,
146
+ k: a.k * (b.k / a.k) ** s
147
+ };
148
+ }
149
+ // src/viewer.ts
150
+ const whole = (s) => ({
151
+ x: 0,
152
+ y: 0,
153
+ w: s.width,
154
+ h: s.height
155
+ });
156
+ /**
157
+ * The reader's view of a board that pans and zooms: the board in pixels with what floats over it, and where the reader
158
+ * has moved. It comes with the code that pans and zooms, so a page that never zooms does not load it; the engine asks
159
+ * it what part of the scene to show.
160
+ */
161
+ var Viewer = class {
162
+ engine;
163
+ viewport;
164
+ /** The reader's view; null keeps the diagram fitted as the board changes. */
165
+ #view = null;
166
+ constructor(engine, viewport) {
167
+ this.engine = engine;
168
+ this.viewport = viewport;
169
+ }
170
+ /** The board changed size or what floats over it; a reader's view stays, kept in sight. */
171
+ setViewport(vp) {
172
+ this.viewport = vp;
173
+ const fit = this.fit();
174
+ if (this.#view && fit) this.#view = clamp(this.#view, vp, whole(this.engine.frame), range(fit));
175
+ this.#show();
176
+ }
177
+ /** The view Fit gives: the whole diagram in the free part of the board. Null while nothing is drawn. */
178
+ fit() {
179
+ const f = this.engine.frame;
180
+ return f && frame(whole(f), this.viewport);
181
+ }
182
+ /** The reader's own view; null while the diagram stays fitted. */
183
+ get readerView() {
184
+ return this.#view;
185
+ }
186
+ /** What the board shows now, the story's camera included. */
187
+ shownView() {
188
+ const fit = this.fit();
189
+ const c = this.engine.camera;
190
+ if (!fit) return null;
191
+ return c ? shot(c, this.engine.frame, fit, this.viewport) : this.#view ?? fit;
192
+ }
193
+ /** The zoom's range, in board pixels a scene unit. */
194
+ zoomRange() {
195
+ const fit = this.fit();
196
+ return fit && range(fit);
197
+ }
198
+ /**
199
+ * The reader's view, kept in the zoom's range and with the diagram in sight; null fits it again, and keeps it fitted
200
+ * as the board changes. A story playing stops where it is: the reader took the view.
201
+ */
202
+ setView(v) {
203
+ const fit = this.fit();
204
+ if (!fit) return;
205
+ if (this.engine.story) this.engine.stopStory({ cut: true });
206
+ this.#view = v && clamp(v, this.viewport, whole(this.engine.frame), range(fit));
207
+ this.#show();
208
+ }
209
+ /** The part of the scene the board shows now: its viewBox. */
210
+ box() {
211
+ const v = this.shownView();
212
+ return v && shown(v, this.viewport);
213
+ }
214
+ /** Where the board draws the scene now, in its pixels: its scale and offset. */
215
+ placement() {
216
+ const b = this.box();
217
+ return b && placement(b, this.viewport.w, this.viewport.h);
218
+ }
219
+ /** Play starts: the camera the first shot flies from, where the reader had moved; the view is the story's now. */
220
+ lead() {
221
+ const v = this.#view;
222
+ const fit = this.fit();
223
+ this.#view = null;
224
+ return v && fit && !this.engine.reducedMotion ? cameraOf(v, this.engine.frame, fit, this.viewport) : void 0;
225
+ }
226
+ /** Paints the dots on the board's canvas where the SVG draws their places: in the board's pixels, then the canvas's. */
227
+ paint(ctx, canvas) {
228
+ const p = this.placement();
229
+ if (!p) return;
230
+ const sx = canvas.width / this.viewport.w;
231
+ const sy = canvas.height / this.viewport.h;
232
+ ctx.setTransform(p.k * sx, 0, 0, p.k * sy, p.dx * sx, p.dy * sy);
233
+ this.engine.paintDots(ctx);
234
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
235
+ }
236
+ #show() {
237
+ this.engine.applyCamera();
238
+ this.engine.draw();
239
+ this.engine.changed();
240
+ }
241
+ };
242
+ // src/zoom.tsx
243
+ /**
244
+ * Injected with the code that uses it: a board the reader zooms keeps its own shape, the SVG filling it whatever part of
245
+ * the scene it shows; a canvas fills its container, the legend and the buttons floating clear of what the page floats
246
+ * over it; the hand and the zoom buttons.
247
+ */
248
+ const ZOOM_CSS = `
249
+ .lm-board[data-zoom] svg { position: absolute; inset: 0; height: 100%; }
250
+ .lm-wrap[data-canvas] { display: block; height: 100%; }
251
+ .lm-wrap[data-canvas] > .lm-board { height: 100%; border: 0; border-radius: 0; }
252
+ .lm-board[data-zoom] { cursor: grab; user-select: none; -webkit-user-select: none; }
253
+ .lm-board[data-zoom] [role="button"] { cursor: pointer; }
254
+ .lm-board[data-panning], .lm-board[data-panning] [role="button"] { cursor: grabbing; }
255
+ .lm-board[data-zoom]:focus-visible { outline: 2px solid var(--lm-accent); outline-offset: -2px; }
256
+ .lm-zoom { position: absolute; right: 8px; bottom: 8px; z-index: 2; display: flex; gap: 2px; padding: 3px; background: var(--lm-surface); border: 1px solid var(--lm-line); border-radius: 8px; box-shadow: 0 2px 10px rgb(0 0 0 / .12); cursor: auto; }
257
+ .lm-zoom .lm-btn { padding: 4px 10px; border-color: transparent; font-variant-numeric: tabular-nums; }
258
+ .lm-zoom .lm-btn:nth-child(2) { min-width: 4.4em; }
259
+ .lm-zoom [aria-disabled="true"] { opacity: .4; cursor: default; }
260
+ @media (pointer: coarse) { .lm-zoom .lm-btn { min-width: 44px; min-height: 44px; } }
261
+ .lm-wrap[data-canvas] .lm-zoom { right: calc(var(--lm-inset-right) + 12px); bottom: calc(var(--lm-inset-bottom) + 12px); }
262
+ .lm-wrap[data-canvas] > .lm-bar { position: absolute; left: calc(var(--lm-inset-left) + 12px); bottom: calc(var(--lm-inset-bottom) + 12px); z-index: 2; max-width: calc(100% - var(--lm-inset-left) - var(--lm-inset-right) - 260px); padding: 4px 8px; border-radius: 6px; background: color-mix(in srgb, var(--lm-surface) 88%, transparent); pointer-events: none; }
263
+ `;
264
+ function injectZoomStyles(doc) {
265
+ if (doc.getElementById("loadmap-zoom-css")) return;
266
+ const el = doc.createElement("style");
267
+ el.id = "loadmap-zoom-css";
268
+ el.textContent = ZOOM_CSS;
269
+ doc.head.appendChild(el);
270
+ }
271
+ /** Pixels a press may move and still be a click: a finger wobbles more than a mouse. */
272
+ const SLOP = {
273
+ mouse: 4,
274
+ other: 8
275
+ };
276
+ /** What a button or a key zooms by. */
277
+ const ZOOM_STEP = 1.25;
278
+ /** Pixels an arrow key pans, and with Shift. */
279
+ const ARROW = {
280
+ step: 40,
281
+ shift: 200
282
+ };
283
+ /** Seconds a jump from a button or a key takes; reduced motion cuts. */
284
+ const FLIGHT = .2;
285
+ const flights = /* @__PURE__ */ new WeakMap();
286
+ function land(viewer) {
287
+ const raf = flights.get(viewer);
288
+ if (raf != null) cancelAnimationFrame(raf);
289
+ flights.delete(viewer);
290
+ }
291
+ /**
292
+ * Moves the reader's view to `to` (null: fitted). From a button or a key it flies there, on the wall clock (a reader's
293
+ * view is never in a recording); from a drag or the wheel, and with reduced motion, it is there at once.
294
+ */
295
+ function goTo(viewer, to, fly) {
296
+ land(viewer);
297
+ const from = viewer.shownView();
298
+ const target = to ?? viewer.fit();
299
+ if (!fly || viewer.engine.reducedMotion || !from || !target) {
300
+ viewer.setView(to);
301
+ return;
302
+ }
303
+ if (viewer.engine.playing) viewer.setView(from);
304
+ const t0 = performance.now();
305
+ const frame = (now) => {
306
+ if (viewer.engine.playing) {
307
+ flights.delete(viewer);
308
+ return;
309
+ }
310
+ const t = (now - t0) / 1e3 / FLIGHT;
311
+ if (t >= 1) {
312
+ flights.delete(viewer);
313
+ viewer.setView(to);
314
+ return;
315
+ }
316
+ viewer.setView(towards(from, target, t));
317
+ flights.set(viewer, requestAnimationFrame(frame));
318
+ };
319
+ flights.set(viewer, requestAnimationFrame(frame));
320
+ }
321
+ /** Zooms by `factor` about the middle of the free part of the board. */
322
+ function zoomBy(viewer, factor) {
323
+ const v = viewer.shownView();
324
+ const vp = viewer.viewport;
325
+ const r = viewer.zoomRange();
326
+ if (!v || !vp || !r) return;
327
+ const f = free(vp);
328
+ const k = Math.min(r.max, Math.max(r.min, v.k * factor));
329
+ goTo(viewer, zoomAt(v, vp, f.x + f.w / 2, f.y + f.h / 2, k / v.k), true);
330
+ }
331
+ /** The scene at one board pixel a scene unit, about the middle of the free part. */
332
+ function actualSize(viewer) {
333
+ const v = viewer.shownView();
334
+ if (v) zoomBy(viewer, 1 / v.k);
335
+ }
336
+ /**
337
+ * The reader's hands on the board: a drag pans (a press that hardly moves is still a click), a pinch zooms about its
338
+ * middle, and the keys + − 0 and the arrows work while the board or a box has the focus. The wheel zooms about the
339
+ * pointer `always` (a board that fills the page), else only with Ctrl or ⌘ held, or a trackpad's pinch, so a page
340
+ * that scrolls keeps its wheel. Returns a function that lets go.
341
+ */
342
+ function attachZoom(board, viewer, o) {
343
+ board.tabIndex = 0;
344
+ board.setAttribute("role", "group");
345
+ board.setAttribute("aria-label", "Diagram: drag to pan; + and − zoom, 0 fits");
346
+ const points = /* @__PURE__ */ new Map();
347
+ let start = null;
348
+ let panning = false;
349
+ let swallowUntil = -1;
350
+ const at = (e) => {
351
+ const r = board.getBoundingClientRect();
352
+ return {
353
+ x: e.clientX - r.left - board.clientLeft,
354
+ y: e.clientY - r.top - board.clientTop
355
+ };
356
+ };
357
+ const capture = (id) => {
358
+ try {
359
+ board.setPointerCapture(id);
360
+ } catch {}
361
+ };
362
+ const move = (to) => {
363
+ land(viewer);
364
+ viewer.setView(to);
365
+ };
366
+ const touch = () => {
367
+ const action = o.always || viewer.readerView ? "none" : "pan-x pan-y";
368
+ if (board.style.touchAction !== action) board.style.touchAction = action;
369
+ };
370
+ const down = (e) => {
371
+ if (e.button !== 0 && e.button !== 1 || e.target.closest(".lm-zoom")) return;
372
+ if (!viewer.shownView()) return;
373
+ points.set(e.pointerId, at(e));
374
+ if (points.size === 1) {
375
+ start = at(e);
376
+ panning = false;
377
+ }
378
+ if (e.button === 1) e.preventDefault();
379
+ };
380
+ const drag = (e) => {
381
+ const was = points.get(e.pointerId);
382
+ const v = viewer.shownView();
383
+ const vp = viewer.viewport;
384
+ if (!was || !v || !vp) return;
385
+ const p = at(e);
386
+ if (points.size >= 2) {
387
+ const [a, b] = [...points.values()];
388
+ const mid0 = {
389
+ x: (a.x + b.x) / 2,
390
+ y: (a.y + b.y) / 2
391
+ };
392
+ const d0 = Math.hypot(a.x - b.x, a.y - b.y);
393
+ points.set(e.pointerId, p);
394
+ const [c, d] = [...points.values()];
395
+ const mid1 = {
396
+ x: (c.x + d.x) / 2,
397
+ y: (c.y + d.y) / 2
398
+ };
399
+ const d1 = Math.hypot(c.x - d.x, c.y - d.y);
400
+ if (!panning) {
401
+ panning = true;
402
+ for (const id of points.keys()) capture(id);
403
+ }
404
+ if (d0 > 0) move(panBy(zoomAt(v, vp, mid0.x, mid0.y, d1 / d0), mid1.x - mid0.x, mid1.y - mid0.y));
405
+ return;
406
+ }
407
+ points.set(e.pointerId, p);
408
+ if (!panning) {
409
+ const slop = e.pointerType === "mouse" ? SLOP.mouse : SLOP.other;
410
+ if (!start || Math.hypot(p.x - start.x, p.y - start.y) < slop) return;
411
+ panning = true;
412
+ capture(e.pointerId);
413
+ board.setAttribute("data-panning", "");
414
+ move(panBy(v, p.x - start.x, p.y - start.y));
415
+ return;
416
+ }
417
+ move(panBy(v, p.x - was.x, p.y - was.y));
418
+ };
419
+ const up = (e) => {
420
+ if (!points.delete(e.pointerId)) return;
421
+ if (points.size) return;
422
+ if (panning) swallowUntil = e.timeStamp + 300;
423
+ panning = false;
424
+ start = null;
425
+ board.removeAttribute("data-panning");
426
+ };
427
+ const click = (e) => {
428
+ if (e.timeStamp > swallowUntil) return;
429
+ swallowUntil = -1;
430
+ e.stopPropagation();
431
+ e.preventDefault();
432
+ };
433
+ const wheel = (e) => {
434
+ if (!o.always && !e.ctrlKey && !e.metaKey) return;
435
+ const v = viewer.shownView();
436
+ const vp = viewer.viewport;
437
+ if (!v || !vp) return;
438
+ e.preventDefault();
439
+ const dy = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vp.h : 1);
440
+ const pinch = e.ctrlKey && Math.abs(dy) < 50;
441
+ const p = at(e);
442
+ move(zoomAt(v, vp, p.x, p.y, Math.exp(pinch ? -dy * .01 : -Math.max(-100, Math.min(100, dy)) * .002)));
443
+ };
444
+ const key = (e) => {
445
+ if (e.ctrlKey || e.metaKey || e.altKey) return;
446
+ const v = viewer.shownView();
447
+ if (!v) return;
448
+ const step = e.shiftKey ? ARROW.shift : ARROW.step;
449
+ const pan = {
450
+ ArrowLeft: [step, 0],
451
+ ArrowRight: [-step, 0],
452
+ ArrowUp: [0, step],
453
+ ArrowDown: [0, -step]
454
+ }[e.key];
455
+ if (e.key === "+" || e.key === "=") zoomBy(viewer, ZOOM_STEP);
456
+ else if (e.key === "-" || e.key === "_") zoomBy(viewer, 1 / ZOOM_STEP);
457
+ else if (e.key === "0") goTo(viewer, null, true);
458
+ else if (pan) goTo(viewer, panBy(v, pan[0], pan[1]), false);
459
+ else return;
460
+ e.preventDefault();
461
+ };
462
+ const focus = (e) => {
463
+ const t = e.target;
464
+ if (!t.matches?.(":focus-visible")) return;
465
+ const id = t.closest?.("[data-node]")?.getAttribute("data-node");
466
+ const n = id != null && viewer.engine.frame?.nodes.find((x) => x.id === id);
467
+ const v = viewer.shownView();
468
+ if (!n || !v || !viewer.viewport || viewer.engine.playing) return;
469
+ const to = bring(v, viewer.viewport, n);
470
+ if (to) goTo(viewer, to, true);
471
+ };
472
+ touch();
473
+ const off = viewer.engine.subscribe(touch);
474
+ board.addEventListener("pointerdown", down);
475
+ board.addEventListener("pointermove", drag);
476
+ board.addEventListener("pointerup", up);
477
+ board.addEventListener("pointercancel", up);
478
+ board.addEventListener("click", click, true);
479
+ board.addEventListener("wheel", wheel, { passive: false });
480
+ board.addEventListener("keydown", key);
481
+ board.addEventListener("focusin", focus);
482
+ return () => {
483
+ land(viewer);
484
+ off();
485
+ for (const a of [
486
+ "tabindex",
487
+ "role",
488
+ "aria-label"
489
+ ]) board.removeAttribute(a);
490
+ board.style.removeProperty("touch-action");
491
+ board.removeAttribute("data-panning");
492
+ board.removeEventListener("pointerdown", down);
493
+ board.removeEventListener("pointermove", drag);
494
+ board.removeEventListener("pointerup", up);
495
+ board.removeEventListener("pointercancel", up);
496
+ board.removeEventListener("click", click, true);
497
+ board.removeEventListener("wheel", wheel);
498
+ board.removeEventListener("keydown", key);
499
+ board.removeEventListener("focusin", focus);
500
+ };
501
+ }
502
+ const SIDES = [
503
+ "top",
504
+ "right",
505
+ "bottom",
506
+ "left"
507
+ ];
508
+ /**
509
+ * Makes a board pan and zoom: its size in pixels with what floats over it (`shape`, read again on each measure), the
510
+ * reader's view on the engine, and the hands. A canvas leaves the diagram room to breathe and takes the wheel alone.
511
+ * `measure` reads the board again (new insets); `off` lets go, and the board shows what the camera frames.
512
+ */
513
+ function mountZoom(board, engine, shape) {
514
+ injectZoomStyles(board.ownerDocument);
515
+ let off;
516
+ const wrap = board.parentElement;
517
+ const measure = () => {
518
+ const { canvas, inset } = shape();
519
+ if (canvas) for (const s of SIDES) wrap?.style.setProperty(`--lm-inset-${s}`, `${inset[s]}px`);
520
+ const r = board.getBoundingClientRect();
521
+ const w = r.width - 2 * board.clientLeft;
522
+ const h = r.height - 2 * board.clientTop;
523
+ if (!(w > 0 && h > 0)) return;
524
+ const vp = {
525
+ w,
526
+ h,
527
+ inset,
528
+ margin: canvas ? w < 640 ? 16 : 24 : 0,
529
+ fitMax: canvas ? 1.5 : Number.POSITIVE_INFINITY
530
+ };
531
+ if (!engine.viewer) {
532
+ engine.viewer = new Viewer(engine, vp);
533
+ off = attachZoom(board, engine.viewer, { always: canvas });
534
+ }
535
+ engine.viewer.setViewport(vp);
536
+ };
537
+ measure();
538
+ const ro = new ResizeObserver(measure);
539
+ ro.observe(board);
540
+ const corner = board.ownerDocument.createElement("div");
541
+ board.appendChild(corner);
542
+ const buttons = createRoot(corner);
543
+ buttons.render(/* @__PURE__ */ jsx(ZoomControls, { engine }));
544
+ return {
545
+ measure,
546
+ off: () => {
547
+ ro.disconnect();
548
+ off?.();
549
+ queueMicrotask(() => {
550
+ buttons.unmount();
551
+ corner.remove();
552
+ });
553
+ for (const s of SIDES) wrap?.style.removeProperty(`--lm-inset-${s}`);
554
+ engine.viewer = null;
555
+ engine.applyCamera();
556
+ engine.draw();
557
+ }
558
+ };
559
+ }
560
+ /** − · the zoom · + · Fit, over a corner of the board. The zoom shown is a button too: one pixel a scene unit. */
561
+ function ZoomControls({ engine }) {
562
+ useSyncExternalStore(engine.subscribe, () => engine.viewId);
563
+ const viewer = engine.viewer;
564
+ const v = viewer?.shownView();
565
+ const r = viewer?.zoomRange();
566
+ if (!viewer || !v || !r) return null;
567
+ const furthest = v.k <= r.min * 1.001;
568
+ const closest = v.k >= r.max * .999;
569
+ return /* @__PURE__ */ jsxs("div", {
570
+ className: "lm-zoom",
571
+ role: "toolbar",
572
+ "aria-label": "Zoom",
573
+ children: [
574
+ /* @__PURE__ */ jsx("button", {
575
+ type: "button",
576
+ className: "lm-btn",
577
+ "aria-label": "Zoom out",
578
+ "aria-keyshortcuts": "-",
579
+ "aria-disabled": furthest,
580
+ onClick: () => !furthest && zoomBy(viewer, 1 / 1.25),
581
+ children: "−"
582
+ }),
583
+ /* @__PURE__ */ jsxs("button", {
584
+ type: "button",
585
+ className: "lm-btn",
586
+ "aria-label": "Zoom to 100 %",
587
+ onClick: () => actualSize(viewer),
588
+ children: [Math.round(v.k * 100), " %"]
589
+ }),
590
+ /* @__PURE__ */ jsx("button", {
591
+ type: "button",
592
+ className: "lm-btn",
593
+ "aria-label": "Zoom in",
594
+ "aria-keyshortcuts": "+",
595
+ "aria-disabled": closest,
596
+ onClick: () => !closest && zoomBy(viewer, 1.25),
597
+ children: "+"
598
+ }),
599
+ /* @__PURE__ */ jsx("button", {
600
+ type: "button",
601
+ className: "lm-btn",
602
+ "aria-keyshortcuts": "0",
603
+ onClick: () => goTo(viewer, null, true),
604
+ children: "Fit"
605
+ })
606
+ ]
607
+ });
608
+ }
609
+ export { mountZoom };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loadmap/react",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Interactive capacity models for React: an architecture diagram with moving dots, the limits ranked, the verdict, scenarios and headroom.",
5
5
  "keywords": [
6
6
  "loadmap",
@@ -35,10 +35,10 @@
35
35
  "dist"
36
36
  ],
37
37
  "dependencies": {
38
- "@loadmap/core": "0.13.0",
39
- "@loadmap/dsl": "0.13.0",
40
- "@loadmap/sim": "0.13.0",
41
- "@loadmap/layout": "0.13.0",
38
+ "@loadmap/core": "0.15.0",
39
+ "@loadmap/dsl": "0.15.0",
40
+ "@loadmap/sim": "0.15.0",
41
+ "@loadmap/layout": "0.15.0",
42
42
  "elkjs": "^0.12.1"
43
43
  },
44
44
  "peerDependencies": {