@embedpdf/plugin-stage 3.0.0-next.5 → 3.0.0-next.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,8 +1,151 @@
1
1
  import { createCapabilityToken, definePlugin } from "@embedpdf/core";
2
- import { InteractionToken } from "@embedpdf/plugin-interaction";
3
2
  import * as S from "@embedpdf/core-stage";
4
3
  import { ZoomMode, ZoomMode as ZoomMode$1 } from "@embedpdf/core-stage";
5
4
  import { addRotations, applyPoint, applyRect, displaySize, intersectRects, pageGeometry, pageTransform, rotateScaleMatrix, snapToDevice } from "@embedpdf/core-geometry";
5
+ //#region src/motion.ts
6
+ /**
7
+ * The camera's MOTION vocabulary — every formula, pure.
8
+ *
9
+ * The capability owns the impure drivers (scheduler frames, dispatch, gesture
10
+ * state); the LAWS those drivers integrate live here, parameterized by dt/t
11
+ * with no state, no scheduler, no dispatch — unit-testable in isolation, and
12
+ * with zero plugin dependencies, liftable into stage-core if the native port
13
+ * ever wants them.
14
+ *
15
+ * Two families:
16
+ * • ballistic — one per-axis law for every free motion: `glideStep` (the
17
+ * touch fling, decaying on UIScrollView's curve) and `springStep` (a
18
+ * critically-damped return to the clamp). A glide that reaches a content
19
+ * edge converts to a spring with its velocity carried in, so the bounce
20
+ * grows out of physics rather than a scripted overshoot.
21
+ * • resistance — the iOS rubber-band curve `rubberOut` and its exact
22
+ * inverse `rubberIn`: dragging `d` px past an edge displays an
23
+ * asymptotically-bounded stretch, and re-entering a gesture over a
24
+ * stretched camera (the mid-bounce catch) reconstructs the
25
+ * finger-integrated position the curve came from.
26
+ * Plus the tween interpolators: `easeOutCubic` for coordinate tweens and
27
+ * `zoomLerp` — GEOMETRIC zoom interpolation (zoom is multiplicative; equal
28
+ * ratios per unit time, not equal deltas — the linear lerp's visible "dip").
29
+ */
30
+ /** Resistance coefficient of the rubber-band curve (the iOS feel). */
31
+ const RUBBER = .55;
32
+ /** Glide decay per ms — UIScrollView's normal deceleration rate. */
33
+ const FLING_DECAY = .998;
34
+ /** rad/ms — critically damped, ~400ms to settle. */
35
+ const SPRING_OMEGA = .015;
36
+ /**
37
+ * Overshoot distance → displayed stretch: asymptotic to the viewport
38
+ * dimension `dim`, so the content never leaves the screen however far the
39
+ * finger travels. `rubberOut(0) === 0` — inside the bounds the curve is
40
+ * invisible.
41
+ */
42
+ const rubberOut = (d, dim) => (1 - 1 / (d * RUBBER / dim + 1)) * dim;
43
+ /** Exact inverse of `rubberOut` (defined for `out < dim`). */
44
+ const rubberIn = (out, dim) => out * dim / (RUBBER * (dim - out));
45
+ /**
46
+ * One frame of free glide: exponential velocity decay, position advanced by
47
+ * the midpoint integral of the decay over `dt` (exact for the exponential —
48
+ * frame-rate independent).
49
+ */
50
+ const glideStep = (p, v, dt) => {
51
+ const k = Math.pow(FLING_DECAY, dt);
52
+ const np = p + v * (dt * (1 + k) / 2);
53
+ const nv = v * k;
54
+ return Math.abs(nv) < .02 ? {
55
+ p: np,
56
+ v: 0,
57
+ done: true
58
+ } : {
59
+ p: np,
60
+ v: nv,
61
+ done: false
62
+ };
63
+ };
64
+ /**
65
+ * One frame of critically-damped spring toward `edge` — semi-implicit Euler
66
+ * on x'' = −ω²(x−e) − 2ωx'. Lands exactly ON the edge (position snapped,
67
+ * velocity zeroed) once inside half a pixel at negligible speed.
68
+ */
69
+ const springStep = (p, v, edge, dt) => {
70
+ const nv = v + (-.015 * SPRING_OMEGA * (p - edge) - 2 * SPRING_OMEGA * v) * dt;
71
+ const np = p + nv * dt;
72
+ if (Math.abs(np - edge) < .5 && Math.abs(nv) < .01) return {
73
+ p: edge,
74
+ v: 0,
75
+ done: true
76
+ };
77
+ return {
78
+ p: np,
79
+ v: nv,
80
+ done: false
81
+ };
82
+ };
83
+ const easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
84
+ /**
85
+ * Geometric zoom interpolation: `z0·(z1/z0)^k`. Zoom is multiplicative, so a
86
+ * constant-RATE zoom moves equal ratios per unit time; interpolating the
87
+ * level linearly instead makes the start rush and the end crawl (and, under
88
+ * a focal anchor, swings the anchored point along a curved path).
89
+ */
90
+ const zoomLerp = (z0, z1, k) => z0 * Math.pow(z1 / z0, k);
91
+ //#endregion
92
+ //#region src/responsive.ts
93
+ /**
94
+ * Container queries for the settings bag — the pure half. The capability owns
95
+ * the driver (when to re-resolve, how to react); the LAWS live here: what a
96
+ * box is, when a query matches, and what the effective settings are. No state,
97
+ * no dispatch — property-testable in isolation (the `wheel.ts` convention).
98
+ *
99
+ * The query vocabulary is deliberately just the box: a headless, DOM-free
100
+ * stage knows exactly one environmental fact — the viewport it was told.
101
+ * Anything else (pointer coarseness, platform, app state) is either per-event
102
+ * (`PointerSample.pointerType`) or the app's own business via `update()`.
103
+ */
104
+ /** The reported viewport as a queryable box. A square box is 'portrait' (CSS). */
105
+ const boxOf = (vp) => ({
106
+ width: vp.width,
107
+ height: vp.height,
108
+ orientation: vp.height >= vp.width ? "portrait" : "landscape"
109
+ });
110
+ /** All bounds inclusive, all fields optional, conditions AND-ed (CSS ranges). */
111
+ const matchesQuery = (q, box) => (q.minWidth === void 0 || box.width >= q.minWidth) && (q.maxWidth === void 0 || box.width <= q.maxWidth) && (q.minHeight === void 0 || box.height >= q.minHeight) && (q.maxHeight === void 0 || box.height <= q.maxHeight) && (q.orientation === void 0 || box.orientation === q.orientation);
112
+ const ruleMatches = (rule, box) => typeof rule.when === "function" ? !!rule.when(box) : matchesQuery(rule.when, box);
113
+ /** Merge a patch over settings, skipping undefined values (Partial semantics). */
114
+ const mergeSettings = (into, patch) => {
115
+ const out = { ...into };
116
+ let key;
117
+ for (key in patch) {
118
+ const value = patch[key];
119
+ if (value !== void 0) Object.assign(out, { [key]: value });
120
+ }
121
+ return out;
122
+ };
123
+ /**
124
+ * Setting-value equality: primitives by identity, the flat objects the
125
+ * settings vocabulary uses (`{ px }`, `{ x, y }`, page frames, zoom specs) by
126
+ * one level of own-key comparison. Settings values are never nested deeper.
127
+ */
128
+ const eqSetting = (a, b) => {
129
+ if (a === b) return true;
130
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
131
+ const ka = Object.keys(a);
132
+ const kb = Object.keys(b);
133
+ return ka.length === kb.length && ka.every((k) => a[k] === b[k]);
134
+ };
135
+ const resolveResponsive = (base, rules, box) => {
136
+ let effective = base;
137
+ const active = [];
138
+ for (const rule of rules) {
139
+ if (!ruleMatches(rule, box)) continue;
140
+ if (rule.name !== void 0) active.push(rule.name);
141
+ if (rule.settings) effective = mergeSettings(effective, rule.settings);
142
+ }
143
+ return {
144
+ effective,
145
+ active
146
+ };
147
+ };
148
+ //#endregion
6
149
  //#region src/settings.ts
7
150
  /**
8
151
  * Out-of-the-box defaults — a sensible document-reading feel. They are JUST
@@ -48,6 +191,18 @@ const DEFAULT_SETTINGS = {
48
191
  scrollBehavior: "smooth",
49
192
  viewUnitsPerPoint: 96 / 72
50
193
  };
194
+ /**
195
+ * Out-of-the-box responsive rules — the platform feel with zero configuration:
196
+ * a compact container gets the thin gutter phones use (space, not device — an
197
+ * embedded 500px pane on a desktop is compact too). Override the whole list
198
+ * with `stagePlugin({ responsive: [...] })`; `responsive: []` opts out. The
199
+ * 'compact' name is queryable app-side via `stage.matches('compact')`.
200
+ */
201
+ const DEFAULT_RESPONSIVE = [{
202
+ name: "compact",
203
+ when: { maxWidth: 600 },
204
+ settings: { padding: 4 }
205
+ }];
51
206
  const SETTINGS_EFFECT = {
52
207
  flow: "reflow",
53
208
  layout: "scene",
@@ -86,8 +241,9 @@ const settingsEqual = (a, b) => SETTING_KEYS.every((k) => valueEq(a[k], b[k]));
86
241
  * Layering, stated honestly:
87
242
  * • stage-core — PURE spatial math (no DOM, no time). Ports to Rust later.
88
243
  * • this capability — the IMPURE platform shell. It dispatches, caches, and owns
89
- * the camera tween. It is NOT pure; the one host dependency it has (frame
90
- * timing) enters through an injected Scheduler, never a hidden global.
244
+ * the camera tween. It is NOT pure; its one host dependency (frame timing)
245
+ * enters through the Scheduler seam — injectable, defaulting to the host's
246
+ * own frame clock when one exists (see the seam below).
91
247
  *
92
248
  * The model: EVERY camera move is defined by what it holds fixed.
93
249
  * • gestures (pan/pinch/wheel) — the content under the pointer. Physics, no setting.
@@ -373,12 +529,100 @@ function createStageCapability(ctx, config = {}) {
373
529
  fitAlign: ctx.getState().fitAlign,
374
530
  direction: ctx.getState().direction
375
531
  });
532
+ let hasPlaced = false;
533
+ let gestureDepth = 0;
534
+ let gestureZoomed = false;
535
+ let gestureElastic = false;
536
+ let gestureRaw = null;
537
+ const REST_MS = 150;
538
+ let restRaf = 0;
539
+ const armRest = () => {
540
+ if (!canAnimate || !hasPlaced) return;
541
+ if (ctx.getState().cameraResting) ctx.dispatch({
542
+ type: "CAMERA_REST",
543
+ resting: false
544
+ });
545
+ if (restRaf) scheduler.caf(restRaf);
546
+ let t0 = 0;
547
+ const tick = (ts) => {
548
+ restRaf = 0;
549
+ if (!t0) t0 = ts;
550
+ if (ts - t0 >= REST_MS) {
551
+ ctx.dispatch({
552
+ type: "CAMERA_REST",
553
+ resting: true
554
+ });
555
+ return;
556
+ }
557
+ restRaf = scheduler.raf(tick);
558
+ };
559
+ restRaf = scheduler.raf(tick);
560
+ };
376
561
  const setCam = (next, bounds = stayBounds()) => {
562
+ const clamped = S.clampCamera(next, bounds, vp(), constraint());
563
+ if (clamped.zoom !== cam().zoom) if (gestureDepth > 0) {
564
+ gestureZoomed = true;
565
+ if (ctx.getState().cameraResting) ctx.dispatch({
566
+ type: "CAMERA_REST",
567
+ resting: false
568
+ });
569
+ if (restRaf) {
570
+ scheduler.caf(restRaf);
571
+ restRaf = 0;
572
+ }
573
+ } else armRest();
377
574
  ctx.dispatch({
378
575
  type: "CAMERA",
379
- camera: S.clampCamera(next, bounds, vp(), constraint())
576
+ camera: clamped
380
577
  });
381
578
  };
579
+ const axisTravels = (origin, content, view, zoom) => !S.travelRange(origin, content, view, zoom, constraint().padding).fits;
580
+ /** Unclamped finger-integrated camera → the DISPLAYED camera: clamp, then
581
+ * re-apply the overshoot through the resistance curve, per travelling axis.
582
+ * Inside the bounds this is exactly the clamp (rubber of zero is zero);
583
+ * on a fitting axis it is exactly the clamp ALWAYS. */
584
+ const rubberize = (raw) => {
585
+ const clamped = S.clampCamera(raw, stayBounds(), vp(), constraint());
586
+ const v = vp();
587
+ const b = stayBounds();
588
+ const axis = (rawA, clampedA, dim, travels) => {
589
+ if (!travels) return clampedA;
590
+ const dWorld = rawA - clampedA;
591
+ if (dWorld === 0) return clampedA;
592
+ const out = rubberOut(Math.abs(dWorld) * raw.zoom, Math.max(1, dim));
593
+ return clampedA + Math.sign(dWorld) * out / raw.zoom;
594
+ };
595
+ return {
596
+ zoom: raw.zoom,
597
+ x: axis(raw.x, clamped.x, v.width, axisTravels(b.x, b.width, v.width, raw.zoom)),
598
+ y: axis(raw.y, clamped.y, v.height, axisTravels(b.y, b.height, v.height, raw.zoom))
599
+ };
600
+ };
601
+ /** Displayed camera → the raw position `rubberize` would have produced it
602
+ * from. Overshoot is capped just under the asymptote so the inverse stays
603
+ * finite whatever state a catch finds the camera in. */
604
+ const unrubberize = (displayed) => {
605
+ const clamped = S.clampCamera(displayed, stayBounds(), vp(), constraint());
606
+ const v = vp();
607
+ const b = stayBounds();
608
+ const axis = (dispA, clampedA, dim, travels) => {
609
+ if (!travels) return clampedA;
610
+ const dWorld = dispA - clampedA;
611
+ if (dWorld === 0) return clampedA;
612
+ const d = Math.max(1, dim);
613
+ const out = Math.min(Math.abs(dWorld) * displayed.zoom, d - 1);
614
+ return clampedA + Math.sign(dWorld) * rubberIn(out, d) / displayed.zoom;
615
+ };
616
+ return {
617
+ zoom: displayed.zoom,
618
+ x: axis(displayed.x, clamped.x, v.width, axisTravels(b.x, b.width, v.width, displayed.zoom)),
619
+ y: axis(displayed.y, clamped.y, v.height, axisTravels(b.y, b.height, v.height, displayed.zoom))
620
+ };
621
+ };
622
+ const setCamRaw = (c) => ctx.dispatch({
623
+ type: "CAMERA",
624
+ camera: c
625
+ });
382
626
  /**
383
627
  * Cursor reconciliation, one direction per interaction:
384
628
  * navigation → cursor is INTENT, set explicitly; the camera honors it as far
@@ -412,7 +656,6 @@ function createStageCapability(ctx, config = {}) {
412
656
  }
413
657
  cancelAnim();
414
658
  const from = cam();
415
- const ease = (t) => 1 - Math.pow(1 - t, 3);
416
659
  let started = false;
417
660
  let t0 = 0;
418
661
  const tick = (now) => {
@@ -420,7 +663,7 @@ function createStageCapability(ctx, config = {}) {
420
663
  started = true;
421
664
  t0 = now;
422
665
  }
423
- const k = ease(Math.min(1, (now - t0) / ms));
666
+ const k = easeOutCubic(Math.min(1, (now - t0) / ms));
424
667
  setCam({
425
668
  x: lerp(from.x, target.x, k),
426
669
  y: lerp(from.y, target.y, k),
@@ -434,6 +677,116 @@ function createStageCapability(ctx, config = {}) {
434
677
  };
435
678
  raf = scheduler.raf(tick);
436
679
  };
680
+ /**
681
+ * Zoom-anchored tween — animates THE INVARIANT, not the coordinates. A
682
+ * camera move is defined by what it holds fixed; for an anchored zoom that
683
+ * is "the focal page-point stays at `pt` while scale changes". Lerping
684
+ * x/y/zoom independently (the plain tween) breaks that mid-flight: holding
685
+ * a screen point requires `x(t) = focal.x − pt.x / zoom(t)` — hyperbolic in
686
+ * the zoom — so linear coordinates swing the tapped point along a curved
687
+ * path (the visible "dip"). Here the zoom interpolates GEOMETRICALLY
688
+ * (constant rate — zoom is multiplicative) and each frame's camera derives
689
+ * from the anchor at that zoom, then clamps: the focal point is stationary
690
+ * by construction, and a zoom-out rails into its fit progressively as the
691
+ * shrinking travel range lets the clamp take over.
692
+ */
693
+ const animateZoomAnchored = (focal, pt, targetZoom, ms = 240, then) => {
694
+ if (!canAnimate) {
695
+ const sc = buildScene();
696
+ if (sc.itemCount) setCam(S.cameraForAnchorAtScreen(focal, sc, pt, targetZoom));
697
+ then?.();
698
+ return;
699
+ }
700
+ cancelAnim();
701
+ const z0 = cam().zoom;
702
+ let started = false;
703
+ let t0 = 0;
704
+ const tick = (now) => {
705
+ if (!started) {
706
+ started = true;
707
+ t0 = now;
708
+ }
709
+ const k = easeOutCubic(Math.min(1, (now - t0) / ms));
710
+ const z = zoomLerp(z0, targetZoom, k);
711
+ const sc = buildScene();
712
+ if (sc.itemCount) setCam(S.cameraForAnchorAtScreen(focal, sc, pt, z));
713
+ if (k < 1) raf = scheduler.raf(tick);
714
+ else {
715
+ raf = 0;
716
+ then?.();
717
+ }
718
+ };
719
+ raf = scheduler.raf(tick);
720
+ };
721
+ const startCameraPhysics = (vxFinger, vyFinger) => {
722
+ if (!canAnimate) {
723
+ setCam(cam());
724
+ return;
725
+ }
726
+ cancelAnim();
727
+ const zoom = cam().zoom;
728
+ const c0 = cam();
729
+ const cl0 = S.clampCamera(c0, stayBounds(), vp(), constraint());
730
+ const b0 = stayBounds();
731
+ const v0 = vp();
732
+ const mk = (camA, clampedA, fingerV) => ({
733
+ p: camA * zoom,
734
+ v: -fingerV / 1e3,
735
+ springing: camA !== clampedA,
736
+ done: false
737
+ });
738
+ const ax = mk(c0.x, cl0.x, axisTravels(b0.x, b0.width, v0.width, zoom) ? vxFinger : 0);
739
+ const ay = mk(c0.y, cl0.y, axisTravels(b0.y, b0.height, v0.height, zoom) ? vyFinger : 0);
740
+ if (!ax.springing && !ay.springing && Math.hypot(ax.v, ay.v) < .02) return;
741
+ let started = false;
742
+ let last = 0;
743
+ const tick = (now) => {
744
+ raf = 0;
745
+ const dt = started ? Math.min(64, Math.max(1, now - last)) : 16;
746
+ started = true;
747
+ last = now;
748
+ const cl = S.clampCamera({
749
+ x: ax.p / zoom,
750
+ y: ay.p / zoom,
751
+ zoom
752
+ }, stayBounds(), vp(), constraint());
753
+ const step = (a, edgeWorld) => {
754
+ if (a.done) return;
755
+ const r = a.springing ? springStep(a.p, a.v, edgeWorld * zoom, dt) : glideStep(a.p, a.v, dt);
756
+ a.p = r.p;
757
+ a.v = r.v;
758
+ a.done = r.done;
759
+ };
760
+ step(ax, cl.x);
761
+ step(ay, cl.y);
762
+ const ncl = S.clampCamera({
763
+ x: ax.p / zoom,
764
+ y: ay.p / zoom,
765
+ zoom
766
+ }, stayBounds(), vp(), constraint());
767
+ const convert = (a, clampedA) => {
768
+ if (!a.springing && Math.abs(clampedA * zoom - a.p) > 1e-6) {
769
+ a.p = clampedA * zoom;
770
+ a.springing = true;
771
+ a.done = false;
772
+ }
773
+ };
774
+ convert(ax, ncl.x);
775
+ convert(ay, ncl.y);
776
+ setCamRaw({
777
+ x: ax.p / zoom,
778
+ y: ay.p / zoom,
779
+ zoom
780
+ });
781
+ syncCursorFromCamera();
782
+ if (ax.done && ay.done) {
783
+ setCam(cam());
784
+ return;
785
+ }
786
+ raf = scheduler.raf(tick);
787
+ };
788
+ raf = scheduler.raf(tick);
789
+ };
437
790
  const anchorAt = (at) => {
438
791
  const sc = buildScene();
439
792
  return sc.itemCount ? S.anchorFromCamera(cam(), sc, vp(), at) : {
@@ -561,8 +914,11 @@ function createStageCapability(ctx, config = {}) {
561
914
  } : unrotatedPoints(box);
562
915
  const c = cam();
563
916
  const ratio = dpr();
564
- const screenX = snapToDevice((box.x - c.x) * c.zoom, ratio);
565
- const screenY = snapToDevice((box.y - c.y) * c.zoom, ratio);
917
+ const rawX = (box.x - c.x) * c.zoom;
918
+ const rawY = (box.y - c.y) * c.zoom;
919
+ const resting = ctx.getState().cameraResting;
920
+ const screenX = resting ? snapToDevice(rawX, ratio) : rawX;
921
+ const screenY = resting ? snapToDevice(rawY, ratio) : rawY;
566
922
  const transform = pageTransform({
567
923
  pageSize,
568
924
  rotation: box.rotation,
@@ -618,7 +974,7 @@ function createStageCapability(ctx, config = {}) {
618
974
  const v = vp();
619
975
  const sc = buildScene();
620
976
  const items = paged() ? sc.items.slice(0, 1) : sc.query(S.cameraWorldRect(c, v));
621
- const sig = `${sceneCache.key}|${ctx.getState().flow}|${c.x},${c.y},${c.zoom}|${v.width}x${v.height}|${dpr()}`;
977
+ const sig = `${sceneCache.key}|${ctx.getState().flow}|${c.x},${c.y},${c.zoom}|${v.width}x${v.height}|${dpr()}|r${ctx.getState().cameraResting ? 1 : 0}`;
622
978
  if (sig === visSig) return vis;
623
979
  visSig = sig;
624
980
  vis = items.flatMap((it) => it.pages).map(withTransform);
@@ -630,8 +986,73 @@ function createStageCapability(ctx, config = {}) {
630
986
  return out;
631
987
  };
632
988
  const snapshotSettings = () => pickSettings(ctx.getState());
989
+ /**
990
+ * Settings change, reacted per the registry — the strongest effect among the
991
+ * touched settings wins: 'reflow' ⊃ 'scene'/'refit' ⊃ 'reclamp' ⊃ 'none'.
992
+ * The ONE reactive applier: the public `update()` and the responsive driver
993
+ * both land here, so a breakpoint crossing gets exactly the invariant a
994
+ * hand-written update would.
995
+ */
996
+ const applySettings = (patch) => {
997
+ cancelAnim();
998
+ const touched = (effect) => SETTING_KEYS.some((k) => patch[k] !== void 0 && SETTINGS_EFFECT[k] === effect);
999
+ const align = touched("refit") && !touched("scene") && !touched("reflow") ? ctx.getState().zoomAlign : ctx.getState().anchorAlign;
1000
+ const anchor = anchorAt(alignPoint(align, vp()));
1001
+ ctx.dispatch({
1002
+ type: "PATCH",
1003
+ patch
1004
+ });
1005
+ if (touched("scene")) sceneCache = null;
1006
+ if (touched("reflow")) goToTarget(ctx.getState().cursor, { behavior: "instant" });
1007
+ else if (touched("scene") || touched("refit")) reapply(anchor, align);
1008
+ else if (touched("reclamp")) setCam(cam());
1009
+ };
1010
+ let base = snapshotSettings();
1011
+ let rules = config.responsive ?? DEFAULT_RESPONSIVE;
1012
+ let lastEffective = base;
1013
+ const publishActive = (active) => {
1014
+ const prev = ctx.getState().activeRules;
1015
+ if (prev.length !== active.length || active.some((n, i) => n !== prev[i])) ctx.dispatch({
1016
+ type: "RESPONSIVE",
1017
+ active
1018
+ });
1019
+ };
1020
+ /**
1021
+ * Re-resolve and apply what CHANGED since the last resolution. `react: true`
1022
+ * routes the patch through `applySettings` (full anchor-preserving update);
1023
+ * `react: false` (inside `setViewport`, whose caller runs its own reframe)
1024
+ * just lands the patch + cache invalidation and reports the strongest
1025
+ * effect so the caller can escalate a rule-driven flow flip.
1026
+ */
1027
+ const syncResponsive = (react) => {
1028
+ const { effective, active } = resolveResponsive(base, rules, boxOf(vp()));
1029
+ const patch = {};
1030
+ for (const k of SETTING_KEYS) if (!eqSetting(effective[k], lastEffective[k])) Object.assign(patch, { [k]: effective[k] });
1031
+ lastEffective = effective;
1032
+ publishActive(active);
1033
+ const keys = Object.keys(patch);
1034
+ if (keys.length === 0) return "none";
1035
+ if (react) {
1036
+ applySettings(patch);
1037
+ return "none";
1038
+ }
1039
+ ctx.dispatch({
1040
+ type: "PATCH",
1041
+ patch
1042
+ });
1043
+ const rank = {
1044
+ none: 0,
1045
+ reclamp: 1,
1046
+ refit: 2,
1047
+ scene: 3,
1048
+ reflow: 4
1049
+ };
1050
+ let strongest = "none";
1051
+ for (const k of keys) if (rank[SETTINGS_EFFECT[k]] > rank[strongest]) strongest = SETTINGS_EFFECT[k];
1052
+ if (strongest === "scene" || strongest === "reflow") sceneCache = null;
1053
+ return strongest;
1054
+ };
633
1055
  const initialViewProviders = [];
634
- let hasPlaced = false;
635
1056
  const api = {
636
1057
  camera: cam,
637
1058
  viewport: vp,
@@ -754,6 +1175,7 @@ function createStageCapability(ctx, config = {}) {
754
1175
  type: "VP",
755
1176
  vp: v
756
1177
  });
1178
+ syncResponsive(false);
757
1179
  if (v.width > 0 && v.height > 0 && (ctx.document()?.pageCount ?? 0) > 0) api.placeInitial();
758
1180
  return;
759
1181
  }
@@ -763,7 +1185,8 @@ function createStageCapability(ctx, config = {}) {
763
1185
  type: "VP",
764
1186
  vp: v
765
1187
  });
766
- reapply(anchor);
1188
+ if (syncResponsive(false) === "reflow") goToTarget(ctx.getState().cursor, { behavior: "instant" });
1189
+ else reapply(anchor);
767
1190
  },
768
1191
  setDevicePixelRatio: (ratio) => {
769
1192
  if (ratio > 0 && ratio !== dpr()) ctx.dispatch({
@@ -778,7 +1201,10 @@ function createStageCapability(ctx, config = {}) {
778
1201
  },
779
1202
  panBy: (dx, dy) => {
780
1203
  cancelAnim();
781
- setCam(S.panByScreen(cam(), dx, dy));
1204
+ if (gestureDepth > 0 && gestureElastic) {
1205
+ gestureRaw = S.panByScreen(gestureRaw ?? unrubberize(cam()), dx, dy);
1206
+ setCamRaw(rubberize(gestureRaw));
1207
+ } else setCam(S.panByScreen(cam(), dx, dy));
782
1208
  syncCursorFromCamera();
783
1209
  },
784
1210
  scrollTo: (opts) => {
@@ -804,13 +1230,71 @@ function createStageCapability(ctx, config = {}) {
804
1230
  const before = buildScene();
805
1231
  const focal = before.itemCount ? S.anchorAtPoint(before, S.toWorld(cam(), pt)) : null;
806
1232
  setCam(S.zoomAround(cam(), pt, factor));
807
- ctx.dispatch({
1233
+ if (gestureDepth === 0) ctx.dispatch({
808
1234
  type: "PATCH",
809
1235
  patch: { zoom: { level: cam().zoom } }
810
1236
  });
811
1237
  const after = buildScene();
812
1238
  if (after !== before && focal && after.itemCount) setCam(S.cameraForAnchorAtScreen(focal, after, pt, cam().zoom));
1239
+ if (gestureDepth > 0 && gestureElastic) gestureRaw = unrubberize(cam());
1240
+ syncCursorFromCamera();
1241
+ },
1242
+ beginGesture: (options) => {
1243
+ gestureDepth++;
1244
+ if (gestureDepth === 1) {
1245
+ cancelAnim();
1246
+ gestureZoomed = false;
1247
+ gestureElastic = options?.elastic === true;
1248
+ gestureRaw = null;
1249
+ }
1250
+ },
1251
+ endGesture: () => {
1252
+ if (gestureDepth === 0) return;
1253
+ gestureDepth--;
1254
+ if (gestureDepth > 0) return;
1255
+ gestureRaw = null;
1256
+ const wasElastic = gestureElastic;
1257
+ gestureElastic = false;
1258
+ if (gestureZoomed) {
1259
+ gestureZoomed = false;
1260
+ ctx.dispatch({
1261
+ type: "PATCH",
1262
+ patch: { zoom: { level: cam().zoom } }
1263
+ });
1264
+ armRest();
1265
+ }
813
1266
  syncCursorFromCamera();
1267
+ if (wasElastic) {
1268
+ const c = cam();
1269
+ const cl = S.clampCamera(c, stayBounds(), vp(), constraint());
1270
+ if (cl.x !== c.x || cl.y !== c.y) startCameraPhysics(0, 0);
1271
+ }
1272
+ },
1273
+ fling: (vx, vy) => startCameraPhysics(vx, vy),
1274
+ cameraInMotion: () => raf !== 0,
1275
+ doubleTapZoom: (pt) => {
1276
+ cancelAnim();
1277
+ const sc0 = buildScene();
1278
+ if (!sc0.itemCount) return;
1279
+ const item = paged() ? sc0.items[0] : sc0.items[itemIndexOfPage(ctx.getState().cursor)];
1280
+ const fit = fitBox(item);
1281
+ const auto = S.resolveZoom({ mode: S.ZoomMode.Automatic }, fit, vp(), pad());
1282
+ const stops = [
1283
+ auto,
1284
+ S.resolveZoom({ mode: S.ZoomMode.FitWidth }, fit, vp(), pad()),
1285
+ Math.min(auto * 2.5, S.ZOOM_MAX)
1286
+ ].sort((a, b) => a - b).filter((z, i, all) => i === 0 || z > all[i - 1] * 1.1);
1287
+ const near = (z, stop) => z > stop / 1.1 && z < stop * 1.1;
1288
+ const onRung = stops.findIndex((s) => near(cam().zoom, s));
1289
+ const target = onRung >= 0 ? stops[(onRung + 1) % stops.length] : stops[0];
1290
+ ctx.dispatch({
1291
+ type: "PATCH",
1292
+ patch: { zoom: { level: target } }
1293
+ });
1294
+ const sc = buildScene();
1295
+ if (!sc.itemCount) return;
1296
+ const focal = S.anchorAtPoint(sc, S.toWorld(cam(), pt));
1297
+ animateZoomAnchored(focal, pt, target, 240, syncCursorFromCamera);
814
1298
  },
815
1299
  zoomIn: () => api.zoomAround(alignPoint(ctx.getState().zoomAlign, vp()), 1.2),
816
1300
  zoomOut: () => api.zoomAround(alignPoint(ctx.getState().zoomAlign, vp()), 1 / 1.2),
@@ -895,20 +1379,17 @@ function createStageCapability(ctx, config = {}) {
895
1379
  },
896
1380
  next: (opts) => step(1, opts),
897
1381
  prev: (opts) => step(-1, opts),
1382
+ lensId: () => ctx.id,
898
1383
  update: (patch) => {
899
- cancelAnim();
900
- const touched = (effect) => SETTING_KEYS.some((k) => patch[k] !== void 0 && SETTINGS_EFFECT[k] === effect);
901
- const align = touched("refit") && !touched("scene") && !touched("reflow") ? ctx.getState().zoomAlign : ctx.getState().anchorAlign;
902
- const anchor = anchorAt(alignPoint(align, vp()));
903
- ctx.dispatch({
904
- type: "PATCH",
905
- patch
906
- });
907
- if (touched("scene")) sceneCache = null;
908
- if (touched("reflow")) goToTarget(ctx.getState().cursor, { behavior: "instant" });
909
- else if (touched("scene") || touched("refit")) reapply(anchor, align);
910
- else if (touched("reclamp")) setCam(cam());
1384
+ base = mergeSettings(base, patch);
1385
+ syncResponsive(true);
911
1386
  },
1387
+ setResponsive: (next) => {
1388
+ rules = next;
1389
+ syncResponsive(true);
1390
+ },
1391
+ matches: (name) => ctx.getState().activeRules.includes(name),
1392
+ activeRules: () => ctx.getState().activeRules,
912
1393
  setFlow: (flow) => api.update({ flow }),
913
1394
  setLayout: (layout) => api.update({ layout }),
914
1395
  setSpread: (spread) => api.update({ spread }),
@@ -928,9 +1409,13 @@ function createStageCapability(ctx, config = {}) {
928
1409
  setScrollBehavior: (behavior) => api.update({ scrollBehavior: behavior }),
929
1410
  applyViewState: (view) => {
930
1411
  cancelAnim();
1412
+ base = mergeSettings(base, pickSettings(view));
1413
+ const { effective, active } = resolveResponsive(base, rules, boxOf(vp()));
1414
+ lastEffective = effective;
1415
+ publishActive(active);
931
1416
  ctx.dispatch({
932
1417
  type: "PATCH",
933
- patch: pickSettings(view)
1418
+ patch: effective
934
1419
  });
935
1420
  ctx.dispatch({
936
1421
  type: "CURSOR",
@@ -962,46 +1447,6 @@ function createStageCapability(ctx, config = {}) {
962
1447
  return api;
963
1448
  }
964
1449
  //#endregion
965
- //#region src/scroll-handler.ts
966
- /**
967
- * Pan-the-camera as an interaction handler. Live under the built-in `pan` tool
968
- * (the `scroll` tag) and — with `panFallback` — under every tool, where it then
969
- * captures ONLY page-gap drags (on-page downs belong to the active tool, which
970
- * claims them first at higher priority). It uses the sample's viewport delta, so
971
- * it pans over the whole viewport (pages and gaps).
972
- *
973
- * Cursor feel: a closed hand (`grabbing`) while dragging; an open hand (`grab`)
974
- * hovering a gap under a non-pan tool. The pan tool's own resting `grab` comes
975
- * from its `Tool.cursor`, so hovering it needs no claim here.
976
- */
977
- function createScrollHandler(stage, interaction, options = {}) {
978
- const panFallback = options.panFallback ?? true;
979
- let last = {
980
- x: 0,
981
- y: 0
982
- };
983
- const isPanTool = () => interaction.activeTool().enables.has("scroll");
984
- return {
985
- id: "stage-scroll",
986
- priority: 10,
987
- enabledFor: (tool) => tool.enables.has("scroll") || panFallback,
988
- onDown: (s) => {
989
- if (!isPanTool() && s.page) return false;
990
- last = s.viewport;
991
- interaction.setCursor("stage-grab", "grabbing", 40);
992
- return true;
993
- },
994
- onMove: (s) => {
995
- stage.panBy(s.viewport.x - last.x, s.viewport.y - last.y);
996
- last = s.viewport;
997
- },
998
- onUp: () => interaction.setCursor("stage-grab", null),
999
- onHover: (s) => {
1000
- if (panFallback && !isPanTool()) interaction.setCursor("stage-pan-fallback", s.page ? null : "grab", 5);
1001
- }
1002
- };
1003
- }
1004
- //#endregion
1005
1450
  //#region src/effects.ts
1006
1451
  /**
1007
1452
  * The one effect a stage lens needs: when the document's page registry changes
@@ -1024,19 +1469,21 @@ function registerStageEffects(ctx, token) {
1024
1469
  //#endregion
1025
1470
  //#region src/reducer.ts
1026
1471
  const initialStageState = (config) => {
1027
- const { scheduler: _scheduler, ...overrides } = config;
1472
+ const { scheduler: _scheduler, responsive: _responsive, ...overrides } = config;
1028
1473
  return {
1029
1474
  camera: {
1030
1475
  x: 0,
1031
1476
  y: 0,
1032
1477
  zoom: 1
1033
1478
  },
1479
+ cameraResting: true,
1034
1480
  vp: {
1035
1481
  width: 0,
1036
1482
  height: 0
1037
1483
  },
1038
1484
  dpr: 1,
1039
1485
  cursor: 0,
1486
+ activeRules: [],
1040
1487
  ...DEFAULT_SETTINGS,
1041
1488
  ...overrides
1042
1489
  };
@@ -1062,6 +1509,10 @@ const stageReducer = (state, a) => {
1062
1509
  ...state,
1063
1510
  camera: a.camera
1064
1511
  };
1512
+ case "CAMERA_REST": return state.cameraResting === a.resting ? state : {
1513
+ ...state,
1514
+ cameraResting: a.resting
1515
+ };
1065
1516
  case "VP": return {
1066
1517
  ...state,
1067
1518
  vp: a.vp
@@ -1075,6 +1526,10 @@ const stageReducer = (state, a) => {
1075
1526
  cursor: a.cursor
1076
1527
  };
1077
1528
  case "PATCH": return applyPatch(state, a.patch);
1529
+ case "RESPONSIVE": return {
1530
+ ...state,
1531
+ activeRules: a.active
1532
+ };
1078
1533
  default: return state;
1079
1534
  }
1080
1535
  };
@@ -1089,19 +1544,14 @@ const StageToken = createCapabilityToken("stage");
1089
1544
  * sibling files.
1090
1545
  */
1091
1546
  const stagePlugin = (options = {}) => {
1092
- const { id = "stage", token = StageToken, interaction = false, panFallback = true, ...config } = options;
1547
+ const { id = "stage", token = StageToken, ...config } = options;
1093
1548
  return definePlugin({
1094
1549
  id,
1095
1550
  token,
1096
1551
  scope: "document",
1097
- optional: interaction ? [InteractionToken] : void 0,
1098
1552
  initialState: () => initialStageState(config),
1099
1553
  reduce: stageReducer,
1100
1554
  capability: (ctx) => createStageCapability(ctx, config),
1101
- init: interaction ? (ctx) => {
1102
- const ix = ctx.tryGet(InteractionToken);
1103
- if (ix) ix.registerHandler(createScrollHandler(ctx.get(token), ix, { panFallback }));
1104
- } : void 0,
1105
1555
  effects: (ctx) => registerStageEffects(ctx, token)
1106
1556
  });
1107
1557
  };
@@ -1248,19 +1698,46 @@ function destinationToReveal(dest, layout, bbox) {
1248
1698
  };
1249
1699
  }
1250
1700
  //#endregion
1251
- //#region src/wheel.ts
1252
- /** The per-notch step for discrete wheels — the zoom buttons' ratio. */
1253
- const NOTCH = 1.2;
1254
- /** Zoom factor for a ctrl/cmd wheel event (the caller decides zoom vs pan). */
1255
- function wheelZoomFactor(e) {
1256
- if (e.deltaMode !== 0) return Math.pow(NOTCH, -Math.sign(e.deltaY));
1257
- if (e.ctrlKey) {
1258
- if (Math.abs(e.deltaY) >= 100) return Math.pow(NOTCH, -Math.sign(e.deltaY));
1259
- return Math.exp(-e.deltaY / 100);
1260
- }
1261
- return Math.exp(-e.deltaY * .0015);
1701
+ //#region src/scroll-handler.ts
1702
+ /**
1703
+ * Pan-the-camera as an interaction handler. Live under the built-in `pan` tool
1704
+ * (the `scroll` tag) and — with `panFallback` — under every tool, where it then
1705
+ * captures ONLY page-gap drags (on-page downs belong to the active tool, which
1706
+ * claims them first at higher priority). It uses the sample's viewport delta, so
1707
+ * it pans over the whole viewport (pages and gaps).
1708
+ *
1709
+ * Cursor feel: a closed hand (`grabbing`) while dragging; an open hand (`grab`)
1710
+ * hovering a gap under a non-pan tool. The pan tool's own resting `grab` comes
1711
+ * from its `Tool.cursor`, so hovering it needs no claim here.
1712
+ */
1713
+ function createScrollHandler(stage, interaction, options = {}) {
1714
+ const panFallback = options.panFallback ?? true;
1715
+ let last = {
1716
+ x: 0,
1717
+ y: 0
1718
+ };
1719
+ const isPanTool = () => interaction.activeTool().enables.has("scroll");
1720
+ return {
1721
+ id: "stage-scroll",
1722
+ priority: 10,
1723
+ enabledFor: (tool) => tool.enables.has("scroll") || panFallback,
1724
+ onDown: (s) => {
1725
+ if (!isPanTool() && s.page) return false;
1726
+ last = s.viewport;
1727
+ interaction.setCursor("stage-grab", "grabbing", 40);
1728
+ return true;
1729
+ },
1730
+ onMove: (s) => {
1731
+ stage.panBy(s.viewport.x - last.x, s.viewport.y - last.y);
1732
+ last = s.viewport;
1733
+ },
1734
+ onUp: () => interaction.setCursor("stage-grab", null),
1735
+ onHover: (s) => {
1736
+ if (panFallback && !isPanTool()) interaction.setCursor("stage-pan-fallback", s.page ? null : "grab", 5);
1737
+ }
1738
+ };
1262
1739
  }
1263
1740
  //#endregion
1264
- export { DEFAULT_SETTINGS, StageToken, ZoomMode, destinationToReveal, settingsEqual, stagePlugin, wheelZoomFactor };
1741
+ export { DEFAULT_RESPONSIVE, DEFAULT_SETTINGS, StageToken, ZoomMode, boxOf, createScrollHandler, destinationToReveal, matchesQuery, resolveResponsive, settingsEqual, stagePlugin };
1265
1742
 
1266
1743
  //# sourceMappingURL=index.js.map