@embedpdf/plugin-stage 3.0.0-next.4 → 3.0.0-next.6

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.cjs CHANGED
@@ -396,10 +396,37 @@ function createStageCapability(ctx, config = {}) {
396
396
  fitAlign: ctx.getState().fitAlign,
397
397
  direction: ctx.getState().direction
398
398
  });
399
+ let hasPlaced = false;
400
+ const REST_MS = 150;
401
+ let restRaf = 0;
402
+ const armRest = () => {
403
+ if (!canAnimate || !hasPlaced) return;
404
+ if (ctx.getState().cameraResting) ctx.dispatch({
405
+ type: "CAMERA_REST",
406
+ resting: false
407
+ });
408
+ if (restRaf) scheduler.caf(restRaf);
409
+ let t0 = 0;
410
+ const tick = (ts) => {
411
+ restRaf = 0;
412
+ if (!t0) t0 = ts;
413
+ if (ts - t0 >= REST_MS) {
414
+ ctx.dispatch({
415
+ type: "CAMERA_REST",
416
+ resting: true
417
+ });
418
+ return;
419
+ }
420
+ restRaf = scheduler.raf(tick);
421
+ };
422
+ restRaf = scheduler.raf(tick);
423
+ };
399
424
  const setCam = (next, bounds = stayBounds()) => {
425
+ const clamped = _embedpdf_core_stage.clampCamera(next, bounds, vp(), constraint());
426
+ if (clamped.zoom !== cam().zoom) armRest();
400
427
  ctx.dispatch({
401
428
  type: "CAMERA",
402
- camera: _embedpdf_core_stage.clampCamera(next, bounds, vp(), constraint())
429
+ camera: clamped
403
430
  });
404
431
  };
405
432
  /**
@@ -584,8 +611,11 @@ function createStageCapability(ctx, config = {}) {
584
611
  } : unrotatedPoints(box);
585
612
  const c = cam();
586
613
  const ratio = dpr();
587
- const screenX = (0, _embedpdf_core_geometry.snapToDevice)((box.x - c.x) * c.zoom, ratio);
588
- const screenY = (0, _embedpdf_core_geometry.snapToDevice)((box.y - c.y) * c.zoom, ratio);
614
+ const rawX = (box.x - c.x) * c.zoom;
615
+ const rawY = (box.y - c.y) * c.zoom;
616
+ const resting = ctx.getState().cameraResting;
617
+ const screenX = resting ? (0, _embedpdf_core_geometry.snapToDevice)(rawX, ratio) : rawX;
618
+ const screenY = resting ? (0, _embedpdf_core_geometry.snapToDevice)(rawY, ratio) : rawY;
589
619
  const transform = (0, _embedpdf_core_geometry.pageTransform)({
590
620
  pageSize,
591
621
  rotation: box.rotation,
@@ -641,7 +671,7 @@ function createStageCapability(ctx, config = {}) {
641
671
  const v = vp();
642
672
  const sc = buildScene();
643
673
  const items = paged() ? sc.items.slice(0, 1) : sc.query(_embedpdf_core_stage.cameraWorldRect(c, v));
644
- const sig = `${sceneCache.key}|${ctx.getState().flow}|${c.x},${c.y},${c.zoom}|${v.width}x${v.height}|${dpr()}`;
674
+ 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}`;
645
675
  if (sig === visSig) return vis;
646
676
  visSig = sig;
647
677
  vis = items.flatMap((it) => it.pages).map(withTransform);
@@ -654,7 +684,6 @@ function createStageCapability(ctx, config = {}) {
654
684
  };
655
685
  const snapshotSettings = () => pickSettings(ctx.getState());
656
686
  const initialViewProviders = [];
657
- let hasPlaced = false;
658
687
  const api = {
659
688
  camera: cam,
660
689
  viewport: vp,
@@ -1054,6 +1083,7 @@ const initialStageState = (config) => {
1054
1083
  y: 0,
1055
1084
  zoom: 1
1056
1085
  },
1086
+ cameraResting: true,
1057
1087
  vp: {
1058
1088
  width: 0,
1059
1089
  height: 0
@@ -1085,6 +1115,10 @@ const stageReducer = (state, a) => {
1085
1115
  ...state,
1086
1116
  camera: a.camera
1087
1117
  };
1118
+ case "CAMERA_REST": return state.cameraResting === a.resting ? state : {
1119
+ ...state,
1120
+ cameraResting: a.resting
1121
+ };
1088
1122
  case "VP": return {
1089
1123
  ...state,
1090
1124
  vp: a.vp